mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
Implement month-based date formatting and filtering in PowerClamp service
This commit is contained in:
@ -12,3 +12,27 @@ export function toDDMMYYYY(dateString?: string | null): string | null {
|
||||
const [year, month, day] = dateString.split('-');
|
||||
return `${day}-${month}-${year}`;
|
||||
}
|
||||
export function toMMYYYY(dateString?: string | null): string | null {
|
||||
if (!dateString) return null;
|
||||
|
||||
// Ensure dateString is valid format YYYY-MM
|
||||
const regex = /^\d{4}-\d{2}$/;
|
||||
if (!regex.test(dateString)) {
|
||||
throw new Error(
|
||||
`Invalid date format: ${dateString}. Expected format is YYYY-MM`,
|
||||
);
|
||||
}
|
||||
|
||||
const [year, month] = dateString.split('-');
|
||||
return `${month}-${year}`;
|
||||
}
|
||||
export function filterByMonth(data: any[], monthDate: string) {
|
||||
const [year, month] = monthDate.split('-').map(Number);
|
||||
|
||||
return data.filter((item) => {
|
||||
const itemDate = new Date(item.date);
|
||||
return (
|
||||
itemDate.getUTCFullYear() === year && itemDate.getUTCMonth() + 1 === month
|
||||
);
|
||||
});
|
||||
}
|
||||
|
@ -35,19 +35,11 @@ export class GetPowerClampDto {
|
||||
}
|
||||
export class GetPowerClampBySpaceDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'Input date in ISO format (YYYY-MM-DD) to filter the data',
|
||||
example: '2025-04-23',
|
||||
description: 'monthDate must be in YYYY-MM format',
|
||||
example: '2025-04',
|
||||
required: true,
|
||||
})
|
||||
@IsDateString()
|
||||
@IsNotEmpty()
|
||||
public startDate: string;
|
||||
@ApiPropertyOptional({
|
||||
description: 'Input date in ISO format (YYYY-MM-DD) to filter the data',
|
||||
example: '2025-05-23',
|
||||
required: true,
|
||||
})
|
||||
@IsDateString()
|
||||
@IsNotEmpty()
|
||||
public endDate: string;
|
||||
public monthDate: string;
|
||||
}
|
||||
|
@ -18,7 +18,8 @@ import { SpaceDeviceService } from 'src/space/services';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { toDDMMYYYY } from '@app/common/helper/date-format';
|
||||
import { filterByMonth, toMMYYYY } from '@app/common/helper/date-format';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class PowerClampService {
|
||||
@ -36,25 +37,40 @@ export class PowerClampService {
|
||||
params: SpaceParamsDto,
|
||||
query: GetPowerClampBySpaceDto,
|
||||
) {
|
||||
const { startDate, endDate } = query;
|
||||
const { monthDate } = query;
|
||||
const { spaceUuid } = params;
|
||||
|
||||
try {
|
||||
const devices =
|
||||
await this.spaceDeviceService.getAllDevicesBySpace(spaceUuid);
|
||||
console.log('devices', devices);
|
||||
|
||||
const deviceUuids = devices.map((device) => device.uuid).join(',');
|
||||
const deviceUuids = devices
|
||||
.filter((device) => device.productDevice.prodType === ProductType.PC)
|
||||
.map((device) => device.uuid)
|
||||
.join(',');
|
||||
console.log('deviceUuids', deviceUuids);
|
||||
|
||||
const formattedMonthDate = toMMYYYY(monthDate);
|
||||
|
||||
const formattedStartDate = toDDMMYYYY(startDate);
|
||||
const formattedEndDate = toDDMMYYYY(endDate);
|
||||
const data = await this.executeProcedure(
|
||||
'fact_monthly_space_energy_consumed_procedure',
|
||||
[formattedStartDate, formattedEndDate, deviceUuids],
|
||||
'fact_daily_space_energy_consumed_procedure',
|
||||
[formattedMonthDate, deviceUuids],
|
||||
);
|
||||
|
||||
// Format date to YYYY-MM-DD
|
||||
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 this.buildResponse(
|
||||
`Power clamp data for space ${spaceUuid} fetched successfully`,
|
||||
data,
|
||||
filteredData,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
|
@ -133,7 +133,7 @@ export class SpaceDeviceService {
|
||||
async getAllDevicesBySpace(spaceUuid: string): Promise<DeviceEntity[]> {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['children', 'devices'],
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
if (!space) {
|
||||
@ -146,7 +146,7 @@ export class SpaceDeviceService {
|
||||
const fetchChildren = async (parentSpace: SpaceEntity) => {
|
||||
const children = await this.spaceRepository.find({
|
||||
where: { parent: { uuid: parentSpace.uuid } },
|
||||
relations: ['children', 'devices'],
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
for (const child of children) {
|
||||
|
Reference in New Issue
Block a user