added site area id and site id to soncumption
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -9,6 +9,7 @@ import AddLastChangePropertiesToBadgeTask from './tasks/AddLastChangePropertiesT
import AddLastChangedOnToCarCatalogTask from './tasks/AddLastChangedOnToCarCatalogTask';
import AddNotificationsFlagsToUsersTask from './tasks/AddNotificationsFlagsToUsersTask';
import AddSensitiveDataInSettingsTask from './tasks/AddSensitiveDataInSettingsTask';
+import AddSiteAreaIDToAssetConsumptionTask from './tasks/AddSiteAreaIDToAssetConsumptionTask';
import AddSiteAreaLimitToConsumptionsTask from './tasks/AddSiteAreaLimitToConsumptionsTask';
import AddTagTypeTask from './tasks/AddTagTypeTask';
import AddTransactionRefundStatusTask from './tasks/AddTransactionRefundStatusTask';
@@ -64,7 +65,7 @@ export default class MigrationHandler {
const startMigrationTime = moment();
const currentMigrationTasks: MigrationTask[] = [];
// Log
- Logging.logInfo({
+ await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.MIGRATION,
module: MODULE_NAME, method: 'migrate',
@@ -113,6 +114,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new MigrateUserSettingsTask());
currentMigrationTasks.push(new RenameSMTPAuthErrorTask());
currentMigrationTasks.push(new ResetCarCatalogsHashTask());
+ currentMigrationTasks.push(new AddSiteAreaIDToAssetConsumptionTask());
// Get the already done migrations from the DB
const migrationTasksDone = await MigrationStorage.getMigrations();
// Check
@@ -138,14 +140,14 @@ export default class MigrationHandler {
}
// Log Total Processing Time
const totalMigrationTimeSecs = moment.duration(moment().diff(startMigrationTime)).asSeconds();
- Logging.logInfo({
+ await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.MIGRATION,
module: MODULE_NAME, method: 'migrate',
message: `The ${processAsyncTasksOnly ? 'asynchronous' : 'synchronous'} migration has been run in ${totalMigrationTimeSecs} secs`
});
} catch (error) {
- Logging.logError({
+ await Logging.logError({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.MIGRATION,
module: MODULE_NAME, method: 'migrate',
@@ -169,7 +171,7 @@ export default class MigrationHandler {
try {
// Log Start Task
let logMsg = `${currentMigrationTask.isAsynchronous() ? 'Asynchronous' : 'Synchronous'} Migration Task '${currentMigrationTask.getName()}' Version '${currentMigrationTask.getVersion()}' is running ${cluster.isWorker ? 'in worker ' + cluster.worker.id.toString() : 'in master'}...`;
- Logging.logInfo({
+ await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.MIGRATION,
module: MODULE_NAME, method: 'executeTask',
@@ -193,7 +195,7 @@ export default class MigrationHandler {
durationSecs: totalTaskTimeSecs
});
logMsg = `${currentMigrationTask.isAsynchronous() ? 'Asynchronous' : 'Synchronous'} Migration Task '${currentMigrationTask.getName()}' Version '${currentMigrationTask.getVersion()}' has run with success in ${totalTaskTimeSecs} secs ${cluster.isWorker ? 'in worker ' + cluster.worker.id.toString() : 'in master'}`;
- Logging.logInfo({
+ await Logging.logInfo({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.MIGRATION,
module: MODULE_NAME, method: 'executeTask',
@@ -203,7 +205,7 @@ export default class MigrationHandler {
console.log(logMsg);
} catch (error) {
const logMsg = `${currentMigrationTask.isAsynchronous() ? 'Asynchronous' : 'Synchronous'} Migration Task '${currentMigrationTask.getName()}' Version '${currentMigrationTask.getVersion()}' has failed with error: ${error.toString()} ${cluster.isWorker ? 'in worker ' + cluster.worker.id.toString() : 'in master'}`;
- Logging.logError({
+ await Logging.logError({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.MIGRATION,
module: MODULE_NAME, method: 'executeTask',

ADD
src/migration/tasks/AddSiteAreaIDToAssetConsumptionTask.ts
+import AssetStorage from '../../storage/mongodb/AssetStorage';
+import Constants from '../../utils/Constants';
+import Logging from '../../utils/Logging';
+import MigrationTask from '../MigrationTask';
+import { ServerAction } from '../../types/Server';
+import Tenant from '../../types/Tenant';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'AddSiteAreaIDToAssetConsumptionTask';
+
+export default class AddSiteAreaIDToAssetConsumptionTask extends MigrationTask {
+ async migrate(): Promise<void> {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ async migrateTenant(tenant: Tenant): Promise<void> {
+ let modifiedCount = 0;
+ // Get Assets
+ const assets = await AssetStorage.getAssets(tenant.id, { withSiteArea: true }, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const asset of assets.result) {
+
+ const result = await global.database.getCollection(tenant.id, 'consumptions').updateMany(
+ {
+ assetID: Utils.convertToObjectID(asset.id),
+ },
+ {
+ $set: {
+ siteAreaID: Utils.convertToObjectID(asset.siteAreaID),
+ siteID: Utils.convertToObjectID(asset.siteArea.siteID),
+ }
+ }
+ );
+ modifiedCount += result.modifiedCount;
+
+ }
+ // Log in the default tenant
+ if (modifiedCount > 0) {
+ await Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrateTenant',
+ message: `${modifiedCount} Consumptions have been updated in Tenant '${tenant.name}'`
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'AddSiteAreaIDToAssetConsumptionTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}

MODIFY
src/scheduler/tasks/AssetGetConsumptionTask.ts
src/scheduler/tasks/AssetGetConsumptionTask.ts
@@ -55,6 +55,8 @@ export default class AssetGetConsumptionTask extends SchedulerTask {
startedAt: asset.lastConsumption?.timestamp ? asset.lastConsumption.timestamp : moment(consumption.lastConsumption.timestamp).subtract(1, 'minutes').toDate(),
endedAt: consumption.lastConsumption.timestamp,
assetID: asset.id,
+ siteAreaID: asset.siteAreaID,
+ siteID: asset.siteArea.siteID,
cumulatedConsumptionWh: consumption.currentConsumptionWh,
cumulatedConsumptionAmps: Math.floor(consumption.currentConsumptionWh / asset.siteArea.voltage),
instantAmps: consumption.currentInstantAmps,

added site area id and site id to soncumption