mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 06:39:58 -07:00
website: snapshot
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
/** Track unused metrics (dev only) */
|
||||
|
||||
import { localhost } from "../utils/env.js";
|
||||
|
||||
/** @type {Set<AnyMetricPattern> | null} */
|
||||
export const unused = localhost ? new Set() : null;
|
||||
/** @type {Map<AnyMetricPattern, string[]> | null} */
|
||||
export const unused = localhost ? new Map() : null;
|
||||
|
||||
/**
|
||||
* Walk and collect AnyMetricPatterns
|
||||
* @param {TreeNode | null | undefined} node
|
||||
* @param {Set<AnyMetricPattern>} set
|
||||
* @param {Map<AnyMetricPattern, string[]>} map
|
||||
* @param {string[]} path
|
||||
*/
|
||||
function walk(node, set) {
|
||||
function walk(node, map, path) {
|
||||
if (node && "by" in node) {
|
||||
set.add(/** @type {AnyMetricPattern} */ (node));
|
||||
map.set(/** @type {AnyMetricPattern} */ (node), path);
|
||||
} else if (node && typeof node === "object") {
|
||||
for (const value of Object.values(node)) {
|
||||
walk(/** @type {TreeNode | null | undefined} */ (value), set);
|
||||
for (const [key, value] of Object.entries(node)) {
|
||||
walk(/** @type {TreeNode | null | undefined} */ (value), map, [
|
||||
...path,
|
||||
key,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +26,7 @@ function walk(node, set) {
|
||||
* @param {TreeNode} tree
|
||||
*/
|
||||
export function collect(tree) {
|
||||
if (unused) walk(tree, unused);
|
||||
if (unused) walk(tree, unused, []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,6 +40,22 @@ export function markUsed(metric) {
|
||||
/** Log unused metrics to console */
|
||||
export function logUnused() {
|
||||
if (!unused?.size) return;
|
||||
const paths = [...unused].map((m) => Object.values(m.by)[0].path);
|
||||
console.warn("Unused metrics:", paths);
|
||||
|
||||
/** @type {Record<string, any>} */
|
||||
const tree = {};
|
||||
|
||||
for (const path of unused.values()) {
|
||||
let current = tree;
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
const part = path[i];
|
||||
if (i === path.length - 1) {
|
||||
current[part] = null;
|
||||
} else {
|
||||
current[part] = current[part] || {};
|
||||
current = current[part];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Unused metrics:", { count: unused.size, tree });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user