| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package ETL::Yertl::Command::yts; | 
| 2 |  |  |  |  |  |  | our $VERSION = '0.037'; | 
| 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 |  | 9 | use ETL::Yertl; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 12 |  | 
| 16 | 1 |  |  | 1 |  | 41 | use ETL::Yertl::Util qw( load_module ); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 52 |  | 
| 17 | 1 |  |  | 1 |  | 7 | use Getopt::Long qw( GetOptionsFromArray :config pass_through ); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 18 | 1 |  |  | 1 |  | 501 | use IO::Interactive qw( is_interactive ); | 
|  | 1 |  |  |  |  | 845 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub main { | 
| 21 | 7 |  |  | 7 | 0 | 15 | my $class = shift; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 7 |  |  |  |  | 12 | my %opt; | 
| 24 | 7 | 50 |  |  |  | 40 | if ( ref $_[-1] eq 'HASH' ) { | 
| 25 | 7 |  |  |  |  | 11 | %opt = %{ pop @_ }; | 
|  | 7 |  |  |  |  | 30 |  | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 7 |  |  |  |  | 17 | 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 |  |  |  |  | 2270 | my ( $db_spec, $metric ) = @args; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 7 | 100 |  |  |  | 22 | die "Must give a database\n" unless $db_spec; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 6 |  |  |  |  | 28 | my ( $db_type ) = $db_spec =~ m{^([^:]+):}; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 6 |  |  |  |  | 24 | my $db = load_module( adapter => $db_type )->new( $db_spec ); | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # Write metrics | 
| 48 | 6 | 50 |  |  |  | 60 | if ( !is_interactive( \*STDIN ) ) { | 
| 49 | 6 | 100 |  |  |  | 71 | if ( $opt{short} ) { | 
| 50 | 2 | 50 |  |  |  | 4 | die "Must give a metric\n" unless $metric; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 6 |  |  |  |  | 16 | my $in_fmt = load_module( format => 'default' )->new( input => \*STDIN ); | 
| 54 | 6 |  |  |  |  | 12 | my $count = 0; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 6 |  |  |  |  | 27 | 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 |  |  |  |  | 2 | 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 |  |  |  | 10 | ( $opt{tags} ? ( tags => $opt{tags} ) : () ), | 
| 68 |  |  |  |  |  |  | }; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | #; use Data::Dumper; | 
| 71 |  |  |  |  |  |  | #; print Dumper \@docs; | 
| 72 | 1 |  |  |  |  | 6 | $db->write_ts( @docs ); | 
| 73 | 1 |  |  |  |  | 8 | $count += @docs; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | else { | 
| 76 | 2 |  | 33 |  |  | 6 | $doc->{metric} ||= $metric; | 
| 77 | 2 | 50 | 0 |  |  | 5 | $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 |  |  |  |  | 26 | return 0; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | 1; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | __END__ |