Skip to content

Commit

Permalink
Clean up statsmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Segaran committed Aug 1, 2020
1 parent 9bc8f2c commit 93e55a4
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions packages/apollo-engine-reporting/src/agent.ts
Expand Up @@ -7,12 +7,12 @@ import {
printSchema,
} from 'graphql';
import {
IContextualizedStats,
IStatsContext,
Report,
ReportHeader,
Trace,
Report,
TracesAndStats,
IStatsContext,
IContextualizedStats,
} from 'apollo-engine-reporting-protobuf';

import { fetch, RequestAgent, Response } from 'apollo-server-env';
Expand Down Expand Up @@ -434,11 +434,12 @@ class ReportData {
}

class StatsMap {
readonly map: Map<IStatsContext, ContextualizedStats> = new Map<
IStatsContext,
ContextualizedStats
>();
readonly map: { [k: string]: ContextualizedStats } = Object.create(null);

/**
* This function is used by the protobuf generator to convert this map into
* an array of contextualized stats to serialize
*/
public toArray(): IContextualizedStats[] {
return Array.from(this.map.values());
}
Expand All @@ -450,17 +451,13 @@ class StatsMap {
clientReferenceId: trace.clientReferenceId,
};

// TODO: Update sizing
let contextualizedStats: ContextualizedStats | undefined = this.map.get(
statsContext,
);
const statsContextKey = JSON.stringify(statsContext);

if (!contextualizedStats) {
contextualizedStats = new ContextualizedStats(statsContext);
this.map.set(statsContext, contextualizedStats);
}

contextualizedStats.addTrace(trace);
// TODO: Update sizing
(
this.map[statsContextKey] ||
(this.map[statsContextKey] = new ContextualizedStats(statsContext))
).addTrace(trace);
}
}

Expand Down Expand Up @@ -683,9 +680,7 @@ export class EngineReportingAgent<TContext = any> {
const convertTraceToStats = reportData.traceCache[traceCacheKey];

if (convertTraceToStats) {
(report.tracesPerQuery[statsReportKey] as any).statsWithContext.addTrace(
trace,
);
(report.tracesPerQuery[statsReportKey].statsWithContext as StatsMap).addTrace(trace);
} else {
const protobufError = Trace.verify(trace);
if (protobufError) {
Expand Down

0 comments on commit 93e55a4

Please sign in to comment.