Merge pull request #369 from SyncrowIOT/SP-1552-be-implement-energy-consumption-include-all-device-api-for-analytics-dashboard

feat: add groupByDevice option to GetPowerClampBySpaceDto and update service logic
This commit is contained in:
faljawhary
2025-05-11 02:00:51 +03:00
committed by GitHub
2 changed files with 81 additions and 26 deletions

View File

@ -1,5 +1,13 @@
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
import { ApiPropertyOptional } from '@nestjs/swagger'; import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsOptional, IsDateString, Matches, IsNotEmpty } from 'class-validator'; import { Transform } from 'class-transformer';
import {
IsOptional,
IsDateString,
Matches,
IsNotEmpty,
IsBoolean,
} from 'class-validator';
export class GetPowerClampDto { export class GetPowerClampDto {
@ApiPropertyOptional({ @ApiPropertyOptional({
@ -42,4 +50,17 @@ export class GetPowerClampBySpaceDto {
@IsDateString() @IsDateString()
@IsNotEmpty() @IsNotEmpty()
public monthDate: string; public monthDate: string;
@ApiPropertyOptional({
example: true,
description: 'Whether to group results by device or not',
required: false,
default: false,
})
@IsOptional()
@IsBoolean()
@Transform((value) => {
return value.obj.groupByDevice === BooleanValues.TRUE;
})
public groupByDevice?: boolean = false;
} }

View File

@ -45,7 +45,7 @@ export class PowerClampService {
params: ResourceParamsDto, params: ResourceParamsDto,
query: GetPowerClampBySpaceDto, query: GetPowerClampBySpaceDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { monthDate } = query; const { monthDate, groupByDevice } = query;
const { spaceUuid, communityUuid } = params; const { spaceUuid, communityUuid } = params;
try { try {
@ -63,44 +63,78 @@ export class PowerClampService {
if (!devices?.length) { if (!devices?.length) {
return this.buildResponse( return this.buildResponse(
'No power clamp devices found for the specified criteria', `No devices found for ${spaceUuid ? 'space' : 'community'}`,
[], [],
); );
} }
// Filter and prepare device UUIDs // Filter power clamp devices
const deviceUuids = devices const powerClampDevices = devices.filter(
.filter((device) => device.productDevice?.prodType === ProductType.PC) (device) => device.productDevice?.prodType === ProductType.PC,
.map((device) => device.uuid); );
if (deviceUuids.length === 0) { if (powerClampDevices.length === 0) {
return this.buildResponse( return this.buildResponse(
'No power clamp devices (PC type) found for the specified criteria', `No power clamp devices found for ${spaceUuid ? 'space' : 'community'}`,
[], [],
); );
} }
// Execute procedure
const formattedMonthDate = toMMYYYY(monthDate); const formattedMonthDate = toMMYYYY(monthDate);
const data = await this.executeProcedure(
'fact_daily_space_energy_consumed_procedure',
[formattedMonthDate, deviceUuids.join(',')],
);
// Format and filter data if (groupByDevice) {
const formattedData = data.map((item) => ({ // Handle per-device response
...item, const deviceDataPromises = powerClampDevices.map(async (device) => {
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD const data = await this.executeProcedure(
})); 'fact_daily_space_energy_consumed_procedure',
[formattedMonthDate, device.uuid],
);
const resultData = monthDate const formattedData = data.map((item) => ({
? filterByMonth(formattedData, monthDate) ...item,
: formattedData; date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
}));
return this.buildResponse( const filteredData = monthDate
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`, ? filterByMonth(formattedData, monthDate)
resultData, : formattedData;
);
return {
deviceUuid: device.uuid,
deviceName: device.name || `Power Clamp Device`,
data: filteredData,
};
});
const deviceDataResults = await Promise.all(deviceDataPromises);
return this.buildResponse(
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
deviceDataResults,
);
} else {
// Original behavior - all devices together
const deviceUuids = powerClampDevices.map((device) => device.uuid);
const data = await this.executeProcedure(
'fact_daily_space_energy_consumed_procedure',
[formattedMonthDate, deviceUuids.join(',')],
);
// Format and filter data
const formattedData = data.map((item) => ({
...item,
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
}));
const resultData = monthDate
? filterByMonth(formattedData, monthDate)
: formattedData;
return this.buildResponse(
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
resultData,
);
}
} catch (error) { } catch (error) {
console.error('Error fetching power clamp data', { console.error('Error fetching power clamp data', {
error, error,