-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLog.js
338 lines (317 loc) · 13.3 KB
/
Log.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// MIT License
//
// Copyright 2018 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const Util = require('util');
const Readline = require('readline');
const ImpCentralApiHelper = require('./util/ImpCentralApiHelper');
const UserInteractor = require('./util/UserInteractor');
const ProjectConfig = require('./util/ProjectConfig');
const Identifier = require('./util/Identifier');
const Errors = require('./util/Errors');
const DeviceGroup = require('./DeviceGroup');
const Device = require('./Device');
// This class represents Logs impCentral API entity.
// Provides methods used by impt Logs Manipulation Commands.
class Log {
constructor(options) {
options = options || {};
this._helper = ImpCentralApiHelper.getEntity();
if (options.debug) {
this._helper.debug = true;
}
if (options != {} && options._options.output == "JSON"){
UserInteractor.PRINT_FORMAT = UserInteractor.PRINT_FORMAT_JSON
}
this._projectConfig = ProjectConfig.getEntity();
this._devices = [];
this._deviceIds = [];
this._logStreamId = undefined;
this._logsFinished = false;
this._reconnecting = false;
this._init();
}
// Initializes readline interface to handle <Ctrl-C> and <Enter> presses.
_init() {
this._readline = Readline.createInterface({
input: process.stdin,
output: process.stdout
});
this._readline.on('SIGINT', () => { process.emit('SIGINT') });
process.on('SIGINT', () => {
this._exitWithStatus();
});
}
_closeLogStream() {
if (this._logStreamId) {
return this._helper.closeLogStream(this._logStreamId);
this._logStreamId = undefined;
}
return Promise.resolve();
}
_exitWithStatus(error = null) {
this._readline.close();
this._closeLogStream().
then(() => {
if (error) {
UserInteractor.processError(error);
}
else {
UserInteractor.printSuccessStatus();
}
}).
catch(err => {
UserInteractor.processError(error ? error : err);
});
}
// Displays historical logs for the specified Device.
//
// If --device option is missed but the current directory contains
// Project File with the specified Device Group:
// If there is only one Device assigned to this Device Group, the command is processed for it.
// If there no or more than one Devices assigned to this Device Group,
// the command informs a user and is not processed.
//
// Parameters:
// options : Options impt options of the corresponding command
//
// Returns: Nothing.
get(options) {
this._setOptions(options).then(() => {
if (this._devices.length === 0) {
return Promise.reject(new Errors.NoIdentifierError(Identifier.ENTITY_TYPE.TYPE_DEVICE));
}
else {
const device = this._devices[0];
if (this._devices.length > 1) {
return device._collectListEntities(this._devices).then(() =>
Promise.reject(
new Errors.NotUniqueIdentifierError(
device.entityType,
null,
this._devices.map(entity => entity.displayData))));
}
else {
// the only device found
return device.getEntityId().then(() => {
if (options.pageNumber !== undefined) {
return this._printDeviceLogsPage(device.id, options.pageNumber, options.pageSize).
then(() => this._exitWithStatus());
}
else {
let pageNumber = 1;
this._printDeviceLogs(device.id, pageNumber, options.pageSize);
this._readline.on('line', () => {
pageNumber++;
this._printDeviceLogs(device.id, pageNumber, options.pageSize);
});
}
});
}
}
}).catch(error => {
this._exitWithStatus(error);
});
}
// Prints a portion of historical logs for the specified Device.
//
// Parameters:
// deviceId : string id of the Device
// pageNumber : number logs pagination page number to be printed
//
// Returns: Nothing.
_printDeviceLogs(deviceId, pageNumber, pageSize) {
if (this._logsFinished) {
UserInteractor.printMessage(UserInteractor.MESSAGES.LOG_GET_FINISHED);
this._exitWithStatus();
}
else {
this._printDeviceLogsPage(deviceId, pageNumber, pageSize).then(() => {
UserInteractor.printSuccessStatus();
UserInteractor.printMessage('');
UserInteractor.printMessage(UserInteractor.MESSAGES.LOG_GET_NEXT);
}).catch(error => {
this._exitWithStatus(error);
});
}
}
_printDeviceLogsPage(deviceId, pageNumber, pageSize) {
return this._helper.logs(deviceId, pageNumber, pageSize).
then(logs => {
logs.map(log => UserInteractor.printJsonData(this._formatLog(log)));
if (logs.length < pageSize) {
this._logsFinished = true;
}
});
}
// Displays logs from the specified Devices in real-time.
//
// --device option may be repeated several times to specify several Devices.
// Logs from these Devices will be displayed in a one stream.
//
// If --dg option is specified, logs from all Devices assigned to this Device Group will be displayed.
// --dg option may be repeated several times to specify several Device Groups.
//
// --device and --dg options are cumulative. Logs from all the Devices specified by these options will
// be displayed in a one stream.
//
// If --device and --dg options are missed but the current directory contains Project File,
// then all Devices assigned to Device Group specified in that Project File are assumed.
//
// Parameters:
// options : Options impt options of the corresponding command
//
// Returns: Nothing.
stream(options) {
this._setOptions(options).
then(() => this._stream(this._devices)).
catch(error => {
this._exitWithStatus(error);
});
}
_stream(devices) {
if (devices.length === 0) {
return Promise.reject(new Errors.NoIdentifierError(Identifier.ENTITY_TYPE.TYPE_DEVICE));
}
else {
return this._helper.makeConcurrentOperations(devices, (device) => device.getEntityId()).then(() => {
this._deviceIds = devices.map(device => device.id);
// remove duplicates from _deviceIds
this._deviceIds = [...new Set(this._deviceIds)];
return this._helper.logStream(
this._deviceIds,
this._messageHandler.bind(this),
this._stateChangeHandler.bind(this),
this._errorHandler.bind(this)).
then((logStreamId) => {
this._logStreamId = logStreamId;
this._reconnecting = false;
UserInteractor.spinnerStop();
let deviceIds = this._deviceIds;
if (deviceIds.length > UserInteractor.PRINTED_ENTITIES_LIMIT) {
deviceIds = deviceIds.slice(0, UserInteractor.PRINTED_ENTITIES_LIMIT);
deviceIds.push('...');
}
UserInteractor.printMessage(UserInteractor.MESSAGES.LOG_STREAM_OPENED, deviceIds.join(', '));
UserInteractor.printSuccessStatus();
UserInteractor.printMessage('');
UserInteractor.printMessage(UserInteractor.MESSAGES.LOG_STREAM_EXIT);
});
});
}
}
// Log stream message handler
_messageHandler(message) {
try {
UserInteractor.printJsonData(this._formatLog(JSON.parse(message), true));
}
catch (e) {
this._exitWithStatus(new Errors.InternalLibraryError(
Util.format(UserInteractor.ERRORS.LOG_STREAM_UNEXPECTED_MSG, message)));
}
}
// Log stream state change handler
_stateChangeHandler(state) {
try {
const json = JSON.parse(state);
if (json.state === 'closed') {
UserInteractor.printMessage(UserInteractor.MESSAGES.LOG_STREAM_CLOSED);
this._exitWithStatus();
}
}
catch (e) {
this._exitWithStatus(new Errors.InternalLibraryError(
Util.format(UserInteractor.ERRORS.LOG_STREAM_UNEXPECTED_MSG, state)));
}
}
_errorHandler(error) {
UserInteractor.printError(error);
if (!this._reconnecting) {
this._reconnect();
}
}
_reconnect() {
UserInteractor.printMessage(UserInteractor.MESSAGES.LOG_STREAM_RECONNECT);
this._reconnecting = true;
this._closeLogStream().
then(() => this._stream(this._devices)).
catch(error => {
this._exitWithStatus(error);
});
}
// Formats the single log message
_formatLog(log, printDeviceId = false) {
if (('device_id' in log) && printDeviceId) {
return Util.format('[%s] %s [%s] %s', log.device_id, log.ts, log.type, log.msg);
}
else {
return Util.format('%s [%s] %s', log.ts, log.type, log.msg);
}
}
// Sets the Log specific options based on impt command options:
// if (--device <DEVICE_IDENTIFIER>)* option is specified, the Devices identifiers
// are initialized by its values
// if (--dg <DEVICE_GROUP_IDENTIFIER>)* option is specified, all Devices assigned
// to the Device Groups are used
//
//
// Parameters:
// options : Options impt options of the corresponding command
//
// Returns: Promise that resolves if the operation succeeded,
// or rejects with an error
_setOptions(options) {
options = options || {};
if (options.deviceIdentifier) {
if (Array.isArray(options.deviceIdentifier)) {
this._devices = this._devices.concat(options.deviceIdentifier.map(
device => new Device().initByIdentifier(device)));
}
else {
this._devices.push(new Device().initByIdentifier(options.deviceIdentifier));
}
}
if (options.deviceGroupIdentifier) {
return this._helper.makeConcurrentOperations(
Array.isArray(options.deviceGroupIdentifier) ? options.deviceGroupIdentifier : [options.deviceGroupIdentifier],
(devGroup) => new DeviceGroup().initByIdentifier(devGroup).getEntity()).
then(devGroups => this._helper.makeConcurrentOperations(devGroups,
(devGroup) => this._setDevicesByDeviceGroup(devGroup)));
}
if (!options.deviceIdentifier && !options.deviceGroupIdentifier &&
this._projectConfig.exists()) {
return this._projectConfig.checkConfig().
then(() => new DeviceGroup().initByIdFromConfig(this._projectConfig.deviceGroupId, this._projectConfig.type).getEntity()).
then((deviceGroup) => this._setDevicesByDeviceGroup(deviceGroup));
}
return Promise.resolve();
}
_setDevicesByDeviceGroup(deviceGroup) {
return new Device().listByDeviceGroup(deviceGroup.id).
then((devices) => {
this._devices = this._devices.concat(devices);
return Promise.resolve();
});
}
}
module.exports = Log;