Class: PgReports::Grafana::DashboardBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_reports/grafana/dashboard_builder.rb

Overview

Builds an importable Grafana dashboard JSON from configured favorites. Each report becomes a row with two panels:

- "rows" stat (total)
- "issues by severity" timeseries (ok / warning / critical)

Severity colours are wired so that any warning lights yellow, any critical lights red.

Constant Summary collapse

DATASOURCE_INPUT =
"DS_PROMETHEUS"
DEFAULT_TITLE =
"PgReports — PostgreSQL Health"
DEFAULT_UID =
"pg-reports"
SEVERITY_COLORS =
{
  "ok" => "green",
  "warning" => "yellow",
  "critical" => "red"
}.freeze
GRID_WIDTH =
24
ROW_HEIGHT =
1
TIMESERIES_HEIGHT =
8
TABLE_HEIGHT =
10
REPORT_BLOCK_HEIGHT =
TIMESERIES_HEIGHT + TABLE_HEIGHT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(favorites: PgReports.config.grafana_favorites, title: DEFAULT_TITLE, uid: DEFAULT_UID, refresh: "1m", time_from: "now-6h") ⇒ DashboardBuilder

Returns a new instance of DashboardBuilder.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pg_reports/grafana/dashboard_builder.rb', line 34

def initialize(favorites: PgReports.config.grafana_favorites,
  title: DEFAULT_TITLE,
  uid: DEFAULT_UID,
  refresh: "1m",
  time_from: "now-6h")
  @favorites = normalize(favorites)
  @title = title
  @uid = uid
  @refresh = refresh
  @time_from = time_from
  @panel_id = 0
end

Class Method Details

.build(**opts) ⇒ Object



30
31
32
# File 'lib/pg_reports/grafana/dashboard_builder.rb', line 30

def self.build(**opts)
  new(**opts).build
end

Instance Method Details

#buildObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pg_reports/grafana/dashboard_builder.rb', line 47

def build
  if @favorites.empty?
    raise ArgumentError,
      "No favorites configured. Set PgReports.config.grafana_favorites or pass favorites:."
  end

  {
    "__inputs" => [datasource_input],
    "__requires" => [grafana_require, prometheus_require],
    "annotations" => {"list" => []},
    "editable" => true,
    "graphTooltip" => 1,
    "panels" => build_panels,
    "refresh" => @refresh,
    "schemaVersion" => 38,
    "tags" => ["pg_reports", "postgresql"],
    "templating" => {"list" => []},
    "time" => {"from" => @time_from, "to" => "now"},
    "timezone" => "browser",
    "title" => @title,
    "uid" => @uid,
    "version" => 1,
    "weekStart" => ""
  }
end

#to_jsonObject



73
74
75
# File 'lib/pg_reports/grafana/dashboard_builder.rb', line 73

def to_json(*)
  JSON.pretty_generate(build)
end