Class: RailsPulse::Dashboard::Charts::ThroughputAndErrors

Inherits:
Object
  • Object
show all
Defined in:
app/models/rails_pulse/dashboard/charts/throughput_and_errors.rb

Instance Method Summary collapse

Constructor Details

#initialize(disabled_tags: [], show_non_tagged: true, period: 7, period_type: "day") ⇒ ThroughputAndErrors

Returns a new instance of ThroughputAndErrors.



5
6
7
8
9
10
# File 'app/models/rails_pulse/dashboard/charts/throughput_and_errors.rb', line 5

def initialize(disabled_tags: [], show_non_tagged: true, period: 7, period_type: "day")
  @disabled_tags = disabled_tags
  @show_non_tagged = show_non_tagged
  @period = period
  @period_type = period_type
end

Instance Method Details

#to_chart_dataObject



12
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/rails_pulse/dashboard/charts/throughput_and_errors.rb', line 12

def to_chart_data
  if @period_type == "hour"
    start_time = (@period * 24).hours.ago.beginning_of_hour
    end_time = Time.current.beginning_of_hour

    summaries = RailsPulse::Summary
      .with_tag_filters(@disabled_tags, @show_non_tagged)
      .where(
        summarizable_type: "RailsPulse::Route",
        period_type: "hour",
        period_start: start_time..end_time
      )
      .group(:period_start)
      .select(
        :period_start,
        "SUM(count) as total_count",
        "SUM(error_count) as total_errors"
      )

    return nil if summaries.empty?

    hourly_data = {}
    summaries.each do |summary|
      time_key = summary.period_start.beginning_of_hour
      hourly_data[time_key] = {
        requests: summary.total_count || 0,
        errors: summary.total_errors || 0
      }
    end

    # Build hourly time range
    time_range = []
    current_time = start_time
    while current_time <= end_time
      time_range << current_time
      current_time += 1.hour
    end

    labels = time_range.map { |time| time.strftime("%H:%M") }

    series = [
      {
        name: "Requests",
        data: time_range.map { |time| hourly_data[time]&.[](:requests) || 0 },
        type: "bar",
        color: RailsPulse::ChartColors::DEFAULT,
        itemStyle: { borderRadius: [ 5, 5, 0, 0 ] },
        z: 1
      },
      {
        name: "Errors",
        data: time_range.map { |time| hourly_data[time]&.[](:errors) || 0 },
        type: "bar",
        color: "#dc2626",
        itemStyle: { borderRadius: [ 5, 5, 0, 0 ] },
        barGap: "-100%",
        z: 2
      }
    ]
  else
    start_date = @period.days.ago.beginning_of_day.to_date
    end_date = Time.current.to_date
    date_range = (start_date..end_date)

    summaries = RailsPulse::Summary
      .with_tag_filters(@disabled_tags, @show_non_tagged)
      .where(
        summarizable_type: "RailsPulse::Route",
        period_type: "day",
        period_start: start_date.beginning_of_day..end_date.end_of_day
      )
      .group(:period_start)
      .select(
        :period_start,
        "SUM(count) as total_count",
        "SUM(error_count) as total_errors"
      )

    return nil if summaries.empty?

    daily_data = {}
    summaries.each do |summary|
      date = summary.period_start.to_date
      daily_data[date] = {
        requests: summary.total_count || 0,
        errors: summary.total_errors || 0
      }
    end

    labels = date_range.map { |date| date.strftime("%b %-d") }

    series = [
      {
        name: "Requests",
        data: date_range.map { |date| daily_data[date]&.[](:requests) || 0 },
        type: "bar",
        color: RailsPulse::ChartColors::DEFAULT,
        itemStyle: { borderRadius: [ 5, 5, 0, 0 ] },
        z: 1
      },
      {
        name: "Errors",
        data: date_range.map { |date| daily_data[date]&.[](:errors) || 0 },
        type: "bar",
        color: "#dc2626",
        itemStyle: { borderRadius: [ 5, 5, 0, 0 ] },
        barGap: "-100%",
        z: 2
      }
    ]
  end

  { labels: labels, series: series }
end