Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Last active April 28, 2016 20:09
Show Gist options
  • Save jeffdeville/0eb3d7e92c48f8676cd1bf1ab9ca5abb to your computer and use it in GitHub Desktop.
Save jeffdeville/0eb3d7e92c48f8676cd1bf1ab9ca5abb to your computer and use it in GitHub Desktop.
InfluxDB issues - ruby 2.2.0
#!/usr/bin/env ruby
require "influxdb"
@dbname = "connector_test"
@series = "units_all"
def gen_data_point(i)
{
# series: @series,
tags: {
device_unit_id: ("UNIT_%03d" % i)
},
values: {
consumed_energy: 5.0,
frequency: 5.0,
power_factor: 5.0,
reactive_watts: 5.0,
rms_voltage: 5.0,
temperature: 5.0,
},
timestamp: Time.new(2016, 4, 28, i%24, i/24).to_i
}
end
cli = InfluxDB::Client.new \
database: @dbname,
# username: "root",
# password: "root",
time_precision: "s"
# epoch: "s"
puts "influxdb-ruby: #{InfluxDB::VERSION}"
puts "influx server: #{cli.version}", ""
puts "purging existing data..."
cli.delete_database(@dbname) if cli.list_databases.flat_map(&:values).include?(@dbname)
cli.create_database(@dbname)
cli.create_retention_policy("units_all", @dbname, 'INF', 1)
puts "generating data:"
data = (0..4).map{|i| gen_data_point(i) }.each do |point|
tags, values = point[:tags], point[:values]
puts " #{tags[:device_unit_id]} : #{values.inspect}"
end
puts "writing data... "
data.each do |data_pt|
cli.write_point "units_all", data_pt
end
# cli.write_points data
puts "reading data:"
cli.query("select * from #{@series}") do |name, tags, values|
p values
# values.each do |val|
# puts " #{tags['device_unit_id']} : #{val.inspect}"
# end
end
GIT
remote: git://github.com/activeadmin/activeadmin.git
revision: d84f8190edac8c28f942cd4b6c3e16086da9d7a2
specs:
activeadmin (1.0.0.pre2)
arbre (~> 1.0, >= 1.0.2)
bourbon
coffee-rails
formtastic (~> 3.1)
formtastic_i18n
inherited_resources (~> 1.6)
jquery-rails
jquery-ui-rails
kaminari (~> 0.15)
rails (>= 3.2, < 5.0)
ransack (~> 1.3)
sass-rails
sprockets (< 4)
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.0.13)
actionpack (= 4.0.13)
mail (~> 2.5, >= 2.5.4)
actionpack (4.0.13)
activesupport (= 4.0.13)
builder (~> 3.1.0)
erubis (~> 2.7.0)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
activemodel (4.0.13)
activesupport (= 4.0.13)
builder (~> 3.1.0)
activerecord (4.0.13)
activemodel (= 4.0.13)
activerecord-deprecated_finders (~> 1.0.2)
activesupport (= 4.0.13)
arel (~> 4.0.0)
activerecord-deprecated_finders (1.0.4)
activesupport (4.0.13)
i18n (~> 0.6, >= 0.6.9)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.4.0)
ansi (1.5.0)
arbre (1.0.3)
activesupport (>= 3.0.0)
arel (4.0.2)
autoprefixer-rails (6.3.6)
execjs
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
bcrypt (3.1.10)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
bootstrap-sass (3.3.5)
autoprefixer-rails (>= 5.0.0.1)
sass (>= 3.2.19)
bourbon (3.2.4)
sass (~> 3.2)
thor
builder (3.1.4)
bullet (4.14.7)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.9.0)
byebug (8.2.2)
cancan (1.6.10)
capybara (2.6.2)
addressable
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
childprocess (0.5.9)
ffi (~> 1.0, >= 1.0.11)
cliver (0.3.2)
coderay (1.1.0)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.9.1.1)
concurrent-ruby (1.0.1)
connection_pool (2.2.0)
database_cleaner (1.4.1)
debug_inspector (0.0.2)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
devise (3.4.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
responders
thread_safe (~> 0.1)
warden (~> 1.2.3)
diff-lcs (1.2.5)
domain_name (0.5.20160216)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.1.0)
dotenv-rails (2.1.0)
dotenv (= 2.1.0)
railties (>= 4.0, < 5.1)
draper (2.1.0)
actionpack (>= 3.0)
activemodel (>= 3.0)
activesupport (>= 3.0)
request_store (~> 1.0)
elasticsearch (1.0.15)
elasticsearch-api (= 1.0.15)
elasticsearch-transport (= 1.0.15)
elasticsearch-api (1.0.15)
multi_json
elasticsearch-extensions (0.0.20)
ansi
ruby-prof
elasticsearch-model (0.1.8)
activesupport (> 3)
elasticsearch (> 0.4)
hashie
elasticsearch-persistence (0.1.8)
activemodel (> 3)
activesupport (> 3)
elasticsearch (> 0.4)
elasticsearch-model (>= 0.1)
hashie
virtus
elasticsearch-rails (0.1.8)
elasticsearch-transport (1.0.15)
faraday
multi_json
equalizer (0.0.11)
erubis (2.7.0)
execjs (2.6.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.6.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faker (1.4.3)
i18n (~> 0.5)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.10)
formatador (0.2.5)
formtastic (3.1.4)
actionpack (>= 3.2.13)
formtastic_i18n (0.6.0)
fuubar (2.0.0)
rspec (~> 3.0)
ruby-progressbar (~> 1.4)
geocoder (1.2.8)
gmaps4rails (2.1.2)
guard (2.13.0)
formatador (>= 0.2.4)
listen (>= 2.7, <= 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-bundler (2.1.0)
bundler (~> 1.0)
guard (~> 2.2)
guard-compat (~> 1.1)
guard-compat (1.2.1)
guard-rspec (4.6.5)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
has_scope (0.6.0)
actionpack (>= 3.2, < 5)
activesupport (>= 3.2, < 5)
hashie (3.4.3)
hike (1.2.3)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.7.0)
ice_nine (0.11.2)
influxdb (0.3.0)
json
inherited_resources (1.6.0)
actionpack (>= 3.2, < 5)
has_scope (~> 0.6.0.rc)
railties (>= 3.2, < 5)
responders
jbuilder (1.5.3)
activesupport (>= 3.0.0)
multi_json (>= 1.2.0)
jquery-rails (3.1.2)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (5.0.5)
railties (>= 3.2.16)
json (1.8.3)
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
kgio (2.9.3)
launchy (2.4.3)
addressable (~> 2.3)
listen (3.0.6)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9.7)
lograge (0.3.6)
actionpack (>= 3)
activesupport (>= 3)
railties (>= 3)
lumberjack (1.0.10)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.99.1)
mini_portile2 (2.0.0)
minitest (4.7.5)
multi_json (1.11.2)
multipart-post (2.0.0)
nenv (0.3.0)
netrc (0.11.0)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
notiffany (0.0.8)
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
pg (0.18.1)
poltergeist (1.9.0)
capybara (~> 2.1)
cliver (~> 0.3.1)
multi_json (~> 1.0)
websocket-driver (>= 0.2.0)
polyamorous (1.3.0)
activerecord (>= 3.0)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.3.0)
byebug (~> 8.0)
pry (~> 0.10)
pry-rails (0.3.4)
pry (>= 0.9.10)
pry-stack_explorer (0.4.9.2)
binding_of_caller (>= 0.7)
pry (>= 0.9.11)
pundit (0.3.0)
activesupport (>= 3.0.0)
quiet_assets (1.1.0)
railties (>= 3.1, < 5.0)
rack (1.5.5)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
rack-timeout (0.2.4)
rails (4.0.13)
actionmailer (= 4.0.13)
actionpack (= 4.0.13)
activerecord (= 4.0.13)
activesupport (= 4.0.13)
bundler (>= 1.3.0, < 2.0)
railties (= 4.0.13)
sprockets-rails (~> 2.0)
rails_12factor (0.0.3)
rails_serve_static_assets
rails_stdout_logging
rails_serve_static_assets (0.0.4)
rails_stdout_logging (0.0.3)
railties (4.0.13)
actionpack (= 4.0.13)
activesupport (= 4.0.13)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.13.0)
rake (11.1.2)
ransack (1.7.0)
actionpack (>= 3.0)
activerecord (>= 3.0)
activesupport (>= 3.0)
i18n
polyamorous (~> 1.2)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
rdoc (4.2.0)
redis (3.2.2)
request_store (1.1.0)
responders (1.1.2)
railties (>= 3.2, < 4.2)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rickshaw_rails (1.4.5)
railties (>= 3.1.0)
rspec (3.2.0)
rspec-core (~> 3.2.0)
rspec-expectations (~> 3.2.0)
rspec-mocks (~> 3.2.0)
rspec-core (3.2.3)
rspec-support (~> 3.2.0)
rspec-expectations (3.2.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-mocks (3.2.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-rails (3.2.1)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.2.0)
rspec-expectations (~> 3.2.0)
rspec-mocks (~> 3.2.0)
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
ruby-prof (0.15.9)
ruby-progressbar (1.7.5)
rubyzip (1.2.0)
rufus-scheduler (3.1.10)
sass (3.2.19)
sass-rails (4.0.5)
railties (>= 4.0.0, < 5.0)
sass (~> 3.2.2)
sprockets (~> 2.8, < 3.0)
sprockets-rails (~> 2.0)
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
selenium-webdriver (2.52.0)
childprocess (~> 0.5)
multi_json (~> 1.0)
rubyzip (~> 1.0)
websocket (~> 1.0)
shellany (0.0.1)
sidekiq (4.1.1)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
redis (~> 3.2, >= 3.2.1)
sidekiq-scheduler (2.0.6)
multi_json (~> 1)
redis (~> 3)
rufus-scheduler (~> 3.1.8)
sidekiq (>= 3)
tilt (>= 1.4.0)
sinatra (1.4.7)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
slim (2.1.0)
temple (~> 0.6.9)
tilt (>= 1.3.3, < 2.1)
slim-rails (2.0.1)
actionpack (>= 3.0, < 4.1)
activesupport (>= 3.0, < 4.1)
railties (>= 3.0, < 4.1)
slim (~> 2.0)
slop (3.6.0)
sprockets (2.12.3)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.2.4)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
temple (0.6.10)
thor (0.19.1)
thread_safe (0.3.5)
tilt (1.4.1)
timecop (0.8.0)
tzinfo (0.3.48)
uglifier (2.7.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
underscore-rails (1.8.2)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
unicorn (4.8.3)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
uniform_notifier (1.9.0)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
warden (1.2.3)
rack (>= 1.0)
websocket (1.2.2)
websocket-driver (0.6.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
xpath (2.0.0)
nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
activeadmin!
better_errors
binding_of_caller
bootstrap-sass
bullet
cancan
capybara
coffee-rails (~> 4.0.0)
database_cleaner
devise
dotenv-rails
draper
elasticsearch
elasticsearch-extensions
elasticsearch-model
elasticsearch-persistence
elasticsearch-rails
factory_girl_rails
faker
fuubar
geocoder
gmaps4rails
guard
guard-bundler
guard-rspec
influxdb
jbuilder (~> 1.2)
jquery-rails
launchy
lograge
minitest
pg
poltergeist
pry-byebug
pry-rails
pry-stack_explorer
pundit
quiet_assets
rack-timeout
rails (~> 4.0)
rails_12factor
rest-client
rickshaw_rails
rspec
rspec-rails
sass-rails (~> 4.0.0)
sdoc
selenium-webdriver
sidekiq (~> 4.1)
sidekiq-scheduler (~> 2.0)
sinatra
slim-rails (~> 2.0)
timecop
uglifier (>= 1.3.0)
underscore-rails
unicorn
BUNDLED WITH
1.11.2
class InfluxRepo
attr_reader :client
# This is woefully inefficient
def initialize
InfluxDB::Logging.logger = Logger.new(STDOUT)
@client = InfluxDB::Client.new("connectder_test", time_precision: 'ns')
end
def save(inf_sample_entry)
pp to_point(inf_sample_entry)
pp @client.write_point "units_all", to_point(inf_sample_entry)
end
def find(query = "select * from units_all")
results = @client.query(query)
results.first["values"].map{ |result| from_point(result) }
end
def drop_series(series="units_all")
@client.query("DROP SERIES FROM #{series}")
end
def count(where="")
results = @client.query("select COUNT(active_watts) from units_all where #{where}")
results.first.fetch("values").first.fetch("count")
end
VALUE_FIELDS = %i(active_watts consumed_energy frequency power_factor reactive_watts rms_current rms_voltage temperature)
def to_point(inf_sample_entry)
{
tags: { device_unit_id: inf_sample_entry.device_unit_id },
values: inf_sample_entry.attributes.slice(*VALUE_FIELDS.select{|f| inf_sample_entry.attributes[f].present? }),
timestamp: inf_sample_entry.sampled_at * 1000000000
}
end
def from_point(result)
result["sampled_at"] = result.delete("time")
InfSampleEntry.new(result)
end
end
class InfSampleEntry
include Virtus.model
attribute :active_watts, Float
attribute :consumed_energy, Float
attribute :device_unit_id, String, mapping: { type: "string", index: "not_analyzed" }
attribute :unit_serial_number, String, mapping: { type: "string", index: "not_analyzed" }
attribute :frequency, Float
attribute :power_factor, Float
attribute :reactive_watts, Float
attribute :rms_current, Float
attribute :rms_voltage, Float
attribute :sampled_at, Time, mapping: { type: "date", format: "date_time" }
attribute :temperature, Float
def save
InfluxRepo.new.save(self)
end
def self.create(params)
InfSampleEntry.new(params).save
end
end
require 'spec_helper'
describe InfSampleEntry do
before do
repo = InfluxRepo.new
repo.drop_series
end
describe ".delete_for_units" do
it "deletes all samples for given Unit serial numbers", :focus do
start_time = Time.now
5.times do |x|
entry = InfSampleEntry.new(
consumed_energy: 5, frequency: 5, power_factor: 5, rms_voltage: 5, temperature: 5,
reactive_watts: 5, active_watts: 5, device_unit_id: "UNIT_00#{x}", sampled_at: (Time.now + x.hours).to_i)
entry.save
end
require 'pry'; binding.pry
expect(InfSampleEntry.count_by(device_unit_id: 'UNIT_003')).to eq 5
# Only 1 entry is in the database, not 5.
# InfSampleEntry.delete_for_units(["UNIT_003"])
# expect(InfSampleEntry.count_by(device_unit_id: 'UNIT_003')).to eq 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment