Class: OpenC3::ReducerMicroservice
- Defined in:
- lib/openc3/microservices/reducer_microservice.rb,
ext/openc3/ext/reducer_microservice/reducer_microservice.c
Constant Summary collapse
- MINUTE_METRIC =
'reducer_minute_processing'
- HOUR_METRIC =
'reducer_hour_processing'
- DAY_METRIC =
'reducer_day_processing'
- SHUTDOWN_DELAY_SECS =
How long to wait for any currently running jobs to complete before killing them
5
- MINUTE_ENTRY_NSECS =
60 * 1_000_000_000
- MINUTE_FILE_NSECS =
3600 * 1_000_000_000
- HOUR_ENTRY_NSECS =
3600 * 1_000_000_000
- HOUR_FILE_NSECS =
3600 * 24 * 1_000_000_000
- DAY_ENTRY_NSECS =
3600 * 24 * 1_000_000_000
- DAY_FILE_NSECS =
3600 * 24 * 5 * 1_000_000_000
Instance Method Summary collapse
- #check_new_file(reducer_state, plw, type, target_name, stored, current_time, file_nanoseconds) ⇒ Object
-
#extract_entry_samples(packet) ⇒ Object
Extract just the not reduced fields from a JsonPacket.
- #get_hour_day_samples(packet, state) ⇒ Object
- #get_min_samples(packet, state) ⇒ Object
-
#initialize(name) ⇒ ReducerMicroservice
constructor
A new instance of ReducerMicroservice.
- #metric(name) ⇒ Object
- #process_file(filename, type, entry_nanoseconds, file_nanoseconds) ⇒ Object
- #reduce(type, raw_keys, converted_keys, reduced) ⇒ Object
- #reduce_day ⇒ Object
- #reduce_hour ⇒ Object
- #reduce_minute ⇒ Object
- #reduce_running(key, reduced, samples, samples_sum, avg_key, stddev_key, avgvals_key, stddevvals_key) ⇒ Object
- #run ⇒ Object
- #setup_state(reducer_state, packet, current_time) ⇒ Object
- #shutdown ⇒ Object
- #update_converted_hour_day_stats(packet, reduced, state) ⇒ Object
- #update_min_stats(reduced, state) ⇒ Object
- #update_raw_hour_day_stats(reduced, state) ⇒ Object
- #write_all_entries(reducer_state, plw, type, target_name, stored, throttle = nil) ⇒ Object
- #write_entry(state, plw, type, target_name, packet_name, stored) ⇒ Object
Constructor Details
#initialize(name) ⇒ ReducerMicroservice
Returns a new instance of ReducerMicroservice.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 94 def initialize(name) super(name, is_plugin: false) if @config['options'] @config['options'].each do |option| case option[0].upcase when 'BUFFER_DEPTH' # Buffer depth to write in time order @buffer_depth = option[1].to_i when 'MAX_CPU_UTILIZATION' @max_cpu_utilization = Float(option[1]) else @logger.error("Unknown option passed to microservice #{@name}: #{option}") end end end @buffer_depth = 60 unless @buffer_depth @max_cpu_utilization = 30.0 unless @max_cpu_utilization @target_name = name.split('__')[-1] @packet_logs = {} @mutex = Mutex.new @previous_metrics = {} @error_count = 0 # Initialize metrics @metric.set(name: 'reducer_minute_total', value: 0, type: 'counter') @metric.set(name: 'reducer_hour_total', value: 0, type: 'counter') @metric.set(name: 'reducer_day_total', value: 0, type: 'counter') @metric.set(name: 'reducer_minute_error_total', value: 0, type: 'counter') @metric.set(name: 'reducer_hour_error_total', value: 0, type: 'counter') @metric.set(name: 'reducer_day_error_total', value: 0, type: 'counter') @metric.set(name: 'reducer_minute_processing_sample_seconds', value: 0.0, type: 'gauge', unit: 'seconds') @metric.set(name: 'reducer_hour_processing_sample_seconds', value: 0.0, type: 'gauge', unit: 'seconds') @metric.set(name: 'reducer_day_processing_sample_seconds', value: 0.0, type: 'gauge', unit: 'seconds') @metric.set(name: 'reducer_minute_processing_max_seconds', value: 0.0, type: 'gauge', unit: 'seconds') @metric.set(name: 'reducer_hour_processing_max_seconds', value: 0.0, type: 'gauge', unit: 'seconds') @metric.set(name: 'reducer_day_processing_max_seconds', value: 0.0, type: 'gauge', unit: 'seconds') end |
Instance Method Details
#check_new_file(reducer_state, plw, type, target_name, stored, current_time, file_nanoseconds) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 485 def check_new_file(reducer_state, plw, type, target_name, stored, current_time, file_nanoseconds) plw_first_time_nsec = plw.buffered_first_time_nsec if plw_first_time_nsec && ((current_time - plw_first_time_nsec) >= file_nanoseconds) # Write out all entries in progress write_all_entries(reducer_state, plw, type, target_name, stored) reducer_state.clear plw.close_file return true else return false end end |
#extract_entry_samples(packet) ⇒ Object
Extract just the not reduced fields from a JsonPacket
622 623 624 625 626 627 628 629 630 631 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 622 def extract_entry_samples(packet) result = {} packet.json_hash.each do |key, value| key_split = key.split('__') if (not key_split[1] or not ['N', 'X', 'A', 'S'].include?(key_split[1][-1])) and key != '_NUM_SAMPLES' result[key] = value end end return result end |
#get_hour_day_samples(packet, state) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 364 def get_hour_day_samples(packet, state) # Hour or Day state.entry_samples ||= extract_entry_samples(packet) if state.first state.raw_max_values = packet.read_all(:RAW, :MAX, packet.read_all_names(:RAW, :MAX)) state.raw_keys = state.raw_max_values.keys state.converted_max_values = packet.read_all(:CONVERTED, :MAX, packet.read_all_names(:CONVERTED, :MAX)) state.converted_keys = state.converted_max_values.keys else state.raw_max_values = packet.read_all(:RAW, :MAX, state.raw_keys) state.converted_max_values = packet.read_all(:CONVERTED, :MAX, state.converted_keys) end state.raw_min_values = packet.read_all(:RAW, :MIN, state.raw_keys) state.raw_avg_values = packet.read_all(:RAW, :AVG, state.raw_keys) state.raw_stddev_values = packet.read_all(:RAW, :STDDEV, state.raw_keys) state.converted_min_values = packet.read_all(:CONVERTED, :MIN, state.converted_keys) state.converted_avg_values = packet.read_all(:CONVERTED, :AVG, state.converted_keys) state.converted_stddev_values = packet.read_all(:CONVERTED, :STDDEV, state.converted_keys) end |
#get_min_samples(packet, state) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 351 def get_min_samples(packet, state) state.entry_samples ||= packet.json_hash.dup # Grab all the samples from the first packet if state.first state.raw_values = packet.read_all(:RAW, nil, packet.read_all_names(:RAW)).select { |key, value| value.is_a?(Numeric) } state.raw_keys ||= state.raw_values.keys state.converted_values = packet.read_all(:CONVERTED, nil, packet.read_all_names(:CONVERTED)).select { |key, value| value.is_a?(Numeric) } state.converted_keys ||= state.converted_values.keys else state.raw_values = packet.read_all(:RAW, nil, state.raw_keys).select { |key, value| value.is_a?(Numeric) } state.converted_values = packet.read_all(:CONVERTED, nil, state.converted_keys).select { |key, value| value.is_a?(Numeric) } end end |
#metric(name) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 171 def metric(name) start = Process.clock_gettime(Process::CLOCK_MONOTONIC) processed = yield elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start # seconds as a float if processed sample_name = name + '_sample_seconds' @metric.set(name: sample_name, value: elapsed, type: 'gauge', unit: 'seconds') max_name = name + '_max_seconds' previous_max = @previous_metrics[max_name] || 0.0 if elapsed > previous_max @metric.set(name: max_name, value: elapsed, type: 'gauge', unit: 'seconds') @previous_metrics[max_name] = elapsed end end end |
#process_file(filename, type, entry_nanoseconds, file_nanoseconds) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 235 def process_file(filename, type, entry_nanoseconds, file_nanoseconds) throttle = OpenC3::Throttle.new(@max_cpu_utilization) file = BucketFile.new(filename) file.retrieve unless File.exist?(file.local_path) @logger.warn("Reducer Warning: #{file.local_path}: Does not exist") return end unless File.size(file.local_path) > 0 @logger.warn("Reducer Warning: #{file.local_path}: Is zero bytes") return end throttle.throttle_sleep # Determine if we already have a PacketLogWriter created _, _, scope, target_name, _, rt_or_stored, _ = File.basename(filename).split('__') stored = (rt_or_stored == "stored") if @target_name != target_name raise "Target name in file #{filename} does not match microservice target name #{@target_name}" end plw = @packet_logs["#{scope}__#{target_name}__#{rt_or_stored}__#{type}"] unless plw # Create a new PacketLogWriter for this reduced data # e.g. DEFAULT/reduced_minute_logs/tlm/INST/20220101/ # 20220101204857274290500__20220101205857276524900__DEFAULT__INST__ALL__rt__reduced_minute.bin remote_log_directory = "#{scope}/reduced_#{type}_logs/tlm/#{target_name}" label = "#{scope}__#{target_name}__ALL__#{rt_or_stored}__reduced_#{type}" plw = BufferedPacketLogWriter.new(remote_log_directory, label, true, nil, 1_000_000_000, nil, nil, true, @buffer_depth) @packet_logs["#{scope}__#{target_name}__#{rt_or_stored}__#{type}"] = plw end # The lifetime of all these variables is a single file - single target / multiple packets reducer_state = {} plr = OpenC3::PacketLogReader.new throttle.throttle_sleep plr.each(file.local_path) do |packet| # Check to see if we should start a new log file before processing this packet current_time = packet.packet_time.to_nsec_from_epoch check_new_file(reducer_state, plw, type, target_name, stored, current_time, file_nanoseconds) state = setup_state(reducer_state, packet, current_time) # Determine if we've rolled over a entry boundary # We have to use current % entry_nanoseconds < previous % entry_nanoseconds because # we don't know the data rates. We also have to check for current - previous >= entry_nanoseconds # in case the data rate is so slow we don't have multiple samples per entry if state.previous_time && ( (state.current_time % entry_nanoseconds < state.previous_time % entry_nanoseconds) || # Try to create at perfect intervals (state.current_time - state.previous_time >= entry_nanoseconds) # Handle big gaps ) write_entry(state, plw, type, target_name, packet.packet_name, stored) if check_new_file(reducer_state, plw, type, target_name, stored, current_time, file_nanoseconds) state = setup_state(reducer_state, packet, current_time) end end if type == 'minute' get_min_samples(packet, state) else get_hour_day_samples(packet, state) end reduced = state.reduced if type == 'minute' update_min_stats(reduced, state) else update_raw_hour_day_stats(reduced, state) update_converted_hour_day_stats(packet, reduced, state) end state.first = false throttle.throttle_sleep end file.delete # Remove the local copy write_all_entries(reducer_state, plw, type, target_name, stored, throttle) @logger.debug("Reducer Throttle: #{filename}: total_time: #{Time.now - throttle.reset_time}, sleep_time: #{throttle.total_sleep_time}") @count += 1 if type == 'minute' metric_name = 'reducer_minute_total' elsif type == 'hour' metric_name = 'reducer_hour_total' else metric_name = 'reducer_day_total' end @previous_metrics[metric_name] ||= 0 @previous_metrics[metric_name] += 1 @metric.set(name: metric_name, value: @previous_metrics[metric_name], type: 'counter') true rescue => e if file.local_path and File.exist?(file.local_path) @logger.error("Reducer Error: #{file.local_path}: #{File.size(file.local_path)} bytes: \n#{e.formatted}") else @logger.error("Reducer Error: #{filename}: \n#{e.formatted}") end @error_count += 1 if type == 'minute' metric_name = 'reducer_minute_error_total' elsif type == 'hour' metric_name = 'reducer_hour_error_total' else metric_name = 'reducer_day_error_total' end @previous_metrics[metric_name] ||= 0 @previous_metrics[metric_name] += 1 @metric.set(name: metric_name, value: @previous_metrics[metric_name], type: 'counter') file.delete false end |
#reduce(type, raw_keys, converted_keys, reduced) ⇒ Object
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 552 def reduce(type, raw_keys, converted_keys, reduced) # We've collected all the values so calculate the AVG and STDDEV if type == 'minute' raw_keys.each do |key| if reduced["#{key}__VALS"] reduced["_NUM_SAMPLES"] ||= reduced["#{key}__VALS"].length # Keep a single sample count per packet reduced["#{key}__A"], reduced["#{key}__S"] = Math.stddev_population(reduced["#{key}__VALS"]) # Remove the raw values as they're only used for AVG / STDDEV calculation reduced.delete("#{key}__VALS") end end converted_keys.each do |key| if reduced["#{key}__CVALS"] reduced["_NUM_SAMPLES"] ||= reduced["#{key}__CVALS"].length # Keep a single sample count per packet reduced["#{key}__CA"], reduced["#{key}__CS"] = Math.stddev_population(reduced["#{key}__CVALS"]) # Remove the converted values as they're only used for AVG / STDDEV calculation reduced.delete("#{key}__CVALS") end end else samples = reduced["_NUM_SAMPLES__VALS"] samples_sum = samples.sum reduced["_NUM_SAMPLES"] = samples_sum reduced.delete("_NUM_SAMPLES__VALS") raw_keys.each { |key| reduce_running(key, reduced, samples, samples_sum, "__A", "__S", "__AVGVALS", "__STDDEVVALS") } converted_keys.each { |key| reduce_running(key, reduced, samples, samples_sum, "__CA", "__CS", "__CAVGVALS", "__CSTDDEVVALS") } end end |
#reduce_day ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 219 def reduce_day @mutex.synchronize do metric(DAY_METRIC) do processed = false ReducerModel .all_files(type: :HOUR, target: @target_name, scope: @scope) .each do |file| process_file(file, 'day', DAY_ENTRY_NSECS, DAY_FILE_NSECS) ReducerModel.rm_file(file) processed = true end processed # return to yield end end end |
#reduce_hour ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 203 def reduce_hour @mutex.synchronize do metric(HOUR_METRIC) do processed = false ReducerModel .all_files(type: :MINUTE, target: @target_name, scope: @scope) .each do |file| process_file(file, 'hour', HOUR_ENTRY_NSECS, HOUR_FILE_NSECS) ReducerModel.rm_file(file) processed = true end processed # return to yield end end end |
#reduce_minute ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 187 def reduce_minute @mutex.synchronize do metric(MINUTE_METRIC) do processed = false ReducerModel .all_files(type: :DECOM, target: @target_name, scope: @scope) .each do |file| process_file(file, 'minute', MINUTE_ENTRY_NSECS, MINUTE_FILE_NSECS) ReducerModel.rm_file(file) processed = true end processed # return to yield end end end |
#reduce_running(key, reduced, samples, samples_sum, avg_key, stddev_key, avgvals_key, stddevvals_key) ⇒ Object
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 586 def reduce_running(key, reduced, samples, samples_sum, avg_key, stddev_key, avgvals_key, stddevvals_key) # Calculate Average weighted_sum = 0 avg = reduced["#{key}#{avgvals_key}"] if avg avg.each_with_index do |val, i| weighted_sum += (val * samples[i]) end reduced["#{key}#{avg_key}"] = weighted_sum / samples_sum end # Do the STDDEV calc last so we can use the previously calculated AVG # See https://math.stackexchange.com/questions/1547141/aggregating-standard-deviation-to-a-summary-point s2 = 0 stddev = reduced["#{key}#{stddevvals_key}"] if stddev stddev.each_with_index do |val, i| # puts "i:#{i} val:#{val} samples[i]:#{samples[i]} avg[i]:#{avg[i]}" s2 += (samples[i] * avg[i]**2 + val**2) end # Note: For very large numbers with very small deviations this sqrt can fail. # If so then just set the stddev to 0. begin reduced["#{key}#{stddev_key}"] = Math.sqrt(s2 / samples_sum - reduced["#{key}#{avg_key}"]) rescue Exception reduced["#{key}#{stddev_key}"] = 0.0 end end reduced.delete("#{key}#{avgvals_key}") reduced.delete("#{key}#{stddevvals_key}") end |
#run ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 134 def run # Note it takes several seconds to create the scheduler @scheduler = Rufus::Scheduler.new # Run every minute @scheduler.cron '* * * * *', first: :now do reduce_minute end # Run every 15 minutes @scheduler.cron '*/15 * * * *', first: :now do reduce_hour end # Run hourly at minute 5 to allow the hour reducer to finish @scheduler.cron '5 * * * *', first: :now do reduce_day end # Let the current thread join the scheduler thread and # block until shutdown is called @scheduler.join end |
#setup_state(reducer_state, packet, current_time) ⇒ Object
498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 498 def setup_state(reducer_state, packet, current_time) # Get state for this packet state = reducer_state[packet.packet_name] unless state state = ReducerState.new reducer_state[packet.packet_name] = state end # Update state timestamps state.previous_time = state.current_time # Will be nil first packet state.current_time = current_time state.entry_time ||= state.current_time # Sets the entry time from the first packet return state end |
#shutdown ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 155 def shutdown @logger.info("Shutting down reducer microservice: #{@name}") @scheduler.shutdown(wait: SHUTDOWN_DELAY_SECS) if @scheduler # Make sure all the existing logs are properly closed down threads = [] @packet_logs.each do |name, log| threads.concat(log.shutdown) end # Wait for all the logging threads to move files to buckets threads.flatten.compact.each do |thread| thread.join end super() end |
#update_converted_hour_day_stats(packet, reduced, state) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 450 def update_converted_hour_day_stats(packet, reduced, state) # Update statistics for this packet's converted values state.converted_max_values.each do |key, value| if value max_key = "#{key}__CX" reduced[max_key] ||= value reduced[max_key] = value if value > reduced[max_key] end end state.converted_min_values.each do |key, value| if value min_key = "#{key}__CN" reduced[min_key] ||= value reduced[min_key] = value if value < reduced[min_key] end end state.converted_avg_values.each do |key, value| if value avg_values_key = "#{key}__CAVGVALS" reduced[avg_values_key] ||= [] reduced[avg_values_key] << value end end state.converted_stddev_values.each do |key, value| if value stddev_values_key = "#{key}__CSTDDEVVALS" reduced[stddev_values_key] ||= [] reduced[stddev_values_key] << value end end reduced["_NUM_SAMPLES__VALS"] ||= [] reduced["_NUM_SAMPLES__VALS"] << packet.read('_NUM_SAMPLES') end |
#update_min_stats(reduced, state) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'ext/openc3/ext/reducer_microservice/reducer_microservice.c', line 136 def update_min_stats(reduced, state) # Update statistics for this packet's raw values state.raw_values.each do |key, value| if value vals_key = "#{key}__VALS" reduced[vals_key] ||= [] reduced[vals_key] << value n_key = "#{key}__N" reduced[n_key] ||= value reduced[n_key] = value if value < reduced[n_key] x_key = "#{key}__X" reduced[x_key] ||= value reduced[x_key] = value if value > reduced[x_key] end end # Update statistics for this packet's converted values state.converted_values.each do |key, value| if value cvals_key = "#{key}__CVALS" reduced[cvals_key] ||= [] reduced[cvals_key] << value cn_key = "#{key}__CN" reduced[cn_key] ||= value reduced[cn_key] = value if value < reduced[cn_key] cx_key = "#{key}__CX" reduced[cx_key] ||= value reduced[cx_key] = value if value > reduced[cx_key] end end end |
#update_raw_hour_day_stats(reduced, state) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 418 def update_raw_hour_day_stats(reduced, state) # Update statistics for this packet's raw values state.raw_max_values.each do |key, value| if value max_key = "#{key}__X" reduced[max_key] ||= value reduced[max_key] = value if value > reduced[max_key] end end state.raw_min_values.each do |key, value| if value min_key = "#{key}__N" reduced[min_key] ||= value reduced[min_key] = value if value < reduced[min_key] end end state.raw_avg_values.each do |key, value| if value avg_values_key = "#{key}__AVGVALS" reduced[avg_values_key] ||= [] reduced[avg_values_key] << value end end state.raw_stddev_values.each do |key, value| if value stddev_values_key = "#{key}__STDDEVVALS" reduced[stddev_values_key] ||= [] reduced[stddev_values_key] << value end end end |
#write_all_entries(reducer_state, plw, type, target_name, stored, throttle = nil) ⇒ Object
513 514 515 516 517 518 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 513 def write_all_entries(reducer_state, plw, type, target_name, stored, throttle = nil) reducer_state.each do |packet_name, state| write_entry(state, plw, type, target_name, packet_name, stored) throttle.throttle_sleep if throttle end end |
#write_entry(state, plw, type, target_name, packet_name, stored) ⇒ Object
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/openc3/microservices/reducer_microservice.rb', line 520 def write_entry(state, plw, type, target_name, packet_name, stored) return unless state.reduced.length > 0 reduce(type, state.raw_keys, state.converted_keys, state.reduced) state.reduced.merge!(state.entry_samples) time = state.entry_time data = JSON.generate(state.reduced.as_json(:allow_nan => true)) if type == "minute" redis_topic, redis_offset = TelemetryReducedMinuteTopic.write(target_name: target_name, packet_name: packet_name, stored: stored, time: time, data: data, scope: @scope) elsif type == "hour" redis_topic, redis_offset = TelemetryReducedHourTopic.write(target_name: target_name, packet_name: packet_name, stored: stored, time: time, data: data, scope: @scope) else redis_topic, redis_offset = TelemetryReducedDayTopic.write(target_name: target_name, packet_name: packet_name, stored: stored, time: time, data: data, scope: @scope) end plw.buffered_write( :JSON_PACKET, :TLM, target_name, packet_name, time, stored, data, nil, redis_topic, redis_offset ) # Reset necessary state variables state.entry_time = state.current_time # This packet starts the next entry state.entry_samples = nil state.reduced = {} end |