Migration of consumption limit amps
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -2,7 +2,6 @@ import AddActivePropertyToTagsTask from './tasks/AddActivePropertyToTagsTask';
import AddInactivityStatusInTransactionsTask from './tasks/AddInactivityStatusInTransactionsTask';
import AddIssuerFieldTask from './tasks/AddIssuerFieldTask';
import AddLastChangePropertiesToBadgeTask from './tasks/AddLastChangePropertiesToBadgeTask';
-import AddLimitToConsumptionsTask from './tasks/AddLimitToConsumptionsTask';
import AddNotificationsFlagsToUsersTask from './tasks/AddNotificationsFlagsToUsersTask';
import AddSensitiveDataInSettingsTask from './tasks/AddSensitiveDataInSettingsTask';
import AddSiteAreaLimitToConsumptionsTask from './tasks/AddSiteAreaLimitToConsumptionsTask';
@@ -26,6 +25,7 @@ import SiteUsersHashIDsTask from './tasks/SiteUsersHashIDsTask';
import UpdateChargingStationStaticLimitationTask from './tasks/UpdateChargingStationStaticLimitationTask';
import UpdateChargingStationTemplatesTask from './tasks/UpdateChargingStationTemplatesTask';
import UpdateConsumptionsToObjectIDs from './tasks/UpdateConsumptionsToObjectIDs';
+import UpdateLimitsInConsumptionsTask from './tasks/UpdateLimitsInConsumptionsTask';
import cluster from 'cluster';
import moment from 'moment';
@@ -62,17 +62,17 @@ export default class MigrationHandler {
currentMigrationTasks.push(new CleanupMeterValuesTask());
currentMigrationTasks.push(new RenameTagPropertiesTask());
currentMigrationTasks.push(new AddInactivityStatusInTransactionsTask());
- currentMigrationTasks.push(new UpdateChargingStationTemplatesTask());
currentMigrationTasks.push(new AddIssuerFieldTask());
currentMigrationTasks.push(new CleanupOrphanBadgeTask());
currentMigrationTasks.push(new AddLastChangePropertiesToBadgeTask());
- currentMigrationTasks.push(new AddLimitToConsumptionsTask());
currentMigrationTasks.push(new AddActivePropertyToTagsTask());
currentMigrationTasks.push(new InitialCarImportTask());
currentMigrationTasks.push(new UpdateConsumptionsToObjectIDs());
currentMigrationTasks.push(new AddSiteAreaLimitToConsumptionsTask());
currentMigrationTasks.push(new MigrateOcpiTransactionsTask());
currentMigrationTasks.push(new UpdateChargingStationStaticLimitationTask());
+ currentMigrationTasks.push(new UpdateChargingStationTemplatesTask());
+ // CurrentMigrationTasks.push(new UpdateLimitsInConsumptionsTask());
// Get the already done migrations from the DB
const migrationTasksDone = await MigrationStorage.getMigrations();
// Check

DELETE
src/migration/tasks/AddLimitToConsumptionsTask.ts
-import ChargingStationStorage from '../../storage/mongodb/ChargingStationStorage';
-import { ConnectorCurrentLimitSource } from '../../types/ChargingStation';
-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 global from '../../types/GlobalType';
-
-const MODULE_NAME = 'AddLimitToConsumptionsTask';
-
-export default class AddLimitToConsumptionsTask extends MigrationTask {
- async migrate() {
- const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
- for (const tenant of tenants.result) {
- await this.migrateTenant(tenant);
- }
- }
-
- async migrateTenant(tenant: Tenant) {
- let modifiedCount = 0;
- // Get Charging Stations
- const chargingStations = await ChargingStationStorage.getChargingStations(tenant.id, {}, Constants.DB_PARAMS_MAX_LIMIT);
- for (const chargingStation of chargingStations.result) {
- for (const connector of chargingStation.connectors) {
- // Update
- const result = await global.database.getCollection(tenant.id, 'consumptions').updateMany(
- {
- chargeBoxID: chargingStation.id,
- connectorId: connector.connectorId,
- limitSource: { $exists: false },
- },
- {
- $set: {
- limitAmps: connector.amperage,
- limitWatts: connector.power,
- limitSource: ConnectorCurrentLimitSource.CONNECTOR
- }
- }
- );
- modifiedCount += result.modifiedCount;
- }
- }
- // Log in the default tenant
- if (modifiedCount > 0) {
- 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() {
- return '1.1';
- }
-
- getName() {
- return 'AddLimitToConsumptions';
- }
-
- isAsynchronous() {
- return true;
- }
-}

MODIFY
src/migration/tasks/UpdateChargingStationStaticLimitationTask.ts
src/migration/tasks/UpdateChargingStationStaticLimitationTask.ts
@@ -36,7 +36,7 @@ export default class UpdateChargingStationStaticLimitationTask extends Migration
let updated = 0;
// Get the charging stations
const chargingStations = await ChargingStationStorage.getChargingStations(tenant.id, {
- issuer: true
+ issuer: true, includeDeleted: true
}, Constants.DB_PARAMS_MAX_LIMIT);
// Update
for (const chargingStation of chargingStations.result) {

MODIFY
src/migration/tasks/UpdateChargingStationTemplatesTask.ts
src/migration/tasks/UpdateChargingStationTemplatesTask.ts
@@ -35,14 +35,14 @@ export default class UpdateChargingStationTemplatesTask extends MigrationTask {
}
getVersion() {
- return '1.899991';
+ return '1.8';
}
private async applyTemplateToChargingStations(tenant: Tenant) {
let updated = 0;
// Get the charging stations
const chargingStations = await ChargingStationStorage.getChargingStations(tenant.id, {
- issuer: true
+ issuer: true, includeDeleted: true
}, Constants.DB_PARAMS_MAX_LIMIT);
// Update
for (const chargingStation of chargingStations.result) {

ADD
src/migration/tasks/UpdateLimitsInConsumptionsTask.ts
+import ChargingStationStorage from '../../storage/mongodb/ChargingStationStorage';
+import { ConnectorCurrentLimitSource } from '../../types/ChargingStation';
+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 { UpdateWriteOpResult } from 'mongodb';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'UpdateLimitsInConsumptionsTask';
+
+export default class UpdateLimitsInConsumptionsTask extends MigrationTask {
+ async migrate() {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ async migrateTenant(tenant: Tenant) {
+ let updated = 0;
+ let result: UpdateWriteOpResult;
+ // Get Charging Stations
+ const chargingStations = await ChargingStationStorage.getChargingStations(tenant.id, {
+ issuer: true, includeDeleted: true
+ }, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const chargingStation of chargingStations.result) {
+ for (const connector of chargingStation.connectors) {
+ let limitWatts = 0, limitAmps = 0;
+ // Amps from chargepoint?
+ if (chargingStation.chargePoints) {
+ const chargePoint = Utils.getChargePointFromID(chargingStation, connector.connectorId);
+ limitAmps = Utils.getChargingStationAmperage(chargingStation, chargePoint, connector.connectorId);
+ // Amps from connector
+ } else if (connector.amperage) {
+ limitAmps = connector.amperage;
+ }
+ if (limitAmps) {
+ limitWatts = Utils.convertAmpToWatt(chargingStation, connector.connectorId, limitAmps);
+ // Update
+ console.log('====================================');
+ console.log(chargingStation.id);
+ console.log(connector.connectorId);
+ console.log({ limitWatts, limitAmps });
+ result = await global.database.getCollection(tenant.id, 'consumptions').updateMany(
+ {
+ chargeBoxID: chargingStation.id,
+ connectorId: connector.connectorId,
+ },
+ {
+ $set: {
+ limitAmps: limitAmps,
+ limitWatts: limitWatts,
+ limitSource: ConnectorCurrentLimitSource.CONNECTOR
+ }
+ }
+ );
+ console.log({ updated: result.modifiedCount });
+ console.log('====================================');
+ updated += result.modifiedCount;
+ }
+ }
+ }
+ // Update leftover
+ // 22170 -> 22080 W -> 96 A
+ result = await global.database.getCollection(tenant.id, 'consumptions').updateMany(
+ {
+ limitWatts: 22170,
+ },
+ {
+ $set: {
+ limitAmps: 96,
+ limitWatts: 22080,
+ limitSource: ConnectorCurrentLimitSource.CONNECTOR
+ }
+ }
+ );
+ updated += result.modifiedCount;
+ // Update leftover
+ // 7390 -> 7360 -> 32 A
+ result = await global.database.getCollection(tenant.id, 'consumptions').updateMany(
+ {
+ limitWatts: 7390,
+ },
+ {
+ $set: {
+ limitAmps: 32,
+ limitWatts: 7360,
+ limitSource: ConnectorCurrentLimitSource.CONNECTOR
+ }
+ }
+ );
+ updated += result.modifiedCount;
+ // Log
+ if (updated > 0) {
+ Logging.logDebug({
+ tenantID: tenant.id,
+ action: ServerAction.UPDATE_CHARGING_STATION_WITH_TEMPLATE,
+ module: MODULE_NAME, method: 'migrateTenant',
+ message: `${updated} Charging Stations amperage limit has been updated in Tenant '${tenant.name}'`
+ });
+ }
+ }
+
+ getVersion() {
+ return '1.0';
+ }
+
+ getName() {
+ return 'UpdateLimitsInConsumptionsTask';
+ }
+
+ isAsynchronous() {
+ return true;
+ }
+}

Migration of consumption limit amps