/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict-local
 */

const tally: {
  counts: {[string]: number},
  total: number,
  record(key: string, n: number): number,
  topKey(): string | null,
} = {
  counts: {},
  total: 0,
  record(key: string, n: number): number {
    const prev = tally.counts[key] ?? 0;
    tally.counts[key] = prev + n;
    tally.total += n;
    return tally.counts[key];
  },
  topKey(): string | null {
    let best: string | null = null;
    let bestN = 0;
    for (const k of Object.keys(tally.counts)) {
      const v = tally.counts[k];
      if (v > bestN) {
        bestN = v;
        best = k;
      }
    }
    return best;
  },
};

tally.record('cache_hit', 3);
tally.record('cache_miss', 1);
tally.record('cache_hit', 2);
console.log(`top=${String(tally.topKey())} total=${tally.total}`);
