Class: PlantWatchdog::Aggregation::Runner
- Inherits:
-
Object
- Object
- PlantWatchdog::Aggregation::Runner
- Defined in:
- lib/plantwatchdog/aggregation.rb
Instance Method Summary collapse
- #aggregate(model_plant, year, day_of_year) ⇒ Object
- #find_missing_aggregates ⇒ Object
-
#logger ⇒ Object
TODO: better way to access logger.
- #run ⇒ Object
Instance Method Details
#aggregate(model_plant, year, day_of_year) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/plantwatchdog/aggregation.rb', line 180 def aggregate(model_plant, year, day_of_year) env = AggregationEnv.new(year, day_of_year) model_plant.devices.each { |model_device| logger.debug("Adding device " + model_device.to_s) env.devices << Device.create(model_device, year, day_of_year) } # build the aggregates for the devices first ... aggregates = env.devices.collect do |device| daily = Model::MeasurementAggregate.new daily.device = device.model_device daily.time_year = year daily.time_day_of_year = day_of_year daily.data = device.aggregate daily end # ... and then aggregate the plant gen_aggregates = Plant.new(model_plant, aggregates.collect{|ma| ma.data}).aggregate gen_aggregate = Model::MeasurementAggregate.new gen_aggregate.data = gen_aggregates gen_aggregate.time_year = year gen_aggregate.time_day_of_year = day_of_year gen_aggregate.plant = model_plant # save when everything has been calculated aggregates << gen_aggregate aggregates.each {|a| a.save} aggregates end |
#find_missing_aggregates ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/plantwatchdog/aggregation.rb', line 147 def find_missing_aggregates sql = "select time_year, time_day_of_year, plant_id from\n(select CHUNK.time_year, CHUNK.time_day_of_year, CHUNK.device_id AS device_id, AGG.device_id AS agg_device_id from\n((select time_year, time_day_of_year, device_id from measurement_chunks where type=\"MeasurementChunk\" order by time_year, time_day_of_year, device_id) AS CHUNK \nLEFT OUTER JOIN\n(select time_year, time_day_of_year, device_id from measurement_chunks where type=\"MeasurementAggregate\") AS AGG \nON CHUNK.time_year = AGG.time_year AND CHUNK.time_day_of_year=AGG.time_day_of_year AND CHUNK.device_id = AGG.device_id)\nwhere AGG.device_id IS NULL) AS MISSING\nINNER JOIN\ndevices\nON MISSING.device_id = devices.id\nGROUP BY MISSING.time_year, MISSING.time_day_of_year; \n" rows = ActiveRecord::Base.connection.select_all(sql) result = [] rows.collect { |r| time_year = r["time_year"].to_i time_day_of_year = r["time_day_of_year"].to_i plant_id = r["plant_id"].to_i [time_year, time_day_of_year, plant_id] } end |
#logger ⇒ Object
TODO: better way to access logger
214 215 216 |
# File 'lib/plantwatchdog/aggregation.rb', line 214 def logger return ActiveRecord::Base.logger end |
#run ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/plantwatchdog/aggregation.rb', line 172 def run find_missing_aggregates.each { |m| time_year, time_day_of_year, plant_id = m aggregate(Model::Plant.find_by_id(plant_id), time_year, time_day_of_year) } end |