1 | import {ListWrapper} from 'angular2/src/facade/collection';
|
2 | import {bind, provide, Provider, OpaqueToken} from 'angular2/src/core/di';
|
3 |
|
4 | import {Validator} from '../validator';
|
5 | import {Statistic} from '../statistic';
|
6 | import {MeasureValues} from '../measure_values';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export class RegressionSlopeValidator extends Validator {
|
13 |
|
14 | static get SAMPLE_SIZE(): OpaqueToken { return _SAMPLE_SIZE; }
|
15 |
|
16 | static get METRIC(): OpaqueToken { return _METRIC; }
|
17 |
|
18 | static get BINDINGS(): Provider[] { return _PROVIDERS; }
|
19 |
|
20 | _sampleSize: number;
|
21 | _metric: string;
|
22 |
|
23 | constructor(sampleSize, metric) {
|
24 | super();
|
25 | this._sampleSize = sampleSize;
|
26 | this._metric = metric;
|
27 | }
|
28 |
|
29 | describe(): {[key: string]: any} {
|
30 | return {'sampleSize': this._sampleSize, 'regressionSlopeMetric': this._metric};
|
31 | }
|
32 |
|
33 | validate(completeSample: MeasureValues[]): MeasureValues[] {
|
34 | if (completeSample.length >= this._sampleSize) {
|
35 | var latestSample = ListWrapper.slice(completeSample, completeSample.length - this._sampleSize,
|
36 | completeSample.length);
|
37 | var xValues = [];
|
38 | var yValues = [];
|
39 | for (var i = 0; i < latestSample.length; i++) {
|
40 |
|
41 |
|
42 | xValues.push(i);
|
43 | yValues.push(latestSample[i].values[this._metric]);
|
44 | }
|
45 | var regressionSlope = Statistic.calculateRegressionSlope(
|
46 | xValues, Statistic.calculateMean(xValues), yValues, Statistic.calculateMean(yValues));
|
47 | return regressionSlope >= 0 ? latestSample : null;
|
48 | } else {
|
49 | return null;
|
50 | }
|
51 | }
|
52 | }
|
53 |
|
54 | var _SAMPLE_SIZE = new OpaqueToken('RegressionSlopeValidator.sampleSize');
|
55 | var _METRIC = new OpaqueToken('RegressionSlopeValidator.metric');
|
56 | var _PROVIDERS = [
|
57 | bind(RegressionSlopeValidator)
|
58 | .toFactory((sampleSize, metric) => new RegressionSlopeValidator(sampleSize, metric),
|
59 | [_SAMPLE_SIZE, _METRIC]),
|
60 | provide(_SAMPLE_SIZE, {useValue: 10}),
|
61 | provide(_METRIC, {useValue: 'scriptTime'})
|
62 | ];
|