Skip to content

Commit 080b774

Browse files
committed
Add log system with configurable retention days
1 parent f2ed553 commit 080b774

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

backend/src/modules/device/controllers/device-log.controller.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@ import { UserService } from 'src/modules/user/services/user/user.service';
3737
export class DeviceLogController {
3838
private result;
3939
private storxBucket = process.env.STORX_BUCKET_NAME || '';
40+
private logKeepDays =
41+
Number(process.env.LOG_RETENTION_DAYS ?? '14');
4042

4143
constructor(
4244
@Inject(UserService)
4345
private readonly userService: UserService,
4446
private readonly deviceLogService: DeviceLogService,
4547
private readonly deviceService: DeviceService,
4648
) {
47-
/* setInterval(async () => {
48-
const usersRes = await this.userService.getAllUsers();
49+
setInterval(async () => {
50+
51+
await this.deviceLogService.removeAllDeviceLogsByDayBefore(this.logKeepDays)
52+
53+
/* const usersRes = await this.userService.getAllUsers();
4954
const devicesRes = await this.deviceService.getAllDevices();
5055
5156
devicesRes.map(async (device: any, index) => {
@@ -114,8 +119,8 @@ export class DeviceLogController {
114119
//});
115120
}
116121
});
117-
});
118-
}, 24 * 60 * 60 * 1000); */
122+
}); */
123+
}, 24 * 60 * 60 * 1000);
119124
}
120125

121126
async isAdmin(userId: string) {

backend/src/modules/device/repositories/device-log.repository.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ export class DeviceLogRepository {
3434
// return await this.deviceLogModel.create(data)
3535
}
3636

37+
async deleteDeviceLogs(query) {
38+
try {
39+
const result = await this.deviceLogModel.deleteMany(query);
40+
return result;
41+
} catch (error) {
42+
throw new GeneralException(
43+
ErrorTypeEnum.UNPROCESSABLE_ENTITY,
44+
'An error occurred while deleting device logs.',
45+
);
46+
}
47+
}
48+
49+
3750
async editDeviceLog(id, editedData) {
3851
await this.deviceLogModel
3952
.updateOne({ _id: id }, editedData)

backend/src/modules/device/services/device-log.service.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,77 @@ export class DeviceLogService {
464464
return foundDeviceLogs;
465465
}
466466

467+
async removeAllDeviceLogsByDayBefore(
468+
daysBefore: number,
469+
) {
470+
const startDate = new Date();
471+
startDate.setDate(startDate.getDate() - daysBefore);
472+
startDate.setHours(0, 0, 0, 0);
473+
474+
const endDate = new Date();
475+
endDate.setDate(endDate.getDate() - (daysBefore - 1));
476+
endDate.setHours(0, 0, 0, 0);
477+
478+
const deleteQuery = {
479+
data: { $exists: true },
480+
insertDate: {
481+
$gte: startDate,
482+
$lt: endDate,
483+
},
484+
};
485+
486+
const deleteResult = await this.deviceLogRepository.deleteDeviceLogs(deleteQuery);
487+
488+
return {
489+
success: true,
490+
message: `All device Logs from exactly ${daysBefore} day(s) ago have been deleted.`,
491+
result: deleteResult,
492+
};
493+
}
494+
495+
async removeDeviceLogByEncryptedDeviceIdAndDayBefore(
496+
deviceEncryptedId: string,
497+
daysBefore: number,
498+
userId = '',
499+
isAdmin = false,
500+
) {
501+
const foundDevices = await this.deviceService.getDeviceInfoByEncryptedId(
502+
deviceEncryptedId,
503+
userId,
504+
isAdmin,
505+
) as any;
506+
507+
if (foundDevices?.success === false) {
508+
return foundDevices;
509+
}
510+
511+
const startDate = new Date();
512+
startDate.setDate(startDate.getDate() - daysBefore);
513+
startDate.setHours(0, 0, 0, 0);
514+
515+
const endDate = new Date();
516+
endDate.setDate(endDate.getDate() - (daysBefore - 1));
517+
endDate.setHours(0, 0, 0, 0);
518+
519+
const deleteQuery = {
520+
deviceEncryptedId: deviceEncryptedId,
521+
data: { $exists: true },
522+
insertDate: {
523+
$gte: startDate,
524+
$lt: endDate,
525+
},
526+
};
527+
528+
const deleteResult = await this.deviceLogRepository.deleteDeviceLogs(deleteQuery);
529+
530+
return {
531+
success: true,
532+
message: `Logs from exactly ${daysBefore} day(s) ago have been deleted.`,
533+
result: deleteResult,
534+
};
535+
}
536+
537+
467538
async getDeviceLogByEncryptedDeviceIdAndDate(
468539
deviceEncryptedId,
469540
reportYear,

0 commit comments

Comments
 (0)