forked from JackKelly/CurrentCostLogger
-
Notifications
You must be signed in to change notification settings - Fork 2
/
import-old-data
executable file
·110 lines (88 loc) · 3.39 KB
/
import-old-data
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/perl -w
#
# Quick script to do one-off import from history
#
use strict;
use Time::Piece;
use Time::Seconds;
my $process = $ARGV[0]; # months, days, hours
my $basetime = Time::Piece->strptime("$ARGV[1]", "%Y-%m-%d %H:%M:%S");
# Double-check these offsets if you're relying on them for something important
# An improvement would be to see what the oldest day and hour actually in the dataset are.
# Oops! see the notes file, this is not correct — according to me 3 years ago, the h/d/m000 would be the current hour/day/month.
my $finalHourEnd = Time::Piece->strptime($basetime->year . "-" . $basetime->mon . "-" . $basetime->mday . " " . $basetime->hour . ":00:00", "%Y-%m-%d %H:%M:%S") + ONE_HOUR;
my $firstHourStart = $basetime - 31 * ONE_DAY;
my $finalDayEnd = Time::Piece->strptime($basetime->year . "-" . $basetime->mon . "-" . $basetime->mday, "%Y-%m-%d") + ONE_DAY;
my $firstDayStart = $basetime - 89 * ONE_DAY;
$firstDayStart = Time::Piece->strptime($firstDayStart->ymd, "%Y-%m-%d");
my $finalMonthEnd = Time::Piece->strptime($basetime->year . "-" . $basetime->mon . "-01", "%Y-%m-%d")->add_months(1);
my $firstMonthStart = $finalMonthEnd->add_months(-83);
print "Log dump time " . $basetime->datetime . "\n";
print "\n";
print "Start of first hour " . $firstHourStart->datetime . "\n";
print "Start of first day " . $firstDayStart->datetime . "\n";
print "Start of first month " . $firstMonthStart->datetime . "\n";
print "\n";
print "End of final hour " . $finalHourEnd->datetime . "\n";
print "End of final day " . $finalDayEnd->datetime . "\n";
print "End of final month " . $finalMonthEnd->datetime . "\n";
while (<STDIN>) {
if ($process eq "hours") {
while (m!<h(\d+)>([.\d]+)</h\d+>!g) {
my $n = $1 - 2;
my $w = $2 / 2 * 1000;
if ($w != "0.000") {
my $start = $finalHourEnd - $n * ONE_HOUR - ONE_HOUR;
my $end = $finalHourEnd - $n * ONE_HOUR;
writeInterval($start, $end, $basetime, $w);
}
}
}
elsif ($process eq "days") {
while (m!<d(\d+)>([.\d]+)</d\d+>!g) {
my $n = $1 - 1;
my $w = $2 / 24 * 1000;
if ($w != "0.000") {
my $start = $finalDayEnd - $n * ONE_DAY - ONE_DAY;
my $end = $finalDayEnd - $n * ONE_DAY;
writeInterval($start, $end, $firstHourStart, $w);
}
}
}
elsif ($process eq "months") {
while (m!<m(\d+)>([.\d]+)</m\d+>!g) {
#print "matches $1 $2\n";
my $n = $1 - 1;
my $w = $2 / 24 / (365.25/12) * 1000;
if ($w != "0.000") {
my $start = $finalMonthEnd->add_months(- $n - 1);
my $end = $finalMonthEnd->add_months(- $n);
writeInterval($start, $end, $firstDayStart, $w);
}
}
}
else {
die "Interval is months, days or hours\n";
}
}
sub writeInterval {
(my $start, my $end, my $limit, my $power) = @_;
my $updates = "";
print "Period " . $start->datetime . " to " . $end->datetime . " has average power $power\n";
if ($start < $limit) {
my $t;
for ($t = $start->epoch; $t < $end->epoch && $t < $limit->epoch; $t += 5) {
if (length($updates) > 10000) {
print qx(rrdtool update newpowertemp.rrd $updates\n);
$updates = "";
}
$updates .= "$t:$power:U ";
}
print qx(rrdtool update newpowertemp.rrd $updates\n);
}
else {
print "Skipping, beyond limit of this interval\n";
}
}
# And to import from data log
# perl -ne '$_ =~ m/(\d+)\t(\d+).*\t([0-9.]+)?/; print "$1\n"; qx(rrdtool update newpowertemp.rrd $1:$2:$3\n);' < data.txt