Added performance records cleanup aligned with logs
MODIFY
src/scheduler/tasks/LoggingDatabaseTableCleanupTask.ts
src/scheduler/tasks/LoggingDatabaseTableCleanupTask.ts
@@ -5,6 +5,7 @@ import { LockEntity } from '../../types/Locking';
import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
import LoggingStorage from '../../storage/mongodb/LoggingStorage';
+import PerformanceStorage from '../../storage/mongodb/PerformanceStorage';
import SchedulerTask from '../SchedulerTask';
import { ServerAction } from '../../types/Server';
import Tenant from '../../types/Tenant';
@@ -16,12 +17,14 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
public async run(name: string, config: TaskConfig): Promise<void> {
// Delete
await this.deleteLogs(Constants.DEFAULT_TENANT, config);
+ // Delete Perfs Records
+ await this.deletePerformanceRecords(Constants.DEFAULT_TENANT, config);
// Call for all Tenants
await super.run(name, config);
}
async processTenant(tenant: Tenant, config: LoggingDatabaseTableCleanupTaskConfig): Promise<void> {
- // Delete
+ // Delete Logs
await this.deleteLogs(tenant.id, config);
}
@@ -30,22 +33,20 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
const logsCleanUpLock = LockingManager.createExclusiveLock(tenantID, LockEntity.LOGGING, 'cleanup');
if (await LockingManager.acquire(logsCleanUpLock)) {
try {
- // Delete date
+ // Delete Standard Logs
const deleteUpToDate = moment().subtract(config.retentionPeriodWeeks, 'w').startOf('week').toDate();
// Delete
let result = await LoggingStorage.deleteLogs(tenantID, deleteUpToDate);
// Ok?
if (result.ok === 1) {
- // Ok
- Logging.logSecurityInfo({
+ await Logging.logSecurityInfo({
tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
- message: `${result.n} Log(s) have been deleted before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
+ message: `${result.n} Log(s) have been deleted successfully before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
});
} else {
- // Error
- Logging.logError({
+ await Logging.logError({
tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
@@ -53,22 +54,20 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
detailedMessages: { result }
});
}
- // Delete date
- const securityDeleteUpToDate: Date = moment().subtract(config.securityRetentionPeriodWeeks, 'w').startOf('week').toDate();
// Delete Security Logs
+ const securityDeleteUpToDate: Date = moment().subtract(config.securityRetentionPeriodWeeks, 'w').startOf('week').toDate();
+ // Delete
result = await LoggingStorage.deleteSecurityLogs(tenantID, securityDeleteUpToDate);
// Ok?
if (result.ok === 1) {
- // Ok
- Logging.logSecurityInfo({
+ await Logging.logSecurityInfo({
tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
message: `${result.n} Security Log(s) have been deleted before '${moment(securityDeleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
});
} else {
- // Error
- Logging.logSecurityError({
+ await Logging.logSecurityError({
tenantID: tenantID,
action: ServerAction.LOGS_CLEANUP,
module: MODULE_NAME, method: 'deleteLogs',
@@ -76,12 +75,46 @@ export default class LoggingDatabaseTableCleanupTask extends SchedulerTask {
detailedMessages: { result }
});
}
+ } catch (error) {
+ await Logging.logActionExceptionMessage(tenantID, ServerAction.LOGS_CLEANUP, error);
+ } finally {
+ await LockingManager.release(logsCleanUpLock);
+ }
+ }
+ }
+
+ private async deletePerformanceRecords(tenantID: string, config: LoggingDatabaseTableCleanupTaskConfig) {
+ // Get the lock
+ const performanceCleanUpLock = LockingManager.createExclusiveLock(tenantID, LockEntity.PERFORMANCE, 'cleanup');
+ if (await LockingManager.acquire(performanceCleanUpLock)) {
+ try {
+ // Delete Performance Records
+ const deleteUpToDate = moment().subtract(config.retentionPeriodWeeks, 'w').startOf('week').toDate();
+ // Delete
+ const result = await PerformanceStorage.deletePerformanceRecords({ deleteUpToDate });
+ // Ok?
+ if (result.ok === 1) {
+ await Logging.logSecurityInfo({
+ tenantID: tenantID,
+ action: ServerAction.PERFORMANCES_CLEANUP,
+ module: MODULE_NAME, method: 'deletePerformanceRecords',
+ message: `${result.n} Performance Record(s) have been deleted successfully before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`
+ });
+ } else {
+ await Logging.logError({
+ tenantID: tenantID,
+ action: ServerAction.PERFORMANCES_CLEANUP,
+ module: MODULE_NAME, method: 'deletePerformanceRecords',
+ message: `An error occurred when deleting Performance Record(s) before '${moment(deleteUpToDate).format('DD/MM/YYYY h:mm A')}'`,
+ detailedMessages: { result }
+ });
+ }
} catch (error) {
// Log error
- Logging.logActionExceptionMessage(tenantID, ServerAction.LOGS_CLEANUP, error);
+ await Logging.logActionExceptionMessage(tenantID, ServerAction.PERFORMANCES_CLEANUP, error);
} finally {
// Release the lock
- await LockingManager.release(logsCleanUpLock);
+ await LockingManager.release(performanceCleanUpLock);
}
}
}

MODIFY
src/storage/mongodb/LoggingStorage.ts
src/storage/mongodb/LoggingStorage.ts
@@ -8,8 +8,6 @@ import { Log } from '../../types/Log';
import Utils from '../../utils/Utils';
import cluster from 'cluster';
-const MODULE_NAME = 'LoggingStorage';
-
export default class LoggingStorage {
public static async deleteLogs(tenantID: string, deleteUpToDate: Date): Promise<{ ok?: number; n?: number; }> {
// Check Tenant

MODIFY
src/storage/mongodb/PerformanceStorage.ts
src/storage/mongodb/PerformanceStorage.ts
+import global, { FilterParams } from '../../types/GlobalType';
+
import Constants from '../../utils/Constants';
import PerformanceRecord from '../../types/Performance';
import Utils from '../../utils/Utils';
-import global from '../../types/GlobalType';
export default class PerformanceStorage {
public static async savePerformanceRecord(performanceRecord: PerformanceRecord): Promise<void> {
@@ -52,4 +53,19 @@ export default class PerformanceStorage {
await global.database.getCollection<any>(Constants.DEFAULT_TENANT, 'performances')
.insertOne(performanceRecordMDB);
}
+
+ public static async deletePerformanceRecords(params?: { deleteUpToDate: Date }): Promise<{ ok?: number; n?: number; }> {
+ // Build filter
+ const filters: FilterParams = {};
+ // Date provided?
+ if (params?.deleteUpToDate) {
+ filters.timestamp = {};
+ filters.timestamp.$lte = Utils.convertToDate(params.deleteUpToDate);
+ }
+ // Delete
+ const result = await global.database.getCollection<PerformanceRecord>(Constants.DEFAULT_TENANT, 'performances')
+ .deleteMany(filters);
+ // Return the result
+ return result.result;
+ }
}

MODIFY
src/types/Locking.ts
src/types/Locking.ts
@@ -20,6 +20,7 @@ export enum LockEntity {
SITE_AREA = 'site-area',
USER = 'user',
LOGGING = 'logging',
+ PERFORMANCE = 'performance',
TRANSACTION = 'transaction',
CAR = 'car',
INVOICE = 'invoice',

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -88,6 +88,7 @@ export enum ServerAction {
OFFLINE_CHARGING_STATION = 'OfflineChargingStation',
LOGS_CLEANUP = 'LogsCleanup',
+ PERFORMANCES_CLEANUP = 'PerformancesCleanup',
SCHEDULER = 'Scheduler',

Added performance records cleanup aligned with logs