import { MetricStatistics } from "../MetricStatistics.js";
import { PeriodicConsolePrinter } from "./PeriodicConsolePrinter.js";

/**
 *
 * @example
 *  const metrics = new MetricCollection(); // get a metrics collection
 *
 *  // ...
 *  MetricCollectionConsoleMonitor.from(metrics).start(); // from now on all metric stats will be printed in console every few seconds
 */
export class MetricCollectionConsoleMonitor {

    /**
     *
     * @param {MetricCollection} metrics
     * @param {number} [timeout_seconds] how frequently should stats be printed? In seconds.
     * @returns {PeriodicConsolePrinter}
     */
    static from(metrics,timeout_seconds= 3) {

        const metric_stats = new MetricStatistics();

        return new PeriodicConsolePrinter(timeout_seconds, () => metrics.list().map(m => {
            metrics.get(m).computeStats(metric_stats);

            return `${m}: ${metric_stats}`;
        }).join('\n'));

    }

}