Chronicle/Reader.pm

filter: Statementstmt 33.7% 28 / 83 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 8 / 16 Podpod 100% 5 / 5 totaltotal 32.2% 41 / 127 CC 12 SCAR 24.2
CC
12
Cov
27%
get_history
30.0
get_for_period
20.0
get_for
12.0
CRAP 11.3
↑ Summary Chronicle/Writer.pm »
1 package Data::Chronicle::Reader;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Data::Chronicle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Date::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::MaybeUTF8 qw(decode_json_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Data::Chronicle::Reader - Provides reading from an efficient data storage for volatile and time-based data
14  
15 =cut
16  
17 3 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  
53 my $chronicle_r = Data::Chronicle::Reader->new(
54 cache_reader => $reader,
55 dbic => $dbic);
56  
57 #store data into Chronicle - each time we call `set` it will also store
58 #a copy of the data for historical data retrieval
59 $chronicle_w->set("log_files", "syslog", $d);
60  
61 #retrieve latest data stored for syslog under log_files category
62 my $dt = $chronicle_r->get("log_files", "syslog");
63  
64 #find historical data for `syslog` at given point in time
65 my $some_old_data = $chronicle_r->get_for("log_files", "syslog", $epoch1);
66  
67 =cut
68  
69 =head1 METHODS
70  
71 =head2 cache_reader
72  
73 cache_reader should be an instance of L<RedisDB>.
74  
75 =cut
76  
77 3 has 'cache_reader' => (
78 is => 'ro',
79 default => undef,
80 );
81  
82 =head2 dbic
83  
84 dbic should be an instance of L<DBIx::Connector>.
85  
86 =cut
87  
88 3 has 'dbic' => (
89 isa => 'Maybe[DBIx::Connector]',
90 is => 'ro',
91 default => undef,
92 );
93  
94 =head2 get
95  
96 Example:
97  
98 my $data = get("category1", "name1");
99  
100 Query for the latest data under "category1::name1" from the cache reader.
101 Will return `undef` if the data does not exist.
102  
103 =cut
104  
105 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
106 0 my $self = shift;
107 0 my $category = shift;
108 0 my $name = shift;
109  
110 0 my @cached_data = $self->mget([[$category, $name]]);
111 0 ▶ return $cached_data[0] if @cached_data;
Branch: 1 branch
BranchTrueFalseTotal
if @cached_data 0 0 0
112  
113 0 return undef;
114 }
115  
116 =head2 mget
117  
118 Example:
119  
120 my @values = mget([["category1", "name1"], ["category2", "name2"], ...]);
121  
122 Query for the latest data under "category1::name1", "category2::name2", etc from the cache reader.
123 Will return an arrayref containing results in the same ordering, with `undef` if the data does not exist.
124  
125 =cut
126  
127 0 ▶sub mget {
Subroutine mget: not called CC=2 SCAR=17.9
Pod documented
128 0 my $self = shift;
129 0 my $pairs = shift;
130  
131 0 my @keys = map { $_->[0] . '::' . $_->[1] } @$pairs;
132  
133 0 my $cached_data = $self->cache_reader->mget(@keys);
134 0 ▶ return map { decode_json_utf8($_) if $_ } @$cached_data;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
135 }
136  
137 =head2 get_for
138  
139 Example:
140  
141 my $data = get_for("category1", "name1", 1447401505);
142  
143 Query Pg archive for the data under "category1::name1" at or exactly before the given epoch/Date::Utility.
144  
145 =cut
146  
147 0 ▶sub get_for {
Subroutine get_for: not called CC=3 SCAR=24.8
Pod documented
148 0 my $self = shift;
149 0 my $category = shift;
150 0 my $name = shift;
151 0 my $date_for = shift; #epoch or Date::Utility
152  
153 0 my $db_timestamp = Date::Utility->new($date_for)->db_timestamp;
154  
155 0 ▶ die "Requesting for historical data without a valid DB connection [$category,$name,$date_for]" if not defined $self->dbic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->dbic 0 0 0
156  
157 my $db_data = $self->dbic->run(
158 0 ▶ fixup => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
159 0 $_->selectall_hashref(q{SELECT * FROM chronicle where category=? and name=? and timestamp<=? order by timestamp desc limit 1},
160 'id', {}, $category, $name, $db_timestamp);
161 0 });
162  
163 0 ▶ return if not %$db_data;
Branch: 1 branch
BranchTrueFalseTotal
unless %$db_data 0 0 0
164  
165 0 my $id_value = (sort keys %{$db_data})[0];
166 0 my $db_value = $db_data->{$id_value}->{value};
167  
168 0 return decode_json_utf8($db_value);
169 }
170  
171 =head2 get_for_period
172  
173 Example:
174  
175 my $data = get_for_period("category1", "name1", 1447401505, 1447401900)
176  
177 Query Pg historical data and return records whose date is between given period.
178  
179 =cut
180  
181 0 ▶sub get_for_period {
Subroutine get_for_period: not called CC=4 SCAR=30.0
Pod documented
182 0 my $self = shift;
183 0 my $category = shift;
184 0 my $name = shift;
185 0 my $start = shift; #epoch or Date::Utility
186 0 my $end = shift; #epoch or Date::Utility
187  
188 0 my $start_timestamp = Date::Utility->new($start)->db_timestamp;
189 0 my $end_timestamp = Date::Utility->new($end)->db_timestamp;
190  
191 0 ▶ die "Requesting for historical period data without a valid DB connection [$category,$name]" if not defined $self->dbic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->dbic 0 0 0
192  
193 my $db_data = $self->dbic->run(
194 0 ▶ fixup => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 0 $_->selectall_hashref(q{SELECT * FROM chronicle where category=? and name=? and timestamp<=? AND timestamp >=? order by timestamp desc},
196 'id', {}, $category, $name, $end_timestamp, $start_timestamp);
197 0 });
198  
199 0 ▶ return if not %$db_data;
Branch: 1 branch
BranchTrueFalseTotal
unless %$db_data 0 0 0
200  
201 0 my @result;
202  
203 0 for my $id_value (keys %$db_data) {
204 0 my $db_value = $db_data->{$id_value}->{value};
205  
206 0 push @result, decode_json_utf8($db_value);
207 }
208  
209 0 return \@result;
210 }
211  
212 =head2 get_history
213  
214 Example:
215  
216 my $data = get_history("category1", "name1", 1);
217  
218 Query Pg archive for the data under "category1::name1" at the provided number of revisions in the past.
219  
220 =cut
221  
222 0 ▶sub get_history {
Subroutine get_history: not called CC=5 SCAR=34.0
Pod documented
223 0 my $self = shift;
224 0 my $category = shift;
225 0 my $name = shift;
226 0 my $rev = shift;
227  
228 0 ▶ die "Revision must be >= 0" unless (defined $rev && $rev >= 0);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rev and $rev >= 0 0 0 0
Condition0%0/3
defined $rev and $rev >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rev and $rev >= 0
defined $rev $rev >= 0
Truth table0%0/3
A and B
A= defined $revB= $rev >= 0
ABresult
0X0
100
111
229 0 ▶ die "Requesting for historical data without a valid DB connection [$category,$name,$rev]" if not defined $self->dbic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->dbic 0 0 0
230  
231 my $db_data = $self->dbic->run(
232 0 ▶ fixup => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 0 $_->selectall_hashref(q{SELECT * FROM chronicle where category=? and name=? order by timestamp desc limit 1 offset ?},
234 'id', {}, $category, $name, $rev);
235 0 });
236  
237 0 ▶ return if not %$db_data;
Branch: 1 branch
BranchTrueFalseTotal
unless %$db_data 0 0 0
238  
239 0 my $id_value = (sort keys %{$db_data})[0];
240 0 my $db_value = $db_data->{$id_value}->{value};
241  
242 0 return decode_json_utf8($db_value);
243 }
244  
245 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
246 3 __PACKAGE__->meta->make_immutable;
247  
248 =head1 AUTHOR
249  
250 Binary.com, C<< <support at binary.com> >>
251  
252 =head1 BUGS
253  
254 Please report any bugs or feature requests to C<bug-data-chronicle at rt.cpan.org>, or through
255 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Chronicle>. I will be notified, and then you'll
256 automatically be notified of progress on your bug as I make changes.
257  
258  
259  
260  
261 =head1 SUPPORT
262  
263 You can find documentation for this module with the perldoc command.
264  
265 perldoc Data::Chronicle::Reader
266  
267  
268 You can also look for information at:
269  
270 =over 4
271  
272 =item * RT: CPAN's request tracker (report bugs here)
273  
274 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Chronicle>
275  
276 =item * AnnoCPAN: Annotated CPAN documentation
277  
278 L<http://annocpan.org/dist/Data-Chronicle>
279  
280 =item * CPAN Ratings
281  
282 L<http://cpanratings.perl.org/d/Data-Chronicle>
283  
284 =item * Search CPAN
285  
286 L<https://metacpan.org/release/Data-Chronicle/>
287  
288 =back
289  
290  
291 =head1 ACKNOWLEDGEMENTS
292  
293 =cut
294  
295 3 1;
↑ Summary Chronicle/Writer.pm »