Class: ForestAdminDatasourceToolkit::Components::Query::Aggregation
- Inherits:
-
Object
- Object
- ForestAdminDatasourceToolkit::Components::Query::Aggregation
- Includes:
- Exceptions
- Defined in:
- lib/forest_admin_datasource_toolkit/components/query/aggregation.rb
Instance Attribute Summary collapse
-
#field ⇒ Object
Returns the value of attribute field.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
Instance Method Summary collapse
- #apply(records, timezone, limit = nil) ⇒ Object
-
#initialize(operation:, field: nil, groups: []) ⇒ Aggregation
constructor
A new instance of Aggregation.
- #nest(prefix = nil) ⇒ Object
- #override(**args) ⇒ Object
- #projection ⇒ Object
- #replace_fields ⇒ Object
- #to_h ⇒ Object
- #validate(operation) ⇒ Object
Constructor Details
#initialize(operation:, field: nil, groups: []) ⇒ Aggregation
Returns a new instance of Aggregation.
12 13 14 15 16 17 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 12 def initialize(operation:, field: nil, groups: []) validate(operation) @operation = operation @field = field @groups = groups end |
Instance Attribute Details
#field ⇒ Object
Returns the value of attribute field.
10 11 12 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 10 def field @field end |
#groups ⇒ Object
Returns the value of attribute groups.
10 11 12 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 10 def groups @groups end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
9 10 11 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 9 def operation @operation end |
Instance Method Details
#apply(records, timezone, limit = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 57 def apply(records, timezone, limit = nil) rows = format_summaries(create_summaries(records, timezone)) rows.sort do |r1, r2| if r1[:value] == r2[:value] 0 else r1[:value] < r2[:value] ? 1 : -1 end end rows = rows[0..(limit - 1)] if limit && rows.size > limit rows end |
#nest(prefix = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 72 def nest(prefix = nil) return self unless prefix nested_field = nil nested_groups = [] nested_field = "#{prefix}:#{field}" if field if groups.size.positive? nested_groups = groups.map do |item| { field: "#{prefix}:#{item[:field]}", operation: item[:operation] } end end self.class.new(operation: operation, field: nested_field, groups: nested_groups) end |
#override(**args) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 48 def override(**args) params = to_h.merge(args) Aggregation.new( operation: params[:operation], field: params[:field], groups: params[:groups] ) end |
#projection ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 25 def projection aggregate_fields = [] aggregate_fields << field.to_s if field groups.each do |group| aggregate_fields << group[:field].to_s end Projection.new(aggregate_fields) end |
#replace_fields ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 36 def replace_fields result = clone result.field = yield(result.field) if result.field result.groups = result.groups.map do |group| { field: yield(group[:field]), operation: group[:operation] || nil } end result end |
#to_h ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 91 def to_h { operation: operation, field: field, groups: groups } end |
#validate(operation) ⇒ Object
19 20 21 22 23 |
# File 'lib/forest_admin_datasource_toolkit/components/query/aggregation.rb', line 19 def validate(operation) return if %w[Count Sum Avg Max Min].include? operation raise ForestException, "Aggregate operation #{operation} not allowed" end |