-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.js
273 lines (221 loc) · 5.9 KB
/
dashboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
var blessed = require('blessed')
, contrib = require('./index')
, inquirer = require('inquirer')
, request = require('request')
, R = require('ramda')
, Q = require('q')
, moment = require('moment');
var companyIds;
var questions = [
{
type: "input",
name: "url",
message: "The URL",
default: 'http://live-hedley.pantheon.io'
},
{
type: "input",
name: "username",
message: "Username",
default: 'demo'
},
{
type: "input",
name: "password",
message: "Password",
default: '1234'
},
{
type: "list",
name: "company",
message: "Which company to show",
choices: function getCompany(answers) {
// Declare function as asynchronous, and save the done callback
var done = this.async();
var url = answers.url + '/api/companies';
var auth = {
user: answers.username,
password: answers.password,
sendImmediately: true
};
request.get(url, {auth: auth}, function (error, response, body) {
var data = JSON.parse(body).data;
companyIds = data;
var result = R.map(R.prop('label'), data);
done(result);
});
},
filter: function filterCompany(answer) {
// Return the company ID.
var result = R.find(R.propEq('label', answer))(companyIds);
return (R.prop('id', result));
}
}
];
inquirer.prompt( questions, function(answers) {
getEvents(answers).then(function (events) {
renderScreen(events, answers);
});
});
/**
* Get events from the server.
*
* @param answers
*/
getEvents = function(answers) {
var defer = Q.defer();
var url = answers.url + '/api/events';
var qs = {
filter: {
company: answers.company
}
};
var auth = {
user: answers.username,
password: answers.password,
sendImmediately: true
};
request.get({uri: url, qs: qs, auth: auth}, function (error, response, body) {
defer.resolve(JSON.parse(body).data);
});
return defer.promise;
};
function renderScreen(events, answers) {
var screen = blessed.screen();
// Register the escape early, so if there's an error in the code, it's
// possible to exit the process.
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});
// Create layout and widgets.
var grid = new contrib.grid({rows: 1, cols: 2})
var grid1 = new contrib.grid({rows: 1, cols: 1})
grid1.set(0, 0, 1, 1, contrib.log,
{ fg: "green"
, selectedFg: "green"
, label: 'Server Log'});
var grid3 = new contrib.grid({rows: 1, cols: 1})
grid3.set(0, 0, 1, 1, contrib.table,
{ keys: true
, fg: 'green'
, label: 'Events'
, columnSpacing: [24, 10, 10]})
var grid4 = new contrib.grid({rows: 2, cols: 1})
grid4.set(0, 0, 1, 1, grid3);
grid4.set(1, 0, 1, 1, grid1);
var grid5 = new contrib.grid({rows: 2, cols: 1})
grid5.set(0, 0, 1, 1, contrib.map, {label: 'Map'})
grid5.set(1, 0, 1, 1, contrib.line,
{ maxY: 500,
label: 'Request time (ms)'
});
grid.set(0, 0, 1, 1, grid5)
grid.set(0, 1, 1, 1, grid4)
grid.applyLayout(screen)
var map = grid5.get(0, 0);
var transactionsLine = grid5.get(1, 0);
var log = grid1.get(0, 0);
var table = grid3.get(0,0);
var commands = ['grep', 'node', 'java', 'timer', '~/ls -l', 'netns', 'watchdog', 'gulp', 'tar -xvf', 'awk', 'npm install']
//set dummy data for table
function generateTable() {
var data = [];
/**
* Preapre the data for the events table.
*
* @param obj
* @returns {Array}
*/
function getEventsRow(obj) {
var row = [];
// Convert timestamp to JS date.
var date = new Date(R.prop('created', obj) * 1000);
row.push(R.prop('label', obj));
row.push(R.path('user.label', obj));
row.push(moment(date).format('DD:MM:YY'));
return row;
}
var data = R.map(getEventsRow)(events);
table.setData({headers: ['Title', 'Author', 'Time'], data: data})
}
generateTable();
table.focus();
// Set log data.
log.log('Starting process');
screen.render();
/**
* Set the response time from the server.
*
* @param value
*/
function setResponseTime(value) {
log.log('Request time ' + value + 'ms');
screen.render();
}
// Set map markers.
var marker = true;
setInterval(function() {
if (marker) {
/**
* Transform lat lng values to the one expected.
*
* @param obj
*
* @returns {{lon: string, lat: string}}
*/
var transformLatLng = function(obj) {
return {
lon: obj.lng,
lat: obj.lat
};
};
/**
* Wrapper function to add a marker to the map.
*
* @param obj
* Object with "lon" and "lat".
*/
var addMarker = function(obj) {
map.addMarker(obj);
};
var addMarkers = R.compose(addMarker, transformLatLng, R.prop('location'));
R.mapObj(addMarkers, events);
}
else {
map.clearMarkers()
}
marker =! marker
screen.render()
}, 1000)
// Set line charts data.
var transactionsData = {
x: [],
y: []
}
for (var i = 10; i > 1; i--) {
transactionsData.x.push(moment().subtract(i, 'seconds').format('hh:mm:ss'));
transactionsData.y.push(0);
}
setLineData(transactionsData, transactionsLine)
function setLineData(data, line, value) {
// Add the new value.
data.y.shift();
data.y.push(value)
// Change the time of the last item.
data.x.shift();
data.x.push(moment().format('hh:mm:ss'));
line.setData(data.x, data.y)
}
screen.render();
setInterval(function() {
// Keep queries the server.
var start = new Date();
getEvents(answers).then(function (response) {
events = response;
var responseTime = new Date() - start;
// Set linedata.
setLineData(transactionsData, transactionsLine, responseTime);
setResponseTime(responseTime);
});
}, 1000);
}