Skip to content

Instantly share code, notes, and snippets.

@yukirii
Created August 14, 2016 07:57
Show Gist options
  • Save yukirii/bc1985a1341d7bd255d828ff3d664a26 to your computer and use it in GitHub Desktop.
Save yukirii/bc1985a1341d7bd255d828ff3d664a26 to your computer and use it in GitHub Desktop.
influxdb gem sample
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