Chronicle/Writer.pm

filter: Statementstmt 89.6% 69 / 77 Branchbran 57.1% 8 / 14 Conditioncond 43.4% 10 / 23 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 86.6% 13 / 15 Podpod 100% 2 / 2 totaltotal 71.0% 103 / 145 CC 19 SCAR 17.8
CC
19
Cov
76%
mset
12.5
_validate_value
4.8
set
4.2
CRAP 5.9
« Chronicle/Reader.pm ↑ Summary Chronicle/Mock.pm »
1 package Data::Chronicle::Writer;
2  
3 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Data::Chronicle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Date::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON::MaybeUTF8 qw(encode_json_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Data::Chronicle::Writer - Provides writing to an efficient data storage for volatile and time-based data
14  
15 =cut
16  
17 5 our $VERSION = '0.21'; ## VERSION
18  
19 =head1 DESCRIPTION
20  
21 This module contains helper methods which can be used to store and retrieve information
22 on an efficient storage with below properties:
23  
24 =over 4
25  
26 =item B<Timeliness>
27  
28 It is assumed that data to be stored are time-based meaning they change over time and the latest version is most important for us.
29  
30 =item B<Efficient>
31  
32 The module uses Redis cache to provide efficient data storage and retrieval.
33  
34 =item B<Persistent>
35  
36 In addition to caching every incoming data, it is also stored in PostgreSQL for future retrieval.
37  
38 =item B<Transparent>
39  
40 This modules hides all the internal details including distribution, caching, and database structure from the developer. He only needs to call a method
41 to save data and another method to retrieve it. All the underlying complexities are handled by the module.
42  
43 =back
44  
45 =head1 EXAMPLE
46  
47 my $d = get_some_log_data();
48  
49 my $chronicle_w = Data::Chronicle::Writer->new(
50 cache_writer => $writer,
51 dbic => $dbic,
52 ttl => 86400,
53 publish_on_set => 1);
54  
55 my $chronicle_r = Data::Chronicle::Reader->new(
56 cache_reader => $reader,
57 dbic => $dbic);
58  
59  
60 #store data into Chronicle - each time we call `set` it will also store
61 #a copy of the data for historical data retrieval
62 $chronicle_w->set("log_files", "syslog", $d);
63  
64 #retrieve latest data stored for syslog under log_files category
65 my $dt = $chronicle_r->get("log_files", "syslog");
66  
67 #find historical data for `syslog` at given point in time
68 my $some_old_data = $chronicle_r->get_for("log_files", "syslog", $epoch1);
69  
70 =cut
71  
72 =head1 METHODS
73  
74 =head2 cache_writer
75  
76 cache_writer should be an instance of L<RedisDB>.
77  
78 =cut
79  
80 5 has 'cache_writer' => (
81 is => 'ro',
82 default => undef,
83 );
84  
85 =head2 dbic
86  
87 dbic should be an instance of L<DBIx::Connector>.
88  
89 =cut
90  
91 5 has 'dbic' => (
92 isa => 'Maybe[DBIx::Connector]',
93 is => 'ro',
94 default => undef,
95 );
96  
97 =head2 ttl
98  
99 If a TTL value is provided when constructing the instance, this will be used as the expiry time for the data.
100  
101 Expiry time is not currently recorded in the PostgreSQL database backend - it is only used for the cache layer.
102  
103 This represents the seconds until expiry, and default is C<undef>, meaning that keys will not expire.
104  
105 =cut
106  
107 5 has 'ttl' => (
108 isa => 'Maybe[Int]',
109 is => 'ro',
110 default => undef,
111 );
112  
113 =head2 publish_on_set
114  
115 Will invoke
116  
117 $cache_writer->publish("$category::$name1", $value);
118  
119 if set to true. This is useful, if to provide redis or postgres notificaitons on new data.
120  
121 Default value: 0 (false)
122  
123 =cut
124  
125 has 'publish_on_set' => (
126 isa => 'Int',
127 is => 'ro',
128 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 5 );
130  
131 =head2 set
132  
133 Example:
134  
135 $chronicle_writer->set("category1", "name1", $value1);
136  
137 Store a piece of data "value1" under key "category1::name1" in Pg and Redis. Will
138 publish "category1::name1" in Redis if C<publish_on_set> is true.
139  
140 =cut
141  
142 2 ▶sub set {
Subroutine set: called CC=4 SCAR=14.4
Pod documented
143 2 my $self = shift;
144 2 my $category = shift;
145 2 my $name = shift;
146 2 my $value = shift;
147 2 my $rec_date = shift;
148 2 ▶ my $archive = shift // 1;
Condition50%1/2
shift() // 1
l!l
20
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
149 2 ▶ my $suppress_pub = shift // 0;
Condition50%1/2
shift() // 0
l!l
02
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
150 2 ▶ my $ttl = shift // $self->ttl;
Condition33%1/3
shift() // $self->ttl
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() // $self->ttl
shift() $self->ttl
Truth table33%1/3
A // B
A= shift()B= $self->ttl
ABresult
1X1
011
000
151  
152 2 $self->mset([[$category, $name, $value]], $rec_date, $archive, $suppress_pub, $ttl);
153  
154 2 return 1;
155 }
156  
157 =head2 mset
158  
159 Example:
160  
161 $chronicle_writer->mset([["category1", "name1", $value1], ["category2, "name2", $value2], ...]);
162  
163 Store a piece of data "value1" under key "category1::name1", "category2::name2", etc in Pg and Redis. Will
164 publish "category1::name1", "category2::name2", etc in Redis if C<publish_on_set> is true.
165  
166 =cut
167  
168 2 ▶sub mset {
Subroutine mset: called CC=11 SCAR=25.2
Pod documented
169 2 my $self = shift;
170 2 my $entries = shift;
171 2 my $rec_date = shift;
172 2 ▶ my $archive = shift // 1;
Condition50%1/2
shift() // 1
l!l
20
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
173 2 ▶ my $suppress_pub = shift // 0;
Condition50%1/2
shift() // 0
l!l
20
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
174 2 ▶ my $ttl = shift // $self->ttl;
Condition33%1/3
shift() // $self->ttl
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() // $self->ttl
shift() $self->ttl
Truth table33%1/3
A // B
A= shift()B= $self->ttl
ABresult
1X1
011
000
175  
176 2 $self->_validate_value(@$_) foreach @$entries;
177 2 $self->_validate_rec_date($rec_date);
178  
179 2 my $writer = $self->cache_writer;
180  
181 # ping the writter to make sure the connection is on
182 2 $writer->ping();
183  
184 # publish & set in transaction
185 2 $writer->multi;
186 2 foreach my $entry (@$entries) {
187 2 my ($category, $name, $value) = @$entry;
188  
189 2 my $key = $category . '::' . $name;
190  
191 2 my $encoded = encode_json_utf8($value);
192 2 ▶ $writer->publish($key, $encoded) if $self->publish_on_set && !$suppress_pub;
Branch: 1 branch
BranchTrueFalseTotal
if $self->publish_on_set and not $suppress_pub 1 1 2
Condition66%2/3
$self->publish_on_set and not $suppress_pub
!ll&&!rl&&r
101
MC/DC50%1/2
$self->publish_on_set and not $suppress_pub
$self->publish_on_set not $suppress_pub
Truth table66%2/3
A and not(B)
A= $self->publish_on_setB= not $suppress_pub
ABresult
0X0
100
111
193 2 ▶ $writer->set(
Branch: 1 branch
BranchTrueFalseTotal
$ttl ? : 2 0 2
194 $key => $encoded,
195 $ttl ? ('EX' => $ttl) : ());
196  
197 2 ▶ $self->_archive($category, $name, $encoded, $rec_date) if $archive and $self->dbic;
Branch: 1 branch
BranchTrueFalseTotal
if $archive and $self->dbic 0 2 2
Condition33%1/3
$archive and $self->dbic
!ll&&!rl&&r
200
MC/DC0%0/2
$archive and $self->dbic
$archive $self->dbic
Truth table33%1/3
A and B
A= $archiveB= $self->dbic
ABresult
0X0
100
111
198 }
199 2 $writer->exec;
200  
201 2 return 1;
202 }
203  
204 2 ▶sub _validate_value {
Subroutine _validate_value: called CC=4 SCAR=15.6
205 2 my ($self, $category, $name, $value) = @_;
206 2 ▶ die "Cannot store an undefined value for ${category}::${name} in Chronicle!" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
207 2 ▶ die "You can only store hash-ref or array-ref in Chronicle!" unless (ref $value eq 'ARRAY' or ref $value eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" or ref $value eq "HASH" 0 2 2
Condition33%1/3
ref $value eq "ARRAY" or ref $value eq "HASH"
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $value eq "ARRAY" or ref $value eq "HASH"
ref $value eq "ARRAY" ref $value eq "HASH"
Truth table33%1/3
A or B
A= ref $value eq "ARRAY"B= ref $value eq "HASH"
ABresult
1X1
011
000
208 2 return;
209 }
210  
211 2 ▶sub _validate_rec_date {
Subroutine _validate_rec_date: called CC=3 SCAR=11.4
212 2 my ($self, $rec_date) = @_;
213 2 ▶ die "Recorded date is undefined" unless $rec_date;
Branch: 1 branch
BranchTrueFalseTotal
unless $rec_date 0 2 2
214 2 ▶ die "Recorded date is not a Date::Utility object" if ref $rec_date ne 'Date::Utility';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rec_date ne "Date::Utility" 0 2 2
215 2 return;
216 }
217  
218 0 ▶sub _archive {
Subroutine _archive: not called CC=1 SCAR=6.9
219 0 my $self = shift;
220 0 my $category = shift;
221 0 my $name = shift;
222 0 my $value = shift;
223 0 my $rec_date = shift;
224  
225 0 my $db_timestamp = $rec_date->db_timestamp;
226  
227 return $self->dbic->run(
228 0 ▶ fixup => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 $_->prepare(<<'SQL')->execute($category, $name, $value, $db_timestamp) });
230 WITH ups AS (
231 UPDATE chronicle
232 SET value=$3
233 WHERE timestamp=$4
234 AND category=$1
235 AND name=$2
236 RETURNING *
237 )
238 INSERT INTO chronicle (timestamp, category, name, value)
239 SELECT $4, $1, $2, $3
240 WHERE NOT EXISTS (SELECT * FROM ups)
241 SQL
242 }
243  
244 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
245 5 __PACKAGE__->meta->make_immutable;
246  
247 =head1 AUTHOR
248  
249 Binary.com, C<< <support at binary.com> >>
250  
251 =head1 BUGS
252  
253 Please report any bugs or feature requests to C<bug-data-chronicle at rt.cpan.org>, or through
254 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Chronicle>. I will be notified, and then you'll
255 automatically be notified of progress on your bug as I make changes.
256  
257  
258  
259  
260 =head1 SUPPORT
261  
262 You can find documentation for this module with the perldoc command.
263  
264 perldoc Data::Chronicle::Writer
265  
266  
267 You can also look for information at:
268  
269 =over 4
270  
271 =item * RT: CPAN's request tracker (report bugs here)
272  
273 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Chronicle>
274  
275 =item * AnnoCPAN: Annotated CPAN documentation
276  
277 L<http://annocpan.org/dist/Data-Chronicle>
278  
279 =item * CPAN Ratings
280  
281 L<http://cpanratings.perl.org/d/Data-Chronicle>
282  
283 =item * Search CPAN
284  
285 L<https://metacpan.org/release/Data-Chronicle/>
286  
287 =back
288  
289  
290 =head1 ACKNOWLEDGEMENTS
291  
292 =cut
293  
294 5 1;
« Chronicle/Reader.pm ↑ Summary Chronicle/Mock.pm »