UNPKG

1.94 kBPlain TextView Raw
1import {DateWrapper, isPresent, isBlank, Json} from 'angular2/src/facade/lang';
2import {PromiseWrapper} from 'angular2/src/facade/async';
3
4import {bind, provide, Provider, OpaqueToken} from 'angular2/src/core/di';
5
6import {Reporter} from '../reporter';
7import {SampleDescription} from '../sample_description';
8import {MeasureValues} from '../measure_values';
9import {Options} from '../common_options';
10
11/**
12 * A reporter that writes results into a json file.
13 */
14export class JsonFileReporter extends Reporter {
15 // TODO(tbosch): use static values when our transpiler supports them
16 static get PATH(): OpaqueToken { return _PATH; }
17 // TODO(tbosch): use static values when our transpiler supports them
18 static get BINDINGS(): Provider[] { return _PROVIDERS; }
19
20 _writeFile: Function;
21 _path: string;
22 _description: SampleDescription;
23 _now: Function;
24
25 constructor(sampleDescription, path, writeFile, now) {
26 super();
27 this._description = sampleDescription;
28 this._path = path;
29 this._writeFile = writeFile;
30 this._now = now;
31 }
32
33 reportMeasureValues(measureValues: MeasureValues): Promise<any> {
34 return PromiseWrapper.resolve(null);
35 }
36
37 reportSample(completeSample: MeasureValues[], validSample: MeasureValues[]): Promise<any> {
38 var content = Json.stringify({
39 'description': this._description,
40 'completeSample': completeSample,
41 'validSample': validSample
42 });
43 var filePath =
44 `${this._path}/${this._description.id}_${DateWrapper.toMillis(this._now())}.json`;
45 return this._writeFile(filePath, content);
46 }
47}
48
49var _PATH = new OpaqueToken('JsonFileReporter.path');
50var _PROVIDERS = [
51 bind(JsonFileReporter)
52 .toFactory((sampleDescription, path, writeFile, now) =>
53 new JsonFileReporter(sampleDescription, path, writeFile, now),
54 [SampleDescription, _PATH, Options.WRITE_FILE, Options.NOW]),
55 provide(_PATH, {useValue: '.'})
56];