Class: PlantWatchdog::Aggregation::Device

Inherits:
Object
  • Object
show all
Includes:
RuleEvaluation
Defined in:
lib/plantwatchdog/aggregation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RuleEvaluation

#eval_rule

Constructor Details

#initialize(model_device, data) ⇒ Device

Returns a new instance of Device.



73
74
75
76
77
# File 'lib/plantwatchdog/aggregation.rb', line 73

def initialize(model_device, data)
  @model_device = model_device
  @aggregates = {}
  @data = data
end

Instance Attribute Details

#model_deviceObject

Returns the value of attribute model_device.



71
72
73
# File 'lib/plantwatchdog/aggregation.rb', line 71

def model_device
  @model_device
end

Class Method Details

.create(model_device, year, day) ⇒ Object



66
67
68
69
70
# File 'lib/plantwatchdog/aggregation.rb', line 66

def Device.create(model_device, year, day)
  # select data
  data = Model::MeasurementChunk.find(:first, :conditions => ["device_id=? and time_year=? and time_day_of_year=?", model_device.id, year, day])
  Device.new(model_device, data)
end

Instance Method Details

#aggregateObject

execute the aggregation rules of the device



98
99
100
101
102
103
104
105
106
107
# File 'lib/plantwatchdog/aggregation.rb', line 98

def aggregate
  result = {}
  logger.debug "Aggregating device #{model_device.id}, aggrules: #{model_device.aggrules}"
  model_device.aggrules.each_pair do
    |agg_key, rule_array|
    result[agg_key] = eval_rule(rule_array, measurements)
  end
  logger.debug "Aggregation results: " + result.to_s
  result
end

#aggregatesObject

return the dict with aggregated values



93
94
95
# File 'lib/plantwatchdog/aggregation.rb', line 93

def aggregates

end

#loggerObject

TODO: better way to access logger



116
117
118
# File 'lib/plantwatchdog/aggregation.rb', line 116

def logger
  return ActiveRecord::Base.logger
end

#measurementsObject



84
85
86
# File 'lib/plantwatchdog/aggregation.rb', line 84

def measurements
  @data ? @data.measurements : []
end

#metaObject



88
89
90
# File 'lib/plantwatchdog/aggregation.rb', line 88

def meta
  model_device.meta
end

#not_understandObject

generic access to the fields of the underlying model_device



80
81
82
# File 'lib/plantwatchdog/aggregation.rb', line 80

def not_understand
  # model_device
end

#persistObject



109
110
111
112
113
# File 'lib/plantwatchdog/aggregation.rb', line 109

def persist
  dm = Model::DailyMeasurement.new()
  dm.description = JSON(aggregates)
  return dm
end