Last active
August 29, 2015 14:02
-
-
Save yoggy/fba076c2b187ee65b594 to your computer and use it in GitHub Desktop.
InfluxDBから温度・湿度を取得してグラフ画像を生成&画像をGyazoにアップするサンプル
This file contains hidden or 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
#!/usr/bin/ruby | |
# -*- encoding: utf-8 -*- | |
# | |
# sensor_gruff.rb - create line charts & upload images to gyazo | |
# | |
# $ sudo apt-get install rmagic libmagickcore-dev libmagickwand-dev | |
# $ sudo gem install gruff | |
# $ gem install gyazo | |
# | |
require 'rubygems' | |
require 'influxdb' | |
require 'gruff' | |
require 'gyazo' | |
# configure | |
influxdb_params = { | |
:host => 'influxdb.local', | |
:username => 'username', | |
:password => 'password', | |
} | |
database = 'sensorsdb' | |
influxdb = InfluxDB::Client.new(database, influxdb_params) | |
query_str = 'select time, mean(temperature) as temperature, mean(humidity) as humidity from office_dht11 group by time(180m) where time > now() - 5d order asc' | |
gyazo = Gyazo::Client.new | |
# | |
# draw temperature line chart | |
# | |
g = Gruff::Line.new("480x270") | |
g.theme = { | |
:colors => %w(orange), | |
:font_color => 'black', | |
:marker_color => 'black', | |
:background_colors => %w(white white) | |
} | |
idx = 0; | |
arr = [] | |
influxdb.query query_str do |name, res| | |
res.each do |h| | |
t = Time.at(h["time"]) | |
if t.hour == 0 | |
g.labels[idx] = Time.at(h["time"]).strftime("%m/%d") | |
elsif | |
g.labels[idx] = " " | |
end | |
arr << h["temperature"].round(2) | |
idx += 1 | |
end | |
end | |
g.data :temperature, arr | |
g.minimum_value = arr.min.round() - 1 | |
g.maximum_value = arr.max.round() + 1 | |
g.y_axis_increment = 1 | |
g.write("temperature.png"); | |
url = gyazo.upload("temperature.png", :raw => true); | |
puts url+".png" | |
# | |
# draw humidity line chart | |
# | |
g = Gruff::Line.new("480x270") | |
g.theme = { | |
:colors => %w(cyan), | |
:font_color => 'black', | |
:marker_color => 'black', | |
:background_colors => %w(white white) | |
} | |
idx = 0; | |
arr = [] | |
influxdb.query query_str do |name, res| | |
res.each do |h| | |
t = Time.at(h["time"]) | |
if t.hour == 0 | |
g.labels[idx] = Time.at(h["time"]).strftime("%m/%d") | |
elsif | |
g.labels[idx] = " " | |
end | |
arr << h["humidity"].round(2) | |
idx += 1 | |
end | |
end | |
g.data :humidity, arr | |
g.write("humidity.png"); | |
url = gyazo.upload("humidity.png", :raw => true); | |
puts url+".png" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment