-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
9d96334
commit ccc3159
Showing
9 changed files
with
756 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
x-pack/plugins/apm/common/utils/array_union_to_callable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { ValuesType } from 'utility-types'; | ||
|
||
// work around a TypeScript limitation described in https://stackoverflow.com/posts/49511416 | ||
|
||
export const arrayUnionToCallable = <T extends any[]>( | ||
array: T | ||
): Array<ValuesType<T>> => { | ||
return array; | ||
}; |
104 changes: 104 additions & 0 deletions
104
x-pack/plugins/apm/common/utils/join_by_key/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { joinByKey } from './'; | ||
|
||
describe('joinByKey', () => { | ||
it('joins by a string key', () => { | ||
const joined = joinByKey( | ||
[ | ||
{ | ||
serviceName: 'opbeans-node', | ||
avg: 10, | ||
}, | ||
{ | ||
serviceName: 'opbeans-node', | ||
count: 12, | ||
}, | ||
{ | ||
serviceName: 'opbeans-java', | ||
avg: 11, | ||
}, | ||
{ | ||
serviceName: 'opbeans-java', | ||
p95: 18, | ||
}, | ||
], | ||
'serviceName' | ||
); | ||
|
||
expect(joined.length).toBe(2); | ||
|
||
expect(joined).toEqual([ | ||
{ | ||
serviceName: 'opbeans-node', | ||
avg: 10, | ||
count: 12, | ||
}, | ||
{ | ||
serviceName: 'opbeans-java', | ||
avg: 11, | ||
p95: 18, | ||
}, | ||
]); | ||
}); | ||
|
||
it('joins by a record key', () => { | ||
const joined = joinByKey( | ||
[ | ||
{ | ||
key: { | ||
serviceName: 'opbeans-node', | ||
transactionName: '/api/opbeans-node', | ||
}, | ||
avg: 10, | ||
}, | ||
{ | ||
key: { | ||
serviceName: 'opbeans-node', | ||
transactionName: '/api/opbeans-node', | ||
}, | ||
count: 12, | ||
}, | ||
{ | ||
key: { | ||
serviceName: 'opbeans-java', | ||
transactionName: '/api/opbeans-java', | ||
}, | ||
avg: 11, | ||
}, | ||
{ | ||
key: { | ||
serviceName: 'opbeans-java', | ||
transactionName: '/api/opbeans-java', | ||
}, | ||
p95: 18, | ||
}, | ||
], | ||
'key' | ||
); | ||
|
||
expect(joined.length).toBe(2); | ||
|
||
expect(joined).toEqual([ | ||
{ | ||
key: { | ||
serviceName: 'opbeans-node', | ||
transactionName: '/api/opbeans-node', | ||
}, | ||
avg: 10, | ||
count: 12, | ||
}, | ||
{ | ||
key: { | ||
serviceName: 'opbeans-java', | ||
transactionName: '/api/opbeans-java', | ||
}, | ||
avg: 11, | ||
p95: 18, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { UnionToIntersection, ValuesType } from 'utility-types'; | ||
import { isEqual } from 'lodash'; | ||
|
||
/** | ||
* Joins a list of records by a given key. Key can be any type of value, from | ||
* strings to plain objects, as long as it is present in all records. `isEqual` | ||
* is used for comparing keys. | ||
* | ||
* UnionToIntersection is needed to get all keys of union types, see below for | ||
* example. | ||
* | ||
const agentNames = [{ serviceName: '', agentName: '' }]; | ||
const transactionRates = [{ serviceName: '', transactionsPerMinute: 1 }]; | ||
const flattened = joinByKey( | ||
[...agentNames, ...transactionRates], | ||
'serviceName' | ||
); | ||
*/ | ||
|
||
type JoinedReturnType< | ||
T extends Record<string, any>, | ||
U extends UnionToIntersection<T>, | ||
V extends keyof T & keyof U | ||
> = Array<Partial<U> & Record<V, U[V]>>; | ||
|
||
export function joinByKey< | ||
T extends Record<string, any>, | ||
U extends UnionToIntersection<T>, | ||
V extends keyof T & keyof U | ||
>(items: T[], key: V): JoinedReturnType<T, U, V> { | ||
return items.reduce<JoinedReturnType<T, U, V>>((prev, current) => { | ||
let item = prev.find((prevItem) => isEqual(prevItem[key], current[key])); | ||
|
||
if (!item) { | ||
item = { ...current } as ValuesType<JoinedReturnType<T, U, V>>; | ||
prev.push(item); | ||
} else { | ||
Object.assign(item, current); | ||
} | ||
|
||
return prev; | ||
}, []); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.