Skip to content

Commit a78cd94

Browse files
committed
chore: make json file for flag usage metrics append only
1 parent dac1881 commit a78cd94

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

scripts/update_flag_usage_metrics.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getPossibleFlagMetrics,
1313
type FlagMetric,
1414
} from '../build/src/telemetry/flagUtils.js';
15+
import {applyToExisting} from '../build/src/telemetry/toolMetricsUtils.js';
1516

1617
function writeFlagUsageMetrics() {
1718
const outputPath = path.resolve('src/telemetry/flag_usage_metrics.json');
@@ -21,15 +22,29 @@ function writeFlagUsageMetrics() {
2122
throw new Error(`Error: Directory ${dir} does not exist.`);
2223
}
2324

24-
const metrics = getPossibleFlagMetrics(cliOptions);
25+
let existingMetrics: FlagMetric[] = [];
26+
if (fs.existsSync(outputPath)) {
27+
try {
28+
existingMetrics = JSON.parse(
29+
fs.readFileSync(outputPath, 'utf8'),
30+
) as FlagMetric[];
31+
} catch {
32+
console.warn(
33+
`Warning: Failed to parse existing metrics from ${outputPath}. Starting fresh.`,
34+
);
35+
}
36+
}
2537

26-
// Sort metrics by name for deterministic output
27-
metrics.sort((a: FlagMetric, b: FlagMetric) => a.name.localeCompare(b.name));
38+
const newMetrics = getPossibleFlagMetrics(cliOptions);
39+
const mergedMetrics = applyToExisting<FlagMetric>(
40+
existingMetrics,
41+
newMetrics,
42+
);
2843

29-
fs.writeFileSync(outputPath, JSON.stringify(metrics, null, 2) + '\n');
44+
fs.writeFileSync(outputPath, JSON.stringify(mergedMetrics, null, 2) + '\n');
3045

3146
console.log(
32-
`Successfully wrote ${metrics.length} flag usage metrics to ${outputPath}`,
47+
`Successfully wrote ${mergedMetrics.length} flag usage metrics to ${outputPath}`,
3348
);
3449
}
3550

src/telemetry/toolMetricsUtils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ export function applyToExistingMetrics(
6262
});
6363
}
6464

65-
function applyToExisting<T extends {name: string; isDeprecated?: boolean}>(
66-
existing: T[],
67-
update: T[],
68-
): T[] {
65+
export function applyToExisting<
66+
T extends {name: string; isDeprecated?: boolean},
67+
>(existing: T[], update: T[]): T[] {
6968
const existingNames = new Set(existing.map(item => item.name));
7069
const updatedNames = new Set(update.map(item => item.name));
7170

0 commit comments

Comments
 (0)