Skip to content
This repository was archived by the owner on Mar 4, 2022. It is now read-only.

Commit fd58bc1

Browse files
committed
extract time functions
1 parent bb3b5e4 commit fd58bc1

File tree

2 files changed

+180
-181
lines changed

2 files changed

+180
-181
lines changed

lib/providers/metrics-provider.js

Lines changed: 7 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
var EventEmitter = require("events").EventEmitter;
88
var _ = require("lodash");
99
var constants = require("../constants");
10+
var time = require("../time");
1011

1112
// get the defined aggregation levels
12-
var TIME_SCALES = constants.TIME_SCALES;
1313
var AGGREGATE_TIME_LEVELS = constants.AGGREGATE_TIME_LEVELS;
1414

1515
// what a valid time offset looks like
@@ -79,177 +79,6 @@ var MetricsProvider =
7979
// MetricsProvider inherits from EventEmitter
8080
MetricsProvider.prototype = Object.create(EventEmitter.prototype);
8181

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

765591
return xAxis;
@@ -793,7 +619,7 @@ MetricsProvider.prototype.validateTimeLabel =
793619
}
794620

795621
// must be able to convert (this can throw too)
796-
timeValue = convertTimeLabelToMilliseconds(label);
622+
timeValue = time.convertTimeLabelToMilliseconds(label);
797623

798624
// must be a number in range
799625
if (isNaN(timeValue) || !_.inRange(timeValue, 0, timeRange.maxTime.value + 1)) {

0 commit comments

Comments
 (0)