File Coverage

blib/lib/Mojo/InfluxDB.pm
Criterion Covered Total %
statement 12 44 27.2
branch 0 4 0.0
condition n/a
subroutine 4 11 36.3
pod 3 3 100.0
total 19 62 30.6


line stmt bran cond sub pod time code
1             package Mojo::InfluxDB;
2             # ABSTRACT: Super simple InfluxDB async cappable client with a nice interface
3             $Mojo::InfluxDB::VERSION = '0.1';
4 1     1   102483 use Mojo::Base -base, -signatures;
  1         192864  
  1         10  
5 1     1   5056 use Mojo::Collection qw/ c /;
  1         4320  
  1         60  
6 1     1   597 use Mojo::UserAgent;
  1         270716  
  1         11  
7              
8 1     1   685 use Mojo::InfluxDB::Result;
  1         4  
  1         10  
9              
10             has host => 'localhost';
11             has port => 8086;
12             has database => sub { die "database ir required" };
13              
14             has ua => sub { Mojo::UserAgent->new };
15             has url => sub ( $self ) {
16             Mojo::URL->new( sprintf( 'http://%s:%s', $self->host, $self->port ) );
17             };
18              
19             has time_zone => undef;
20              
21 0     0 1   sub query ( $self, $query ) {
  0            
  0            
  0            
22 0           my ($results, $error);
23              
24             $self->query_p( $query )->then(sub {
25 0     0     $results = shift
26             })->catch( sub {
27 0     0     $error = shift
28 0           })->wait;
29              
30 0 0         die $error if $error;
31 0           $results;
32             }
33              
34 0     0 1   sub query_p ( $self, $query ) {
  0            
  0            
  0            
35 0     0     $self->raw_query_p( $query )->then(sub($tx){
  0            
  0            
36 0           c($tx->res->json('/results')->@*)->map(sub($src){
37 0           Mojo::InfluxDB::Result->new(
38             time_zone => $self->time_zone,
39             src => $src
40             );
41 0           });
42 0           });
43             }
44              
45 0     0 1   sub raw_query_p ( $self, $query ) {
  0            
  0            
  0            
46 0 0         $query = join( ';', @$query ) if $query eq 'ARRAY';
47 0           $self->ua->get_p(
48             $self->_url('query')->query({ q => $query, db => $self->database })
49             );
50             }
51              
52 0     0     sub _url ( $self, $action ) {
  0            
  0            
  0            
53 0           $self->url->path("/$action")->clone
54             }
55              
56             1;
57              
58             __END__
59              
60             =pod
61              
62             =encoding UTF-8
63              
64             =head1 NAME
65              
66             Mojo::InfluxDB - Super simple InfluxDB async cappable client with a nice interface
67              
68             =head1 VERSION
69              
70             version 0.1
71              
72             =head1 SYNOPSIS
73              
74             use Mojo::InfluxDB;
75              
76             my $client = Mojo::InfluxDB->new( database => 'telegraf' );
77              
78             my $result = $client->query('
79             SELECT last(state) AS state
80             FROM telegraf.thirty_days.mongodb
81             WHERE time > now() - 5h
82             GROUP BY time(1h), host
83             ');
84              
85             $result->first->points;
86              
87             =head1 DESCRIPTION
88              
89             We needed to do some async queries on our company InfluxDB instance and with some time this module has been growing. As it's useful for Us, it might also be useful for others so here I am releasing it.
90              
91             This is not yet a full implementation of an InfluxDB driver. I will be very happy to accept contributions and to modify anything about this group of classes, so be warned that this is "beta" quality and the interface will change if it's needed to implement new features or if me or someone else found a nicer way.
92              
93             =head1 ATTRIBUTES
94              
95             =head2 host
96              
97             Host of your InfluxDB instance: 'localhost' by default.
98              
99             =head2 port
100              
101             Port of your InfluxDB instance: 8086 by default.
102              
103             =head2 database
104              
105             The name of the database you want this client to send the queries. You can change it at any time.
106              
107             =head2 time_zone
108              
109             An optional time_zone to be passed into results which will finally allow L<InfluxDB::Point> to build L<DateTime> objects on your requested time_zone.
110              
111             =head1 METHODS
112              
113             =head2 query
114              
115             will run queries synchronously. See query_p().
116              
117             =head2 query_p
118              
119             will run queries asynchronously and return a promise to get a L<Mojo::Collection> of L<Mojo::InfluxDB::Result> objects.
120              
121             =head2 raw_query_p
122              
123             will run a query and return a L<Mojo::Transaction::HTTP>.
124              
125             =head1 BUGS
126              
127             As in any piece of software there might be bugs around.
128             If you found one, please report it at the github repo:
129              
130             L<https://github.com/gonzalo-radio/mojo-influxdb>
131              
132             Pull requests to fix bugs or add functionality are very welcomed, but please include
133             an explanation of what you want to achieve.
134              
135             =head1 TODO
136              
137             =for :list * Implement writing functionality so we can have real tests
138              
139             =head1 SEE ALSO
140              
141             =for :list * L<InfluxDB>
142             * L<InfluxDB::Writer>
143             * L<AnyEvent::InfluxDB>
144             * L<InfluxDB::Client::Simple>
145              
146             =head1 AUTHOR
147              
148             Gonzalo Radio <gonzalo@gnzl.net>
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is copyright (c) 2020 by Gonzalo Radio.
153              
154             This is free software; you can redistribute it and/or modify it under
155             the same terms as the Perl 5 programming language system itself.
156              
157             =cut