From 337b2e879c6de4144aabbfe4213444a2db831949 Mon Sep 17 00:00:00 2001 From: Suban05 Date: Tue, 27 Aug 2024 21:30:34 +0500 Subject: [PATCH] #189 use `duration` --- judges/quality-of-service/quality-of-service.rb | 8 +++++--- test/judges/test-quality-of-service.rb | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/judges/quality-of-service/quality-of-service.rb b/judges/quality-of-service/quality-of-service.rb index abbabaa1..e5fafe0d 100644 --- a/judges/quality-of-service/quality-of-service.rb +++ b/judges/quality-of-service/quality-of-service.rb @@ -37,15 +37,17 @@ Fbe.unmask_repos.each do |repo| Fbe.octo.repository_workflow_runs(repo, created: ">#{f.since.utc.iso8601[0..9]}")[:workflow_runs].each do |json| workflow_id = json[:workflow_id] + run_duration = (Fbe.octo.workflow_run_usage(repo, json[:id])[:run_duration_ms] || 0) / 1000 + completed = json[:run_started_at] + run_duration if json[:conclusion] == 'failure' && failed[workflow_id].nil? - failed[workflow_id] = json[:updated_at] + failed[workflow_id] = completed elsif json[:conclusion] == 'success' && failed[workflow_id] - ttrs << (json[:updated_at] - failed[workflow_id]).to_i + ttrs << (completed - failed[workflow_id]).to_i failed.delete(workflow_id) end total += 1 success += json[:conclusion] == 'success' ? 1 : 0 - duration += (Fbe.octo.workflow_run_usage(repo, json[:id])[:run_duration_ms] || 0) / 1000 + duration += run_duration end end f.average_build_success_rate = total.zero? ? 0 : success.to_f / total diff --git a/test/judges/test-quality-of-service.rb b/test/judges/test-quality-of-service.rb index 50c725f1..5f97e61c 100644 --- a/test/judges/test-quality-of-service.rb +++ b/test/judges/test-quality-of-service.rb @@ -50,7 +50,7 @@ def test_runs_when_run_duration_ms_is_nil status: 200, body: { workflow_runs: [ - { id: 1 } + { id: 1, run_started_at: Time.now - rand(10_000) } ] }.to_json, headers: { @@ -145,6 +145,7 @@ def test_quality_of_service_average_issues workflow_id: 42, created_at: Time.now - rand(10_000), updated_at: Time.now - rand(10_000) + 100, + run_started_at: Time.now - rand(10_000), repository: { id: 1, full_name: 'foo/foo', default_branch: 'master', private: false, owner: { login: 'foo', id: 526_301, site_admin: false }, @@ -189,6 +190,7 @@ def test_quality_of_service_average_build_mttr workflow_id: 101, created_at: '2024-08-07T10:00:00Z', updated_at: '2024-08-07T10:10:00Z', + run_started_at: '2024-08-07T10:00:00Z', repository: { full_name: 'foo/foo' } }, { @@ -202,6 +204,7 @@ def test_quality_of_service_average_build_mttr workflow_id: 101, created_at: '2024-08-07T11:00:00Z', updated_at: '2024-08-07T11:15:00Z', + run_started_at: '2024-08-07T11:00:00Z', repository: { full_name: 'foo/foo' } }, { @@ -215,6 +218,7 @@ def test_quality_of_service_average_build_mttr workflow_id: 102, created_at: '2024-08-08T12:00:00Z', updated_at: '2024-08-08T12:10:00Z', + run_started_at: '2024-08-08T12:00:00Z', repository: { full_name: 'foo/foo' } }, { @@ -228,6 +232,7 @@ def test_quality_of_service_average_build_mttr workflow_id: 102, created_at: '2024-08-08T13:00:00Z', updated_at: '2024-08-08T13:20:00Z', + run_started_at: '2024-08-08T13:00:00Z', repository: { full_name: 'foo/foo' } } ] @@ -241,7 +246,7 @@ def test_quality_of_service_average_build_mttr Time.stub(:now, Time.parse('2024-08-09 21:00:00 UTC')) do load_it('quality-of-service', fb) f = fb.query('(eq what "quality-of-service")').each.to_a.first - assert_in_delta((3900 + 4200) / 2.0, f.average_build_mttr) + assert_in_delta(3600, f.average_build_mttr) end end @@ -261,6 +266,7 @@ def test_quality_of_service_average_build_mttr_when_failure_several_times_in_a_r workflow_id: 101, created_at: '2024-08-07T10:00:00Z', updated_at: '2024-08-07T10:10:00Z', + run_started_at: '2024-08-07T10:00:00Z', repository: { full_name: 'foo/foo' } }, { @@ -274,6 +280,7 @@ def test_quality_of_service_average_build_mttr_when_failure_several_times_in_a_r workflow_id: 101, created_at: '2024-08-07T11:00:00Z', updated_at: '2024-08-07T11:15:00Z', + run_started_at: '2024-08-07T11:00:00Z', repository: { full_name: 'foo/foo' } }, { @@ -287,6 +294,7 @@ def test_quality_of_service_average_build_mttr_when_failure_several_times_in_a_r workflow_id: 101, created_at: '2024-08-08T12:00:00Z', updated_at: '2024-08-08T12:10:00Z', + run_started_at: '2024-08-08T12:00:00Z', repository: { full_name: 'foo/foo' } }, { @@ -300,6 +308,7 @@ def test_quality_of_service_average_build_mttr_when_failure_several_times_in_a_r workflow_id: 101, created_at: '2024-08-08T13:00:00Z', updated_at: '2024-08-08T13:20:00Z', + run_started_at: '2024-08-08T13:00:00Z', repository: { full_name: 'foo/foo' } }, { @@ -313,6 +322,7 @@ def test_quality_of_service_average_build_mttr_when_failure_several_times_in_a_r workflow_id: 101, created_at: '2024-08-08T14:00:00Z', updated_at: '2024-08-08T14:20:00Z', + run_started_at: '2024-08-08T14:00:00Z', repository: { full_name: 'foo/foo' } } ] @@ -326,7 +336,7 @@ def test_quality_of_service_average_build_mttr_when_failure_several_times_in_a_r Time.stub(:now, Time.parse('2024-08-09 21:00:00 UTC')) do load_it('quality-of-service', fb) f = fb.query('(eq what "quality-of-service")').each.to_a.first - assert_in_delta(97_500, f.average_build_mttr) + assert_in_delta(97_200, f.average_build_mttr) end end