|
6 | 6 |
|
7 | 7 | var EventEmitter = require("events").EventEmitter; |
8 | 8 | var _ = require("lodash"); |
| 9 | +var constants = require("../constants"); |
| 10 | +var time = require("../time"); |
9 | 11 |
|
10 | 12 | // get the defined aggregation levels |
11 | | -var AGGREGATE_TIME_LEVELS = require("../constants.js").AGGREGATE_TIME_LEVELS; |
12 | | -var TIME_SCALES = require("../constants.js").TIME_SCALES; |
| 13 | +var AGGREGATE_TIME_LEVELS = constants.AGGREGATE_TIME_LEVELS; |
13 | 14 |
|
14 | 15 | // what a valid time offset looks like |
15 | 16 | var TIME_LABEL_PATTERN = /^(\d+y)?\s*(\d{1,3}d)?\s*(\d{1,2})?(:\d{1,2})?(:\d{2})?$/i; |
@@ -78,177 +79,6 @@ var MetricsProvider = |
78 | 79 | // MetricsProvider inherits from EventEmitter |
79 | 80 | MetricsProvider.prototype = Object.create(EventEmitter.prototype); |
80 | 81 |
|
81 | | -/** |
82 | | - * Given a time index and unit of time measure, compute a condensed, human-readable label. |
83 | | - * |
84 | | - * @param {Number} timeIndex |
85 | | - * The logical index of time. |
86 | | - * |
87 | | - * @param {Number} aggregateTimeUnits |
88 | | - * The unit of time measure. |
89 | | - * |
90 | | - * @returns {String} |
91 | | - * A scaled, string-representation of time at the index is returned. |
92 | | - */ |
93 | | -var getTimeIndexLabel = |
94 | | - function getTimeIndexLabel(timeIndex, aggregateTimeUnits) { |
95 | | - var DIGITS_PER_UNIT = 2; |
96 | | - |
97 | | - var timeValue = timeIndex * aggregateTimeUnits; |
98 | | - var timeElements = []; |
99 | | - |
100 | | - if (timeValue === 0) { |
101 | | - return ":00"; |
102 | | - } |
103 | | - |
104 | | - _.every(TIME_SCALES, function (timeScale, index, timeScales) { |
105 | | - var timeElement = { |
106 | | - units: timeScale.units, |
107 | | - value: 0 |
108 | | - }; |
109 | | - |
110 | | - // stop reducing when it cannot be divided |
111 | | - if (timeValue < timeScale.divisor) { |
112 | | - return false; |
113 | | - } |
114 | | - |
115 | | - // don't capture a time element for milliseconds |
116 | | - if (timeScale.units !== "ms") { |
117 | | - // reduce by the divisor |
118 | | - timeElement.value = timeValue / timeScale.divisor; |
119 | | - |
120 | | - // if there are more elements after, take the modulo to get the remainder |
121 | | - if (index < timeScales.length - 1) { |
122 | | - timeElement.value = Math.floor(timeElement.value % timeScales[index + 1].divisor); |
123 | | - } else { |
124 | | - timeElement.value = Math.floor(timeElement.value); |
125 | | - } |
126 | | - |
127 | | - timeElements.push(timeElement); |
128 | | - } |
129 | | - |
130 | | - // reduce |
131 | | - timeValue /= timeScale.divisor; |
132 | | - |
133 | | - return true; |
134 | | - }); |
135 | | - |
136 | | - return _.reduce(timeElements, function (prev, curr, index) { |
137 | | - switch (curr.units) { |
138 | | - case "s": |
139 | | - return ":" + _.padStart(curr.value, DIGITS_PER_UNIT, "0"); |
140 | | - case "m": |
141 | | - case "h": |
142 | | - if (index < timeElements.length - 1) { |
143 | | - return (curr.units === "m" ? ":" : " ") |
144 | | - + _.padStart(curr.value, DIGITS_PER_UNIT, "0") |
145 | | - + prev; |
146 | | - } else { |
147 | | - return curr.value + prev; |
148 | | - } |
149 | | - default: |
150 | | - return curr.value + curr.units + prev; |
151 | | - } |
152 | | - }, ""); |
153 | | - }; |
154 | | - |
155 | | -/** |
156 | | - * Given a time label value (ex: 2y 5d 1:22:33), produce the actual |
157 | | - * time value in ms. |
158 | | - * |
159 | | - * @param {String} label |
160 | | - * The time label to convert. |
161 | | - * |
162 | | - * @throws {Error} |
163 | | - * An error is thrown if the time label cannot be converted to ms. |
164 | | - * |
165 | | - * @returns {Number} |
166 | | - * The time value in ms is returned. |
167 | | - */ |
168 | | -var convertTimeLabelToMilliseconds = function (label) { |
169 | | - /* eslint-disable no-magic-numbers */ |
170 | | - |
171 | | - // a container for all time elements |
172 | | - var timeElements = { |
173 | | - y: 0, |
174 | | - d: 0, |
175 | | - t: [], |
176 | | - h: 0, |
177 | | - m: 0, |
178 | | - s: 0 |
179 | | - }; |
180 | | - |
181 | | - // the initial divisor |
182 | | - var divisor = TIME_SCALES[0].divisor; |
183 | | - |
184 | | - // break up the input |
185 | | - var split = TIME_LABEL_PATTERN.exec(label); |
186 | | - |
187 | | - // take the broken apart pieces and consume them |
188 | | - _.each(_.slice(split, 1), function (value, index) { |
189 | | - var pieces; |
190 | | - |
191 | | - // skip undefined values |
192 | | - if (value === undefined) { |
193 | | - return; |
194 | | - } |
195 | | - |
196 | | - // get the numeric and unit components, if any |
197 | | - pieces = /^:?(\d*)([yd])?/.exec(value); |
198 | | - |
199 | | - switch (index) { |
200 | | - case 0: |
201 | | - case 1: |
202 | | - // year and day are just keys |
203 | | - timeElements[pieces[2]] = +pieces[1]; |
204 | | - break; |
205 | | - case 2: |
206 | | - case 3: |
207 | | - case 4: |
208 | | - // time is only slightly trickier; missing elements get pushed down |
209 | | - timeElements.t.push(+pieces[1]); |
210 | | - break; |
211 | | - } |
212 | | - }); |
213 | | - |
214 | | - while (timeElements.t.length < 3) { |
215 | | - // complete the time picture with leading zeros |
216 | | - timeElements.t.unshift(0); |
217 | | - } |
218 | | - |
219 | | - // convert time parts to keys |
220 | | - timeElements.h = timeElements.t[0]; |
221 | | - timeElements.m = timeElements.t[1]; |
222 | | - timeElements.s = timeElements.t[2]; |
223 | | - |
224 | | - // now we can discard the time array |
225 | | - delete timeElements.t; |
226 | | - |
227 | | - // now, reduce the time elements by the scaling factors |
228 | | - return _.reduce(TIME_SCALES, function (prev, timeScale, index) { |
229 | | - // the divisor grows with each time scale factor |
230 | | - divisor *= timeScale.divisor; |
231 | | - |
232 | | - // if the time element is represented, multiply by current divisor |
233 | | - if (timeElements[timeScale.units]) { |
234 | | - // if there are more time scales to go, make sure the current value |
235 | | - // does not exceed its limits (ex: 90s should be 1:30 instead) |
236 | | - if (index < TIME_SCALES.length - 1) { |
237 | | - if (timeElements[timeScale.units] >= TIME_SCALES[index + 1].divisor) { |
238 | | - throw new Error("Enter a valid time value"); |
239 | | - } |
240 | | - } |
241 | | - |
242 | | - // continue to accumulate the time |
243 | | - prev += timeElements[timeScale.units] * divisor; |
244 | | - } |
245 | | - |
246 | | - return prev; |
247 | | - }, 0); |
248 | | - |
249 | | - /* eslint-enable no-magic-numbers */ |
250 | | -}; |
251 | | - |
252 | 82 | /** |
253 | 83 | * Given a moment in time, the start time, and time units, produce the |
254 | 84 | * correct time index. |
@@ -304,18 +134,15 @@ MetricsProvider.prototype.setZoomLevel = function setZoomLevel(zoom) { |
304 | 134 | * An object containing the time range is returned |
305 | 135 | */ |
306 | 136 | MetricsProvider.prototype.getAvailableTimeRange = function getAvailableTimeRange() { |
| 137 | + var maxAverages = this._aggregation[this.lowestAggregateTimeUnits].data.length - 1; |
307 | 138 | return { |
308 | 139 | minTime: { |
309 | | - label: getTimeIndexLabel(0, this.lowestAggregateTimeUnits), |
| 140 | + label: time.getLabel(0), |
310 | 141 | value: 0 |
311 | 142 | }, |
312 | 143 | maxTime: { |
313 | | - label: getTimeIndexLabel( |
314 | | - this._aggregation[this.lowestAggregateTimeUnits].data.length - 1, |
315 | | - this.lowestAggregateTimeUnits |
316 | | - ), |
317 | | - value: (this._aggregation[this.lowestAggregateTimeUnits].data.length - 1) |
318 | | - * this.lowestAggregateTimeUnits |
| 144 | + label: time.getLabel(maxAverages * this.lowestAggregateTimeUnits), |
| 145 | + value: maxAverages * this.lowestAggregateTimeUnits |
319 | 146 | } |
320 | 147 | }; |
321 | 148 | }; |
@@ -758,7 +585,7 @@ MetricsProvider.prototype.getXAxis = |
758 | 585 | timeIndex >= -scrollOffset; |
759 | 586 | timeIndex-- |
760 | 587 | ) { |
761 | | - xAxis.push(getTimeIndexLabel(timeIndex, +this.zoomLevelKey)); |
| 588 | + xAxis.push(time.getLabel(timeIndex * +this.zoomLevelKey)); |
762 | 589 | } |
763 | 590 |
|
764 | 591 | return xAxis; |
@@ -792,7 +619,7 @@ MetricsProvider.prototype.validateTimeLabel = |
792 | 619 | } |
793 | 620 |
|
794 | 621 | // must be able to convert (this can throw too) |
795 | | - timeValue = convertTimeLabelToMilliseconds(label); |
| 622 | + timeValue = time.convertTimeLabelToMilliseconds(label); |
796 | 623 |
|
797 | 624 | // must be a number in range |
798 | 625 | if (isNaN(timeValue) || !_.inRange(timeValue, 0, timeRange.maxTime.value + 1)) { |
|
0 commit comments