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

Commit 59d41f4

Browse files
committed
Merge branch 'master' into issue_80
2 parents bec91df + caf09c3 commit 59d41f4

File tree

7 files changed

+232
-192
lines changed

7 files changed

+232
-192
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ To launch: `nodejs-dashboard -- node -r nodejs-dashboard index.js`
4949

5050
#### Environment variables
5151

52-
`nodejs-dashboard` uses several environment variables to modify its behavior. This include some required values to prevent mangled output.
52+
`nodejs-dashboard` uses several environment variables to modify its behavior. These include some required values to prevent mangled output.
5353

5454
Variable | Required | Source | Description |
5555
--- | --- | --- | --- |

bin/nodejs-dashboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var parseSettings = require("../lib/parse-settings");
1515
var appName = appPkg.name || "node";
1616
var program = new commander.Command(pkg.name);
1717

18-
// Mimic commander sintax errors (with offsets) for consistency
18+
// Mimic commander syntax errors (with offsets) for consistency
1919
/* eslint-disable no-console */
2020
var exitWithError = function () {
2121
var args = Array.prototype.slice.call(arguments);

lib/dashboard.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var generateLayouts = require("./generate-layouts");
1313
var LogProvider = require("./providers/log-provider");
1414
var MetricsProvider = require("./providers/metrics-provider");
1515
var BaseView = require("./views/base-view");
16-
var GotoTimeView = require("./views/goto-time-view.js");
16+
var GotoTimeView = require("./views/goto-time-view");
1717

1818
var THROTTLE_TIMEOUT = 150;
1919

@@ -72,21 +72,21 @@ Dashboard.prototype._configureKeys = function () {
7272
this._showLayout(target);
7373
}.bind(this), THROTTLE_TIMEOUT));
7474

75-
var helpNode = this.helpView.node;
7675
this.container.key(["?", "h", "S-h"], function () {
77-
helpNode.toggle();
76+
this.gotoTimeView.hide();
77+
this.helpView.toggle();
7878
this.screen.render();
7979
}.bind(this));
8080

8181
this.container.key(["g", "S-g"], function () {
82-
helpNode.hide();
82+
this.helpView.hide();
8383
this.gotoTimeView.toggle();
8484
this.screen.render();
8585
}.bind(this));
8686

8787
this.container.key("escape", function () {
88-
if (helpNode.visible || this.gotoTimeView.isVisible()) {
89-
helpNode.hide();
88+
if (this.helpView.isVisible() || this.gotoTimeView.isVisible()) {
89+
this.helpView.hide();
9090
this.gotoTimeView.hide();
9191
this.screen.render();
9292
} else {

lib/providers/metrics-provider.js

Lines changed: 9 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
var EventEmitter = require("events").EventEmitter;
88
var _ = require("lodash");
9+
var constants = require("../constants");
10+
var time = require("../time");
911

1012
// 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;
1314

1415
// what a valid time offset looks like
1516
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 =
7879
// MetricsProvider inherits from EventEmitter
7980
MetricsProvider.prototype = Object.create(EventEmitter.prototype);
8081

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-
25282
/**
25383
* Given a moment in time, the start time, and time units, produce the
25484
* correct time index.
@@ -304,18 +134,15 @@ MetricsProvider.prototype.setZoomLevel = function setZoomLevel(zoom) {
304134
* An object containing the time range is returned
305135
*/
306136
MetricsProvider.prototype.getAvailableTimeRange = function getAvailableTimeRange() {
137+
var maxAverages = this._aggregation[this.lowestAggregateTimeUnits].data.length - 1;
307138
return {
308139
minTime: {
309-
label: getTimeIndexLabel(0, this.lowestAggregateTimeUnits),
140+
label: time.getLabel(0),
310141
value: 0
311142
},
312143
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
319146
}
320147
};
321148
};
@@ -758,7 +585,7 @@ MetricsProvider.prototype.getXAxis =
758585
timeIndex >= -scrollOffset;
759586
timeIndex--
760587
) {
761-
xAxis.push(getTimeIndexLabel(timeIndex, +this.zoomLevelKey));
588+
xAxis.push(time.getLabel(timeIndex * +this.zoomLevelKey));
762589
}
763590

764591
return xAxis;
@@ -792,7 +619,7 @@ MetricsProvider.prototype.validateTimeLabel =
792619
}
793620

794621
// must be able to convert (this can throw too)
795-
timeValue = convertTimeLabelToMilliseconds(label);
622+
timeValue = time.convertTimeLabelToMilliseconds(label);
796623

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

0 commit comments

Comments
 (0)