5104aa51f1cebc228146b631cf9fa604464efd62
[WebKit-https.git] / Websites / perf.webkit.org / public / v3 / models / analysis-results.js
1
2 class AnalysisResults {
3     constructor()
4     {
5         this._metricToBuildMap = {};
6         this._metricIds = [];
7         this._lazilyComputedHighestTests = new LazilyEvaluatedFunction(this._computeHighestTests);
8     }
9
10     findResult(buildId, metricId)
11     {
12         const map = this._metricToBuildMap[metricId];
13         if (!map)
14             return null;
15         return map[buildId];
16     }
17
18     highestTests() { return this._lazilyComputedHighestTests.evaluate(this._metricIds); }
19
20     containsTest(test)
21     {
22         console.assert(test instanceof Test);
23         for (const metric of test.metrics()) {
24             if (metric.id() in this._metricToBuildMap)
25                 return true;
26         }
27         return false;
28     }
29
30     _computeHighestTests(metricIds)
31     {
32         const testsInResults = new Set(metricIds.map((metricId) => Metric.findById(metricId).test()));
33         return [...testsInResults].filter((test) => !testsInResults.has(test.parentTest()));
34     }
35
36     add(measurement)
37     {
38         console.assert(measurement.configType == 'current');
39         const metricId = measurement.metricId;
40         if (!(metricId in this._metricToBuildMap)) {
41             this._metricToBuildMap[metricId] = {};
42             this._metricIds = Object.keys(this._metricToBuildMap);
43         }
44         const map = this._metricToBuildMap[metricId];
45         console.assert(!map[measurement.buildId]);
46         map[measurement.buildId] = measurement;
47     }
48
49     commitSetForRequest(buildRequest)
50     {
51         if (!this._metricIds.length)
52             return null;
53         const result = this.findResult(buildRequest.buildId(), this._metricIds[0]);
54         if (!result)
55             return null;
56         return result.commitSet();
57     }
58
59     viewForMetric(metric)
60     {
61         console.assert(metric instanceof Metric);
62         return new AnalysisResultsView(this, metric);
63     }
64
65     static fetch(taskId)
66     {
67         taskId = parseInt(taskId);
68         return RemoteAPI.getJSONWithStatus(`../api/measurement-set?analysisTask=${taskId}`).then(function (response) {
69
70             Instrumentation.startMeasuringTime('AnalysisResults', 'fetch');
71
72             const adaptor = new MeasurementAdaptor(response['formatMap']);
73             const results = new AnalysisResults;
74             for (const rawMeasurement of response['measurements'])
75                 results.add(adaptor.applyToAnalysisResults(rawMeasurement));
76
77             Instrumentation.endMeasuringTime('AnalysisResults', 'fetch');
78
79             return results;
80         });
81     }
82 }
83
84 class AnalysisResultsView {
85     constructor(analysisResults, metric)
86     {
87         console.assert(analysisResults instanceof AnalysisResults);
88         console.assert(metric instanceof Metric);
89         this._results = analysisResults;
90         this._metric = metric;
91     }
92
93     metric() { return this._metric; }
94
95     resultForRequest(buildRequest)
96     {
97         return this._results.findResult(buildRequest.buildId(), this._metric.id());
98     }
99 }