mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +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('-');
|
const [year, month, day] = dateString.split('-');
|
||||||
return `${day}-${month}-${year}`;
|
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 {
|
export class GetPowerClampBySpaceDto {
|
||||||
@ApiPropertyOptional({
|
@ApiPropertyOptional({
|
||||||
description: 'Input date in ISO format (YYYY-MM-DD) to filter the data',
|
description: 'monthDate must be in YYYY-MM format',
|
||||||
example: '2025-04-23',
|
example: '2025-04',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsDateString()
|
@IsDateString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public startDate: string;
|
public monthDate: 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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,8 @@ import { SpaceDeviceService } from 'src/space/services';
|
|||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
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()
|
@Injectable()
|
||||||
export class PowerClampService {
|
export class PowerClampService {
|
||||||
@ -36,25 +37,40 @@ export class PowerClampService {
|
|||||||
params: SpaceParamsDto,
|
params: SpaceParamsDto,
|
||||||
query: GetPowerClampBySpaceDto,
|
query: GetPowerClampBySpaceDto,
|
||||||
) {
|
) {
|
||||||
const { startDate, endDate } = query;
|
const { monthDate } = query;
|
||||||
const { spaceUuid } = params;
|
const { spaceUuid } = params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const devices =
|
const devices =
|
||||||
await this.spaceDeviceService.getAllDevicesBySpace(spaceUuid);
|
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(
|
const data = await this.executeProcedure(
|
||||||
'fact_monthly_space_energy_consumed_procedure',
|
'fact_daily_space_energy_consumed_procedure',
|
||||||
[formattedStartDate, formattedEndDate, deviceUuids],
|
[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(
|
return this.buildResponse(
|
||||||
`Power clamp data for space ${spaceUuid} fetched successfully`,
|
`Power clamp data for space ${spaceUuid} fetched successfully`,
|
||||||
data,
|
filteredData,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
|||||||
@ -133,7 +133,7 @@ export class SpaceDeviceService {
|
|||||||
async getAllDevicesBySpace(spaceUuid: string): Promise<DeviceEntity[]> {
|
async getAllDevicesBySpace(spaceUuid: string): Promise<DeviceEntity[]> {
|
||||||
const space = await this.spaceRepository.findOne({
|
const space = await this.spaceRepository.findOne({
|
||||||
where: { uuid: spaceUuid },
|
where: { uuid: spaceUuid },
|
||||||
relations: ['children', 'devices'],
|
relations: ['children', 'devices', 'devices.productDevice'],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!space) {
|
if (!space) {
|
||||||
@ -146,7 +146,7 @@ export class SpaceDeviceService {
|
|||||||
const fetchChildren = async (parentSpace: SpaceEntity) => {
|
const fetchChildren = async (parentSpace: SpaceEntity) => {
|
||||||
const children = await this.spaceRepository.find({
|
const children = await this.spaceRepository.find({
|
||||||
where: { parent: { uuid: parentSpace.uuid } },
|
where: { parent: { uuid: parentSpace.uuid } },
|
||||||
relations: ['children', 'devices'],
|
relations: ['children', 'devices', 'devices.productDevice'],
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const child of children) {
|
for (const child of children) {
|
||||||
|
|||||||
Reference in New Issue
Block a user