OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <!-- | |
3 Copyright 2016 The Chromium Authors. All rights reserved. | |
4 Use of this source code is governed by a BSD-style license that can be | |
5 found in the LICENSE file. | |
6 --> | |
7 | |
8 <link rel="import" href="/tracing/base/iteration_helpers.html"> | |
9 <link rel="import" href="/tracing/value/diagnostics/diagnostic.html"> | |
10 <link rel="import" href="/tracing/value/diagnostics/histogram_ref.html"> | |
11 | |
12 <script> | |
13 'use strict'; | |
14 | |
15 tr.exportTo('tr.v.d', function() { | |
16 class RelatedHistogramSet extends tr.v.d.Diagnostic { | |
17 constructor(opt_histograms) { | |
18 super(); | |
19 this.histogramsByGuid_ = new Map(); | |
20 | |
21 if (opt_histograms) { | |
22 for (const hist of opt_histograms) { | |
23 this.add(hist); | |
24 } | |
25 } | |
26 } | |
27 | |
28 canAddDiagnostic(otherDiagnostic) { | |
29 return otherDiagnostic instanceof RelatedHistogramSet; | |
30 } | |
31 | |
32 addDiagnostic(otherDiagnostic) { | |
33 // Related Histograms might not exist yet. | |
34 } | |
35 | |
36 mergeRelationships(otherDiagnostic, parentHist, otherParentHist) { | |
37 /* | |
38 Modify |this| to contain Histograms that should be related to | |
39 |parentHist|. | |
40 | |
41 |otherParentHist| was merged to |parentHist|. | |
42 |otherDiagnostic| contains Histograms that are related to | |
43 |otherParentHist|. | |
44 | |
45 Since mergeRelationships() is called after all Histograms are merged, | |
46 then the MERGED_TO diagnostics of the Histograms in |otherDiagnostic| | |
47 contain Histograms that should be related to |parentHist| via |this| | |
48 RelatedHistogramSet. | |
49 | |
50 otherParentHist -----------------------------> parentHist | |
51 | merged to RelatedHistogramSet | | |
52 | | | |
53 | otherDiagnostic | this | |
54 | RelatedHistogramSet | RelatedHistogramSet | |
55 v v | |
56 otherRelatedHist -----------------------------> relatedHist | |
57 merged to RelatedHistogramSet | |
58 | |
59 However, |otherRelatedHist| may have been merged to Histograms using | |
60 different grouping keys, so if |relatedHist|'s merge path is different | |
61 from |parentHist|'s merge path, then |relatedHist| should not be related | |
62 to |parentHist|. | |
63 */ | |
64 const parentGroupingPath = tr.v.d.GroupingPath.getFromHistogram( | |
65 parentHist); | |
66 for (const otherRelatedHist of otherDiagnostic) { | |
67 const mergedTo = otherRelatedHist.diagnostics.get( | |
68 tr.v.d.RESERVED_NAMES.MERGED_TO); | |
69 if (mergedTo === undefined) continue; | |
70 | |
71 for (const relatedHist of mergedTo) { | |
72 if (this.has(relatedHist)) continue; | |
73 | |
74 const relatedGroupingPath = tr.v.d.GroupingPath.getFromHistogram( | |
75 relatedHist); | |
76 if (relatedGroupingPath === undefined) continue; | |
77 if (!parentGroupingPath.equals(relatedGroupingPath)) continue; | |
78 | |
79 this.add(relatedHist); | |
80 } | |
81 } | |
82 } | |
83 | |
84 /** | |
85 * @param {!(tr.v.d.HistogramRef|tr.v.Histogram)} v | |
86 */ | |
87 add(hist) { | |
88 if (!(hist instanceof tr.v.Histogram) && | |
89 !(hist instanceof tr.v.d.HistogramRef)) { | |
90 throw new Error('Must be instanceof Histogram or HistogramRef: ' + | |
91 hist); | |
92 } | |
93 | |
94 if (this.histogramsByGuid_.has(hist.guid)) { | |
95 throw new Error('Tried to add same hist twice'); | |
96 } | |
97 | |
98 this.histogramsByGuid_.set(hist.guid, hist); | |
99 } | |
100 | |
101 has(hist) { | |
102 return this.histogramsByGuid_.has(hist.guid); | |
103 } | |
104 | |
105 get length() { | |
106 return this.histogramsByGuid_.size; | |
107 } | |
108 | |
109 * [Symbol.iterator]() { | |
110 for (const [guid, hist] of this.histogramsByGuid_) { | |
111 yield hist; | |
112 } | |
113 } | |
114 | |
115 /** | |
116 * Resolve all HistogramRefs into Histograms by looking up their guids in | |
117 * |histograms|. | |
118 * If a Histogram cannot be found and |opt_required| is true, then throw an | |
119 * Error. | |
120 * If a Histogram cannot be found and |opt_required| is false, then the | |
121 * HistogramRef will remain a HistogramRef. | |
122 * | |
123 * @param {!tr.v.HistogramSet} histograms | |
124 * @param {boolean=} opt_required | |
125 */ | |
126 resolve(histograms, opt_required) { | |
127 for (const [guid, value] of this.histogramsByGuid_) { | |
128 if (!(value instanceof tr.v.d.HistogramRef)) continue; | |
129 | |
130 const hist = histograms.lookupHistogram(guid); | |
131 if (hist instanceof tr.v.Histogram) { | |
132 this.histogramsByGuid_.set(guid, hist); | |
133 } else if (opt_required) { | |
134 throw new Error('Unable to find Histogram ' + guid); | |
135 } | |
136 } | |
137 } | |
138 | |
139 asDictInto_(d) { | |
140 d.guids = []; | |
141 for (const hist of this) { | |
142 d.guids.push(hist.guid); | |
143 } | |
144 } | |
145 | |
146 static fromDict(d) { | |
147 return new RelatedHistogramSet(d.guids.map( | |
148 guid => new tr.v.d.HistogramRef(guid))); | |
149 } | |
150 } | |
151 | |
152 tr.v.d.Diagnostic.register(RelatedHistogramSet, { | |
153 elementName: 'tr-v-ui-related-histogram-set-span' | |
154 }); | |
155 | |
156 return { | |
157 RelatedHistogramSet, | |
158 }; | |
159 }); | |
160 </script> | |
OLD | NEW |