line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ETL::Yertl::Command::yts; |
2
|
|
|
|
|
|
|
our $VERSION = '0.035'; |
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
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
10
|
|
16
|
1
|
|
|
1
|
|
299
|
use ETL::Yertl::Util qw( load_module ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
17
|
1
|
|
|
1
|
|
6
|
use Getopt::Long qw( GetOptionsFromArray :config pass_through ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
18
|
1
|
|
|
1
|
|
354
|
use IO::Interactive qw( is_interactive ); |
|
1
|
|
|
|
|
664
|
|
|
1
|
|
|
|
|
5
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub main { |
21
|
7
|
|
|
7
|
0
|
13
|
my $class = shift; |
22
|
|
|
|
|
|
|
|
23
|
7
|
|
|
|
|
9
|
my %opt; |
24
|
7
|
50
|
|
|
|
18
|
if ( ref $_[-1] eq 'HASH' ) { |
25
|
7
|
|
|
|
|
25
|
%opt = %{ pop @_ }; |
|
7
|
|
|
|
|
24
|
|
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
7
|
|
|
|
|
26
|
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
|
|
|
|
|
2246
|
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
|
|
|
|
|
18
|
my $db = load_module( adapter => $db_type )->new( $db_spec ); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Write metrics |
48
|
6
|
50
|
|
|
|
62
|
if ( !is_interactive( \*STDIN ) ) { |
49
|
6
|
100
|
|
|
|
65
|
if ( $opt{short} ) { |
50
|
2
|
50
|
|
|
|
7
|
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
|
|
|
|
|
10
|
my $count = 0; |
55
|
|
|
|
|
|
|
|
56
|
6
|
|
|
|
|
19
|
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
|
|
|
|
8
|
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
|
|
|
|
|
5
|
$db->write_ts( @docs ); |
73
|
1
|
|
|
|
|
9
|
$count += @docs; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
else { |
76
|
2
|
|
33
|
|
|
5
|
$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
|
|
|
|
|
25
|
return 0; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |