Skip to content

Commit b329cf1

Browse files
Merge pull request #100 from RodrigoMNardi/feature/github/calc_time_execution
Execution time
2 parents 94d3c53 + f343aca commit b329cf1

7 files changed

Lines changed: 61 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# SPDX-License-Identifier: BSD-2-Clause
4+
#
5+
# 20240924140825_add_ci_job_stage_execution_time.rb
6+
# Part of NetDEF CI System
7+
#
8+
# Copyright (c) 2024 by
9+
# Network Device Education Foundation, Inc. ("NetDEF")
10+
#
11+
12+
class AddCiJobStageExecutionTime < ActiveRecord::Migration[6.0]
13+
def change
14+
add_column :ci_jobs, :execution_time, :integer
15+
add_column :stages, :execution_time, :integer
16+
end
17+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.2].define(version: 2024_06_17_121935) do
13+
ActiveRecord::Schema[7.2].define(version: 2024_09_24_140825) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "plpgsql"
1616

@@ -77,6 +77,7 @@
7777
t.integer "retry", default: 0
7878
t.integer "parent_stage_id"
7979
t.bigint "stage_id"
80+
t.integer "execution_time"
8081
t.index ["check_suite_id"], name: "index_ci_jobs_on_check_suite_id"
8182
t.index ["stage_id"], name: "index_ci_jobs_on_stage_id"
8283
end
@@ -160,6 +161,7 @@
160161
t.datetime "updated_at", null: false
161162
t.bigint "check_suite_id"
162163
t.bigint "stage_configuration_id"
164+
t.integer "execution_time"
163165
t.index ["check_suite_id"], name: "index_stages_on_check_suite_id"
164166
t.index ["stage_configuration_id"], name: "index_stages_on_stage_configuration_id"
165167
end

lib/github/build/summary.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,14 @@ def finished_stage_update(stage, output)
135135
if stage.jobs.failure.empty?
136136
logger(Logger::WARN, "Stage: #{stage.name} finished - success")
137137
stage.success(@github, output: output, agent: @agent)
138+
stage.update_execution_time
138139

139140
return
140141
end
141142

142143
logger(Logger::WARN, "Stage: #{stage.name} finished - failure")
143144
stage.failure(@github, output: output, agent: @agent)
145+
stage.update_execution_time
144146
end
145147

146148
def update_summary(stage)

lib/github/update_status.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ def update_status
6262
@job.in_progress(@github_check)
6363
when 'success'
6464
@job.success(@github_check)
65+
@job.update_execution_time
6566
slack_notify_success
6667
else
6768
failure
69+
@job.update_execution_time
6870
slack_notify_failure
6971
end
7072

lib/models/ci_job.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ def finished?
3636
!%w[queued in_progress].include?(status)
3737
end
3838

39+
def update_execution_time
40+
started = audit_statuses.find_by(status: :in_progress)
41+
finished = audit_statuses.find_by(status: %i[success failure])
42+
43+
return if started.nil? || finished.nil?
44+
45+
update(execution_time: finished.created_at - started.created_at)
46+
end
47+
3948
def create_check_run(agent: 'Github')
4049
AuditStatus.create(auditable: self, status: :queued, agent: agent, created_at: Time.now)
4150
update(status: :queued)

lib/models/stage.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ class Stage < ActiveRecord::Base
1818

1919
default_scope -> { order(id: :asc) }, all_queries: true
2020

21+
def update_execution_time
22+
started = audit_statuses.find_by(status: :in_progress)
23+
finished = audit_statuses.find_by(status: %i[success failure])
24+
25+
return if started.nil? || finished.nil?
26+
27+
update(execution_time: finished.created_at - started.created_at)
28+
end
29+
2130
def running?
2231
jobs.where(status: %i[queued in_progress]).any?
2332
end

spec/lib/models/ci_job_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,23 @@
8080
end
8181
end
8282
end
83+
84+
describe '#execution_time' do
85+
let(:stage) { create(:ci_job, check_ref: nil) }
86+
87+
context 'when CiJob started and finished' do
88+
it 'must update status' do
89+
stage.in_progress(github)
90+
stage.success(github)
91+
stage.update_execution_time
92+
end
93+
end
94+
95+
context 'when CiJob started and not finished' do
96+
it 'must update status' do
97+
stage.in_progress(github)
98+
stage.update_execution_time
99+
end
100+
end
101+
end
83102
end

0 commit comments

Comments
 (0)