Class: Tiler::Widgets::StatusGridQuery

Inherits:
Query::Base show all
Defined in:
lib/tiler/widgets/status_grid.rb

Constant Summary collapse

PASS =

Catalog states map to four tokens. Built-in vocabulary covers most health-check payloads; custom value lists are out of scope per spec.

%w[pass passed ok success true].freeze
FAIL =
%w[fail failed error false].freeze
WARN =
%w[flaky warn warning skip skipped].freeze
DEFAULT_LIMIT =
50

Constants inherited from Query::Base

Query::Base::SAFE_COLUMN_RE

Instance Attribute Summary

Attributes inherited from Query::Base

#config, #panel, #source

Instance Method Summary collapse

Methods inherited from Query::Base

#initialize

Constructor Details

This class inherits a constructor from Tiler::Query::Base

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tiler/widgets/status_grid.rb', line 14

def call
  group_col  = config["group_column"]
  status_col = config["status_column"]
  sub_col    = config["sub_column"]
  limit      = DEFAULT_LIMIT
  return { rows: [] } if group_col.blank? || status_col.blank?

  groups = distinct_values(group_col).first(limit)
  rows = groups.map do |group_val|
    scope = base_scope.where("json_extract(payload, ?) = ?", "$.#{group_col}", group_val)
    statuses = scope.pluck(Arel.sql(json_extract(status_col)))
    last     = scope.order(recorded_at: :desc).first
    payload  = last&.parsed_payload.to_h

    {
      name:        group_val,
      status:      classify(payload[status_col]),
      last_status: payload[status_col],
      sub:         (sub_col.present? ? payload[sub_col] : nil),
      pass:        statuses.count { |s| PASS.include?(s.to_s.downcase) },
      fail:        statuses.count { |s| FAIL.include?(s.to_s.downcase) },
      warn:        statuses.count { |s| WARN.include?(s.to_s.downcase) },
      total:       statuses.size,
      last_run:    last&.recorded_at&.strftime("%Y-%m-%d %H:%M")
    }
  end
  { rows: rows.sort_by { |r| -r[:fail] } }
end