Converting changes to facet based querying
MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
import { Action, Entity } from '../../../../types/Authorization';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
-import SiteArea, { SiteAreaValueTypes } from '../../../../types/SiteArea';
+import SiteArea, { SiteAreaValueTypes, SiteAreaValues } from '../../../../types/SiteArea';
import { ActionsResponse } from '../../../../types/GlobalType';
import AppAuthError from '../../../../exception/AppAuthError';
@@ -9,6 +9,7 @@ import AppError from '../../../../exception/AppError';
import AuthorizationService from './AuthorizationService';
import { ChargingProfilePurposeType } from '../../../../types/ChargingProfile';
import Constants from '../../../../utils/Constants';
+import Consumption from '../../../../types/Consumption';
import ConsumptionStorage from '../../../../storage/mongodb/ConsumptionStorage';
import LockingHelper from '../../../../locking/LockingHelper';
import LockingManager from '../../../../locking/LockingManager';
@@ -255,27 +256,13 @@ export default class SiteAreaService {
// Check and Get Site Area
const siteArea = await UtilsService.checkAndGetSiteAreaAuthorization(
req.tenant, req.user, filteredRequest.SiteAreaID, Action.READ, action);
- // Get the Asset Consumption Values
- siteArea.values.assetConsumptions = await ConsumptionStorage.getSiteAreaConsumptions(req.user.tenantID, {
+ // Store result in temporary object
+ const siteAreaDetailedData = await ConsumptionStorage.getSiteAreaConsumptions(req.user.tenantID, {
siteAreaID: filteredRequest.SiteAreaID,
startDate: filteredRequest.StartDate,
endDate: filteredRequest.EndDate,
- type: SiteAreaValueTypes.ASSET_CONSUMPTIONS
- }, [ 'startedAt', 'instantAmps', 'instantWatts', 'limitAmps', 'limitWatts' ]);
- // Get the Charging Station Consumption Values
- siteArea.values.chargingStationConsumptions = await ConsumptionStorage.getSiteAreaConsumptions(req.user.tenantID, {
- siteAreaID: filteredRequest.SiteAreaID,
- startDate: filteredRequest.StartDate,
- endDate: filteredRequest.EndDate,
- type: SiteAreaValueTypes.CHARGING_STATION_CONSUMPTIONS
- }, [ 'startedAt', 'instantAmps', 'instantWatts', 'limitAmps', 'limitWatts' ]);
- // Get the Asset production values
- siteArea.values.assetProductions = await ConsumptionStorage.getSiteAreaConsumptions(req.user.tenantID, {
- siteAreaID: filteredRequest.SiteAreaID,
- startDate: filteredRequest.StartDate,
- endDate: filteredRequest.EndDate,
- type: SiteAreaValueTypes.ASSET_PRODUCTIONS
- }, [ 'startedAt', 'instantAmps', 'instantWatts', 'limitAmps', 'limitWatts' ]);
+ });
+ siteArea.values = siteAreaDetailedData;
// Return
res.json(siteArea);
next();

MODIFY
src/storage/mongodb/ConsumptionStorage.ts
src/storage/mongodb/ConsumptionStorage.ts
@@ -7,7 +7,7 @@ import { DataResult } from '../../types/DataResult';
import DatabaseUtils from './DatabaseUtils';
import DbParams from '../../types/database/DbParams';
import Logging from '../../utils/Logging';
-import { SiteAreaValueTypes } from '../../types/SiteArea';
+import { SiteAreaValues } from '../../types/SiteArea';
import Tenant from '../../types/Tenant';
import Utils from '../../utils/Utils';
@@ -184,12 +184,37 @@ export default class ConsumptionStorage {
}
static async getSiteAreaConsumptions(tenantID: string,
- params: { siteAreaID: string; startDate: Date; endDate: Date, type: SiteAreaValueTypes },
- projectFields?: string[]): Promise<Consumption[]> {
+ params: { siteAreaID: string; startDate: Date; endDate: Date},
+ projectFields?: string[]): Promise<SiteAreaValues> {
// Debug
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getSiteAreaConsumptions');
// Check
await DatabaseUtils.checkTenant(tenantID);
+ const facets = {};
+ // Specific filters for each type of data
+ const filterTypes = [
+ {
+ name: 'assetConsumptions',
+ filters: { instantWatts : { '$gte': 0 }, assetID : { '$ne': null } },
+ projectFields: [ 'startedAt', 'instantAmps', 'instantWatts' ]
+ },
+ // {
+ // name: 'assetProductions',
+ // filters: { instantWatts : { '$lt': 0 }, assetID : { '$ne': null } },
+ // projectFields: [ 'startedAt', 'instantAmps', 'instantWatts' ]
+ // },
+ // {
+ // name: 'chargingStationConsumptions',
+ // filters: { chargeBoxID : { '$ne': null } },
+ // projectFields: [ 'startedAt', 'instantAmps', 'instantWatts' ]
+ // },
+ // {
+ // name: 'netConsumptions',
+ // filters: {},
+ // projectFields: ['startedAt', 'instantAmps', 'instantWatts', 'limitAmps', 'limitWatts']
+ // }
+ ];
+ for (const filterValue of filterTypes) {
// Create filters
const filters: FilterParams = {};
// ID
@@ -209,16 +234,7 @@ export default class ConsumptionStorage {
filters.startedAt.$lte = Utils.convertToDate(params.endDate);
}
// Type of query
- if (params.type && params.type === SiteAreaValueTypes.ASSET_CONSUMPTIONS) {
- filters.instantWatts.$gte = 0;
- filters.assetID.$ne = null;
- } else if (params.type && params.type === SiteAreaValueTypes.ASSET_PRODUCTIONS) {
- filters.instantWatts.$lt = 0;
- filters.assetID.$ne = null;
- } else if (params.type && params.type === SiteAreaValueTypes.CHARGING_STATION_CONSUMPTIONS) {
- // filters.instantWatts.$gte = 0;
- filters.chargeBoxID.$ne = null;
- }
+ Object.assign(filters, filterValue.filters);
// Create Aggregation
const aggregation = [];
// Filters
@@ -267,14 +283,26 @@ export default class ConsumptionStorage {
}
});
// Project
- DatabaseUtils.projectFields(aggregation, projectFields, ['_id']);
+ DatabaseUtils.projectFields(aggregation, filterValue.projectFields, ['_id']);
+ // Add projection as field in facet
+ facets[filterValue.name] = aggregation;
+ }
+
// Read DB
const consumptionsMDB = await global.database.getCollection<Consumption>(tenantID, 'consumptions')
- .aggregate(...aggregation, { allowDiskUse: true })
+ .aggregate([{ $facet: facets }], { allowDiskUse: true })
.toArray();
+
+ const siteAreaData: SiteAreaValues = {
+ assetConsumptions: consumptionsMDB[0]['assetConsumptions'],
+ assetProductions: consumptionsMDB[0]['assetProductions'],
+ chargingStationConsumptions: consumptionsMDB[0]['chargingStationConsumptions'],
+ netConsumptions: consumptionsMDB[0]['netConsumptions']
+ };
+
// Debug
await Logging.traceEnd(tenantID, MODULE_NAME, 'getSiteAreaConsumptions', uniqueTimerID, consumptionsMDB);
- return consumptionsMDB;
+ return siteAreaData;
}
static async getTransactionConsumptions(tenantID: string, params: { transactionId: number },

MODIFY
src/types/SiteArea.ts
src/types/SiteArea.ts
@@ -17,6 +17,7 @@ export interface SiteAreaValues {
assetConsumptions: Consumption[];
assetProductions: Consumption[];
chargingStationConsumptions: Consumption[];
+ netConsumptions: Consumption[];
}
export default interface SiteArea extends CreatedUpdatedProps, SiteAreaAuthorizationActions {

Converting changes to facet based querying