Change tenant id to object for statistics storage
MODIFY
src/server/rest/v1/service/StatisticService.ts
src/server/rest/v1/service/StatisticService.ts
@@ -39,7 +39,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getChargingStationStats(
- req.user.tenantID, filter, StatsGroupBy.CONSUMPTION);
+ req.tenant, filter, StatsGroupBy.CONSUMPTION);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.CHARGING_STATION);
@@ -69,7 +69,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getChargingStationStats(
- req.user.tenantID, filter, StatsGroupBy.USAGE);
+ req.tenant, filter, StatsGroupBy.USAGE);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.CHARGING_STATION);
@@ -99,7 +99,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getChargingStationStats(
- req.user.tenantID, filter, StatsGroupBy.INACTIVITY);
+ req.tenant, filter, StatsGroupBy.INACTIVITY);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.CHARGING_STATION);
@@ -129,7 +129,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getChargingStationStats(
- req.user.tenantID, filter, StatsGroupBy.TRANSACTIONS);
+ req.tenant, filter, StatsGroupBy.TRANSACTIONS);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.CHARGING_STATION);
@@ -159,7 +159,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getChargingStationStats(
- req.user.tenantID, filter, StatsGroupBy.PRICING);
+ req.tenant, filter, StatsGroupBy.PRICING);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.CHARGING_STATION);
@@ -189,7 +189,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getUserStats(
- req.user.tenantID, filter, StatsGroupBy.CONSUMPTION);
+ req.tenant, filter, StatsGroupBy.CONSUMPTION);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.USER);
@@ -219,7 +219,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getUserStats(
- req.user.tenantID, filter, StatsGroupBy.USAGE);
+ req.tenant, filter, StatsGroupBy.USAGE);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.USER);
@@ -249,7 +249,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getUserStats(
- req.user.tenantID, filter, StatsGroupBy.INACTIVITY);
+ req.tenant, filter, StatsGroupBy.INACTIVITY);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.USER);
@@ -279,7 +279,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getUserStats(
- req.user.tenantID, filter, StatsGroupBy.TRANSACTIONS);
+ req.tenant, filter, StatsGroupBy.TRANSACTIONS);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.USER);
@@ -309,7 +309,7 @@ export default class StatisticService {
const filter = StatisticService.buildFilter(filteredRequest, req.user);
// Get Stats
const transactionStats = await StatisticsStorage.getUserStats(
- req.user.tenantID, filter, StatsGroupBy.PRICING);
+ req.tenant, filter, StatsGroupBy.PRICING);
// Convert
const transactions = StatisticService.convertToGraphData(
transactionStats, StatsDataCategory.USER);
@@ -361,9 +361,9 @@ export default class StatisticService {
// Query data
let transactionStats: ChargingStationStats[] | UserStats[];
if (filteredRequest.DataCategory === StatsDataCategory.CHARGING_STATION) {
- transactionStats = await StatisticsStorage.getChargingStationStats(req.user.tenantID, filter, groupBy);
+ transactionStats = await StatisticsStorage.getChargingStationStats(req.tenant, filter, groupBy);
} else {
- transactionStats = await StatisticsStorage.getUserStats(req.user.tenantID, filter, groupBy);
+ transactionStats = await StatisticsStorage.getUserStats(req.tenant, filter, groupBy);
}
// Set the attachement name
res.attachment('exported-' + filteredRequest.DataType.toLowerCase() + '-statistics.csv');

MODIFY
src/storage/mongodb/StatisticsStorage.ts
src/storage/mongodb/StatisticsStorage.ts
@@ -2,6 +2,7 @@ import StatisticFilter, { ChargingStationStats, StatsGroupBy, UserStats } from '
import DatabaseUtils from './DatabaseUtils';
import Logging from '../../utils/Logging';
+import Tenant from '../../types/Tenant';
import Utils from '../../utils/Utils';
import global from '../../types/GlobalType';
@@ -9,11 +10,11 @@ const MODULE_NAME = 'StatisticsStorage';
export default class StatisticsStorage {
- static async getChargingStationStats(tenantID: string, params: StatisticFilter, groupBy: string): Promise<ChargingStationStats[]> {
+ static async getChargingStationStats(tenant: Tenant, params: StatisticFilter, groupBy: string): Promise<ChargingStationStats[]> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getChargingStationStats');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getChargingStationStats');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Build filter
const filters: any = {};
// Date provided?
@@ -122,19 +123,19 @@ export default class StatisticsStorage {
$sort: { 'month': 1, 'unit': 1, 'chargeBox': 1 }
});
// Read DB
- const chargingStationStatsMDB = await global.database.getCollection<any>(tenantID, 'transactions')
+ const chargingStationStatsMDB = await global.database.getCollection<any>(tenant.id, 'transactions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getChargingStationStats', uniqueTimerID, chargingStationStatsMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getChargingStationStats', uniqueTimerID, chargingStationStatsMDB);
return chargingStationStatsMDB;
}
- static async getUserStats(tenantID: string, params: StatisticFilter, groupBy: string): Promise<UserStats[]> {
+ static async getUserStats(tenant: Tenant, params: StatisticFilter, groupBy: string): Promise<UserStats[]> {
// Debug
- const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'getUserStats');
+ const uniqueTimerID = Logging.traceStart(tenant.id, MODULE_NAME, 'getUserStats');
// Check Tenant
- await DatabaseUtils.checkTenant(tenantID);
+ DatabaseUtils.checkTenantObject(tenant);
// Build filter
const filters: any = {};
// Date provided?
@@ -234,7 +235,7 @@ export default class StatisticsStorage {
}
// Lookup for users
DatabaseUtils.pushUserLookupInAggregation({
- tenantID, aggregation, localField: '_id.userID', foreignField: '_id',
+ tenantID: tenant.id, aggregation, localField: '_id.userID', foreignField: '_id',
asField: 'user', oneToOneCardinality: true, oneToOneCardinalityNotNull: true
}, [ { $project: { _id: 1, name: 1, firstName: 1 } } ]);
// Single Record
@@ -252,11 +253,11 @@ export default class StatisticsStorage {
$sort: { 'month': 1, 'unit': 1, 'userID': 1 } // Instead of chargeBox userID ?
});
// Read DB
- const userStatsMDB = await global.database.getCollection<any>(tenantID, 'transactions')
+ const userStatsMDB = await global.database.getCollection<any>(tenant.id, 'transactions')
.aggregate(aggregation, { allowDiskUse: true })
.toArray();
// Debug
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getUserStats', uniqueTimerID, userStatsMDB);
+ await Logging.traceEnd(tenant.id, MODULE_NAME, 'getUserStats', uniqueTimerID, userStatsMDB);
return userStatsMDB;
}
}

Change tenant id to object for statistics storage