-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimebucket.pl
executable file
·57 lines (54 loc) · 1.31 KB
/
timebucket.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env perl
use Getopt::Std;
$|=1;
my %options={};
getopts("ps:dz",\%options);
my $DEBUG = defined $options{d} ? 1 : 0;
my $bucketsize;
if (defined $options{s}) {
my $size = $options{s};
my $unit = lc chop $size;
print "size:$size\tunit:$unit\n" if $DEBUG;
if($unit eq 's'){
$bucketsize = $size*1000*1000;
}elsif($unit eq 'm'){
$bucketsize = $size*60*1000*1000;
}elsif($unit eq 'h'){
$bucketsize = $size*60*60*1000*1000;
}else{
$bucketsize = $size*10+$unit;
};
}else{
$bucketsize = 1000*1000;
}
print "bucketsize: $bucketsize\n" if $DEBUG;
#TODO - support [0-9]+[smhdwy]
my $sparse = defined $options{z} ? 1 : 0;
my $passthru = defined $options{p} ? 1 : 0;
my $thisPeriod = 0,
$vol = 0,
$spend = 0;
while(<>){
if(/^#/){
print $_ if $passthru;
}else{
print "#" . $_ if $passthru;
chomp;
my ($microtime, $price, $amount) = split(/\t/);
my $period = int $microtime/$bucketsize;
my $average = $vol ? $spend/$vol : 0;
print "period: $period\tthisPeriod:$thisPeriod\n" if $DEBUG;
while ($period > $thisPeriod) {
##print aggregates from last period
print "$thisPeriod\t$spend\t$vol\t$average\n" unless ($thisPeriod == 0);
if ($sparse || $thisPeriod == 0){
$thisPeriod = $period;
}else{
$thisPeriod++;
}
$vol = $spend = 0;
}
$vol += $amount;
$spend += $price*$amount;
}
}