import * as path from 'path' ; import { metric } from 'napajs' ; import * as utils from './utils' ; export type MetricCollection = { [ name ] : metric . Metric ; } ; export interface MetricDef { name : string ; sectionName : string ; displayName : string ; description ? : string ; type : metric . MetricType ; dimensionNames ? : string [ ] ; } const SCHEMA_DIR = path . resolve ( __dirname , '../schema' ) ; export class MetricConfig { private static readonly METRIC_CONFIG_SCHEMA : utils . JsonSchema = new utils . JsonSchema ( path . resolve ( SCHEMA_DIR , "metric-config.schema.json" ) ) ; private static _transform : utils . Transform = new utils . SetDefaultValue ( { 'dimensionNames' : [ ] } ) . add ( new utils . TransformPropertyValues ( { 'type' : ( metricTypeName ) => { let lowerCaseTypeName = metricTypeName . toLowerCase ( ) ; switch ( lowerCaseTypeName ) { case 'number' : return metric . MetricType . Number ; case 'rate' : return metric . MetricType . Rate ; case 'percentile' : return metric . MetricType . Percentile ; default : throw new Error ( "Invalid metric type: '" + metricTypeName + "'." ) ; } } } ) ) ; public static fromConfigObject ( sectionName , jsValue ) { utils . ensureSchema ( jsValue , this . METRIC_CONFIG_SCHEMA ) ; jsValue . forEach ( obj => { this . _transform . apply ( obj ) ; obj . sectionName = sectionName ; } ) ; return < MetricDef [ ] > ( jsValue ) ; } public static fromConfig ( sectionName , metricConfigFile ) { return utils . appendMessageOnException ( "Error found in metric definition file '" + metricConfigFile + "'." , ( ) => { return this . fromConfigObject ( sectionName , utils . readConfig ( metricConfigFile ) ) ; } ) ; } } export function createMetricCollection ( defs ) { let metrics = { } ; for ( let m of defs ) { metrics [ m . name ] = metric . get ( m . sectionName , m . displayName , m . type , m . dimensionNames ) ; } return metrics ; }