-
Notifications
You must be signed in to change notification settings - Fork 1
/
solar-index
89 lines (70 loc) · 2.1 KB
/
solar-index
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
#!/usr/bin/env perl
use strict;
use warnings;
use JSON;
use Search::Elasticsearch;
use File::Slurper qw( read_text read_binary );
use Try::Tiny;
use Path::Tiny;
sub read_json {
my ($filename) = @_;
from_json( read_binary($filename) );
}
my $e = Search::Elasticsearch->new();
if ( $ENV{CREATE} ) {
try { $e->indices->delete( index => 'solar' ); };
$e->indices->create( index => 'solar' );
$e->indices->put_mapping(
index => 'solar',
# type => 'device',
#include_type_name => \1,
body => {
properties => {
p_3phsum_kw => { type => 'float' }, # AC power kWh
p_mpptsum_kw => { type => 'float' }, # AC power kWh
ltea_3phsum_kwh => { type => 'float' }, # total energy kWh
vln_3phavg_v => { type => 'float' }, # AC voltage
t_htsnk_degc => { type => 'float' }, # heatsink temp
}
}
);
exit;
}
my @files = @ARGV ? @ARGV : <STDIN>;
my $PROCESSED = 0;
for my $file_or_dir (@files) {
next unless -s $file_or_dir;
#warn "processing $file_or_dir\n";
if ( -d $file_or_dir ) {
my $dir = path($file_or_dir);
for my $f ( $dir->children ) {
# warn "processing $f\n";
process_file($f);
}
}
else {
process_file($file_or_dir);
}
}
# printf("Processed %s files\n", $PROCESSED);
sub process_file {
my $json_file = shift;
my $buf;
try { $buf = read_json($json_file) };
return unless $buf;
my ($id) = ( $json_file =~ m/([\d\-]+)\.json/ );
#warn $id;
# each device is a "document"
my $bulk = $e->bulk_helper( index => 'solar', );
for my $device ( @{ $buf->{devices} } ) {
my $id = join( "-", $id, $device->{SERIAL} );
my @dtime = split( ',', $device->{CURTIME} );
$device->{tstamp} = sprintf( "%s-%s-%sT%s:%s:%s", @dtime );
$bulk->index( { id => $id, source => $device } );
}
$bulk->flush;
$PROCESSED++;
if ( !$PROCESSED % 1000 ) {
warn "processed $PROCESSED\n";
}
}