Chore: Use @types/chance types (#66569)

This commit is contained in:
Josh Hunt 2023-04-14 12:20:25 +01:00 committed by GitHub
parent 7bc31ab04b
commit a438576a6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 14 deletions

View File

@ -120,6 +120,7 @@
"@testing-library/user-event": "14.4.3", "@testing-library/user-event": "14.4.3",
"@types/angular": "1.8.4", "@types/angular": "1.8.4",
"@types/angular-route": "1.7.2", "@types/angular-route": "1.7.2",
"@types/chance": "^1.1.3",
"@types/common-tags": "^1.8.0", "@types/common-tags": "^1.8.0",
"@types/d3": "7.4.0", "@types/d3": "7.4.0",
"@types/d3-force": "^3.0.0", "@types/d3-force": "^3.0.0",

View File

@ -1 +0,0 @@
declare module 'chance';

View File

@ -14,11 +14,62 @@
import Chance from 'chance'; import Chance from 'chance';
import { TraceSpanData, TraceProcess } from 'app/features/explore/TraceView/components/types/trace'; import {
TraceSpanData,
TraceProcess,
TraceKeyValuePair,
TraceResponse,
} from 'app/features/explore/TraceView/components/types/trace';
import { getSpanId } from '../selectors/span'; import { getSpanId } from '../selectors/span';
const chance = new Chance(); interface Process extends TraceProcess {
processID: string;
}
interface ChanceSpanOptions {
traceID?: string;
processes?: Record<string, unknown>;
traceStartTime?: number;
traceEndTime?: number;
operations?: string[];
}
interface ChanceTraceOptions {
numberOfSpans?: number;
numberOfProcesses?: number;
maxDepth?: number;
spansPerLevel?: number | null;
}
interface ChanceTracesOptions {
numberOfTraces?: number;
}
interface ChanceProcessOptions {
services?: string[];
}
interface ChanceProcessesOptions {
numberOfProcesses?: number;
}
interface ChanceMixins {
tag(): TraceKeyValuePair;
tags(): TraceKeyValuePair[];
span(options: ChanceSpanOptions): TraceSpanData;
trace(options: ChanceTraceOptions): TraceResponse;
traces(options: ChanceTracesOptions): TraceResponse[];
process(options: ChanceProcessOptions): Process;
processes(options: ChanceProcessesOptions): Process[];
}
// Difficult to extend Chance interface with our mixins, so we just steam roll them in instead
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const chance = new Chance() as Chance.Chance & ChanceMixins;
export const SERVICE_LIST = ['serviceA', 'serviceB', 'serviceC', 'serviceD', 'serviceE', 'serviceF']; export const SERVICE_LIST = ['serviceA', 'serviceB', 'serviceC', 'serviceD', 'serviceE', 'serviceF'];
export const OPERATIONS_LIST = [ export const OPERATIONS_LIST = [
@ -32,10 +83,6 @@ export const OPERATIONS_LIST = [
'MongoDB::update', 'MongoDB::update',
]; ];
type Process = TraceProcess & {
processID: string;
};
function setupParentSpan(spans: TraceSpanData[], parentSpanValues: TraceSpanData) { function setupParentSpan(spans: TraceSpanData[], parentSpanValues: TraceSpanData) {
Object.assign(spans[0], parentSpanValues); Object.assign(spans[0], parentSpanValues);
return spans; return spans;
@ -55,7 +102,7 @@ function getParentSpanId(span: TraceSpanData, levels: string[][]) {
} }
/* this simulates the hierarchy created by CHILD_OF tags */ /* this simulates the hierarchy created by CHILD_OF tags */
function attachReferences(spans: TraceSpanData[], depth: number, spansPerLevel: null) { function attachReferences(spans: TraceSpanData[], depth: number, spansPerLevel: number | null) {
let levels: string[][] = [[getSpanId(spans[0])]]; let levels: string[][] = [[getSpanId(spans[0])]];
const duplicateLevelFilter = (currentLevels: string[][]) => (span: TraceSpanData) => const duplicateLevelFilter = (currentLevels: string[][]) => (span: TraceSpanData) =>
@ -81,7 +128,7 @@ function attachReferences(spans: TraceSpanData[], depth: number, spansPerLevel:
...span, ...span,
references: [ references: [
{ {
refType: 'CHILD_OF', refType: 'CHILD_OF' as const,
traceID: span.traceID, traceID: span.traceID,
spanID: parentSpanId, spanID: parentSpanId,
}, },
@ -104,7 +151,7 @@ export default chance.mixin({
numberOfProcesses = chance.integer({ min: 1, max: 10 }), numberOfProcesses = chance.integer({ min: 1, max: 10 }),
maxDepth = chance.integer({ min: 1, max: 10 }), maxDepth = chance.integer({ min: 1, max: 10 }),
spansPerLevel = null, spansPerLevel = null,
}) { }: ChanceTraceOptions) {
const traceID = chance.guid(); const traceID = chance.guid();
const duration: number = chance.integer({ min: 10000, max: 5000000 }); const duration: number = chance.integer({ min: 10000, max: 5000000 });
const timestamp = (new Date().getTime() - chance.integer({ min: 0, max: 1000 }) * 1000) * 1000; const timestamp = (new Date().getTime() - chance.integer({ min: 0, max: 1000 }) * 1000) * 1000;
@ -129,6 +176,7 @@ export default chance.mixin({
processes, processes,
}; };
}, },
tag() { tag() {
return { return {
key: 'http.url', key: 'http.url',
@ -136,13 +184,14 @@ export default chance.mixin({
value: `/v2/${chance.pickone(['alpha', 'beta', 'gamma'])}/${chance.guid()}`, value: `/v2/${chance.pickone(['alpha', 'beta', 'gamma'])}/${chance.guid()}`,
}; };
}, },
span({ span({
traceID = chance.guid(), traceID = chance.guid(),
processes = {}, processes = {},
traceStartTime = 0, traceStartTime = 0,
traceEndTime = 0, traceEndTime = 0,
operations = OPERATIONS_LIST, operations = OPERATIONS_LIST,
}) { }: ChanceSpanOptions) {
// Set default values for trace start/end time. // Set default values for trace start/end time.
traceStartTime = traceStartTime || chance.timestamp() * 1000 * 1000; traceStartTime = traceStartTime || chance.timestamp() * 1000 * 1000;
traceEndTime = traceEndTime || traceStartTime + 100000; traceEndTime = traceEndTime || traceStartTime + 100000;
@ -167,20 +216,24 @@ export default chance.mixin({
logs: [], logs: [],
}; };
}, },
process({ services = SERVICE_LIST }) {
process({ services = SERVICE_LIST }: ChanceProcessOptions) {
return { return {
processID: chance.guid(), processID: chance.guid(),
serviceName: chance.pickone(services), serviceName: chance.pickone(services),
tags: chance.tags(), tags: chance.tags(),
}; };
}, },
traces({ numberOfTraces = chance.integer({ min: 5, max: 15 }) }) {
traces({ numberOfTraces = chance.integer({ min: 5, max: 15 }) }: ChanceTracesOptions) {
return chance.n(chance.trace, numberOfTraces, {}); return chance.n(chance.trace, numberOfTraces, {});
}, },
tags() { tags() {
return chance.n(chance.tag, chance.integer({ min: 1, max: 10 }), {}); return chance.n(chance.tag, chance.integer({ min: 1, max: 10 }), {});
}, },
processes({ numberOfProcesses = chance.integer({ min: 1, max: 25 }) }) {
processes({ numberOfProcesses = chance.integer({ min: 1, max: 25 }) }: ChanceProcessesOptions) {
return chance.n(chance.process, numberOfProcesses, {}); return chance.n(chance.process, numberOfProcesses, {});
}, },
}); });

View File

@ -8942,6 +8942,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/chance@npm:^1.1.3":
version: 1.1.3
resolution: "@types/chance@npm:1.1.3"
checksum: df2be43fab359e5fa734fb91aa1c4c15108310be0e4990d00203805a63f9b2182da74eaabc8859b742e150d970decf27a773fcc6cbf28fb5b372c9b1bd7e9b89
languageName: node
linkType: hard
"@types/chrome-remote-interface@npm:0.31.9": "@types/chrome-remote-interface@npm:0.31.9":
version: 0.31.9 version: 0.31.9
resolution: "@types/chrome-remote-interface@npm:0.31.9" resolution: "@types/chrome-remote-interface@npm:0.31.9"
@ -20182,6 +20189,7 @@ __metadata:
"@testing-library/user-event": 14.4.3 "@testing-library/user-event": 14.4.3
"@types/angular": 1.8.4 "@types/angular": 1.8.4
"@types/angular-route": 1.7.2 "@types/angular-route": 1.7.2
"@types/chance": ^1.1.3
"@types/common-tags": ^1.8.0 "@types/common-tags": ^1.8.0
"@types/d3": 7.4.0 "@types/d3": 7.4.0
"@types/d3-force": ^3.0.0 "@types/d3-force": ^3.0.0