File Coverage

blib/lib/ETL/Yertl/Command/yts.pm
Criterion Covered Total %
statement 41 49 83.6
branch 11 20 55.0
condition 1 6 16.6
subroutine 5 5 100.0
pod 0 1 0.0
total 58 81 71.6


line stmt bran cond sub pod time code
1             package ETL::Yertl::Command::yts;
2             our $VERSION = '0.036';
3             # ABSTRACT: Read/Write time series data
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod =head1 DESCRIPTION
8             #pod
9             #pod =head1 SEE ALSO
10             #pod
11             #pod L
12             #pod
13             #pod =cut
14              
15 1     1   6 use ETL::Yertl;
  1         2  
  1         10  
16 1     1   31 use ETL::Yertl::Util qw( load_module );
  1         2  
  1         67  
17 1     1   7 use Getopt::Long qw( GetOptionsFromArray :config pass_through );
  1         2  
  1         4  
18 1     1   372 use IO::Interactive qw( is_interactive );
  1         715  
  1         5  
19              
20             sub main {
21 7     7 0 16 my $class = shift;
22              
23 7         10 my %opt;
24 7 50       21 if ( ref $_[-1] eq 'HASH' ) {
25 7         17 %opt = %{ pop @_ };
  7         16  
26             }
27              
28 7         19 my @args = @_;
29 7         20 GetOptionsFromArray( \@args, \%opt,
30             'start=s',
31             'end=s',
32             'short|s',
33             'tags=s%',
34             );
35             #; use Data::Dumper;
36             #; say Dumper \@args;
37             #; say Dumper \%opt;
38              
39 7         2509 my ( $db_spec, $metric ) = @args;
40              
41 7 100       23 die "Must give a database\n" unless $db_spec;
42              
43 6         45 my ( $db_type ) = $db_spec =~ m{^([^:]+):};
44              
45 6         22 my $db = load_module( adapter => $db_type )->new( $db_spec );
46              
47             # Write metrics
48 6 50       54 if ( !is_interactive( \*STDIN ) ) {
49 6 100       78 if ( $opt{short} ) {
50 2 50       6 die "Must give a metric\n" unless $metric;
51             }
52              
53 6         15 my $in_fmt = load_module( format => 'default' )->new( input => \*STDIN );
54 6         11 my $count = 0;
55              
56 6         20 while ( my @docs = $in_fmt->read ) {
57 2         7 for my $doc ( @docs ) {
58             #; use Data::Dumper
59             #; say "Got doc: " . Dumper $doc;
60 3 100       7 if ( $opt{short} ) {
61 1         3 my @docs;
62 1         5 for my $stamp ( sort keys %$doc ) {
63             push @docs, {
64             timestamp => $stamp,
65             metric => $metric,
66             value => $doc->{ $stamp },
67 2 50       11 ( $opt{tags} ? ( tags => $opt{tags} ) : () ),
68             };
69             }
70             #; use Data::Dumper;
71             #; print Dumper \@docs;
72 1         4 $db->write_ts( @docs );
73 1         16 $count += @docs;
74             }
75             else {
76 2   33     6 $doc->{metric} ||= $metric;
77 2 50 0     6 $doc->{tags} ||= $opt{tags} if $opt{tags};
78 2         7 $db->write_ts( $doc );
79 2         12 $count++;
80             }
81             }
82             }
83              
84             #; say "Wrote $count points";
85             }
86             # Read metrics
87             else {
88 0 0       0 die "Must give a metric\n" unless $metric;
89 0         0 my $out_fmt = load_module( format => 'default' )->new;
90             my @points = $db->read_ts( {
91             metric => $metric,
92             tags => $opt{tags},
93             start => $opt{start},
94             end => $opt{end},
95 0         0 } );
96 0 0       0 if ( $opt{short} ) {
97 0         0 my %ts = map { $_->{timestamp} => $_->{value} } @points;
  0         0  
98 0         0 print $out_fmt->write( \%ts );
99             }
100             else {
101 0         0 print $out_fmt->write( $_ ) for @points;
102             }
103             }
104              
105 6         24 return 0;
106             }
107              
108             1;
109              
110             __END__