Created
August 14, 2016 07:57
-
-
Save yukirii/bc1985a1341d7bd255d828ff3d664a26 to your computer and use it in GitHub Desktop.
influxdb gem sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'influxdb' | |
require 'time' | |
class Time | |
def timezone(timezone = 'UTC') | |
old = ENV['TZ'] | |
utc = self.dup.utc | |
ENV['TZ'] = timezone | |
output = utc.localtime | |
ENV['TZ'] = old | |
output | |
end | |
end | |
database = 'electric_usage' | |
influxdb = InfluxDB::Client.new(database, | |
host: '192.168.x.x', | |
time_precision: 's') | |
File.open('2016_usage.csv', 'r') do |file| | |
file.each_line do |line| | |
p data = line.chomp.split(',') | |
time = Time.parse(data[0].split('+')[0]).timezone('Asia/Tokyo').to_i | |
value = data[1].to_f | |
name = 'usage' | |
write_data = { values: { value: value }, timestamp: time } | |
influxdb.write_point('usage', write_data) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment