Class: RailsPulse::Jobs::Cards::TotalJobs

Inherits:
Base
  • Object
show all
Defined in:
app/models/rails_pulse/jobs/cards/total_jobs.rb

Constant Summary

Constants inherited from Base

Base::RANGE_DAYS, Base::WINDOW_DAYS

Instance Method Summary collapse

Constructor Details

#initialize(job: nil) ⇒ TotalJobs

Returns a new instance of TotalJobs.



5
6
7
# File 'app/models/rails_pulse/jobs/cards/total_jobs.rb', line 5

def initialize(job: nil)
  @job = job
end

Instance Method Details

#to_metric_cardObject



9
10
11
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
# File 'app/models/rails_pulse/jobs/cards/total_jobs.rb', line 9

def to_metric_card
  # When scoped to a job, show runs count instead of job count
  if @job
    base_query = RailsPulse::Summary
      .where(
        summarizable_type: "RailsPulse::Job",
        summarizable_id: @job.id,
        period_type: "day",
        period_start: range_start..now
      )

    metrics = base_query.select(
      "SUM(count) AS total_count",
      "SUM(CASE WHEN period_start >= #{quote(current_window_start)} THEN count ELSE 0 END) AS current_count",
      "SUM(CASE WHEN period_start >= #{quote(range_start)} AND period_start < #{quote(current_window_start)} THEN count ELSE 0 END) AS previous_count"
    ).take

    total_runs = metrics&.total_count.to_i
    current_runs = metrics&.current_count.to_i
    previous_runs = metrics&.previous_count.to_i

    trend_icon, trend_amount = trend_for(current_runs, previous_runs)

    grouped_runs = base_query
      .group_by_date(:period_start)
      .sum(:count)

    {
      id: "jobs_total_jobs",
      context: "jobs",
      title: "Total Runs",
      summary: "#{format_number(total_runs)} runs",
      chart_data: sparkline_from(grouped_runs),
      trend_icon: trend_icon,
      trend_amount: trend_amount,
      trend_text: "Compared to previous week"
    }
  else
    total_jobs = RailsPulse::Job.count

    current_new_jobs = RailsPulse::Job.where(created_at: current_window_start..now).count
    previous_new_jobs = RailsPulse::Job.where(created_at: range_start...current_window_start).count

    trend_icon, trend_amount = trend_for(current_new_jobs, previous_new_jobs)

    grouped_new_jobs = RailsPulse::Job
      .where(created_at: range_start..now)
      .group_by_date(:created_at)
      .count

    {
      id: "jobs_total_jobs",
      context: "jobs",
      title: "Total Jobs",
      summary: "#{format_number(total_jobs)} jobs",
      chart_data: sparkline_from(grouped_new_jobs),
      trend_icon: trend_icon,
      trend_amount: trend_amount,
      trend_text: "New jobs vs previous week"
    }
  end
end