Class: Wonkavision::Analytics::SplitByAggregation

Inherits:
Object
  • Object
show all
Includes:
EventHandler
Defined in:
lib/wonkavision/plugins/analytics/handlers/split_by_aggregation.rb

Instance Method Summary collapse

Methods included from EventHandler

included

Instance Method Details

#aggregation_for(aggregation_name) ⇒ Object



54
55
56
# File 'lib/wonkavision/plugins/analytics/handlers/split_by_aggregation.rb', line 54

def aggregation_for(aggregation_name)
  Wonkavision::Aggregation.all[aggregation_name]
end

#process_aggregations(messages) ⇒ Object



36
37
38
39
40
41
# File 'lib/wonkavision/plugins/analytics/handlers/split_by_aggregation.rb', line 36

def process_aggregations(messages)
  messages = [messages].flatten
  event_path = self.class.event_path( Wonkavision.join('aggregation', 'updated') )
  messages.each {  |message| submit(event_path, message) }
  messages
end

#process_event(event) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wonkavision/plugins/analytics/handlers/split_by_aggregation.rb', line 12

def process_event(event)
  return false unless
    (aggregation = aggregation_for(event["aggregation"])) &&
    (action = event["action"]) &&
    (entity = event["data"])

  return [] unless aggregation.matches(entity)

  measures = aggregation.measures.keys.inject({}) do |measures,measure|
    measures[measure] = entity[measure.to_s]
    measures
  end

  messages = split_dimensions_by_aggregation(aggregation,entity).map do |dimensions|
    {
      "action" => action,
      "aggregation" => aggregation.name,
      "dimensions" => dimensions,
      "measures" => measures
    }
  end
  process_aggregations messages
end

#split_dimensions_by_aggregation(aggregation, entity) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/wonkavision/plugins/analytics/handlers/split_by_aggregation.rb', line 43

def split_dimensions_by_aggregation(aggregation,entity)
  aggregation.aggregations.inject([]) do |aggregations,aggregate_by|
    aggregations << aggregate_by.inject({}) do |dimensions,dimension_name|
      dimension = aggregation.dimensions[dimension_name]
      dimensions[dimension_name.to_s] = dimension.extract(entity)
      dimensions
    end
    aggregations
  end
end