"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/metrics.ts" (20 Feb 2024, 3512 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 const debug = require('debug')('snyk-metrics');
    2 
    3 type MetricType = 'timer' | 'synthetic';
    4 export type MetricValue = number | undefined;
    5 export const METRIC_TYPE_TIMER = 'timer';
    6 export const METRIC_TYPE_SYNTHETIC = 'synthetic';
    7 
    8 export abstract class MetricInstance {
    9   abstract getValue(): MetricValue;
   10 }
   11 
   12 export class TimerMetricInstance extends MetricInstance {
   13   startTimeMs = 0;
   14   endTimeMs = 0;
   15   metricTag: string;
   16 
   17   /**
   18    * Creates a new TimerMetricInstance
   19    * @param metricTag used for logging to identify the metric
   20    */
   21   public constructor(metricTag: string) {
   22     super();
   23     this.metricTag = metricTag;
   24   }
   25 
   26   public getValue(): MetricValue {
   27     if (this.startTimeMs !== 0 && this.endTimeMs !== 0) {
   28       return this.endTimeMs - this.startTimeMs;
   29     } else {
   30       return undefined;
   31     }
   32   }
   33 
   34   public start() {
   35     if (this.startTimeMs === 0) {
   36       this.startTimeMs = Date.now();
   37       debug(`Timer ${this.metricTag} started at ${this.startTimeMs}.`);
   38     } else {
   39       debug('Invalid Timer use: start() called when timer already stopped');
   40     }
   41   }
   42 
   43   public stop() {
   44     if (this.endTimeMs === 0) {
   45       this.endTimeMs = Date.now();
   46       debug(
   47         `Timer ${this.metricTag} stopped at ${
   48           this.endTimeMs
   49         }. Elapsed time is ${this.getValue()}`,
   50       );
   51     } else {
   52       debug('Invalid Timer use: stop() called when timer already stopped');
   53     }
   54   }
   55 }
   56 
   57 export class SyntheticMetricInstance extends MetricInstance {
   58   private value = 0;
   59 
   60   public setValue(value: number) {
   61     this.value = value;
   62   }
   63 
   64   public getValue(): number {
   65     return this.value;
   66   }
   67 }
   68 
   69 export abstract class Metric {
   70   public name: string;
   71   public context: string;
   72   public metricType: MetricType;
   73   protected instances: Array<MetricInstance> = [];
   74 
   75   public clear() {
   76     this.instances = [];
   77   }
   78 
   79   public getValues(): number[] {
   80     return this.instances.map((mi) => mi.getValue() || 0);
   81   }
   82 
   83   public getTotal(): number {
   84     const sumMetricValues = (accum: number, current: MetricInstance) => {
   85       const currentTimerMs = current.getValue() || 0;
   86       return (accum = accum + currentTimerMs);
   87     };
   88 
   89     const total = this.instances.reduce(sumMetricValues, 0);
   90     return total;
   91   }
   92 
   93   public constructor(name: string, metricType: MetricType, context: string) {
   94     this.name = name;
   95     this.metricType = metricType;
   96     this.context = context;
   97   }
   98 }
   99 
  100 export class TimerMetric extends Metric {
  101   public createInstance(): TimerMetricInstance {
  102     const t = new TimerMetricInstance(`${this.metricType}/${this.name}`);
  103     this.instances.push(t);
  104     return t;
  105   }
  106 }
  107 
  108 export class SyntheticMetric extends Metric {
  109   public createInstance(): SyntheticMetricInstance {
  110     const sm = new SyntheticMetricInstance();
  111     this.instances.push(sm);
  112     return sm;
  113   }
  114 }
  115 
  116 export class MetricsCollector {
  117   public static NETWORK_TIME: TimerMetric = new TimerMetric(
  118     'network_time',
  119     'timer',
  120     'Total time spent making and waiting on network requests',
  121   );
  122   public static CPU_TIME: SyntheticMetric = new SyntheticMetric(
  123     'cpu_time',
  124     'synthetic',
  125     'Time spent on things other than network requests',
  126   );
  127 
  128   public static getAllMetrics(): any[] {
  129     const metrics: Metric[] = [
  130       MetricsCollector.NETWORK_TIME,
  131       MetricsCollector.CPU_TIME,
  132     ];
  133 
  134     const res: any = {};
  135     for (const m of metrics) {
  136       res[m.name] = {
  137         type: m.metricType,
  138         values: m.getValues(),
  139         total: m.getTotal(),
  140       };
  141     }
  142     return res;
  143   }
  144 }