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

Commit c0a2d23

Browse files
committed
Merge branch 'master' into single_config
2 parents eeb88c9 + fd3b84b commit c0a2d23

File tree

8 files changed

+264
-214
lines changed

8 files changed

+264
-214
lines changed

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: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
var _ = require("lodash");
44
var blessed = require("blessed");
55

6-
var StreamView = require("./views/stream-view");
7-
var EventLoopView = require("./views/eventloop-view");
8-
var MemoryGaugeView = require("./views/memory-gauge-view");
9-
var MemoryGraphView = require("./views/memory-graph-view");
10-
var CpuView = require("./views/cpu-view");
116
var HelpView = require("./views/help");
127
var generateLayouts = require("./generate-layouts");
138
var LogProvider = require("./providers/log-provider");
149
var MetricsProvider = require("./providers/metrics-provider");
15-
var BaseView = require("./views/base-view");
16-
var GotoTimeView = require("./views/goto-time-view.js");
10+
var GotoTimeView = require("./views/goto-time-view");
11+
var views = require("./views");
1712

1813
var THROTTLE_TIMEOUT = 150;
1914

@@ -71,21 +66,21 @@ Dashboard.prototype._configureKeys = function () {
7166
this._showLayout(target);
7267
}.bind(this), THROTTLE_TIMEOUT));
7368

74-
var helpNode = this.helpView.node;
7569
this.container.key(["?", "h", "S-h"], function () {
76-
helpNode.toggle();
70+
this.gotoTimeView.hide();
71+
this.helpView.toggle();
7772
this.screen.render();
7873
}.bind(this));
7974

8075
this.container.key(["g", "S-g"], function () {
81-
helpNode.hide();
76+
this.helpView.hide();
8277
this.gotoTimeView.toggle();
8378
this.screen.render();
8479
}.bind(this));
8580

8681
this.container.key("escape", function () {
87-
if (helpNode.visible || this.gotoTimeView.isVisible()) {
88-
helpNode.hide();
82+
if (this.helpView.isVisible() || this.gotoTimeView.isVisible()) {
83+
this.helpView.hide();
8984
this.gotoTimeView.hide();
9085
this.screen.render();
9186
} else {
@@ -126,14 +121,6 @@ Dashboard.prototype.onEvent = function (event) {
126121
}
127122
};
128123

129-
var VIEW_MAP = {
130-
log: StreamView,
131-
cpu: CpuView,
132-
memory: MemoryGaugeView,
133-
memoryGraph: MemoryGraphView,
134-
eventLoop: EventLoopView
135-
};
136-
137124
Dashboard.prototype._showLayout = function (id) {
138125
if (this.currentLayout === id) {
139126
return;
@@ -145,14 +132,7 @@ Dashboard.prototype._showLayout = function (id) {
145132
this.views = [];
146133

147134
_.each(this.layouts[id], function (layoutConfig) {
148-
var View;
149-
150-
if (VIEW_MAP[layoutConfig.view.type]) {
151-
View = VIEW_MAP[layoutConfig.view.type];
152-
} else if (layoutConfig.view.module) {
153-
// eslint-disable-next-line global-require
154-
View = require(layoutConfig.view.module)(BaseView);
155-
}
135+
var View = views.getConstructor(layoutConfig.view);
156136

157137
if (View) {
158138
var view = new View({

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)