Module: PlantWatchdog::Datadetection
- Included in:
- UI::SinatraApp
- Defined in:
- lib/plantwatchdog/data.rb
Instance Method Summary collapse
- #days_with_data(user, year) ⇒ Object
- #plant_aggregates(plant, days) ⇒ Object
- #time_series(inverter, year, day_of_year) ⇒ Object
-
#utc_offset(user, secs_since_epoch) ⇒ Object
extract timeseries from the data chunks helpers for the presentation layer.
- #years_with_data(user) ⇒ Object
Instance Method Details
#days_with_data(user, year) ⇒ Object
26 27 28 29 30 |
# File 'lib/plantwatchdog/data.rb', line 26 def days_with_data(user, year) device_ids = user.plant.devices.collect { |d| d.id.to_s } rows = ActiveRecord::Base.connection.select_all("select distinct time_day_of_year from measurement_chunks where device_id in (#{device_ids.join(",")}) and time_year=#{year} order by time_day_of_year") return rows.collect {|r| r['time_day_of_year'].to_i} end |
#plant_aggregates(plant, days) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/plantwatchdog/data.rb', line 62 def plant_aggregates(plant, days) result = {} plant.aggrules.keys.each { |k| result[k] = [] } daymillis = 3600*24*1000 # TODO: check for changed aggrules days.each { | a | year = a.first yday = a.second year_start_millis = Time.utc(year, "jan", 1, 12, 0, 0).tv_sec * 1000 time_millis = year_start_millis + (yday-1)*daymillis chunk = Model::MeasurementAggregate.find(:first, :conditions => ["plant_id=? and time_year=? and time_day_of_year=?", plant.id, year, yday]) plant.aggrules.keys.each { |key| value = 0 if (chunk) then v = chunk.data[key] value = v if v end result[key] << [time_millis, value] } } return result end |
#time_series(inverter, year, day_of_year) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/plantwatchdog/data.rb', line 45 def time_series(inverter, year, day_of_year) chunk = Model::MeasurementChunk.find(:first, :conditions => ["device_id=? and time_year=? and time_day_of_year=?", inverter.id, year, day_of_year]) result= {} return result unless chunk # ENV["TZ"] must be set, does not work on windows utc_offset = chunk.measurements.empty? ? 0 : Time.at(chunk.measurements.first.time).utc_offset keys = chunk..collect {|m| m.first} - ["time"] keys.each { |key| result[key] = chunk.measurements.collect { |m| [(m.time + utc_offset) * 1000, m[key]] } } return result end |
#utc_offset(user, secs_since_epoch) ⇒ Object
extract timeseries from the data chunks helpers for the presentation layer
35 36 37 38 39 40 41 42 43 |
# File 'lib/plantwatchdog/data.rb', line 35 def utc_offset(user, secs_since_epoch) # mind that the offset also depends on daylight_saving_time # use libc to transform timezones, does not work on windows, # there is a pure ruby implementation, too ENV["TZ"] = user.timezone # eg "Europe/Berlin" offset = Time.at(secs_since_epoch).utc_offset ENV["TZ"] = "UTC" return offset end |
#years_with_data(user) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/plantwatchdog/data.rb', line 19 def years_with_data(user) start_year = user.start_year end_year = user.end_year return [] unless start_year and end_year return (start_year .. end_year).to_a end |