lib/metricAggregators.js
function AggregatorFactory(aggregatorFn) {
	return metrics => {
		if (metrics.length === 0) return;
		const result = {
			help: metrics[0].help,
			name: metrics[0].name,
			type: metrics[0].type,
			values: [],
			aggregator: metrics[0].aggregator
		};
		// Gather metrics by metricName and labels.
		const byLabels = new Grouper();
		metrics.forEach(metric => {
			metric.values.forEach(value => {
				const key = hashObject(value.labels);
				byLabels.add(`${value.metricName}_${key}`, value);
			});
		});
		// Apply aggregator function to gathered metrics.
		byLabels.forEach(values => {
			if (values.length === 0) return;
			const valObj = {
				value: aggregatorFn(values),
				labels: values[0].labels
			};
			if (values[0].metricName) {
				valObj.metricName = values[0].metricName;
			}
			// NB: Timestamps are omitted.
			result.values.push(valObj);
		});
		return result;
	};
}
function AggregatorFactory(aggregatorFn) {
	return metrics => {
		if (metrics.length === 0) return;
		const result = {
			help: metrics[0].help,
			name: metrics[0].name,
			type: metrics[0].type,
			values: [],
			aggregator: metrics[0].aggregator
		};
		// Gather metrics by metricName and labels.
		const byLabels = new Grouper();
		metrics.forEach(metric => {
			metric.values.forEach(value => {
				const key = hashObject(value.labels);
				byLabels.add(`${value.metricName}_${key}`, value);
			});
		});
		// Apply aggregator function to gathered metrics.
		byLabels.forEach(values => {
			if (values.length === 0) return;
			const valObj = {
				value: aggregatorFn(values),
				labels: values[0].labels
			};
			if (values[0].metricName) {
				valObj.metricName = values[0].metricName;
			}
			// NB: Timestamps are omitted.
			result.values.push(valObj);
		});
		return result;
	};
}
function AggregatorFactory ( aggregatorFn ) { return metrics => { if ( metrics . length === 0 ) return ; const result = { help : metrics [ 0 ] . help , name : metrics [ 0 ] . name , type : metrics [ 0 ] . type , values : [ ] , aggregator : metrics [ 0 ] . aggregator } ; // Gather metrics by metricName and labels. const byLabels = new Grouper ( ) ; metrics . forEach ( metric => { metric . values . forEach ( value => { const key = hashObject ( value . labels ) ; byLabels . add ( ` ${ value . metricName } ${ key } ` , value ) ; } ) ; } ) ; // Apply aggregator function to gathered metrics. byLabels . forEach ( values => { if ( values . length === 0 ) return ; const valObj = { value : aggregatorFn ( values ) , labels : values [ 0 ] . labels } ; if ( values [ 0 ] . metricName ) { valObj . metricName = values [ 0 ] . metricName ; } // NB: Timestamps are omitted. result . values . push ( valObj ) ; } ) ; return result ; } ; }
Returns a new function that applies the `aggregatorFn` to the values.
@param {Function} aggregatorFn function to apply to values.
@return {Function} aggregator function
Returns a new function that applies the aggregatorFn to the values .