-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
1042 lines (929 loc) · 34.2 KB
/
Copy pathapp.js
File metadata and controls
1042 lines (929 loc) · 34.2 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-await-in-loop */
/*
Copyright 2017 - 2026 Robin de Gruijter
This file is part of com.gruijter.insights2csv.
com.gruijter.insights2csv is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
com.gruijter.insights2csv is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with com.gruijter.insights2csv. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const Homey = require('homey');
const fs = require('fs');
const util = require('util');
const Logger = require('./lib/captureLogs');
const setTimeoutPromise = util.promisify(setTimeout);
// ============================================================
// Some helper functions here
const JSDateToExcelDate = (inDate) => {
// convert to yyyy-MM-dd HH:mm:ss
const dateTime = inDate.toISOString().replace(/T/, ' ').replace(/\..+/, '');
return dateTime;
};
class App extends Homey.App {
log(...args) {
if (this.logger) {
try {
const msg = args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' ');
this.logger.addLog('log', msg);
} catch (e) { /* ignore */ }
}
try {
if (this.homey && this.homey.app) {
super.log(...args);
} else {
console.log(...args);
}
} catch (e) {
console.log(...args);
}
}
error(...args) {
if (this.logger) {
try {
const msg = args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' ');
this.logger.addLog('error', msg);
} catch (e) { /* ignore */ }
}
try {
if (this.homey && this.homey.app) {
super.error(...args);
} else {
console.error(...args);
}
} catch (e) {
console.error(...args);
}
}
async onInit() {
try {
if (!this.logger) this.logger = new Logger({ name: 'log', length: 200, homey: this.homey });
// generic properties
this.homeyAPI = undefined;
this.devices = {};
this.logs = [];
this.allNames = [];
this.webdavSettings = {};
this.smbSettings = {};
this.FTPSettings = {};
this.CPUSettings = this.homey.settings.get('CPUSettings');
if (!this.CPUSettings) {
this.CPUSettings = { lowCPU: false };
this.homey.settings.set('CPUSettings', this.CPUSettings);
}
this.OnlyZipWithLogs = {};
this.resolutionSelection = ['lastHour', 'last6Hours', 'last24Hours', 'last7Days', 'last14Days', 'last31Days',
'last2Years', 'today', 'thisWeek', 'thisMonth', 'thisYear', 'yesterday', 'lastWeek', 'lastMonth', 'lastYear'];
// queue properties
this.abort = false;
this.queue = [];
this.queueRunning = false;
// register some listeners
this.homey
.on('memwarn', () => {
this.log('memwarn! Reclaiming memory...');
if (this.logger) this.logger.trim(50);
this.clearCache();
})
.on('cpuwarn', () => {
this.log('cpuwarn!');
});
this.homey.settings.on('set', (key) => {
this.log(`${key} changed from frontend`);
});
// ==============FLOW CARD STUFF======================================
const archiveAllAction = this.homey.flow.getActionCard('archive_all');
archiveAllAction
.registerRunListener(async (args) => {
this.log(`Exporting all insights ${args.resolution}`);
await this.exportAll(args.resolution);
return true;
});
const archiveAppAction = this.homey.flow.getActionCard('archive_app');
archiveAppAction
.registerRunListener(async (args) => {
await this.exportApp(args.selectedApp.id, args.resolution);
return true;
})
.registerArgumentAutocompleteListener(
'selectedApp',
async (query) => {
const allNames = await this.getAppList();
const results = allNames.filter((result) => { // filter for query on appId and appName
const appIdFound = result.id.toLowerCase().indexOf(query.toLowerCase()) > -1;
const appNameFound = result.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
return appIdFound || appNameFound;
});
return results;
},
);
const purgeAction = this.homey.flow.getActionCard('purge');
purgeAction
.registerRunListener(async (args) => {
this.log(`Deleting old data on ${args.storage}`);
try {
if (args.storage === 'FTP') {
await this.getFtpHelper().purge(args.daysOld, args.types === 'allTypes', this.FTPSettings);
}
if (args.storage === 'SMB') {
await this.getSmbHelper().purge(args.daysOld, args.types === 'allTypes', this.smbSettings);
}
if (args.storage === 'WebDAV') {
await this.getWebDavHelper().purge(args.daysOld, args.types === 'allTypes', this.webdavSettings);
}
} catch (err) {
this.error('Purge error:', err.message);
throw err;
}
return true;
});
const archiveAllTypeFolderAction = this.homey.flow.getActionCard('archive_all_type_folder');
archiveAllTypeFolderAction
.registerRunListener(async (args) => {
this.log(`Exporting all insights ${args.resolution} of type ${args.type} into subfolder ${args.subfolder} `);
const type = args.type === 'all' ? undefined : args.type;
const subfolder = args.subfolder && args.subfolder !== 'undefined' ? args.subfolder : undefined;
await this.exportAll(args.resolution, type, subfolder);
return true;
});
const archiveAppTypeFolderAction = this.homey.flow.getActionCard('archive_app_type_folder');
archiveAppTypeFolderAction
.registerRunListener(async (args) => {
const type = args.type === 'all' ? undefined : args.type;
const subfolder = args.subfolder && args.subfolder !== 'undefined' ? args.subfolder : undefined;
await this.exportApp(args.selectedApp.id, args.resolution, null, true, type, subfolder);
return true;
})
.registerArgumentAutocompleteListener(
'selectedApp',
async (query) => {
const allNames = await this.getAppList();
const results = allNames.filter((result) => { // filter for query on appId and appName
const appIdFound = result.id.toLowerCase().indexOf(query.toLowerCase()) > -1;
const appNameFound = result.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
return appIdFound || appNameFound;
});
return results;
},
);
this.exportFinishedTrigger = this.homey.flow.getTriggerCard('export_finished');
this.deleteAllFiles().catch((err) => this.error(err));
// initiate test stuff from here
this.test();
this.log('ExportInsights App is running!');
} catch (error) {
this.error(error);
}
}
async onUninit() {
this.abort = true;
this.flushQueue();
if (this.logger) {
this.logger.saveLogs();
this.logger.releaseStdOut();
this.logger.releaseStdErr();
}
if (this.ftpHelper) {
try { this.ftpHelper.close(); } catch (e) { /* ignore */ }
}
if (this.smbHelper) {
try { this.smbHelper.close(); } catch (e) { /* ignore */ }
}
}
// ============================================================
// do the stuff from here
async test() {
try {
} catch (error) {
this.error(error);
}
}
// ============================================================
// stuff for queue handling here
async enQueue(item) {
this.queue.push(item);
if (!this.queueRunning) {
this.queueRunning = true;
this.runQueue();
}
}
deQueue() {
return this.queue.shift();
}
flushQueue() {
this.queue = [];
this.queueRunning = false;
this.log('Export queue is flushed');
}
async runQueue() {
this.queueRunning = true;
while (this.queue.length > 0) {
if (this.abort) break;
const item = this.deQueue();
if (item.isTrigger) {
const durationMs = item.startTime ? Date.now() - item.startTime : 0;
const durationSec = Math.round(durationMs / 1000);
this.exportFinishedTrigger.trigger({
duration: durationSec,
status: 'Success',
resolution: item.resolution || '',
identifier: item.identifier || '',
timestamp: item.timestamp || '',
}).catch(err => this.error(err));
continue;
}
await this._exportApp(item.appId, item.resolution, item.date, item.type, item.subfolder, item.timestamp)
.catch(err => this.error(err));
// Wait 1 solid second between apps to reset the 10-second cpuwarn window and clear thread queues
await setTimeoutPromise(this.CPUSettings && this.CPUSettings.lowCPU ? 10 * 1000 : 1000, 'waiting is done');
}
this.queueRunning = false;
this.clearCache();
this.log('Finished all exports');
}
// ============================================================
// stuff for frontend API here
clearCache() {
this.devices = {};
this.logs = [];
this.allNames = [];
if (!this.queueRunning) {
this.homeyAPI = null;
this._homeyAPIPromise = null;
this._logsPromise = null;
this._devicesPromise = null;
this._namesPromise = null;
}
if (this.ftpHelper) {
try { this.ftpHelper.close(); } catch (e) { /* ignore */ }
this.ftpHelper = null;
}
if (this.smbHelper) {
try { this.smbHelper.close(); } catch (e) { /* ignore */ }
this.smbHelper = null;
}
if (this.webdavHelper) {
this.webdavHelper = null;
}
}
deleteLogs() {
return this.logger.deleteLogs();
}
getLogs() {
return this.logger.logArray;
}
getSmbHelper() {
if (!this.smbHelper) {
const SmbHelper = require('./lib/SmbHelper');
this.smbHelper = new SmbHelper(this);
}
return this.smbHelper;
}
getWebDavHelper() {
if (!this.webdavHelper) {
const WebDavHelper = require('./lib/WebDavHelper');
this.webdavHelper = new WebDavHelper(this);
}
return this.webdavHelper;
}
getFtpHelper() {
if (!this.ftpHelper) {
const FtpHelper = require('./lib/FtpHelper');
this.ftpHelper = new FtpHelper(this);
}
return this.ftpHelper;
}
async testSmb(smbSettings) {
return this.getSmbHelper().test(smbSettings);
}
async testWebdav(webdavSettings) {
return this.getWebDavHelper().test(webdavSettings);
}
async testFTP(FTPSettings) {
return this.getFtpHelper().test(FTPSettings);
}
getResolutions() {
return this.resolutionSelection;
}
async getAppList() {
if (this.allNames && this.allNames.length > 0) return this.allNames;
this.log('Lazy loading app list for frontend/flow...');
await this.loginHomeyApi();
await setTimeoutPromise(200, 'breathe');
await this.getAllLogs();
await setTimeoutPromise(200, 'breathe');
return this.getAllNames();
}
async exportAll(resolution, type, subfolder) {
const date = new Date();
const startTime = Date.now();
const timestamp = date.toISOString()
.replace(/:/g, '') // delete :
.replace(/-/g, '') // delete -
.replace(/\..+/, 'Z'); // delete the dot and everything after
await this.initExport(date);
this.allNames.forEach((name) => {
this.exportApp(name.id, resolution, date, false, type, subfolder, timestamp);
});
this.enQueue({
isTrigger: true,
resolution,
identifier: 'All apps',
timestamp,
startTime,
});
return true;
}
async exportApp(appId, resolution, _date, reload, type, subfolder, passedTimestamp) {
const date = new Date();
const startTime = Date.now();
const timestamp = passedTimestamp || date.toISOString()
.replace(/:/g, '') // delete :
.replace(/-/g, '') // delete -
.replace(/\..+/, 'Z'); // delete the dot and everything after
if (reload !== false) {
await this.initExport(date);
}
this.enQueue({
appId, resolution, date, type, subfolder, timestamp,
});
if (reload !== false) {
this.enQueue({
isTrigger: true,
resolution,
identifier: appId,
timestamp,
startTime,
});
}
return true;
}
stopExport() {
if (this.queueRunning) this.log('aborting export');
this.abort = true;
// Fire aborted triggers for any remaining jobs in the queue
this.queue.filter((item) => item.isTrigger).forEach((item) => {
const durationMs = item.startTime ? Date.now() - item.startTime : 0;
const durationSec = Math.round(durationMs / 1000);
this.exportFinishedTrigger.trigger({
duration: durationSec,
status: 'Aborted',
resolution: item.resolution || '',
identifier: item.identifier || '',
timestamp: item.timestamp || '',
}).catch(err => this.error(err));
});
this.flushQueue();
return true;
}
// ============================================================
// Local file handling in app userdata folder
deleteAllFiles() {
return new Promise((resolve) => {
fs.readdir('/userdata/', (err, res) => {
if (err) {
this.log(err);
return resolve();
}
const unlinkPromises = res
.filter((elem) => elem !== 'log.json')
.map((elem) => new Promise((resUnlink) => {
fs.unlink(`/userdata/${elem}`, (error) => {
if (error) {
this.log(error);
} else {
this.log(`deleted ${elem}`);
}
resUnlink();
});
}));
Promise.all(unlinkPromises).then(() => {
this.log('all local files deleted');
resolve();
});
});
});
}
deleteFile(filename) {
fs.unlink(`/userdata/${filename}`, (error) => {
if (error) {
this.log(error);
}
});
}
// ============================================================
// Homey API stuff here
async loginHomeyApi() {
if (this.homeyAPI) return this.homeyAPI;
if (this._homeyAPIPromise) return this._homeyAPIPromise;
this.log('Initializing Homey API...');
this._homeyAPIPromise = (async () => {
try {
const { HomeyAPI } = require('homey-api');
const api = await HomeyAPI.createAppAPI({ homey: this.homey });
this.homeyAPI = api;
this.log('Homey API initialized successfully.');
return api;
} finally {
this._homeyAPIPromise = null;
}
})();
return this._homeyAPIPromise;
}
async getAllLogs() {
if (this._logsPromise) return this._logsPromise;
this._logsPromise = (async () => {
try {
this.logs = Object.values(await this.homeyAPI.insights.getLogs({ $timeout: 30000 }));
return this.logs;
} finally {
this._logsPromise = null;
}
})();
return this._logsPromise;
}
async getAllDevices() {
if (this._devicesPromise) return this._devicesPromise;
this._devicesPromise = (async () => {
try {
this.devices = await this.homeyAPI.devices.getDevices({
$timeout: 30000,
$select: 'id,name,driverId,ownerUri',
});
return this.devices;
} finally {
this._devicesPromise = null;
}
})();
return this._devicesPromise;
}
// Get a list of all app names
async getAppNameList() {
const allApps = await this.homeyAPI.apps.getApps({
$timeout: 30000,
$select: 'id,name,icon',
});
const mappedArray = Object.entries(allApps).map((app) => {
const map = {
id: app[1].id,
name: app[1].name,
icon: `${app[1].id}${app[1].icon}`,
type: 'app',
};
return map;
});
return mappedArray;
}
// Get a list of all logged manager names
async getManagerNameList() {
// eslint-disable-next-line prefer-destructuring
const logs = this.logs;
const list = logs.filter((log) => {
const uri = log.uri || log.ownerUri || '';
return uri.startsWith('homey:manager:');
})
.map((log) => {
const uri = log.uri || log.ownerUri || '';
const ids = uri.split(':');
const id = ids.pop();
const name = id ? id.charAt(0).toUpperCase() + id.slice(1) : 'Unknown';
if (!name) return null;
const _app = {
id,
name,
icon: '',
type: ids[1] || 'manager',
};
return _app;
});
const seen = new Set();
return list.filter((elem) => {
if (!elem) return false;
if (seen.has(elem.id)) return false;
seen.add(elem.id);
return true;
});
}
async getAllNames() {
if (this._namesPromise) return this._namesPromise;
this._namesPromise = (async () => {
try {
const managerNameList = await this.getManagerNameList();
const appNameList = await this.getAppNameList();
this.allNames = appNameList.concat(managerNameList);
return this.allNames;
} finally {
this._namesPromise = null;
}
})();
return this._namesPromise;
}
async getAppRelatedLogs(appId, type) {
this.log(`getting logs related to ${appId}`);
const appUri = `homey:app:${appId}`;
const managerUri = `homey:manager:${appId}`;
const relatedDeviceUris = new Set();
Object.keys(this.devices).forEach((key) => {
const device = this.devices[key];
// Use driverId instead of the deprecated driverUri
if (device.ownerUri === appUri || (device.driverId && device.driverId.startsWith(appUri))) {
relatedDeviceUris.add(`homey:device:${device.id}`);
}
});
const appRelatedLogs = [];
for (let i = 0; i < this.logs.length; i += 1) {
if (i > 0 && i % 1000 === 0) await new Promise((resolve) => setTimeout(resolve, 2));
const log = this.logs[i];
if (type && log.type !== type) continue;
const uri = log.uri || log.ownerUri;
if (uri === appUri || uri === managerUri || relatedDeviceUris.has(uri)) {
appRelatedLogs.push(log);
} else if (log.ownerUri && (log.ownerUri === appUri || log.ownerUri === managerUri || relatedDeviceUris.has(log.ownerUri))) {
appRelatedLogs.push(log);
}
}
return appRelatedLogs;
}
/**
*
* @param {*} log
* @param {*} resolution
* @param {Date} date
* @returns
*/
async getLogEntries(log, resolution, date) {
try {
const opts = {
uri: (log.uri || log.ownerUri),
id: log.id,
$timeout: 5000,
};
if (log.type !== 'boolean') {
opts.resolution = resolution;
}
const logEntries = await this.homeyAPI.insights.getLogEntries(opts);
if (!logEntries || !Array.isArray(logEntries.values)) {
this.error(`Corrupt or unexpected API response for logEntries for ${log.id} (${log.type}). Expected array, got: ${JSON.stringify(logEntries).substring(0, 200)}...`);
throw new Error('Unexpected API response format for log entries.');
}
if (log.type === 'boolean') {
const dateTimezoned = new Date(this.enLocalDateFormatter.format(date));
let hourOffset = date.getHours() - dateTimezoned.getHours();
if (hourOffset <= -12) hourOffset += 24;
if (hourOffset > 12) hourOffset -= 24;
let _dateFrom = null;
let _dateTo = new Date(date);
let exclusiveEnd = false;
const applyOffset = (dt) => new Date(dt.getTime() + hourOffset * 60 * 60 * 1000);
const y = dateTimezoned.getFullYear();
const m = dateTimezoned.getMonth();
const d = dateTimezoned.getDate();
const day = dateTimezoned.getDay();
switch (resolution) {
case 'lastHour':
_dateFrom = new Date(date.getTime() - 60 * 60 * 1000);
break;
case 'last6Hours':
_dateFrom = new Date(date.getTime() - 6 * 60 * 60 * 1000);
break;
case 'last24Hours':
_dateFrom = new Date(date.getTime() - 24 * 60 * 60 * 1000);
break;
case 'last7Days':
_dateFrom = new Date(date.getTime() - 7 * 24 * 60 * 60 * 1000);
break;
case 'last14Days':
_dateFrom = new Date(date.getTime() - 14 * 24 * 60 * 60 * 1000);
break;
case 'last31Days':
_dateFrom = new Date(date.getTime() - 31 * 24 * 60 * 60 * 1000);
break;
case 'today':
_dateFrom = applyOffset(new Date(y, m, d));
break;
case 'yesterday':
_dateFrom = applyOffset(new Date(y, m, d - 1));
_dateTo = applyOffset(new Date(y, m, d));
exclusiveEnd = true;
break;
case 'thisWeek':
_dateFrom = applyOffset(new Date(y, m, d - (day - 1)));
break;
case 'lastWeek':
_dateFrom = applyOffset(new Date(y, m, d - (day - 1) - 7));
_dateTo = applyOffset(new Date(y, m, d - (day - 1)));
exclusiveEnd = true;
break;
case 'thisMonth':
_dateFrom = applyOffset(new Date(y, m, 1));
break;
case 'lastMonth':
_dateFrom = applyOffset(new Date(y, m - 1, 1));
_dateTo = applyOffset(new Date(y, m, 1));
exclusiveEnd = true;
break;
case 'thisYear':
_dateFrom = applyOffset(new Date(y, 0, 1));
break;
case 'lastYear':
_dateFrom = applyOffset(new Date(y - 1, 0, 1));
_dateTo = applyOffset(new Date(y, 0, 1));
exclusiveEnd = true;
break;
case 'last2Years':
_dateFrom = new Date(new Date(date).setFullYear(date.getFullYear() - 2));
break;
default:
throw new Error(`invalid resolution: ${resolution}`);
}
// Enhanced check for potentially corrupt individual entries
if (logEntries.values.some((entry) => typeof entry.t === 'undefined' || typeof entry.v === 'undefined')) {
const corruptSample = logEntries.values
.filter((entry) => typeof entry.t === 'undefined' || typeof entry.v === 'undefined')
.slice(0, 5);
this.error(`Corrupt individual entries found in boolean log for ${log.id} (${log.type}). Sample: ${JSON.stringify(corruptSample)}`);
// You might choose to filter these out or throw an error. For now, we'll continue.
}
// In-place filter to prevent Array duplication memory spikes
let writeIndex = 0;
const { values } = logEntries;
const len = values.length;
for (let i = 0; i < len; i += 1) {
const x = values[i];
const t = new Date(x.t).getTime();
let keep = true;
if (_dateFrom && t < _dateFrom.getTime()) keep = false;
if (_dateTo) {
if (exclusiveEnd && t >= _dateTo.getTime()) keep = false;
if (!exclusiveEnd && t > _dateTo.getTime()) keep = false;
}
if (keep) {
values[writeIndex] = x;
writeIndex += 1;
}
}
values.length = writeIndex; // Instantly shrink array in-place
}
if (logEntries.values.length > 2925) {
this.error(`Insights data is massive (${logEntries.values.length} entries) and will be truncated to the first 2925 records for ${log.uri || log.ownerUri} ${logEntries.id}.`);
logEntries.values.length = 2925; // Truncate in-place instead of .slice()
await setTimeoutPromise(this.CPUSettings && this.CPUSettings.lowCPU ? 10 * 1000 : 1, 'waiting is done');
}
return logEntries;
} catch (error) {
await setTimeoutPromise(this.CPUSettings && this.CPUSettings.lowCPU ? 10 * 1000 : 1, 'waiting is done');
throw error;
}
}
async initExport(date) {
this.abort = false;
await this.deleteAllFiles();
this.webdavSettings = this.homey.settings.get('webdavSettings');
this.smbSettings = this.homey.settings.get('smbSettings');
this.FTPSettings = this.homey.settings.get('FTPSettings');
this.CPUSettings = this.homey.settings.get('CPUSettings');
if (!this.CPUSettings) {
this.CPUSettings = { lowCPU: false };
this.homey.settings.set('CPUSettings', this.CPUSettings);
}
this.timeZone = this.homey.clock.getTimezone();
this.locale = await this.homey.i18n.getLanguage();
this.localDateFormatter = new Intl.DateTimeFormat(this.locale, {
timeZone: this.timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
this.enLocalDateFormatter = new Intl.DateTimeFormat('en', {
timeZone: this.timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
this.OnlyZipWithLogs = this.homey.settings.get('OnlyZipWithLogs');
if (!this.OnlyZipWithLogs) {
this.OnlyZipWithLogs = { onlyZipWithLogs: false };
this.homey.settings.set('OnlyZipWithLogs', this.OnlyZipWithLogs);
}
this.IncludeLocalDateTime = this.homey.settings.get('IncludeLocalDateTime');
if (!this.IncludeLocalDateTime) {
this.IncludeLocalDateTime = { includeLocalDateTime: false };
this.homey.settings.set('IncludeLocalDateTime', this.IncludeLocalDateTime);
}
if (this.CPUSettings && this.CPUSettings.lowCPU) this.log('Low CPU load selected for export');
await this.loginHomeyApi();
// Fetch sequentially with pauses to let the CPU breathe and prevent 'cpuwarn'
await setTimeoutPromise(200, 'breathe');
await this.getAllLogs();
await setTimeoutPromise(200, 'breathe');
await this.getAllNames();
await setTimeoutPromise(200, 'breathe');
await this.getAllDevices();
await setTimeoutPromise(200, 'breathe');
return true;
}
// ============================================================
// ZIP handling here
// zip all log entries from one app as promise; resolves zipfilename
async zipAppLogs(appId, resolution, date, type) {
let fileName = null;
let output = null;
let archive = null;
try {
const logs = await this.getAppRelatedLogs(appId, type);
if (this.OnlyZipWithLogs.onlyZipWithLogs && !logs.length) return null;
// create a file to stream archive data to.
const timeStamp = date.toISOString()
.replace(/:/g, '') // delete :
.replace(/-/g, '') // delete -
.replace(/\..+/, ''); // delete the dot and everything after
fileName = `${appId}_${timeStamp}Z_${resolution}.zip`;
output = fs.createWriteStream(`/userdata/${fileName}`);
const { ZipArchive } = require('archiver');
const level = this.CPUSettings && this.CPUSettings.lowCPU ? 1 : 6;
archive = new ZipArchive({
zlib: { level }, // Sets the compression level.
forceUTC: true, // Force ZIP file timestamps to be UTC.
});
archive.pipe(output); // pipe archive data to the file
let written = false; // Using boolean flag instead of archive.pointer() for reliability
let entriesProcessed = 0;
let archiveError = false;
const finishPromise = new Promise((resolve, reject) => {
output.on('close', () => { // when zipping and storing is done...
this.log(`${logs.length} files zipped, ${archive.pointer()} total bytes`);
return resolve(fileName);
});
output.on('error', (err) => { // when storing gave an error
this.error(`error saving zipfile: ${err.message}`);
archiveError = true;
return reject(err);
});
archive.on('error', (err) => { // when zipping gave an error
archiveError = true;
this.error(err);
return reject(err);
});
archive.on('warning', (warning) => this.log(warning));
archive.on('entry', () => {
entriesProcessed += 1;
});
});
for (let idx = 0; idx < logs.length; idx += 1) {
if (!this.abort && !archiveError) {
// Periodically force the CPU to idle for 1 second to completely reset Homey's 10-second cpuwarn monitor
if (idx > 0 && idx % 10 === 0) {
await setTimeoutPromise(1000, 'cooling down CPU');
}
const log = logs[idx];
let entries;
try {
entries = await this.getLogEntries(log, resolution, date);
} catch (err) {
this.error(`Skipping log ${log.id} due to error: ${err.message}`);
continue;
}
// eslint-disable-next-line no-continue
if (this.OnlyZipWithLogs.onlyZipWithLogs && (!entries || !entries.values.length)) continue;
written = true;
const meta = { entries: entries.values.length };
Object.keys(entries).forEach((key) => {
if (key === 'values') return;
meta[key] = entries[key];
});
const allMeta = Object.assign(meta, log);
const ids = (log.ownerUri || log.uri).split(':');
const id = ids.pop();
const dev = this.devices[id];
const app = dev ? null : this.allNames.find((x) => x.id === id);
const name = (dev && dev.name) || (app && app.name) || id || 'Unknown';
const fileNameCsv = `${name}/${(log.ownerId || log.id)}.csv`;
const fileNameMeta = `${name}/${(log.ownerId || log.id)}_meta.json`;
const fileNameJson = `${name}/${(log.ownerId || log.id)}.json`;
const delimiter = ';';
const includeLocal = this.IncludeLocalDateTime.includeLocalDateTime;
const localFormatter = this.localDateFormatter;
let targetId = entries.id || 'unknown';
if (targetId.includes(':')) targetId = targetId.split(':').pop();
if (log.ownerUri === 'homey:manager:logic') targetId = log.title;
const localTimeStr = includeLocal ? `${delimiter}Local datetime` : '';
const csvLines = [`Zulu dateTime${delimiter}${targetId}${localTimeStr}`];
for (let i = 0; i < entries.values.length; i += 1) {
const entry = entries.values[i];
const entryValue = entry.v;
const time = JSDateToExcelDate(new Date(entry.t));
let tLocal = '';
let value;
if (typeof entryValue === 'number') {
value = String(entryValue).replace('.', ',');
} else {
value = JSON.stringify(entryValue);
}
if (includeLocal) {
if (!entry.tLocal) entry.tLocal = localFormatter.format(new Date(entry.t));
tLocal = delimiter + entry.tLocal;
}
csvLines.push(`${time}${delimiter}${value}${tLocal}`);
// Brief pause to keep the event loop responsive and avoid CPUwarns
if (i > 0 && i % 500 === 0) {
await new Promise((res) => setTimeout(res, 2));
}
}
let csvString = `${csvLines.join('\r\n')}\r\n`;
csvLines.length = 0;
const expectedEntries = entriesProcessed + 3;
archive.append(csvString, { name: fileNameCsv, date });
csvString = null;
archive.append(JSON.stringify(allMeta), { name: fileNameMeta, date });
let jsonString = JSON.stringify(entries);
archive.append(jsonString, { name: fileNameJson, date });
jsonString = null;
// Wait for archiver to consume streams to prevent RAM overload
let waitCycles = 0;
while (entriesProcessed < expectedEntries) {
if (this.abort || archiveError) break;
await new Promise((res) => setTimeout(res, 25));
waitCycles += 1;
if (waitCycles > 12000) { // 5 mins max wait per file
this.error(`Archiver timeout waiting for entries for ${fileNameCsv}`);
archiveError = true;
break;
}
}
// Force memory release of the massive dataset to assist Garbage Collection
if (entries && entries.values) {
entries.values.length = 0;
entries.values = null;
}
if (this.CPUSettings && this.CPUSettings.lowCPU) {
await setTimeoutPromise(2 * 1000, 'waiting is done'); // relax Homey a bit...
} else {
await setTimeoutPromise(150, 'mini-waiting is done'); // Minor pause to allow V8 GC to flush memory
}
}
}
if (this.OnlyZipWithLogs.onlyZipWithLogs && !written) {
if (archive) {
try { archive.abort(); } catch (e) { /* ignore */ }
}
if (output) {
try { output.close(); } catch (e) { /* ignore */ }
}
if (fileName) {
this.deleteFile(fileName);
}
return null;
}
await archive.finalize();
return await finishPromise;