Commit 4996104f authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'change-payload-format-for-dora-metrics' into 'master'

Improve payload format of DORA metrics API

See merge request gitlab-org/gitlab!57314
parents 89e711cb 5e9c5c87
---
title: Improve payload format of DORA metrics API
merge_request: 57314
author:
type: added
......@@ -38,14 +38,14 @@ Example response:
```json
[
{ "2021-03-01": 3 },
{ "2021-03-02": 6 },
{ "2021-03-03": 0 },
{ "2021-03-04": 0 },
{ "2021-03-05": 0 },
{ "2021-03-06": 0 },
{ "2021-03-07": 0 },
{ "2021-03-08": 4 }
{ "2021-03-01": 3, "date": "2021-03-01", "value": 3 },
{ "2021-03-02": 6, "date": "2021-03-02", "value": 6 },
{ "2021-03-03": 0, "date": "2021-03-03", "value": 0 },
{ "2021-03-04": 0, "date": "2021-03-04", "value": 0 },
{ "2021-03-05": 0, "date": "2021-03-05", "value": 0 },
{ "2021-03-06": 0, "date": "2021-03-06", "value": 0 },
{ "2021-03-07": 0, "date": "2021-03-07", "value": 0 },
{ "2021-03-08": 4, "date": "2021-03-08", "value": 4 }
]
```
......@@ -78,13 +78,13 @@ Example response:
```json
[
{ "2021-03-01": 3 },
{ "2021-03-02": 6 },
{ "2021-03-03": 0 },
{ "2021-03-04": 0 },
{ "2021-03-05": 0 },
{ "2021-03-06": 0 },
{ "2021-03-07": 0 },
{ "2021-03-08": 4 }
{ "2021-03-01": 3, "date": "2021-03-01", "value": 3 },
{ "2021-03-02": 6, "date": "2021-03-02", "value": 6 },
{ "2021-03-03": 0, "date": "2021-03-03", "value": 0 },
{ "2021-03-04": 0, "date": "2021-03-04", "value": 0 },
{ "2021-03-05": 0, "date": "2021-03-05", "value": 0 },
{ "2021-03-06": 0, "date": "2021-03-06", "value": 0 },
{ "2021-03-07": 0, "date": "2021-03-07", "value": 0 },
{ "2021-03-08": 4, "date": "2021-03-08", "value": 4 }
]
```
......@@ -57,6 +57,8 @@ module Dora
def aggregate_for!(metric, interval)
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
when INTERVAL_ALL
select(data_query).take.data
......@@ -64,12 +66,12 @@ module Dora
select("DATE_TRUNC('month', date)::date AS month, #{data_query}")
.group("DATE_TRUNC('month', date)")
.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
select("date, #{data_query}")
.group('date')
.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
raise ArgumentError, 'Unknown interval'
end
......
......@@ -192,7 +192,7 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_MONTHLY }
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
......@@ -200,10 +200,10 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_DAILY }
it 'aggregates the rows' do
is_expected.to eq([{ '2021-01-01' => 6 },
{ '2021-01-02' => 4 },
{ '2021-01-03' => 2 },
{ '2021-01-04' => nil }])
is_expected.to eq([{ '2021-01-01' => 6, 'date' => '2021-01-01', 'value' => 6 },
{ '2021-01-02' => 4, 'date' => '2021-01-02', 'value' => 4 },
{ '2021-01-03' => 2, 'date' => '2021-01-03', 'value' => 2 },
{ '2021-01-04' => nil, 'date' => '2021-01-04', 'value' => nil }])
end
end
......@@ -239,7 +239,7 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_MONTHLY }
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
......@@ -247,10 +247,10 @@ RSpec.describe Dora::DailyMetrics, type: :model do
let(:interval) { described_class::INTERVAL_DAILY }
it 'calculates the median' do
is_expected.to eq([{ '2021-01-01' => 95 },
{ '2021-01-02' => 75 },
{ '2021-01-03' => 55 },
{ '2021-01-04' => nil }])
is_expected.to eq([{ '2021-01-01' => 95, 'date' => '2021-01-01', 'value' => 95 },
{ '2021-01-02' => 75, 'date' => '2021-01-02', 'value' => 75 },
{ '2021-01-03' => 55, 'date' => '2021-01-03', 'value' => 55 },
{ '2021-01-04' => nil, 'date' => '2021-01-04', 'value' => nil }])
end
end
......
......@@ -35,8 +35,8 @@ RSpec.describe API::Dora::Metrics do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq([{ '2021-01-01' => 1 },
{ '2021-01-02' => 2 }])
expect(json_response).to eq([{ '2021-01-01' => 1, 'date' => '2021-01-01', 'value' => 1 },
{ '2021-01-02' => 2, 'date' => '2021-01-02', 'value' => 2 }])
end
context 'when user is guest' do
......@@ -84,8 +84,8 @@ RSpec.describe API::Dora::Metrics do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq([{ 1.day.ago.to_date.to_s => 1 },
{ Time.current.to_date.to_s => 2 }])
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, 'date' => Time.current.to_date.to_s, 'value' => 2 }])
end
context 'when user is guest' do
......
......@@ -114,7 +114,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do
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
context 'when interval is monthly' do
......@@ -122,7 +122,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do
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
......@@ -140,7 +140,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do
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
......@@ -174,7 +174,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do
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
context 'when interval is monthly' do
......@@ -182,7 +182,7 @@ RSpec.describe Dora::AggregateMetricsService do
it 'returns the aggregated data' do
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
......
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