Commit 19344f0e authored by Robert Speicher's avatar Robert Speicher

Merge branch 'eliminate-tz-sensitivity-ci-analytics-charts' into 'master'

Format timestamp data in pipeline charts

See merge request gitlab-org/gitlab!52971
parents 58c2c643 3c6eeb0c
---
title: Fix empty pipeline analytics charts when time_zone is non-UTC
merge_request: 52971
author:
type: fixed
...@@ -31,9 +31,10 @@ module Gitlab ...@@ -31,9 +31,10 @@ module Gitlab
current = @from current = @from
while current <= @to while current <= @to
@labels << current.strftime(@format) label = current.strftime(@format)
@total << (totals_count[current] || 0) @labels << label
@success << (success_count[current] || 0) @total << (totals_count[label] || 0)
@success << (success_count[label] || 0)
current += interval_step current += interval_step
end end
...@@ -45,6 +46,7 @@ module Gitlab ...@@ -45,6 +46,7 @@ module Gitlab
query query
.group("date_trunc('#{interval}', #{::Ci::Pipeline.table_name}.created_at)") .group("date_trunc('#{interval}', #{::Ci::Pipeline.table_name}.created_at)")
.count(:created_at) .count(:created_at)
.transform_keys { |date| date.strftime(@format) }
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -9,6 +9,10 @@ RSpec.describe Gitlab::Ci::Charts do ...@@ -9,6 +9,10 @@ RSpec.describe Gitlab::Ci::Charts do
subject { chart.to } subject { chart.to }
before do
create(:ci_empty_pipeline, project: project, duration: 120)
end
it 'goes until the end of the current month (including the whole last day of the month)' do it 'goes until the end of the current month (including the whole last day of the month)' do
is_expected.to eq(Date.today.end_of_month.end_of_day) is_expected.to eq(Date.today.end_of_month.end_of_day)
end end
...@@ -20,6 +24,10 @@ RSpec.describe Gitlab::Ci::Charts do ...@@ -20,6 +24,10 @@ RSpec.describe Gitlab::Ci::Charts do
it 'uses %B %Y as labels format' do it 'uses %B %Y as labels format' do
expect(chart.labels).to include(chart.from.strftime('%B %Y')) expect(chart.labels).to include(chart.from.strftime('%B %Y'))
end end
it 'returns count of pipelines run each day in the current year' do
expect(chart.total.sum).to eq(1)
end
end end
context 'monthchart' do context 'monthchart' do
...@@ -28,6 +36,10 @@ RSpec.describe Gitlab::Ci::Charts do ...@@ -28,6 +36,10 @@ RSpec.describe Gitlab::Ci::Charts do
subject { chart.to } subject { chart.to }
before do
create(:ci_empty_pipeline, project: project, duration: 120)
end
it 'includes the whole current day' do it 'includes the whole current day' do
is_expected.to eq(Date.today.end_of_day) is_expected.to eq(Date.today.end_of_day)
end end
...@@ -39,6 +51,10 @@ RSpec.describe Gitlab::Ci::Charts do ...@@ -39,6 +51,10 @@ RSpec.describe Gitlab::Ci::Charts do
it 'uses %d %B as labels format' do it 'uses %d %B as labels format' do
expect(chart.labels).to include(chart.from.strftime('%d %B')) expect(chart.labels).to include(chart.from.strftime('%d %B'))
end end
it 'returns count of pipelines run each day in the current month' do
expect(chart.total.sum).to eq(1)
end
end end
context 'weekchart' do context 'weekchart' do
...@@ -47,6 +63,10 @@ RSpec.describe Gitlab::Ci::Charts do ...@@ -47,6 +63,10 @@ RSpec.describe Gitlab::Ci::Charts do
subject { chart.to } subject { chart.to }
before do
create(:ci_empty_pipeline, project: project, duration: 120)
end
it 'includes the whole current day' do it 'includes the whole current day' do
is_expected.to eq(Date.today.end_of_day) is_expected.to eq(Date.today.end_of_day)
end end
...@@ -58,6 +78,68 @@ RSpec.describe Gitlab::Ci::Charts do ...@@ -58,6 +78,68 @@ RSpec.describe Gitlab::Ci::Charts do
it 'uses %d %B as labels format' do it 'uses %d %B as labels format' do
expect(chart.labels).to include(chart.from.strftime('%d %B')) expect(chart.labels).to include(chart.from.strftime('%d %B'))
end end
it 'returns count of pipelines run each day in the current week' do
expect(chart.total.sum).to eq(1)
end
end
context 'weekchart_utc' do
today = Date.today
end_of_today = Time.use_zone(Time.find_zone('UTC')) { today.end_of_day }
let(:project) { create(:project) }
let(:chart) do
allow(Date).to receive(:today).and_return(today)
allow(today).to receive(:end_of_day).and_return(end_of_today)
Gitlab::Ci::Charts::WeekChart.new(project)
end
subject { chart.total }
before do
create(:ci_empty_pipeline, project: project, duration: 120)
end
it 'uses a utc time zone for range times' do
expect(chart.to.zone).to eq(end_of_today.zone)
expect(chart.from.zone).to eq(end_of_today.zone)
end
it 'returns count of pipelines run each day in the current week' do
expect(chart.total.sum).to eq(1)
end
end
context 'weekchart_non_utc' do
today = Date.today
end_of_today = Time.use_zone(Time.find_zone('Asia/Dubai')) { today.end_of_day }
let(:project) { create(:project) }
let(:chart) do
allow(Date).to receive(:today).and_return(today)
allow(today).to receive(:end_of_day).and_return(end_of_today)
Gitlab::Ci::Charts::WeekChart.new(project)
end
subject { chart.total }
before do
# The DB uses UTC always, so our use of a Time Zone in the application
# can cause the creation date of the pipeline to go unmatched depending
# on the offset. We can work around this by requesting the pipeline be
# created a with the `created_at` field set to a day ago in the same week.
create(:ci_empty_pipeline, project: project, duration: 120, created_at: today - 1.day)
end
it 'uses a non-utc time zone for range times' do
expect(chart.to.zone).to eq(end_of_today.zone)
expect(chart.from.zone).to eq(end_of_today.zone)
end
it 'returns count of pipelines run each day in the current week' do
expect(chart.total.sum).to eq(1)
end
end end
context 'pipeline_times' do context 'pipeline_times' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment