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

Commit b3c7bcf

Browse files
committed
Create views for platform information
Create CpuDetailsView to show model and speed of cpus Create EnvDetailsView to show environment variables Create NodeDetailsView to show running node version Create SystemDetailsView to show running platform details Create UserDetailsView to show user information
1 parent fd3b84b commit b3c7bcf

14 files changed

+648
-0
lines changed

lib/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var AGGREGATE_TIME_LEVELS = [
2323
"3600000"
2424
];
2525

26+
var MILLISECONDS_PER_SECOND = 1000;
27+
2628
// this array object is used to reduce ms to its highest human-readable form
2729
// see lib/providers/metrics-provider.js::getTimeIndexLabel
2830
var TIME_SCALES = [
@@ -49,5 +51,6 @@ var TIME_SCALES = [
4951

5052
module.exports = {
5153
AGGREGATE_TIME_LEVELS: AGGREGATE_TIME_LEVELS,
54+
MILLISECONDS_PER_SECOND: MILLISECONDS_PER_SECOND,
5255
TIME_SCALES: TIME_SCALES
5356
};

lib/layout-config-schema.json

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
"$ref": "#/definitions/lineGraphView"
2828
}, {
2929
"$ref": "#/definitions/customView"
30+
}, {
31+
"$ref": "#/definitions/cpuDetailsView"
32+
}, {
33+
"$ref": "#/definitions/envDetailsView"
34+
}, {
35+
"$ref": "#/definitions/nodeDetailsView"
36+
}, {
37+
"$ref": "#/definitions/systemDetailsView"
38+
}, {
39+
"$ref": "#/definitions/userDetailsView"
3040
}]
3141
}
3242
}
@@ -125,6 +135,96 @@
125135
},
126136
"required": ["module"]
127137
},
138+
"cpuDetailsView": {
139+
"type": "object",
140+
"properties": {
141+
"title": {
142+
"type": "string"
143+
},
144+
"borderColor": {
145+
"type": "string"
146+
},
147+
"type": {
148+
"enum": ["cpuDetails"]
149+
},
150+
"position": {
151+
"$ref": "#/definitions/position"
152+
}
153+
},
154+
"required": ["type"]
155+
},
156+
"envDetailsView": {
157+
"type": "object",
158+
"properties": {
159+
"title": {
160+
"type": "string"
161+
},
162+
"borderColor": {
163+
"type": "string"
164+
},
165+
"type": {
166+
"enum": ["envDetails"]
167+
},
168+
"position": {
169+
"$ref": "#/definitions/position"
170+
}
171+
},
172+
"required": ["type"]
173+
},
174+
"nodeDetailsView": {
175+
"type": "object",
176+
"properties": {
177+
"title": {
178+
"type": "string"
179+
},
180+
"borderColor": {
181+
"type": "string"
182+
},
183+
"type": {
184+
"enum": ["nodeDetails"]
185+
},
186+
"position": {
187+
"$ref": "#/definitions/position"
188+
}
189+
},
190+
"required": ["type"]
191+
},
192+
"systemDetailsView": {
193+
"type": "object",
194+
"properties": {
195+
"title": {
196+
"type": "string"
197+
},
198+
"borderColor": {
199+
"type": "string"
200+
},
201+
"type": {
202+
"enum": ["systemDetails"]
203+
},
204+
"position": {
205+
"$ref": "#/definitions/position"
206+
}
207+
},
208+
"required": ["type"]
209+
},
210+
"userDetailsView": {
211+
"type": "object",
212+
"properties": {
213+
"title": {
214+
"type": "string"
215+
},
216+
"borderColor": {
217+
"type": "string"
218+
},
219+
"type": {
220+
"enum": ["userDetails"]
221+
},
222+
"position": {
223+
"$ref": "#/definitions/position"
224+
}
225+
},
226+
"required": ["type"]
227+
},
128228
"position": {
129229
"oneOf": [{
130230
"type": "object",

lib/views/base-details-view.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
3+
var blessed = require("blessed");
4+
var _ = require("lodash");
5+
var BaseView = require("./base-view");
6+
7+
var BaseDetailsView = function BaseDetailsView(options) {
8+
BaseView.call(this, options);
9+
10+
this.screen = options.parent.screen;
11+
this.node = blessed.box(this.layoutConfig);
12+
this.parent.append(this.node);
13+
14+
this.refreshContent();
15+
this.recalculatePosition();
16+
};
17+
18+
BaseDetailsView.prototype = Object.create(BaseView.prototype);
19+
20+
BaseDetailsView.prototype.refreshContent = function () {
21+
this.node.setContent(this._getBoxContent(this.getDetails()));
22+
this.screen.render();
23+
};
24+
25+
BaseDetailsView.prototype.getDetails = function () {
26+
return [];
27+
};
28+
29+
/**
30+
* Given data and optional filters, return the content for a box.
31+
*
32+
* @param {Object[]} data
33+
* This is the array of label/data objects that define each data
34+
* point for the box.
35+
*
36+
* @returns {String}
37+
* The content string for the box is returned.
38+
*/
39+
BaseDetailsView.prototype._getBoxContent = function (data) {
40+
var longestLabel = _.reduce(data, function (prev, detail) {
41+
return Math.max(prev, detail.label.length);
42+
}, 0);
43+
44+
var getFormattedContent = function (prev, details) {
45+
prev += "{cyan-fg}{bold}" + details.label + "{/}"
46+
+ _.repeat(" ", longestLabel - details.label.length + 1)
47+
+ "{green-fg}" + details.data + "{/}\n";
48+
return prev;
49+
};
50+
51+
return _.trimEnd(_.reduce(data, getFormattedContent, ""), "\n");
52+
};
53+
54+
module.exports = BaseDetailsView;

lib/views/cpu-details-view.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"use strict";
2+
3+
var os = require("os");
4+
var _ = require("lodash");
5+
var BaseDetailsView = require("./base-details-view");
6+
7+
var CpuDetailsView = function CpuDetailsView(options) {
8+
BaseDetailsView.call(this, options);
9+
};
10+
11+
CpuDetailsView.prototype = Object.create(BaseDetailsView.prototype);
12+
13+
CpuDetailsView.prototype.getDetails = function () {
14+
var cpuInfo = os.cpus();
15+
16+
return _.map(cpuInfo, function (info, index) {
17+
return {
18+
label: "[" + index + "]",
19+
data: info.model + " " + info.speed
20+
};
21+
});
22+
};
23+
24+
CpuDetailsView.prototype.getDefaultLayoutConfig = function () {
25+
return {
26+
label: " CPU(s) ",
27+
border: "line",
28+
tags: true,
29+
style: {
30+
border: {
31+
fg: "white"
32+
}
33+
}
34+
};
35+
};
36+
37+
module.exports = CpuDetailsView;

lib/views/env-details-view.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
3+
var _ = require("lodash");
4+
var BaseDetailsView = require("./base-details-view");
5+
6+
var EnvDetailsView = function EnvDetailsView(options) {
7+
BaseDetailsView.call(this, options);
8+
};
9+
10+
EnvDetailsView.prototype = Object.create(BaseDetailsView.prototype);
11+
12+
EnvDetailsView.prototype.getDefaultLayoutConfig = function () {
13+
return {
14+
label: " Environment Variables ",
15+
border: "line",
16+
style: {
17+
border: {
18+
fg: "white"
19+
}
20+
},
21+
tags: true,
22+
scrollable: true,
23+
keys: true,
24+
input: true,
25+
scrollbar: {
26+
style: {
27+
fg: "white",
28+
inverse: true
29+
},
30+
track: {
31+
ch: ":",
32+
fg: "cyan"
33+
}
34+
}
35+
};
36+
};
37+
38+
EnvDetailsView.prototype.getDetails = function () {
39+
return _.map(process.env, function (value, key) {
40+
return {
41+
label: key,
42+
data: value
43+
};
44+
});
45+
};
46+
47+
module.exports = EnvDetailsView;

lib/views/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ var MemoryGaugeView = require("./memory-gauge-view");
66
var MemoryGraphView = require("./memory-graph-view");
77
var CpuView = require("./cpu-view");
88
var BaseView = require("./base-view");
9+
var CpuDetailsView = require("./cpu-details-view");
10+
var EnvDetailsView = require("./env-details-view");
11+
var NodeDetailsView = require("./node-details-view");
12+
var SystemDetailsView = require("./system-details-view");
13+
var UserDetailsView = require("./user-details-view");
914

1015
var VIEW_MAP = {
16+
cpuDetails: CpuDetailsView,
17+
envDetails: EnvDetailsView,
18+
nodeDetails: NodeDetailsView,
19+
systemDetails: SystemDetailsView,
20+
userDetails: UserDetailsView,
1121
log: StreamView,
1222
cpu: CpuView,
1323
memory: MemoryGaugeView,
@@ -16,6 +26,7 @@ var VIEW_MAP = {
1626
};
1727

1828
var getConstructor = function (options) {
29+
options = options || {};
1930
if (VIEW_MAP[options.type]) {
2031
return VIEW_MAP[options.type];
2132
} else if (options.module) {

lib/views/node-details-view.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict";
2+
3+
var BaseDetailsView = require("./base-details-view");
4+
var time = require("../time");
5+
var MILLISECONDS_PER_SECOND = require("../constants").MILLISECONDS_PER_SECOND;
6+
var UPTIME_INTERVAL_MS = MILLISECONDS_PER_SECOND;
7+
8+
var NodeDetailsView = function NodeDetailsView(options) {
9+
BaseDetailsView.call(this, options);
10+
11+
this.setupdate();
12+
this.node.on("attach", this.setupdate.bind(this));
13+
14+
this.node.on("detach", function () {
15+
if (this.uptimeInterval) {
16+
clearInterval(this.uptimeInterval);
17+
delete this.uptimeInterval;
18+
}
19+
}.bind(this));
20+
};
21+
22+
NodeDetailsView.prototype = Object.create(BaseDetailsView.prototype);
23+
24+
NodeDetailsView.prototype.setupdate = function () {
25+
this.uptimeInterval = this.uptimeInterval || setInterval(function () {
26+
this.refreshContent();
27+
}.bind(this), UPTIME_INTERVAL_MS);
28+
};
29+
30+
NodeDetailsView.prototype.getDetails = function () {
31+
return [
32+
{
33+
label: "Version",
34+
data: process.version
35+
}, {
36+
label: "LTS",
37+
data: process.release.lts
38+
}, {
39+
label: "Uptime",
40+
data: time.getLabel(process.uptime() * MILLISECONDS_PER_SECOND)
41+
}
42+
];
43+
};
44+
45+
NodeDetailsView.prototype.getDefaultLayoutConfig = function () {
46+
return {
47+
label: " Node ",
48+
border: "line",
49+
tags: true,
50+
height: "shrink",
51+
style: {
52+
border: {
53+
fg: "white"
54+
}
55+
}
56+
};
57+
};
58+
59+
module.exports = NodeDetailsView;

0 commit comments

Comments
 (0)