Class: Sentiero::Analytics::ServerEventMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/sentiero/analytics/server_event_metrics.rb

Overview

Per-day aggregations over an already-fetched list of custom events for the events-index strips (level mix, numeric payload metrics). Operates purely on the passed-in rows — it never re-reads the store, so the dashboard fetches once and aggregates the full pre-pagination list here.

Constant Summary collapse

LEVEL_MIX_MAX_DAYS =

Most-recent day rows rendered in the events-index level-mix strip.

30
SERVER_EVENT_LEVELS =
%w[debug info warn error].freeze
MAX_METRIC_KEYS =

Cap on distinct payload keys offered in the metric_key dropdown.

50

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events) ⇒ ServerEventMetrics

Returns a new instance of ServerEventMetrics.



32
33
34
# File 'lib/sentiero/analytics/server_event_metrics.rb', line 32

def initialize(events)
  @events = events
end

Class Method Details

.adapt_browser_rows(rows) ⇒ Object

Adapts BrowserEventDiscovery rows (symbol-keyed, rrweb epoch-MILLISECOND timestamps) to the string-keyed, epoch-seconds shape these helpers expect, so the browser tab can reuse them.



22
23
24
25
26
27
28
29
30
# File 'lib/sentiero/analytics/server_event_metrics.rb', line 22

def self.adapt_browser_rows(rows)
  rows.map do |row|
    {
      "name" => row[:name],
      "payload" => row[:payload],
      "timestamp" => row[:timestamp] && (row[:timestamp].to_f / 1000.0)
    }
  end
end

Instance Method Details

#level_mix_by_dayObject

Per-UTC-day level tallies. Returns [[date, => count], ...] ascending, capped to the most recent LEVEL_MIX_MAX_DAYS days with data.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sentiero/analytics/server_event_metrics.rb', line 38

def level_mix_by_day
  days = Hash.new { |hash, key| hash[key] = Hash.new(0) }
  @events.each do |event|
    ts = event["timestamp"]&.to_f
    next unless ts && ts > 0
    level = event["level"]
    level = "info" unless SERVER_EVENT_LEVELS.include?(level)
    days[Time.at(ts).utc.to_date.to_s][level] += 1
  end
  days.sort_by { |date, _counts| date }.last(LEVEL_MIX_MAX_DAYS)
end

#payload_metric_locals(requested_key) ⇒ Object

Payload metrics are offered only when the rows share a single event name, computed over those rows. requested_key is the user-selected metric key, honored only if it names a numeric payload key.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sentiero/analytics/server_event_metrics.rb', line 53

def payload_metric_locals(requested_key)
  single_name = single_event_name
  metric_keys = single_name ? numeric_payload_keys : []
  metric_key = metric_keys.include?(requested_key) ? requested_key : nil

  {
    single_name: single_name,
    metric_keys: metric_keys,
    metric_key: metric_key,
    metric_days: metric_key ? payload_metrics_by_day(metric_key) : []
  }
end