|
| 1 | +import { DateWrapper, isPresent, isBlank, Json } from 'angular2/src/facade/lang'; |
| 2 | +import { List } from 'angular2/src/facade/collection'; |
| 3 | +import { Promise, PromiseWrapper } from 'angular2/src/facade/async'; |
| 4 | + |
| 5 | +import { bind, OpaqueToken } from 'angular2/di'; |
| 6 | + |
| 7 | +import { Reporter } from '../reporter'; |
| 8 | +import { SampleDescription } from '../sample_description'; |
| 9 | +import { MeasureValues } from '../measure_values'; |
| 10 | + |
| 11 | +/** |
| 12 | + * A reporter that writes results into a json file. |
| 13 | + * TODO(tbosch): right now we bind the `writeFile` method |
| 14 | + * in benchpres/benchpress.es6. This does not work for Dart, |
| 15 | + * find another way... |
| 16 | + */ |
| 17 | +export class JsonFileReporter extends Reporter { |
| 18 | + // TODO(tbosch): use static values when our transpiler supports them |
| 19 | + static get WRITE_FILE() { return _WRITE_FILE; } |
| 20 | + // TODO(tbosch): use static values when our transpiler supports them |
| 21 | + static get PATH() { return _PATH; } |
| 22 | + static get BINDINGS() { return _BINDINGS; } |
| 23 | + |
| 24 | + _writeFile:Function; |
| 25 | + _path:string; |
| 26 | + _description:SampleDescription; |
| 27 | + |
| 28 | + constructor(sampleDescription, path, writeFile) { |
| 29 | + super(); |
| 30 | + this._description = sampleDescription; |
| 31 | + this._path = path; |
| 32 | + this._writeFile = writeFile; |
| 33 | + } |
| 34 | + |
| 35 | + reportMeasureValues(measureValues:MeasureValues):Promise { |
| 36 | + return PromiseWrapper.resolve(null); |
| 37 | + } |
| 38 | + |
| 39 | + reportSample(completeSample:List<MeasureValues>, validSample:List<MeasureValues>):Promise { |
| 40 | + var content = Json.stringify({ |
| 41 | + 'description': this._description, |
| 42 | + 'completeSample': completeSample, |
| 43 | + 'validSample': validSample |
| 44 | + }); |
| 45 | + var filePath = `${this._path}/${this._description.id}_${DateWrapper.toMillis(DateWrapper.now())}.json`; |
| 46 | + return this._writeFile(filePath, content); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +var _WRITE_FILE = new OpaqueToken('JsonFileReporter.writeFile'); |
| 51 | +var _PATH = new OpaqueToken('JsonFileReporter.path'); |
| 52 | +var _BINDINGS = [ |
| 53 | + bind(JsonFileReporter).toFactory( |
| 54 | + (sampleDescription, path, writeFile) => new JsonFileReporter(sampleDescription, path, writeFile), |
| 55 | + [SampleDescription, _PATH, _WRITE_FILE] |
| 56 | + ), |
| 57 | + bind(_PATH).toValue('.') |
| 58 | +]; |
0 commit comments