Commit 5e9c5c87 authored by Shinya Maeda's avatar Shinya Maeda

Change payload format for DORA metrics

This commit changes the payload format on the DORA metrics.
parent 299fc1e4
---
title: Improve payload format of DORA metrics API
merge_request: 57314
author:
type: added
...@@ -38,14 +38,14 @@ Example response: ...@@ -38,14 +38,14 @@ Example response:
```json ```json
[ [
{ "2021-03-01": 3 }, { "2021-03-01": 3, "date": "2021-03-01", "value": 3 },
{ "2021-03-02": 6 }, { "2021-03-02": 6, "date": "2021-03-02", "value": 6 },
{ "2021-03-03": 0 }, { "2021-03-03": 0, "date": "2021-03-03", "value": 0 },
{ "2021-03-04": 0 }, { "2021-03-04": 0, "date": "2021-03-04", "value": 0 },
{ "2021-03-05": 0 }, { "2021-03-05": 0, "date": "2021-03-05", "value": 0 },
{ "2021-03-06": 0 }, { "2021-03-06": 0, "date": "2021-03-06", "value": 0 },
{ "2021-03-07": 0 }, { "2021-03-07": 0, "date": "2021-03-07", "value": 0 },
{ "2021-03-08": 4 } { "2021-03-08": 4, "date": "2021-03-08", "value": 4 }
] ]
``` ```
...@@ -78,13 +78,13 @@ Example response: ...@@ -78,13 +78,13 @@ Example response:
```json ```json
[ [
{ "2021-03-01": 3 }, { "2021-03-01": 3, "date": "2021-03-01", "value": 3 },
{ "2021-03-02": 6 }, { "2021-03-02": 6, "date": "2021-03-02", "value": 6 },
{ "2021-03-03": 0 }, { "2021-03-03": 0, "date": "2021-03-03", "value": 0 },
{ "2021-03-04": 0 }, { "2021-03-04": 0, "date": "2021-03-04", "value": 0 },
{ "2021-03-05": 0 }, { "2021-03-05": 0, "date": "2021-03-05", "value": 0 },
{ "2021-03-06": 0 }, { "2021-03-06": 0, "date": "2021-03-06", "value": 0 },
{ "2021-03-07": 0 }, { "2021-03-07": 0, "date": "2021-03-07", "value": 0 },
{ "2021-03-08": 4 } { "2021-03-08": 4, "date": "2021-03-08", "value": 4 }
] ]
``` ```
...@@ -57,6 +57,8 @@ module Dora ...@@ -57,6 +57,8 @@ module Dora
def aggregate_for!(metric, interval) def aggregate_for!(metric, interval)
data_query = data_query_for!(metric) data_query = data_query_for!(metric)
# NOTE: We would remove the `{ date => value }` entry in 14.0 in favor of the explicit `date` and `value` keys.
# See more https://gitlab.com/gitlab-org/gitlab/-/issues/325931
case interval case interval
when INTERVAL_ALL when INTERVAL_ALL
select(data_query).take.data select(data_query).take.data
...@@ -64,12 +66,12 @@ module Dora ...@@ -64,12 +66,12 @@ module Dora
select("DATE_TRUNC('month', date)::date AS month, #{data_query}") select("DATE_TRUNC('month', date)::date AS month, #{data_query}")
.group("DATE_TRUNC('month', date)") .group("DATE_TRUNC('month', date)")
.order('month ASC') .order('month ASC')
.map { |row| { row.month.to_s => row.data } } .map { |row| { row.month.to_s => row.data, 'date' => row.month.to_s, 'value' => row.data } }
when INTERVAL_DAILY when INTERVAL_DAILY
select("date, #{data_query}") select("date, #{data_query}")
.group('date') .group('date')
.order('date ASC') .order('date ASC')
.map { |row| { row.date.to_s => row.data } } .map { |row| { row.date.to_s => row.data, 'date' => row.date.to_s, 'value' => row.data } }
else else
raise ArgumentError, 'Unknown interval' raise ArgumentError, 'Unknown interval'
end end
......
...@@ -192,7 +192,7 @@ RSpec.describe Dora::DailyMetrics, type: :model do ...@@ -192,7 +192,7 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_MONTHLY } let(:interval) { described_class::INTERVAL_MONTHLY }
it 'aggregates the rows' do it 'aggregates the rows' do
is_expected.to eq([{ '2021-01-01' => 12 }]) is_expected.to eq([{ '2021-01-01' => 12, 'date' => '2021-01-01', 'value' => 12 }])
end end
end end
...@@ -200,10 +200,10 @@ RSpec.describe Dora::DailyMetrics, type: :model do ...@@ -200,10 +200,10 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_DAILY } let(:interval) { described_class::INTERVAL_DAILY }
it 'aggregates the rows' do it 'aggregates the rows' do
is_expected.to eq([{ '2021-01-01' => 6 }, is_expected.to eq([{ '2021-01-01' => 6, 'date' => '2021-01-01', 'value' => 6 },
{ '2021-01-02' => 4 }, { '2021-01-02' => 4, 'date' => '2021-01-02', 'value' => 4 },
{ '2021-01-03' => 2 }, { '2021-01-03' => 2, 'date' => '2021-01-03', 'value' => 2 },
{ '2021-01-04' => nil }]) { '2021-01-04' => nil, 'date' => '2021-01-04', 'value' => nil }])
end end
end end
...@@ -239,7 +239,7 @@ RSpec.describe Dora::DailyMetrics, type: :model do ...@@ -239,7 +239,7 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_MONTHLY } let(:interval) { described_class::INTERVAL_MONTHLY }
it 'calculates the median' do it 'calculates the median' do
is_expected.to eq([{ '2021-01-01' => 75 }]) is_expected.to eq([{ '2021-01-01' => 75, 'date' => '2021-01-01', 'value' => 75 }])
end end
end end
...@@ -247,10 +247,10 @@ RSpec.describe Dora::DailyMetrics, type: :model do ...@@ -247,10 +247,10 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_DAILY } let(:interval) { described_class::INTERVAL_DAILY }
it 'calculates the median' do it 'calculates the median' do
is_expected.to eq([{ '2021-01-01' => 95 }, is_expected.to eq([{ '2021-01-01' => 95, 'date' => '2021-01-01', 'value' => 95 },
{ '2021-01-02' => 75 }, { '2021-01-02' => 75, 'date' => '2021-01-02', 'value' => 75 },
{ '2021-01-03' => 55 }, { '2021-01-03' => 55, 'date' => '2021-01-03', 'value' => 55 },
{ '2021-01-04' => nil }]) { '2021-01-04' => nil, 'date' => '2021-01-04', 'value' => nil }])
end end
end end
......
...@@ -35,8 +35,8 @@ RSpec.describe API::Dora::Metrics do ...@@ -35,8 +35,8 @@ RSpec.describe API::Dora::Metrics do
subject subject
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq([{ '2021-01-01' => 1 }, expect(json_response).to eq([{ '2021-01-01' => 1, 'date' => '2021-01-01', 'value' => 1 },
{ '2021-01-02' => 2 }]) { '2021-01-02' => 2, 'date' => '2021-01-02', 'value' => 2 }])
end end
context 'when user is guest' do context 'when user is guest' do
...@@ -84,8 +84,8 @@ RSpec.describe API::Dora::Metrics do ...@@ -84,8 +84,8 @@ RSpec.describe API::Dora::Metrics do
subject subject
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq([{ 1.day.ago.to_date.to_s => 1 }, expect(json_response).to eq([{ 1.day.ago.to_date.to_s => 1, 'date' => 1.day.ago.to_date.to_s, 'value' => 1 },
{ Time.current.to_date.to_s => 2 }]) { Time.current.to_date.to_s => 2, 'date' => Time.current.to_date.to_s, 'value' => 2 }])
end end
context 'when user is guest' do context 'when user is guest' do
......
...@@ -114,7 +114,7 @@ RSpec.describe Dora::AggregateMetricsService do ...@@ -114,7 +114,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do it 'returns the aggregated data' do
expect(subject[:status]).to eq(:success) expect(subject[:status]).to eq(:success)
expect(subject[:data]).to eq([{ Time.current.to_date.to_s => 2 }]) expect(subject[:data]).to eq([{ Time.current.to_date.to_s => 2, 'date' => Time.current.to_date.to_s, 'value' => 2 }])
end end
context 'when interval is monthly' do context 'when interval is monthly' do
...@@ -122,7 +122,7 @@ RSpec.describe Dora::AggregateMetricsService do ...@@ -122,7 +122,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do it 'returns the aggregated data' do
expect(subject[:status]).to eq(:success) expect(subject[:status]).to eq(:success)
expect(subject[:data]).to eq([{ Time.current.beginning_of_month.to_date.to_s => 2 }]) expect(subject[:data]).to eq([{ Time.current.beginning_of_month.to_date.to_s => 2, 'date' => Time.current.beginning_of_month.to_date.to_s, 'value' => 2 }])
end end
end end
...@@ -140,7 +140,7 @@ RSpec.describe Dora::AggregateMetricsService do ...@@ -140,7 +140,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do it 'returns the aggregated data' do
expect(subject[:status]).to eq(:success) expect(subject[:status]).to eq(:success)
expect(subject[:data]).to eq([{ Time.current.to_date.to_s => 1 }]) expect(subject[:data]).to eq([{ Time.current.to_date.to_s => 1, 'date' => Time.current.to_date.to_s, 'value' => 1 }])
end end
end end
end end
...@@ -174,7 +174,7 @@ RSpec.describe Dora::AggregateMetricsService do ...@@ -174,7 +174,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do it 'returns the aggregated data' do
expect(subject[:status]).to eq(:success) expect(subject[:status]).to eq(:success)
expect(subject[:data]).to eq([{ Time.current.to_date.to_s => 3 }]) expect(subject[:data]).to eq([{ Time.current.to_date.to_s => 3, 'date' => Time.current.to_date.to_s, 'value' => 3 }])
end end
context 'when interval is monthly' do context 'when interval is monthly' do
...@@ -182,7 +182,7 @@ RSpec.describe Dora::AggregateMetricsService do ...@@ -182,7 +182,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do it 'returns the aggregated data' do
expect(subject[:status]).to eq(:success) expect(subject[:status]).to eq(:success)
expect(subject[:data]).to eq([{ Time.current.beginning_of_month.to_date.to_s => 3 }]) expect(subject[:data]).to eq([{ Time.current.beginning_of_month.to_date.to_s => 3, 'date' => Time.current.beginning_of_month.to_date.to_s, 'value' => 3 }])
end end
end end
......
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