mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-11 15:48:09 +00:00
feat: add groupByDevice option to GetPowerClampBySpaceDto and update service logic
This commit is contained in:
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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,25 +63,58 @@ 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);
|
||||||
|
|
||||||
|
if (groupByDevice) {
|
||||||
|
// Handle per-device response
|
||||||
|
const deviceDataPromises = powerClampDevices.map(async (device) => {
|
||||||
|
const data = await this.executeProcedure(
|
||||||
|
'fact_daily_space_energy_consumed_procedure',
|
||||||
|
[formattedMonthDate, device.uuid],
|
||||||
|
);
|
||||||
|
|
||||||
|
const formattedData = data.map((item) => ({
|
||||||
|
...item,
|
||||||
|
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||||
|
}));
|
||||||
|
|
||||||
|
const filteredData = monthDate
|
||||||
|
? filterByMonth(formattedData, monthDate)
|
||||||
|
: 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(
|
const data = await this.executeProcedure(
|
||||||
'fact_daily_space_energy_consumed_procedure',
|
'fact_daily_space_energy_consumed_procedure',
|
||||||
[formattedMonthDate, deviceUuids.join(',')],
|
[formattedMonthDate, deviceUuids.join(',')],
|
||||||
@ -101,6 +134,7 @@ export class PowerClampService {
|
|||||||
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
|
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
|
||||||
resultData,
|
resultData,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching power clamp data', {
|
console.error('Error fetching power clamp data', {
|
||||||
error,
|
error,
|
||||||
|
Reference in New Issue
Block a user