blib/lib/InfluxDB.pm

filter: Statementstmt 19.7% 39 / 198 Branchbran 1.4% 1 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 26.1% 11 / 42 Podpod 86.2% 25 / 29 totaltotal 22.4% 76 / 339 CC 39 SCAR 25.1
CC
39
Cov
15%
query
42.0
_build_url
42.0
write_points
20.0
CRAP 12.3
↑ Summary Build »
1 package InfluxDB;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '1.003';
8  
9 use Class::Accessor::Lite (
10 3 new => 0,
11 ro => [qw(host port username database ssl json)],
12 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Data::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use JSON 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 enum 'TimePrecision' => qw(s m u);
22  
23 3 subtype 'JSONBool' => as 'ScalarRef';
24 coerce 'JSONBool'
25 0 ▶ => from 'Bool' => via { $_ ? \1 : \0 }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
26 0 ▶ => from 'Object' => via { JSON::is_bool($_) ? ($_ == 1 ? \1 : \0) : \0 }
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
&JSON::is_bool($_) ? : 0 0 0
$_ == 1 ? : 0 0 0
27 3 ;
28  
29 sub new {
30 3 ▶ state $rule = Data::Validator->new(
Subroutine new: called CC=2 SCAR=7.2
Pod documented
31 host => { isa => 'Str' },
32 port => { isa => 'Int', default => 8086 },
33 username => { isa => 'Str' },
34 password => { isa => 'Str' },
35 database => { isa => 'Str' },
36 ssl => { isa => 'Bool', default => 0 },
37  
38 timeout => { isa => 'Int', default => 120 },
39 debug => { isa => 'Bool', optional => 1 },
40 )->with('NoRestricted')->with('Method');
41 # Mouse::Util::apply_all_role leaks memory when takes 2 or more extensions.
42 # so apply for each extension.
43 3 my($class, $args) = $rule->validate(@_);
44  
45 3 ▶ if (delete $args->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args->{"debug"}) 0 3 3
46 0 $ENV{IX_DEBUG} = 1;
47 }
48  
49 my $self = bless {
50 ua => Furl->new(
51 agent => join('/', __PACKAGE__, $VERSION),
52 timeout => $args->{timeout},
53 3 ),
54 json => JSON->new,
55 status => {},
56 %$args
57 }, $class;
58  
59 3 return $self;
60 }
61  
62 sub debugf {
63 0 ▶ return unless $ENV{IX_DEBUG};
Subroutine debugf: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"IX_DEBUG"} 0 0 0
64 0 print STDERR "[DEBUG] ",sprintf(shift @_, @_),"\n";
65 }
66  
67 sub status {
68 0 ▶ my($self, $res) = @_;
Subroutine status: not called CC=2 SCAR=17.9
Pod documented
69  
70 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
71 $self->{status} = {
72 0 code => $res->code,
73 message => $res->message,
74 status_line => $res->status_line,
75 content => $res->content,
76 };
77 0 debugf("content: %s", $res->content);
78 }
79  
80 0 return $self->{status};
81 }
82  
83 sub errstr {
84 0 ▶ my($self) = @_;
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
85 0 my $errstr = "";
86  
87 0 ▶ if (substr($self->{status}{code}, 0, 2) ne "20") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{"status"}{"code"}, 0, 2) ne 20) 0 0 0
88 $errstr = join("\n",
89 $self->{status}{status_line},
90 $self->{status}{content},
91 0 );
92 }
93  
94 0 return $errstr;
95 }
96  
97 sub as_hash {
98 0 ▶ my(undef, $result) = @_;
Subroutine as_hash: not called CC=3 SCAR=24.8
Pod documented
99 0 my $h;
100  
101 0 for my $r (@{ $result }) {
102 0 my $series = $r->{name};
103 0 my @columns = @{ $r->{columns} };
104  
105 0 for my $p (@{ $r->{points} }) {
106 0 my %ph;
107 0 @ph{ @columns } = @$p;
108 0 push @{ $h->{$series} }, \%ph;
109 }
110 }
111  
112 0 return $h;
113 }
114  
115 sub switch_database {
116 0 ▶ state $rule = Data::Validator->new(
Subroutine switch_database: not called CC=1 SCAR=6.9
Pod documented
117 database => { isa => 'Str' },
118 )->with('Method');
119 0 my($self, $args) = $rule->validate(@_);
120  
121 0 $self->{database} = $args->{database};
122  
123 0 return 1;
124 }
125  
126 sub switch_user {
127 0 ▶ state $rule = Data::Validator->new(
Subroutine switch_user: not called CC=1 SCAR=6.9
Pod documented
128 username => { isa => 'Str' },
129 password => { isa => 'Str' },
130 )->with('Method');
131 0 my($self, $args) = $rule->validate(@_);
132  
133 0 $self->{username} = $args->{username};
134 0 $self->{password} = $args->{password};
135  
136 0 return 1;
137 }
138  
139 ### database #############################################################
140 sub create_database {
141 0 ▶ state $rule = Data::Validator->new(
Subroutine create_database: not called CC=2 SCAR=17.9
Pod documented
142 database => { isa => 'Str' },
143 )->with('Method');
144 0 my($self, $args) = $rule->validate(@_);
145  
146 0 my $url = $self->_build_url(
147 path => '/db',
148 );
149  
150 my $res = $self->{ua}->post($url, [], $self->json->encode({
151 name => $args->{database},
152 0 }));
153 0 $self->status($res);
154  
155 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
156 }
157  
158 sub list_database {
159 0 ▶ my $self = shift;
Subroutine list_database: not called CC=2 SCAR=17.9
Pod documented
160  
161 0 my $url = $self->_build_url(
162 path => '/db',
163 );
164  
165 0 my $res = $self->{ua}->get($url);
166 0 $self->status($res);
167  
168 0 ▶ return $res->is_success ? $self->json->decode($res->content) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
169 }
170  
171 sub delete_database {
172 0 ▶ state $rule = Data::Validator->new(
Subroutine delete_database: not called CC=2 SCAR=17.9
Pod documented
173 database => { isa => 'Str' },
174 )->with('Method');
175 0 my($self, $args) = $rule->validate(@_);
176  
177 my $url = $self->_build_url(
178 path => '/db/'. $args->{database},
179 0 );
180  
181 0 my $res = $self->{ua}->delete($url);
182 0 $self->status($res);
183  
184 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
185 }
186  
187 ### series ###############################################################
188 ## hmmm v0.5.1 (latest version) returns empty response
189 ## https://github.com/FGRibreau/influxdb-cli/issues/8
190 sub list_series {
191 0 ▶ my $self = shift;
Subroutine list_series: not called CC=1 SCAR=6.9
Pod documented
192 0 return $self->query(q => "list series");
193 }
194  
195 ### points ###############################################################
196 sub write_points {
197 0 ▶ state $rule = Data::Validator->new(
Subroutine write_points: not called CC=4 SCAR=30.0
Pod documented
198 data => { isa => 'ArrayRef|HashRef' },
199 time_precision => { isa => 'TimePrecision', optional => 1 },
200 )->with('Method');
201 0 my($self, $args) = $rule->validate(@_);
202  
203 0 ▶ my $data = ref($args->{data}) eq 'ARRAY' ? $args->{data} : [$args->{data}];
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'data'} eq 'ARRAY' ? : 0 0 0
204 0 $data = $self->json->encode($data);
205 0 debugf("data: %s", $data);
206  
207 my $url = $self->_build_url(
208 path => '/db/' . $self->database . '/series',
209 qs => {
210 time_precision => exists $args->{time_precision} ? $args->{time_precision} : undef,
211 },
212 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'time_precision'} ? : 0 0 0
213  
214 0 my $res = $self->{ua}->post($url, [], $data);
215 0 $self->status($res);
216  
217 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
218 }
219  
220 sub delete_points {
221 0 ▶ state $rule = Data::Validator->new(
Subroutine delete_points: not called CC=2 SCAR=17.9
Pod documented
222 name => { isa => 'Str' },
223 )->with('Method');
224 0 my($self, $args) = $rule->validate(@_);
225  
226 my $url = $self->_build_url(
227 path => '/db/' . $self->database . '/series/' . $args->{name},
228 0 qs => {
229 %$args,
230 },
231 );
232  
233 0 my $res = $self->{ua}->delete($url);
234 0 $self->status($res);
235  
236 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
237 }
238  
239 sub create_scheduled_deletes {
240 0 ▶ croak "Not implemented in InfluxDB v0.5.1";
Subroutine create_scheduled_deletes: not called CC=1 SCAR=6.9
Pod undocumented
241 # state $rule = Data::Validator->new(
242 # )->with('Method');
243 # my($self, $args) = $rule->validate(@_);
244 }
245  
246 sub list_scheduled_deletes {
247 0 ▶ croak "Not implemented in InfluxDB v0.5.1";
Subroutine list_scheduled_deletes: not called CC=1 SCAR=6.9
Pod undocumented
248 # state $rule = Data::Validator->new(
249 # )->with('Method');
250 # my($self, $args) = $rule->validate(@_);
251 }
252  
253 sub delete_scheduled_deletes {
254 0 ▶ croak "Not implemented in InfluxDB v0.5.1";
Subroutine delete_scheduled_deletes: not called CC=1 SCAR=6.9
Pod undocumented
255 # state $rule = Data::Validator->new(
256 # )->with('Method');
257 # my($self, $args) = $rule->validate(@_);
258 }
259  
260 sub query {
261 0 ▶ state $rule = Data::Validator->new(
Subroutine query: not called CC=6 SCAR=37.4
Pod documented
262 q => { isa => 'Str' },
263 time_precision => { isa => 'TimePrecision', optional => 1 },
264 chunked => { isa => 'Bool', default => 0 },
265 # order => { isa => 'Str', optional => 1 }, # not implemented?
266 )->with('Method');
267 0 my($self, $args) = $rule->validate(@_);
268  
269 my $url = $self->_build_url(
270 path => '/db/' . $self->database . '/series',
271 qs => {
272 q => $args->{q},
273 time_precision => (exists $args->{time_precision} ? $args->{time_precision} : undef),
274 chunked => ($args->{chunked} ? 'true' : 'false'),
275 0 ▶ order => (exists $args->{order} ? $args->{order} : undef),
Branch: 3 branches
BranchTrueFalseTotal
exists $args->{'time_precision'} ? : 0 0 0
$args->{'chunked'} ? : 0 0 0
exists $args->{'order'} ? : 0 0 0
276 },
277 );
278  
279 0 my $res = $self->{ua}->get($url);
280 0 $self->status($res);
281  
282 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
283 0 my $result;
284 0 ▶ if ($args->{chunked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'chunked'}) { } 0 0 0
285 0 $result = [$self->json->incr_parse($res->content)];
286 } else {
287 0 $result = $self->json->decode($res->content);
288 }
289 0 return $result;
290 } else {
291 0 return;
292 }
293 }
294  
295 ### Continuous Queries ###################################################
296 sub create_continuous_query {
297 0 ▶ state $rule = Data::Validator->new(
Subroutine create_continuous_query: not called CC=1 SCAR=6.9
Pod documented
298 q => { isa => 'Str' },
299 name => { isa => 'Str' },
300 )->with('Method');
301 0 my($self, $args) = $rule->validate(@_);
302  
303 0 return $self->query(q => "$args->{q} into $args->{name}");
304 }
305  
306 sub list_continuous_queries {
307 0 ▶ my $self = shift;
Subroutine list_continuous_queries: not called CC=1 SCAR=6.9
Pod documented
308 0 return $self->query(q => "list continuous queries");
309 }
310  
311 sub drop_continuous_query {
312 0 ▶ state $rule = Data::Validator->new(
Subroutine drop_continuous_query: not called CC=1 SCAR=6.9
Pod documented
313 id => { isa => 'Str' },
314 )->with('Method');
315 0 my($self, $args) = $rule->validate(@_);
316  
317 0 return $self->query(q => "drop continuous query $args->{id}");
318 }
319  
320 ### user #################################################################
321 sub create_database_user {
322 0 ▶ state $rule = Data::Validator->new(
Subroutine create_database_user: not called CC=2 SCAR=17.9
Pod documented
323 name => { isa => 'Str' },
324 password => { isa => 'Str' },
325 )->with('Method');
326 0 my($self, $args) = $rule->validate(@_);
327  
328 0 my $url = $self->_build_url(
329 path => '/db/' . $self->database . '/users',
330 );
331  
332 my $res = $self->{ua}->post($url, [], $self->json->encode({
333 name => $args->{name},
334 password => $args->{password},
335 0 }));
336 0 $self->status($res);
337  
338 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
339 }
340  
341 sub delete_database_user {
342 0 ▶ state $rule = Data::Validator->new(
Subroutine delete_database_user: not called CC=2 SCAR=17.9
Pod documented
343 name => { isa => 'Str' },
344 )->with('Method');
345 0 my($self, $args) = $rule->validate(@_);
346  
347 my $url = $self->_build_url(
348 path => '/db/' . $self->database . '/users/' . $args->{name},
349 0 );
350  
351 0 my $res = $self->{ua}->delete($url);
352 0 $self->status($res);
353  
354 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
355 }
356  
357 sub update_database_user {
358 0 ▶ state $rule = Data::Validator->new(
Subroutine update_database_user: not called CC=4 SCAR=30.0
Pod documented
359 name => { isa => 'Str' },
360 password => { isa => 'Str', optional => 1 },
361 admin => { isa => 'JSONBool', optional => 1 },
362 )->with('Method');
363 0 my($self, $args) = $rule->validate(@_);
364  
365 my $url = $self->_build_url(
366 path => '/db/' . $self->database . '/users/' . $args->{name},
367 0 );
368  
369 my $res = $self->{ua}->post($url, [], $self->json->encode({
370 exists $args->{password} ? (password => $args->{password}) : (),
371 0 ▶ exists $args->{admin} ? (admin => $args->{admin}) : (),
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'password'} ? : 0 0 0
exists $args->{'admin'} ? : 0 0 0
372 }));
373 0 $self->status($res);
374  
375 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
376 }
377  
378 sub list_database_users {
379 0 ▶ my $self = shift;
Subroutine list_database_users: not called CC=2 SCAR=17.9
Pod documented
380  
381 0 my $url = $self->_build_url(
382 path => '/db/' . $self->database . '/users',
383 );
384  
385 0 my $res = $self->{ua}->get($url);
386 0 $self->status($res);
387  
388 0 ▶ return $res->is_success ? $self->json->decode($res->content) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
389 }
390  
391 sub show_database_user {
392 0 ▶ state $rule = Data::Validator->new(
Subroutine show_database_user: not called CC=2 SCAR=17.9
Pod documented
393 name => { isa => 'Str' },
394 )->with('Method');
395 0 my($self, $args) = $rule->validate(@_);
396  
397 my $url = $self->_build_url(
398 path => '/db/' . $self->database . '/users/' . $args->{name},
399 0 );
400  
401 0 my $res = $self->{ua}->get($url);
402 0 $self->status($res);
403  
404 0 ▶ return $res->is_success ? $self->json->decode($res->content) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
405 }
406  
407 sub create_cluster_admin {
408 0 ▶ state $rule = Data::Validator->new(
Subroutine create_cluster_admin: not called CC=2 SCAR=17.9
Pod documented
409 name => { isa => 'Str' },
410 password => { isa => 'Str' },
411 )->with('Method');
412 0 my($self, $args) = $rule->validate(@_);
413  
414 0 my $url = $self->_build_url(
415 path => '/cluster_admins',
416 );
417  
418 my $res = $self->{ua}->post($url, [], $self->json->encode({
419 name => $args->{name},
420 password => $args->{password},
421 0 }));
422 0 $self->status($res);
423  
424 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
425 }
426  
427 sub delete_cluster_admin {
428 0 ▶ state $rule = Data::Validator->new(
Subroutine delete_cluster_admin: not called CC=2 SCAR=17.9
Pod documented
429 name => { isa => 'Str' },
430 )->with('Method');
431 0 my($self, $args) = $rule->validate(@_);
432  
433 my $url = $self->_build_url(
434 path => '/cluster_admins/' . $args->{name},
435 0 );
436  
437 0 my $res = $self->{ua}->delete($url);
438 0 $self->status($res);
439  
440 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
441 }
442  
443 sub update_cluster_admin {
444 0 ▶ state $rule = Data::Validator->new(
Subroutine update_cluster_admin: not called CC=3 SCAR=24.8
Pod documented
445 name => { isa => 'Str' },
446 password => { isa => 'Str' },
447 )->with('Method');
448 0 my($self, $args) = $rule->validate(@_);
449  
450 my $url = $self->_build_url(
451 path => '/cluster_admins/' . $args->{name},
452 0 );
453  
454 my $res = $self->{ua}->post($url, [], $self->json->encode({
455 0 ▶ exists $args->{password} ? (password => $args->{password}) : (),
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'password'} ? : 0 0 0
456 }));
457 0 $self->status($res);
458  
459 0 ▶ return $res->is_success ? 1 : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
460 }
461  
462 sub list_cluster_admins {
463 0 ▶ my $self = shift;
Subroutine list_cluster_admins: not called CC=2 SCAR=17.9
Pod documented
464  
465 0 my $url = $self->_build_url(
466 path => '/cluster_admins',
467 );
468  
469 0 my $res = $self->{ua}->get($url);
470 0 $self->status($res);
471  
472 0 ▶ return $res->is_success ? $self->json->decode($res->content) : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
473 }
474  
475 # sub show_cluster_admin {
476 # state $rule = Data::Validator->new(
477 # name => { isa => 'Str' },
478 # )->with('Method');
479 # my($self, $args) = $rule->validate(@_);
480  
481 # my $url = $self->_build_url(
482 # path => '/cluster_admins/' . $args->{name},
483 # );
484  
485 # my $res = $self->{ua}->get($url);
486 # $self->status($res);
487  
488 # return $res->is_success ? $self->json->decode($res->content) : ();
489 # }
490  
491 ### utils ################################################################
492 sub _build_url {
493 0 ▶ state $rule = Data::Validator->new(
Subroutine _build_url: not called CC=6 SCAR=37.4
494 path => { isa => 'Str' },
495 qs => { isa => 'HashRef', optional => 1 },
496 )->with('Method');
497 0 my($self, $args) = $rule->validate(@_);
498  
499 my $url = sprintf("%s://%s:%s@%s:%d%s",
500 ($self->ssl ? 'https' : 'http'),
501 $self->username,
502 $self->{password},
503 $self->host,
504 $self->port,
505 $args->{path},
506 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->ssl ? : 0 0 0
507 0 ▶ if (exists $args->{qs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"qs"}) 0 0 0
508 0 my @qs;
509 0 for my $k (keys %{ $args->{qs} }) {
510 0 ▶ next unless defined $args->{qs}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"qs"}{$k} 0 0 0
511 0 push @qs, join('=', uri_escape($k), uri_escape($args->{qs}{$k}));
512 }
513 0 ▶ $url .= '?' . join('&', @qs) if @qs;
Branch: 1 branch
BranchTrueFalseTotal
if @qs 0 0 0
514 }
515  
516 0 debugf("url: %s", $url);
517 0 return $url
518 }
519  
520 3 1;
521  
522 __END__
↑ Summary Build »