-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7774b08
commit 6d44bc8
Showing
4 changed files
with
226 additions
and
48 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
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,24 @@ | ||
import type { Outcome } from '@sentry/types'; | ||
|
||
/** | ||
* Merges buffer with new outcomes. | ||
*/ | ||
export function mergeOutcomes(...merge: Outcome[][]): Outcome[] { | ||
let counter = 0; | ||
const map = new Map<string, number>(); | ||
const outcomes: Outcome[] = []; | ||
|
||
const process = (outcome: Outcome): void => { | ||
const key = `${outcome.reason}:${outcome.category}`; | ||
const index = map.get(key); | ||
if (typeof(index) !== "undefined") { | ||
outcomes[index].quantity += outcome.quantity; | ||
} else { | ||
map.set(key, counter++); | ||
outcomes.push(outcome); | ||
} | ||
}; | ||
|
||
merge.forEach(outcomes => outcomes.forEach(process)); | ||
return outcomes; | ||
} |
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
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,129 @@ | ||
import type { Outcome } from '@sentry/types'; | ||
|
||
import { mergeOutcomes } from '../../src/utils/outcome'; | ||
|
||
describe('mergeOutcomes', () => { | ||
test('merge same outcomes into one incrementing the quantity', () => { | ||
const outcome1: Outcome[] = | ||
[{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const outcome2: Outcome[] = | ||
[{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const expectedOutcome: Outcome[] = [{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 2, | ||
}]; | ||
const finalOutcomes = mergeOutcomes(outcome1, outcome2); | ||
|
||
expect(finalOutcomes).toStrictEqual(expectedOutcome); | ||
}); | ||
|
||
test('merge different outcomes into separated outcomes', () => { | ||
const outcome1: Outcome[] = | ||
[{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const outcome2: Outcome[] = | ||
[{ | ||
reason: 'event_processor', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const expectedOutcome: Outcome[] = [{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}, | ||
{ | ||
reason: 'event_processor', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const finalOutcomes = mergeOutcomes(outcome1, outcome2); | ||
|
||
expect(finalOutcomes).toStrictEqual(expectedOutcome); | ||
}); | ||
|
||
test('merge outcomes when first outcome is empty', () => { | ||
const outcome1: Outcome[] = []; | ||
const outcome2: Outcome[] = | ||
[{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const expectedOutcome: Outcome[] = [{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const finalOutcomes = mergeOutcomes(outcome1, outcome2); | ||
|
||
expect(finalOutcomes).toStrictEqual(expectedOutcome); | ||
}); | ||
|
||
test('merge outcomes when second outcome is empty', () => { | ||
const outcome1: Outcome[] = | ||
[{ | ||
reason: 'event_processor', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const expectedOutcome: Outcome[] = [{ | ||
reason: 'event_processor', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const outcome2: Outcome[] = []; | ||
const finalOutcomes = mergeOutcomes(outcome1, outcome2); | ||
|
||
expect(finalOutcomes).toStrictEqual(expectedOutcome); | ||
}); | ||
|
||
test('empty outcomes return an array of empty outcomes', () => { | ||
const outcome1: Outcome[] = []; | ||
const expectedOutcome: Outcome[] = []; | ||
const outcome2: Outcome[] = []; | ||
const finalOutcomes = mergeOutcomes(outcome1, outcome2); | ||
|
||
expect(finalOutcomes).toStrictEqual(expectedOutcome); | ||
}); | ||
|
||
test('same outocmes but different category into separated outcomes', () => { | ||
const outcome1: Outcome[] = | ||
[{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}]; | ||
const outcome2: Outcome[] = | ||
[{ | ||
reason: 'before_send', | ||
category: 'default', | ||
quantity: 1, | ||
}]; | ||
const expectedOutcome: Outcome[] = [{ | ||
reason: 'before_send', | ||
category: 'error', | ||
quantity: 1, | ||
}, | ||
{ | ||
reason: 'before_send', | ||
category: 'default', | ||
quantity: 1, | ||
}]; | ||
const finalOutcomes = mergeOutcomes(outcome1, outcome2); | ||
|
||
expect(finalOutcomes).toStrictEqual(expectedOutcome); | ||
}); | ||
}); |