Class: Chronicle::ApiLogs::Metrics

Inherits:
Object
  • Object
show all
Includes:
Filterable
Defined in:
app/services/chronicle/api_logs/metrics.rb

Constant Summary collapse

FILTER_DEFINITION =
{
  client_version: :exact,
  backend_version: :exact,
  device_os: :exact,
  time_zone: :exact,
  start_date: :date_range,
  end_date: :date_range,
  http_method: :exact,
  api_endpoint: :exact,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filterable

#build_query

Constructor Details

#initialize(filters: {}) ⇒ Metrics

Returns a new instance of Metrics.



17
18
19
20
21
22
# File 'app/services/chronicle/api_logs/metrics.rb', line 17

def initialize(filters: {})
  @filters = filters
  start_time, end_time = normalize_date_range
  @filters[:start_date] = start_time.to_date.to_s
  @filters[:end_date] = end_time.to_date.to_s
end

Class Method Details

.distribution_metrics(filters: {}) ⇒ Object



57
58
59
# File 'app/services/chronicle/api_logs/metrics.rb', line 57

def distribution_metrics(filters: {})
  new(filters: filters).distribution_metrics
end

.kpi_cards(filters: {}) ⇒ Object



53
54
55
# File 'app/services/chronicle/api_logs/metrics.rb', line 53

def kpi_cards(filters: {})
  new(filters: filters).kpi_cards
end

Instance Method Details

#distribution_metricsObject



41
42
43
44
45
46
47
48
49
50
# File 'app/services/chronicle/api_logs/metrics.rb', line 41

def distribution_metrics
  start_time, end_time = normalize_date_range
  scope = build_query(ApiLog, filter_definition: FILTER_DEFINITION, filters: @filters, date_column: :timestamp)

  {
    status_code_distribution: fetch_status_code_distribution(scope),
    traffic_over_time: fetch_traffic_over_time(scope, start_time, end_time),
    response_time_trend: fetch_response_time_trend(scope, start_time, end_time),
  }
end

#kpi_cardsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/chronicle/api_logs/metrics.rb', line 24

def kpi_cards
  scope = build_query(ApiLog, filter_definition: FILTER_DEFINITION, filters: @filters, date_column: :timestamp)
  aggregates = fetch_aggregates(scope)

  {
    total_api_calls: aggregates[:total_count],
    unique_users: aggregates[:unique_users],
    unique_devices: aggregates[:unique_devices],
    average_response_time: aggregates[:avg_response_time],
    p50_response_time: aggregates[:p50_response_time],
    p95_response_time: aggregates[:p95_response_time],
    p99_response_time: aggregates[:p99_response_time],
    error_rate_percentage: calculate_error_rate(aggregates[:total_count], aggregates[:error_count]),
    requests_per_hour: calculate_requests_per_hour(aggregates[:total_count]),
  }
end