Module: Archsight::Annotations::ComputedAggregators
- Defined in:
- lib/archsight/annotations/aggregators.rb
Overview
ComputedAggregators provides static methods for aggregating annotation values. These functions handle nil values gracefully and convert types as needed.
Class Method Summary collapse
-
.avg(values) ⇒ Float?
Calculate average of numeric values.
-
.collect(values) ⇒ Array
Collect unique values, flattening arrays and sorting.
-
.count(values) ⇒ Integer
Count non-nil values.
-
.first(values) ⇒ Object?
Get first non-nil value.
-
.max(values) ⇒ Float?
Find maximum numeric value.
-
.min(values) ⇒ Float?
Find minimum numeric value.
-
.most_common(values) ⇒ Object?
Find most common value (mode).
-
.sum(values) ⇒ Float?
Sum numeric values.
Class Method Details
.avg(values) ⇒ Float?
Calculate average of numeric values
27 28 29 30 31 32 |
# File 'lib/archsight/annotations/aggregators.rb', line 27 def avg(values) numeric_values = to_numeric(values) return nil if numeric_values.empty? numeric_values.sum / numeric_values.length.to_f end |
.collect(values) ⇒ Array
Collect unique values, flattening arrays and sorting
57 58 59 60 61 62 63 64 |
# File 'lib/archsight/annotations/aggregators.rb', line 57 def collect(values) flat_values = values.flatten.compact # Handle comma-separated strings (list annotations) = flat_values.flat_map do |v| v.is_a?(String) ? v.split(",").map(&:strip) : v end .compact.uniq.sort_by(&:to_s) end |
.count(values) ⇒ Integer
Count non-nil values
20 21 22 |
# File 'lib/archsight/annotations/aggregators.rb', line 20 def count(values) values.compact.length end |
.first(values) ⇒ Object?
Get first non-nil value
69 70 71 |
# File 'lib/archsight/annotations/aggregators.rb', line 69 def first(values) values.compact.first end |
.max(values) ⇒ Float?
Find maximum numeric value
47 48 49 50 51 52 |
# File 'lib/archsight/annotations/aggregators.rb', line 47 def max(values) numeric_values = to_numeric(values) return nil if numeric_values.empty? numeric_values.max end |
.min(values) ⇒ Float?
Find minimum numeric value
37 38 39 40 41 42 |
# File 'lib/archsight/annotations/aggregators.rb', line 37 def min(values) numeric_values = to_numeric(values) return nil if numeric_values.empty? numeric_values.min end |
.most_common(values) ⇒ Object?
Find most common value (mode)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/archsight/annotations/aggregators.rb', line 76 def most_common(values) flat_values = values.flatten.compact return nil if flat_values.empty? # Handle comma-separated strings (list annotations) = flat_values.flat_map do |v| v.is_a?(String) ? v.split(",").map(&:strip) : v end .compact .group_by(&:itself) .max_by { |_, group| group.length } &.first end |
.sum(values) ⇒ Float?
Sum numeric values
10 11 12 13 14 15 |
# File 'lib/archsight/annotations/aggregators.rb', line 10 def sum(values) numeric_values = to_numeric(values) return nil if numeric_values.empty? numeric_values.sum end |