-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.pl
36 lines (28 loc) · 918 Bytes
/
load_data.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
use DBI;
use strict;
use warnings;
use Data::Dumper;
# this was the price per kwh ages ago
use constant price_per_kwh => 0.1617;
my @files;
if (!defined($ARGV[0])) {
@files = glob "*.csv";
print join(",", @files), "\n";
} else {
@files = $ARGV[0];
}
# Set DBI_DSN, DBI_USER and DBI_PASS
# I use DBD::ODBC of course
my $h = DBI->connect();
my $s = $h->prepare(q/insert into home_electricity (unixtime, watts) values(?,?)/);
foreach (@files) {
open(my $fd, "<", $_) or die "failed to open $ARGV[0]";
while(<$fd>) {
my @values = split(',', $_);
$s->execute($values[0], $values[1]);
}
close $fd;
}
$s = q|select sum(watts) as watts, sum(watts) / 1000 * 0.1617 as cost, convert(varchar(10), dateadd(ss,unixtime,'1970-01-01'), 103) as "date" from home_electricity group by convert(varchar(10), dateadd(ss,unixtime,'1970-01-01'), 103)|;
my $r = $h->selectall_arrayref($s);
print Dumper($r);