blib/lib/Cache/Historical.pm

filter: Statementstmt 94.8% 111 / 117 Branchbran 68.1% 15 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.4% 21 / 22 Podpod 53.8% 7 / 13 totaltotal 88.5% 154 / 174 CC 14 SCAR 7.6
CC
14
Cov
91%
new
4.1
last_update
3.0
values
3.0
CRAP 2.1
↑ Summary
1 ###########################################
2 package Cache::Historical;
3 ###########################################
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Rose::DB::Object::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = "0.05";
14  
15 ###########################################
16 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
17 ###########################################
18 2 my($class, %options) = @_;
19  
20 2 ▶ my($home) = glob "~";
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 my $default_cache_dir = "$home/.cache-historical";
22  
23 2 my $self = {
24 sqlite_file => "$default_cache_dir/cache-historical.dat",
25 %options,
26 };
27  
28 2 my $cache_dir = dirname( $self->{sqlite_file} );
29  
30 2 ▶ if(! -d $cache_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $cache_dir) 0 2 2
31 0 ▶ mkpath [ $cache_dir ] or
Branch: 1 branch
BranchTrueFalseTotal
mkpath([$cache_dir]) or die "Cannot mktree $cache_dir ($!)" 0 0 0
32 die "Cannot mktree $cache_dir ($!)";
33 }
34  
35 2 bless $self, $class;
36  
37 2 $self->{dsn} = "dbi:SQLite:dbname=$self->{sqlite_file}";
38  
39 2 ▶ if(! -f $self->{sqlite_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"sqlite_file"}) 2 0 2
40 2 $self->db_init();
41 }
42  
43 my $loader =
44 Rose::DB::Object::Loader->new(
45 db_dsn => $self->{dsn},
46 2 db_options => { AutoCommit => 1, RaiseError => 1 },
47 class_prefix => 'Cache::Historical',
48 with_managers => 1,
49 );
50  
51 2 $loader->make_classes();
52  
53 2 $self->{loader} = $loader;
54  
55 2 return $self;
56 }
57  
58 ###########################################
59 0 ▶sub make_modules {
Subroutine make_modules: not called CC=1 SCAR=6.9
Pod undocumented
60 ###########################################
61 0 my($self, @options) = @_;
62  
63 0 DEBUG "Making modules in @options";
64 0 $self->{loader}->make_modules( @options );
65 }
66  
67 ###########################################
68 3 ▶sub dbh {
Subroutine dbh: called CC=2 SCAR=6.9
Pod undocumented
69 ###########################################
70 3 my($self) = @_;
71  
72 3 ▶ if(! $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}) 2 1 3
73 2 $self->{dbh} = DBI->connect($self->{dsn}, "", "");
74 }
75  
76 3 return $self->{dbh};
77 }
78  
79 ###########################################
80 2 ▶sub db_init {
Subroutine db_init: called CC=1 SCAR=0.0
Pod undocumented
81 ###########################################
82 2 my($self) = @_;
83  
84 2 my $dbh = $self->dbh();
85  
86 2 DEBUG "Creating new SQLite db $self->{sqlite_file}";
87  
88 2 $dbh->do(<<'EOT');
89 CREATE TABLE vals (
90 id INTEGER PRIMARY KEY,
91 date DATETIME,
92 upd_time DATETIME,
93 key TEXT,
94 value TEXT,
95 UNIQUE(date, key)
96 );
97 EOT
98  
99 2 $dbh->do(<<'EOT');
100 CREATE INDEX vals_date_idx ON vals(date);
101 EOT
102  
103 2 $dbh->do(<<'EOT');
104 CREATE INDEX vals_key_idx ON vals(key);
105 EOT
106  
107 2 return 1;
108 }
109  
110 ###########################################
111 6 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
112 ###########################################
113 6 my($self, $dt, $key, $value) = @_;
114  
115 6 DEBUG "Setting $dt $key => $value";
116  
117 6 my $r = Cache::Historical::Val->new();
118 6 $r->key( $key );
119 6 $r->date( $dt );
120 6 $r->upd_time( DateTime->now() );
121 6 $r->load( speculative => 1 );
122 6 $r->value( $value );
123 6 $r->save();
124 }
125  
126 ###########################################
127 10 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod undocumented
128 ###########################################
129 10 my($self, $dt, $key, $interpolate) = @_;
130  
131 10 my @date_query = (date => $dt);
132 10 ▶ @date_query = (date => {le => $dt}) if $interpolate;
Branch: 1 branch
BranchTrueFalseTotal
if $interpolate 8 2 10
133  
134 10 my $values = Cache::Historical::Val::Manager->get_vals(
135 query => [
136 @date_query,
137 key => $key,
138 ],
139 sort_by => "date DESC",
140 limit => 1,
141 );
142  
143 10 ▶ if(@$values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$values) 4 6 10
144 4 my $value = $values->[0]->value();
145 4 DEBUG "Getting $dt $key => $value";
146 4 return $value;
147 }
148  
149 6 return undef;
150 }
151  
152 ###########################################
153 1 ▶sub keys {
Subroutine keys: called CC=2 SCAR=6.9
Pod documented
154 ###########################################
155 1 my($self) = @_;
156  
157 1 my @keys;
158 1 my $keys = Cache::Historical::Val::Manager->get_vals(
159 distinct => 1,
160 select => [ 'key' ],
161 );
162  
163 1 for(@$keys) {
164 2 push @keys, $_->key();
165 }
166  
167 1 return @keys;
168 }
169  
170 ###########################################
171 1 ▶sub values {
Subroutine values: called CC=3 SCAR=11.0
Pod documented
172 ###########################################
173 1 my($self, $key) = @_;
174  
175 1 my @values = ();
176 1 my @key = ();
177 1 ▶ @key = (key => $key) if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 1 0 1
178  
179 1 my $values = Cache::Historical::Val::Manager->get_vals(
180 query => [ @key ],
181 sort_by => ['date'],
182 );
183  
184 1 for(@$values) {
185 4 push @values, [$_->date(), $_->value()];
186 }
187  
188 1 return @values;
189 }
190  
191 ###########################################
192 4 ▶sub last_update {
Subroutine last_update: called CC=3 SCAR=11.1
Pod documented
193 ###########################################
194 4 my($self, $key) = @_;
195  
196 4 my @key = ();
197 4 ▶ @key = (key => $key) if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 2 2 4
198  
199 4 my $values = Cache::Historical::Val::Manager->get_vals(
200 query => [ @key ],
201 sort_by => ['upd_time DESC'],
202 limit => 1,
203 );
204  
205 4 ▶ if(@$values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$values) 4 0 4
206 4 my $date = $values->[0]->upd_time();
207 4 return $date;
208 }
209  
210 0 return undef;
211 }
212  
213 ###########################################
214 2 ▶sub since_last_update {
Subroutine since_last_update: called CC=2 SCAR=7.4
Pod documented
215 ###########################################
216 2 my($self, $key) = @_;
217  
218 2 my $date = $self->last_update($key);
219  
220 2 ▶ if(defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date) 2 0 2
221 2 return DateTime->now() - $date;
222 }
223  
224 0 return undef;
225 }
226  
227 ###########################################
228 8 ▶sub get_interpolated {
Subroutine get_interpolated: called CC=1 SCAR=0.0
Pod undocumented
229 ###########################################
230 8 my($self, $dtp, $key) = @_;
231  
232 8 return $self->get($dtp, $key, 1);
233 }
234  
235 2 my $date_fmt = DateTime::Format::Strptime->new(
236 pattern => "%Y-%m-%d %H:%M:%S");
237  
238 ###########################################
239 1 ▶sub time_range {
Subroutine time_range: called CC=1 SCAR=0.0
Pod documented
240 ###########################################
241 1 my($self, $key) = @_;
242  
243 1 my $dbh = $self->dbh();
244  
245 1 my($from, $to) = $dbh->selectrow_array(
246 "SELECT MIN(date), MAX(date) FROM vals WHERE key = " .
247 $dbh->quote( $key ));
248  
249 1 $from = $date_fmt->parse_datetime( $from );
250 1 $to = $date_fmt->parse_datetime( $to );
251  
252 1 return($from, $to);
253 }
254  
255 ###########################################
256 2 ▶sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
257 ###########################################
258 2 my($self, $key) = @_;
259  
260 2 my @params = (all => 1);
261  
262 2 ▶ if(defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 1 1 2
263 1 @params = ("where" => [ key => $key ]);
264 }
265  
266 2 my $values = Cache::Historical::Val::Manager->delete_vals( @params );
267 }
268  
269 2 1;
270  
271 __END__
↑ Summary