Class: RailsPulse::Queries::Charts::DatabaseLoad

Inherits:
Object
  • Object
show all
Defined in:
app/models/rails_pulse/queries/charts/database_load.rb

Instance Method Summary collapse

Constructor Details

#initialize(start_time:, end_time:, period_type: :day, disabled_tags: [], show_non_tagged: true) ⇒ DatabaseLoad

Returns a new instance of DatabaseLoad.



5
6
7
8
9
10
11
# File 'app/models/rails_pulse/queries/charts/database_load.rb', line 5

def initialize(start_time:, end_time:, period_type: :day, disabled_tags: [], show_non_tagged: true)
  @start_time = start_time
  @end_time = end_time
  @period_type = period_type
  @disabled_tags = disabled_tags
  @show_non_tagged = show_non_tagged
end

Instance Method Details

#to_chart_dataObject



13
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
42
43
44
45
46
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
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/rails_pulse/queries/charts/database_load.rb', line 13

def to_chart_data
  step = @period_type.to_s == "hour" ? 3600 : 86400

  # Get query summaries (DB time)
  query_summaries = RailsPulse::Summary
    .with_tag_filters(@disabled_tags, @show_non_tagged)
    .where(
      summarizable_type: "RailsPulse::Query",
      period_type: @period_type,
      period_start: Time.at(@start_time)..Time.at(@end_time)
    )

  # Get route summaries (total request time)
  route_summaries = RailsPulse::Summary
    .with_tag_filters(@disabled_tags, @show_non_tagged)
    .where(
      summarizable_type: "RailsPulse::Route",
      period_type: @period_type,
      period_start: Time.at(@start_time)..Time.at(@end_time)
    )

  return nil if query_summaries.empty? || route_summaries.empty?

  # Group by period_start timestamp
  query_time_by_period = {}
  query_summaries.each do |summary|
    ts = summary.period_start.to_i
    query_time_by_period[ts] ||= 0
    query_time_by_period[ts] += summary.total_duration || 0
  end

  request_time_by_period = {}
  route_summaries.each do |summary|
    ts = summary.period_start.to_i
    request_time_by_period[ts] ||= 0
    request_time_by_period[ts] += summary.total_duration || 0
  end

  # Build labels (ms timestamps) and per-bar colored data
  # <25% = green (healthy), 25-40% = yellow (watch), >40% = red (bottleneck)
  labels = []
  bar_data = []

  (@start_time.to_i..@end_time.to_i).step(step) do |timestamp|
    query_time = query_time_by_period[timestamp] || 0
    request_time = request_time_by_period[timestamp] || 0
    percentage = request_time > 0 ? (query_time.to_f / request_time * 100).round(1) : 0

    color = if percentage < 25
      "rgb(34, 197, 94)"  # green
    elsif percentage < 40
      "rgb(234, 179, 8)"  # yellow
    else
      "rgb(239, 68, 68)"  # red
    end

    labels << timestamp * 1000
    bar_data << { value: percentage, itemStyle: { color: color } }
  end

  {
    labels: labels,
    series: [
      {
        name: "DB Load",
        data: bar_data,
        type: "bar"
      }
    ]
  }
end