summaryrefslogtreecommitdiff
path: root/db-from-log.rb
blob: dd4d379eee1c6bac002216667d957aee16be486a (plain)
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
#!/usr/bin/env ruby

require 'json'
require 'sqlite3'
require 'time'

def db_execute db, s, d
  begin
    db.execute(s, d)
    rescue SQLite3::BusyException
      sleep 3
      begin
        db.execute(s, d)
      rescue SQLite3::BusyException
        STDERR.write "DB busy, skipping data point '#{d.to_s}'\n"
      end
  end
end

def parse_log_line line
  _, topic, data = line.split "\t"
  topic_parts = topic.split "/"
  if topic_parts.size == 5 # old format, no hostname in topic
    _, _, device_location_1, device_location_2, _ = topic_parts
    device_name = ""
  else
    _, device_name, _, device_location_1, device_location_2, _ = topic_parts
  end
  device_location = [device_location_1, device_location_2]
  data.gsub!("'", '"')
  data = JSON.parse data

  return device_name, device_location, data
end

def insert_power db, device_name, device_location, data
  if data.has_key? "ENERGY"
    timestamp = Time.parse(data["Time"]).utc.to_i
    total_start_time = Time.parse(data["ENERGY"]["TotalStartTime"]).utc.to_i
    db_execute db, \
      "INSERT INTO power(timestamp, device_name, device_location_primary, device_location_secondary, total, total_start_time) VALUES(?,?,?,?,?,?)", \
      [timestamp, device_name, device_location[0], device_location[1], data["ENERGY"]["Total"], total_start_time]
  end
end

def main
  db = SQLite3::Database.open ARGV[0]
  while line = STDIN.gets
    begin
      device_name, device_location, data = parse_log_line line
      insert_power db, device_name, device_location, data
    rescue
      STDERR.write "Cannot insert log entry '#{line.strip}'\n"
    end
    if File.exist? "#{File.expand_path(File.dirname(__FILE__))}/stop"
      STDERR.write "Stopping loop\n"
      break
    end
  end
  db.close
end


main