mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
feat: enhance PowerClamp service and controller to support filtering by date, month, and year
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
import { Controller, Get, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { PowerClampService } from '../services/power-clamp.service';
|
||||
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
|
||||
@ApiTags('Power Clamp Module')
|
||||
@Controller({
|
||||
@ -15,12 +17,18 @@ export class PowerClampController {
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get()
|
||||
@Get(':powerClampUuid')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_SUMMARY,
|
||||
description: ControllerRoute.PowerClamp.ACTIONS.GET_ENERGY_DESCRIPTION,
|
||||
})
|
||||
async getPowerClampData() {
|
||||
return await this.powerClampService.getPowerClampData();
|
||||
async getPowerClampData(
|
||||
@Param('powerClampUuid') powerClampUuid: string,
|
||||
@Query() params: GetPowerClampDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.powerClampService.getPowerClampData(
|
||||
powerClampUuid,
|
||||
params,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
35
src/power-clamp/dto/get-power-clamp.dto.ts
Normal file
35
src/power-clamp/dto/get-power-clamp.dto.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsDateString, Matches } from 'class-validator';
|
||||
|
||||
export class GetPowerClampDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'Input date in ISO format (YYYY-MM-DD) to filter the data',
|
||||
example: '2025-04-23',
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
dayDate?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Month and year in format YYYY-MM',
|
||||
example: '2025-03',
|
||||
required: false,
|
||||
})
|
||||
@Matches(/^\d{4}-(0[1-9]|1[0-2])$/, {
|
||||
message: 'monthDate must be in YYYY-MM format',
|
||||
})
|
||||
@IsOptional()
|
||||
monthDate?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Input year in YYYY format to filter the data',
|
||||
example: '2025',
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@Matches(/^\d{4}$/, {
|
||||
message: 'Year must be in YYYY format',
|
||||
})
|
||||
year?: string;
|
||||
}
|
@ -2,11 +2,20 @@ import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { PowerClampService } from './services/power-clamp.service';
|
||||
import { PowerClampController } from './controllers';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [PowerClampController],
|
||||
providers: [PowerClampService, SqlLoaderService],
|
||||
providers: [
|
||||
PowerClampService,
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
],
|
||||
exports: [PowerClampService],
|
||||
})
|
||||
export class PowerClampModule {}
|
||||
|
@ -1,30 +1,88 @@
|
||||
import { SQL_QUERIES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PowerClampService {
|
||||
constructor(
|
||||
private readonly sqlLoader: SqlLoaderService,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly powerClampDailyRepository: PowerClampDailyRepository,
|
||||
private readonly powerClampHourlyRepository: PowerClampHourlyRepository,
|
||||
private readonly powerClampMonthlyRepository: PowerClampMonthlyRepository,
|
||||
) {}
|
||||
|
||||
async getPowerClampData() {
|
||||
const sql = this.sqlLoader.loadQuery(
|
||||
'fact_daily_energy_consumed',
|
||||
'fact_daily_energy_consumed',
|
||||
SQL_QUERIES_PATH,
|
||||
);
|
||||
return this.dataSource.manager.query(sql);
|
||||
async getPowerClampData(powerClampUuid: string, params: GetPowerClampDto) {
|
||||
const { dayDate, monthDate, year } = params;
|
||||
|
||||
try {
|
||||
if (dayDate) {
|
||||
const data = await this.powerClampHourlyRepository
|
||||
.createQueryBuilder('hourly')
|
||||
.where('hourly.deviceUuid = :deviceUuid', {
|
||||
deviceUuid: powerClampUuid,
|
||||
})
|
||||
.andWhere('hourly.date = :date', { date: dayDate })
|
||||
.orderBy('CAST(hourly.hour AS INTEGER)', 'ASC')
|
||||
.getMany();
|
||||
|
||||
return this.buildResponse(
|
||||
`Power clamp data for day ${dayDate} fetched successfully`,
|
||||
data,
|
||||
);
|
||||
}
|
||||
|
||||
if (monthDate) {
|
||||
const data = await this.powerClampDailyRepository
|
||||
.createQueryBuilder('daily')
|
||||
.where('daily.deviceUuid = :deviceUuid', {
|
||||
deviceUuid: powerClampUuid,
|
||||
})
|
||||
.andWhere("TO_CHAR(daily.date, 'YYYY-MM') = :monthDate", {
|
||||
monthDate,
|
||||
})
|
||||
.orderBy('daily.date', 'ASC')
|
||||
.getMany();
|
||||
|
||||
return this.buildResponse(
|
||||
`Power clamp data for month ${monthDate} fetched successfully`,
|
||||
data,
|
||||
);
|
||||
}
|
||||
|
||||
if (year) {
|
||||
const data = await this.powerClampMonthlyRepository
|
||||
.createQueryBuilder('monthly')
|
||||
.where('monthly.deviceUuid = :deviceUuid', {
|
||||
deviceUuid: powerClampUuid,
|
||||
})
|
||||
.andWhere('RIGHT(monthly.month, 4) = :year', { year })
|
||||
.orderBy('monthly.month', 'ASC')
|
||||
.getMany();
|
||||
|
||||
return this.buildResponse(
|
||||
`Power clamp data for year ${year} fetched successfully`,
|
||||
data,
|
||||
);
|
||||
}
|
||||
|
||||
return this.buildResponse(`Power clamp data fetched successfully`, []);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching power clamp data',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getEnergyConsumed(code: string) {
|
||||
const sql = this.sqlLoader.loadQuery(
|
||||
'energy',
|
||||
'energy_consumed_with_params',
|
||||
SQL_QUERIES_PATH,
|
||||
);
|
||||
return this.dataSource.manager.query(sql, [code]);
|
||||
private buildResponse(message: string, data: any[]) {
|
||||
return new SuccessResponseDto({
|
||||
message,
|
||||
data,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user