2016-04-28 20:50:03 -04:00
|
|
|
import {bind, provide, Binding, Provider, Injector, OpaqueToken} from '@angular/core/src/di';
|
|
|
|
import {StringMapWrapper} from '@angular/facade';
|
|
|
|
import {PromiseWrapper} from '@angular/facade';
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
import {Metric} from '../metric';
|
|
|
|
|
|
|
|
export class MultiMetric extends Metric {
|
2015-10-12 14:30:34 -04:00
|
|
|
static createBindings(childTokens: any[]): Provider[] {
|
2015-05-27 17:57:54 -04:00
|
|
|
return [
|
2016-04-12 12:40:37 -04:00
|
|
|
bind(_CHILDREN)
|
|
|
|
.toFactory((injector: Injector) => childTokens.map(token => injector.get(token)),
|
|
|
|
[Injector]),
|
2015-06-26 18:59:18 -04:00
|
|
|
bind(MultiMetric).toFactory(children => new MultiMetric(children), [_CHILDREN])
|
2015-05-27 17:57:54 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-08-28 14:29:19 -04:00
|
|
|
constructor(private _metrics: Metric[]) { super(); }
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts measuring
|
|
|
|
*/
|
|
|
|
beginMeasure(): Promise<any> {
|
2015-10-06 21:00:42 -04:00
|
|
|
return PromiseWrapper.all(this._metrics.map(metric => metric.beginMeasure()));
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends measuring and reports the data
|
|
|
|
* since the begin call.
|
|
|
|
* @param restart: Whether to restart right after this.
|
|
|
|
*/
|
2015-10-02 19:47:54 -04:00
|
|
|
endMeasure(restart: boolean): Promise<{[key: string]: any}> {
|
2015-10-06 21:00:42 -04:00
|
|
|
return PromiseWrapper.all(this._metrics.map(metric => metric.endMeasure(restart)))
|
|
|
|
.then(values => mergeStringMaps(values));
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Describes the metrics provided by this metric implementation.
|
|
|
|
* (e.g. units, ...)
|
|
|
|
*/
|
2015-10-02 19:47:54 -04:00
|
|
|
describe(): {[key: string]: any} {
|
2015-05-27 17:57:54 -04:00
|
|
|
return mergeStringMaps(this._metrics.map((metric) => metric.describe()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
function mergeStringMaps(maps: { [key: string]: string }[]): Object {
|
2015-05-27 17:57:54 -04:00
|
|
|
var result = {};
|
2015-10-07 12:09:43 -04:00
|
|
|
maps.forEach(
|
|
|
|
map => { StringMapWrapper.forEach(map, (value, prop) => { result[prop] = value; }); });
|
2015-05-27 17:57:54 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-28 14:29:19 -04:00
|
|
|
var _CHILDREN = new OpaqueToken('MultiMetric.children');
|