2016-08-03 18:00:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
import {Injector, OpaqueToken} from '@angular/core';
|
|
|
|
import {StringMapWrapper} from '../facade/collection';
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
import {Metric} from '../metric';
|
|
|
|
|
|
|
|
export class MultiMetric extends Metric {
|
2016-08-26 19:34:08 -04:00
|
|
|
static provideWith(childTokens: any[]): any[] {
|
2015-05-27 17:57:54 -04:00
|
|
|
return [
|
2016-06-02 20:30:40 -04:00
|
|
|
{
|
|
|
|
provide: _CHILDREN,
|
2016-08-03 18:00:07 -04:00
|
|
|
useFactory: (injector: Injector) => childTokens.map(token => injector.get(token)),
|
2016-06-02 20:30:40 -04:00
|
|
|
deps: [Injector]
|
|
|
|
},
|
2016-08-26 19:34:08 -04:00
|
|
|
{
|
|
|
|
provide: MultiMetric,
|
|
|
|
useFactory: (children: Metric[]) => new MultiMetric(children),
|
|
|
|
deps: [_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> {
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.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}> {
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.all(this._metrics.map(metric => metric.endMeasure(restart)))
|
|
|
|
.then(values => mergeStringMaps(<any>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-08-02 18:53:34 -04:00
|
|
|
function mergeStringMaps(maps: {[key: string]: string}[]): {[key: string]: string} {
|
2016-08-26 19:34:08 -04:00
|
|
|
var result: {[key: string]: string} = {};
|
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');
|