diff options
author | pks <pks@pks.rocks> | 2021-05-16 18:23:12 +0200 |
---|---|---|
committer | pks <pks@pks.rocks> | 2021-05-16 18:23:12 +0200 |
commit | 8a89176b2b563ad20c21eea6f31cef01c46582dc (patch) | |
tree | 7e9f0a44e9d52d6ed2e957ca4b524b2fa3b61f6c /db-from-log.rb | |
parent | 899ba88b76a2f01b1bab87fbb960c2277435c521 (diff) |
restart
Diffstat (limited to 'db-from-log.rb')
-rwxr-xr-x | db-from-log.rb | 64 |
1 files changed, 0 insertions, 64 deletions
diff --git a/db-from-log.rb b/db-from-log.rb deleted file mode 100755 index dd4d379..0000000 --- a/db-from-log.rb +++ /dev/null @@ -1,64 +0,0 @@ -#!/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 |