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

Class Method Details

.avg(values) ⇒ Float?

Calculate average of numeric values

Parameters:

  • values (Array)

    Array of values to average

Returns:

  • (Float, nil)

    Average of values, nil if no valid 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

Parameters:

  • values (Array)

    Array of values (may contain nested arrays)

Returns:

  • (Array)

    Unique sorted values



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)
  expanded = flat_values.flat_map do |v|
    v.is_a?(String) ? v.split(",").map(&:strip) : v
  end
  expanded.compact.uniq.sort_by(&:to_s)
end

.count(values) ⇒ Integer

Count non-nil values

Parameters:

  • values (Array)

    Array of values to count

Returns:

  • (Integer)

    Count of 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

Parameters:

  • values (Array)

    Array of values

Returns:

  • (Object, nil)

    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

Parameters:

  • values (Array)

    Array of values

Returns:

  • (Float, nil)

    Maximum value, nil if no valid values



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

Parameters:

  • values (Array)

    Array of values

Returns:

  • (Float, nil)

    Minimum value, nil if no valid values



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)

Parameters:

  • values (Array)

    Array of values (may contain nested arrays)

Returns:

  • (Object, nil)

    Most frequent value, nil if no values



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)
  expanded = flat_values.flat_map do |v|
    v.is_a?(String) ? v.split(",").map(&:strip) : v
  end

  expanded.compact
          .group_by(&:itself)
          .max_by { |_, group| group.length }
          &.first
end

.sum(values) ⇒ Float?

Sum numeric values

Parameters:

  • values (Array)

    Array of values to sum

Returns:

  • (Float, nil)

    Sum of values converted to float, nil if no valid 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