Module: Legion::Extensions::Metering::Runners::Rollup
- Extended by:
- Rollup
- Included in:
- Rollup
- Defined in:
- lib/legion/extensions/metering/runners/rollup.rb
Instance Method Summary collapse
Instance Method Details
#purge_raw_records(retention_days: 7) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/legion/extensions/metering/runners/rollup.rb', line 48 def purge_raw_records(retention_days: 7, **) return { status: 'skipped', reason: 'data_unavailable' } unless data_available?(:metering_records) cutoff = Time.now.utc - (retention_days * 86_400) count = Legion::Data.connection[:metering_records] .where(::Sequel.lit('recorded_at < ?', cutoff)) .delete log.info("[metering] purge_raw_records: purged=#{count} retention_days=#{retention_days} cutoff=#{cutoff.iso8601}") { purged: count, retention_days: retention_days, cutoff: cutoff.iso8601 } end |
#rollup_hour(hour: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/legion/extensions/metering/runners/rollup.rb', line 10 def rollup_hour(hour: nil, **) return { status: 'skipped', reason: 'data_unavailable' } unless data_available? hour = resolve_hour(hour) hour_end = hour + 3600 records_ds = Legion::Data.connection[:metering_records] .where(::Sequel.lit('recorded_at >= ? AND recorded_at < ?', hour, hour_end)) raw_count = records_ds.count # Group in Ruby, not in SQL: Sequel::Dataset#group_by is an alias for #group # (it builds a SQL GROUP BY clause), so calling it on the dataset emits invalid # SQL (GROUP BY []("worker_id"), ...) instead of the intended in-memory grouping. # Materialise the rows first, then use Enumerable#group_by on the array. groups = records_ds.all.group_by { |r| [r[:worker_id], r[:provider], r[:model_id]] } rollup_dataset = Legion::Data.connection[:metering_hourly_rollup] rolled_up = 0 groups.each do |(w, p, m), rows| rollup_row = build_rollup_row(w, p, m, hour, rows) existing = rollup_dataset.where(worker_id: w, provider: p, model_id: m, hour: hour).first if existing rollup_dataset.where(id: existing[:id]).update( rollup_row.except(:worker_id, :provider, :model_id, :hour) ) else rollup_dataset.insert(rollup_row) end rolled_up += 1 end log.info("[metering] rollup_hour: hour=#{hour.iso8601} groups=#{rolled_up} raw_records=#{raw_count}") { rolled_up: rolled_up, hour: hour.iso8601, raw_records: raw_count } end |