Class: Flycal::Pipeline::Aggregator
- Inherits:
-
Object
- Object
- Flycal::Pipeline::Aggregator
- Defined in:
- lib/flycal/pipeline/aggregator.rb
Overview
Second pipeline layer: group events and compute aggregate metrics.
Default group_by from timeframe:
day — timeframe <= 7 days
week — timeframe > 7 days and <= 30 days
month — timeframe > 30 days
Override with params / --groupBy:
day | week | month | <any other string> (split on | for string grouping)
Constant Summary collapse
- HOURS_PER_WORKING_DAY =
8- TIME_GROUP_BY =
%w[day week month].freeze
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.resolve_group_by(timeframe_days, explicit = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/flycal/pipeline/aggregator.rb', line 59 def self.resolve_group_by(timeframe_days, explicit = nil) raw = explicit.to_s.strip if raw.empty? if timeframe_days > 30 "month" elsif timeframe_days > 7 "week" else "day" end else key = raw.downcase TIME_GROUP_BY.include?(key) ? key : "string" end end |
Instance Method Details
#call(params) ⇒ Object
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 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/flycal/pipeline/aggregator.rb', line 22 def call(params) time_min = params[:time_min] time_max = params[:time_max] events = Array(params[:events]) timeframe_days = (time_max - time_min) / 86400.0 group_by = resolve_group_by(timeframe_days, params[:group_by_option]) params[:timeframe_days] = timeframe_days params[:group_by] = group_by ranges = events.filter_map do |ev| next if ev[:start_at].nil? || ev[:end_at].nil? [ev[:start_at], ev[:end_at]] end total_minutes = ranges.sum { |s, e| (e - s) / 60.0 } params[:totals] = { event_count: events.size, total_minutes: total_minutes, hours: (total_minutes / 60.0).floor, minutes: (total_minutes % 60).round, working_days: (total_minutes / 60.0 / HOURS_PER_WORKING_DAY).round(1) } params[:groups] = case group_by when "week" then weekly_groups(events, ranges, time_min, time_max) when "month" then monthly_groups(events, ranges, time_min, time_max) when "string" then string_groups(events, params[:group_by_option]) else daily_groups(events, ranges, time_min, time_max) end params end |