Skip to content

Commit 23da993

Browse files
committed
refactor: expandSeriesByValues
1 parent ed5aa20 commit 23da993

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

src/pullRequest/expand.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Series } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-v1/models/Series'
22

3-
export const expandSeriesByLabels = (series: Series[], labels: { name: string }[]): Series[] => {
4-
if (labels.length === 0) {
3+
export const expandSeriesByValues = (series: Series[], key: string, values: string[]): Series[] => {
4+
if (values.length === 0) {
55
return series
66
}
77
return series.flatMap((s) =>
8-
labels.map((label) => ({
8+
values.map((v) => ({
99
...s,
10-
tags: [...(s.tags ?? []), `label:${label.name}`],
10+
tags: [...(s.tags ?? []), `${key}:${v}`],
1111
}))
1212
)
1313
}

src/pullRequest/metrics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Series } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-v1/models/Series'
22
import { PullRequestClosedEvent, PullRequestEvent, PullRequestOpenedEvent } from '@octokit/webhooks-types'
33
import { ClosedPullRequest } from '../queries/closedPullRequest'
4-
import { expandSeriesByLabels } from './expand'
4+
import { expandSeriesByValues } from './expand'
55

66
const computeCommonTags = (e: PullRequestEvent): string[] => {
77
const tags = [
@@ -139,7 +139,7 @@ export const computePullRequestClosedMetrics = (
139139
if (options.sendPullRequestLabels) {
140140
// Datadog treats a tag as combination of values.
141141
// For example, if we send a metric with tags `label:foo` and `label:bar`, Datadog will show `label:foo,bar`.
142-
return expandSeriesByLabels(series, e.pull_request.labels)
142+
return expandSeriesByValues(series, 'label', e.pull_request.labels.map((l) => l.name))
143143
}
144144
return series
145145
}

tests/pullRequest/expand.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Series } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-v1/models/Series'
2-
import { expandSeriesByLabels } from '../../src/pullRequest/expand'
2+
import { expandSeriesByValues } from '../../src/pullRequest/expand'
33

44
test('empty', () => {
5-
expect(expandSeriesByLabels([], [])).toStrictEqual([])
5+
expect(expandSeriesByValues([], 'label', [])).toStrictEqual([])
66
})
77

88
test('expand series by no label', () => {
@@ -16,7 +16,7 @@ test('expand series by no label', () => {
1616
points: [[1579721588, 2]],
1717
},
1818
]
19-
expect(expandSeriesByLabels(series, [])).toStrictEqual(series)
19+
expect(expandSeriesByValues(series, 'label', [])).toStrictEqual(series)
2020
})
2121

2222
test('expand series by 1 label', () => {
@@ -30,7 +30,7 @@ test('expand series by 1 label', () => {
3030
points: [[1579721588, 2]],
3131
},
3232
]
33-
expect(expandSeriesByLabels(series, [{ name: 'app' }])).toStrictEqual([
33+
expect(expandSeriesByValues(series, 'label', ['app'])).toStrictEqual([
3434
{
3535
metric: 'github.actions.pull_request_closed.total',
3636
points: [[1579721588, 1]],
@@ -55,7 +55,7 @@ test('expand series by 2 labels', () => {
5555
points: [[1579721588, 2]],
5656
},
5757
]
58-
expect(expandSeriesByLabels(series, [{ name: 'app' }, { name: 'critical' }])).toStrictEqual([
58+
expect(expandSeriesByValues(series, 'label', ['app', 'critical'])).toStrictEqual([
5959
{
6060
metric: 'github.actions.pull_request_closed.total',
6161
points: [[1579721588, 1]],

0 commit comments

Comments
 (0)