mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-08-26 09:09:39 +00:00
Compare commits
16 Commits
3f2c36d48b
...
main
Author | SHA1 | Date | |
---|---|---|---|
b9c4308d1c | |||
87c380ab6f | |||
212d0d1974 | |||
6d529ee0ae | |||
85687e7950 | |||
7e2c3136cf | |||
61348aa351 | |||
dea942f11e | |||
d62e620828 | |||
f0556813ac | |||
6d2252a403 | |||
8d265c9105 | |||
a4095c837b | |||
65d4a56135 | |||
fffa27b6ee | |||
12579fcd6e |
64
.github/workflows/pr-description.yml
vendored
Normal file
64
.github/workflows/pr-description.yml
vendored
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
name: 🤖 AI PR Description Commenter (100% Safe with jq)
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, edited]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
generate-description:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repo
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install GitHub CLI and jq
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install gh jq -y
|
||||||
|
|
||||||
|
- name: Fetch PR Commits
|
||||||
|
id: fetch_commits
|
||||||
|
run: |
|
||||||
|
COMMITS=$(gh pr view ${{ github.event.pull_request.number }} --json commits --jq '.commits[].message' | sed 's/^/- /')
|
||||||
|
echo "commits<<EOF" >> $GITHUB_ENV
|
||||||
|
echo "$COMMITS" >> $GITHUB_ENV
|
||||||
|
echo "EOF" >> $GITHUB_ENV
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
||||||
|
|
||||||
|
- name: Generate PR Description with OpenAI (Safe JSON with jq)
|
||||||
|
run: |
|
||||||
|
REQUEST_BODY=$(jq -n \
|
||||||
|
--arg model "gpt-4o" \
|
||||||
|
--arg content "Given the following commit messages:\n\n${commits}\n\nGenerate a clear and professional pull request description." \
|
||||||
|
'{
|
||||||
|
model: $model,
|
||||||
|
messages: [{ role: "user", content: $content }]
|
||||||
|
}'
|
||||||
|
)
|
||||||
|
|
||||||
|
RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
|
||||||
|
-H "Authorization: Bearer $OPENAI_API_KEY" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d "$REQUEST_BODY")
|
||||||
|
|
||||||
|
DESCRIPTION=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
|
||||||
|
|
||||||
|
echo "---------- OpenAI Raw Response ----------"
|
||||||
|
echo "$RESPONSE"
|
||||||
|
echo "---------- Extracted Description ----------"
|
||||||
|
echo "$DESCRIPTION"
|
||||||
|
|
||||||
|
echo "description<<EOF" >> $GITHUB_ENV
|
||||||
|
echo "$DESCRIPTION" >> $GITHUB_ENV
|
||||||
|
echo "EOF" >> $GITHUB_ENV
|
||||||
|
env:
|
||||||
|
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||||
|
commits: ${{ env.commits }}
|
||||||
|
|
||||||
|
- name: Post AI Generated Description as Comment
|
||||||
|
run: |
|
||||||
|
gh pr comment ${{ github.event.pull_request.number }} --body "${{ env.description }}"
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
@ -1,15 +1,13 @@
|
|||||||
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import {
|
import {
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { firebaseDataBase } from '../../firebase.config';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import {
|
import {
|
||||||
Database,
|
Database,
|
||||||
DataSnapshot,
|
DataSnapshot,
|
||||||
@ -17,7 +15,9 @@ import {
|
|||||||
ref,
|
ref,
|
||||||
runTransaction,
|
runTransaction,
|
||||||
} from 'firebase/database';
|
} from 'firebase/database';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
|
import { firebaseDataBase } from '../../firebase.config';
|
||||||
|
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceStatusFirebaseService {
|
export class DeviceStatusFirebaseService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
@ -79,35 +79,53 @@ export class DeviceStatusFirebaseService {
|
|||||||
device: any;
|
device: any;
|
||||||
}[],
|
}[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const allLogs = [];
|
|
||||||
|
|
||||||
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
||||||
|
|
||||||
|
const allLogs = [];
|
||||||
|
|
||||||
for (const item of batch) {
|
for (const item of batch) {
|
||||||
const device = item.device;
|
const device = item.device;
|
||||||
|
|
||||||
if (!device?.uuid) {
|
if (!device?.uuid) {
|
||||||
console.log(`⛔ Skipped unknown device: ${item.deviceTuyaUuid}`);
|
console.log(`⛔ Skipped unknown device: ${item.deviceTuyaUuid}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const logs = item.log.properties.map((property) =>
|
// Determine properties based on environment
|
||||||
|
const properties =
|
||||||
|
this.isDevEnv && Array.isArray(item.log?.properties)
|
||||||
|
? item.log.properties
|
||||||
|
: Array.isArray(item.status)
|
||||||
|
? item.status
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (!properties) {
|
||||||
|
console.log(
|
||||||
|
`⛔ Skipped invalid status/properties for device: ${item.deviceTuyaUuid}`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const logs = properties.map((property) =>
|
||||||
this.deviceStatusLogRepository.create({
|
this.deviceStatusLogRepository.create({
|
||||||
deviceId: device.uuid,
|
deviceId: device.uuid,
|
||||||
deviceTuyaId: item.deviceTuyaUuid,
|
deviceTuyaId: item.deviceTuyaUuid,
|
||||||
productId: item.log.productId,
|
productId: device.productDevice?.uuid,
|
||||||
log: item.log,
|
log: item.log,
|
||||||
code: property.code,
|
code: property.code,
|
||||||
value: property.value,
|
value: property.value,
|
||||||
eventId: item.log.dataId,
|
eventId: item.log?.dataId,
|
||||||
eventTime: new Date(property.time).toISOString(),
|
eventTime: new Date(
|
||||||
|
this.isDevEnv ? property.time : property.t,
|
||||||
|
).toISOString(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
allLogs.push(...logs);
|
allLogs.push(...logs);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
||||||
|
|
||||||
const insertLogsPromise = (async () => {
|
|
||||||
const chunkSize = 300;
|
const chunkSize = 300;
|
||||||
let insertedCount = 0;
|
let insertedCount = 0;
|
||||||
|
|
||||||
@ -117,9 +135,9 @@ export class DeviceStatusFirebaseService {
|
|||||||
const result = await this.deviceStatusLogRepository
|
const result = await this.deviceStatusLogRepository
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
.into('device-status-log') // or use DeviceStatusLogEntity
|
.into('device-status-log')
|
||||||
.values(chunk)
|
.values(chunk)
|
||||||
.orIgnore() // skip duplicates
|
.orIgnore()
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
insertedCount += result.identifiers.length;
|
insertedCount += result.identifiers.length;
|
||||||
@ -131,12 +149,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`);
|
||||||
`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`,
|
|
||||||
);
|
|
||||||
})();
|
|
||||||
|
|
||||||
await insertLogsPromise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addDeviceStatusToFirebase(
|
async addDeviceStatusToFirebase(
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
|
||||||
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
import { SosHandlerService } from './sos.handler.service';
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
import * as NodeCache from 'node-cache';
|
import * as NodeCache from 'node-cache';
|
||||||
|
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
||||||
|
import { SosHandlerService } from './sos.handler.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TuyaWebSocketService implements OnModuleInit {
|
export class TuyaWebSocketService implements OnModuleInit {
|
||||||
@ -74,7 +74,12 @@ export class TuyaWebSocketService implements OnModuleInit {
|
|||||||
this.client.message(async (ws: WebSocket, message: any) => {
|
this.client.message(async (ws: WebSocket, message: any) => {
|
||||||
try {
|
try {
|
||||||
const { devId, status, logData } = this.extractMessageData(message);
|
const { devId, status, logData } = this.extractMessageData(message);
|
||||||
if (!Array.isArray(logData?.properties)) {
|
// console.log(
|
||||||
|
// `📬 Received message for device: ${devId}, status:`,
|
||||||
|
// status,
|
||||||
|
// logData,
|
||||||
|
// );
|
||||||
|
if (!Array.isArray(status)) {
|
||||||
this.client.ackMessage(message.messageId);
|
this.client.ackMessage(message.messageId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -162,6 +167,8 @@ export class TuyaWebSocketService implements OnModuleInit {
|
|||||||
status: any;
|
status: any;
|
||||||
logData: any;
|
logData: any;
|
||||||
} {
|
} {
|
||||||
|
// console.log('Received message:', message);
|
||||||
|
|
||||||
const payloadData = message.payload.data;
|
const payloadData = message.payload.data;
|
||||||
|
|
||||||
if (this.isDevEnv) {
|
if (this.isDevEnv) {
|
||||||
|
@ -9,8 +9,8 @@ total_energy AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
@ -23,8 +23,8 @@ energy_phase_A AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
@ -37,8 +37,8 @@ energy_phase_B AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
@ -51,8 +51,8 @@ energy_phase_C AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
@ -9,8 +9,8 @@ total_energy AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
@ -23,8 +23,8 @@ energy_phase_A AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
@ -37,8 +37,8 @@ energy_phase_B AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
@ -51,8 +51,8 @@ energy_phase_C AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
EXTRACT(HOUR FROM log.event_time)::text AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
@ -9,8 +9,8 @@ total_energy AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
@ -23,8 +23,8 @@ energy_phase_A AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
@ -37,8 +37,8 @@ energy_phase_B AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
@ -51,8 +51,8 @@ energy_phase_C AS (
|
|||||||
EXTRACT(HOUR FROM log.event_time) AS hour,
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
MIN(log.value)::integer/100 AS min_value,
|
MIN(log.value)::integer AS min_value,
|
||||||
MAX(log.value)::integer/100 AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
@ -4,8 +4,8 @@ WITH total_energy AS (
|
|||||||
SELECT
|
SELECT
|
||||||
device_id,
|
device_id,
|
||||||
event_time::date AS date,
|
event_time::date AS date,
|
||||||
MIN(value)::integer/100 AS min_value,
|
MIN(value)::integer AS min_value,
|
||||||
MAX(value)::integer/100 AS max_value
|
MAX(value)::integer AS max_value
|
||||||
FROM "device-status-log"
|
FROM "device-status-log"
|
||||||
where code='EnergyConsumed'
|
where code='EnergyConsumed'
|
||||||
GROUP BY device_id, date
|
GROUP BY device_id, date
|
||||||
@ -15,8 +15,8 @@ WITH total_energy AS (
|
|||||||
SELECT
|
SELECT
|
||||||
device_id,
|
device_id,
|
||||||
event_time::date AS date,
|
event_time::date AS date,
|
||||||
MIN(value)::integer/100 AS min_value,
|
MIN(value)::integer AS min_value,
|
||||||
MAX(value)::integer/100 AS max_value
|
MAX(value)::integer AS max_value
|
||||||
FROM "device-status-log"
|
FROM "device-status-log"
|
||||||
where code='EnergyConsumedA'
|
where code='EnergyConsumedA'
|
||||||
GROUP BY device_id, date
|
GROUP BY device_id, date
|
||||||
@ -26,8 +26,8 @@ WITH total_energy AS (
|
|||||||
SELECT
|
SELECT
|
||||||
device_id,
|
device_id,
|
||||||
event_time::date AS date,
|
event_time::date AS date,
|
||||||
MIN(value)::integer/100 AS min_value,
|
MIN(value)::integer AS min_value,
|
||||||
MAX(value)::integer/100 AS max_value
|
MAX(value)::integer AS max_value
|
||||||
FROM "device-status-log"
|
FROM "device-status-log"
|
||||||
where code='EnergyConsumedB'
|
where code='EnergyConsumedB'
|
||||||
GROUP BY device_id, date
|
GROUP BY device_id, date
|
||||||
@ -37,8 +37,8 @@ WITH total_energy AS (
|
|||||||
SELECT
|
SELECT
|
||||||
device_id,
|
device_id,
|
||||||
event_time::date AS date,
|
event_time::date AS date,
|
||||||
MIN(value)::integer/100 AS min_value,
|
MIN(value)::integer AS min_value,
|
||||||
MAX(value)::integer/100 AS max_value
|
MAX(value)::integer AS max_value
|
||||||
FROM "device-status-log"
|
FROM "device-status-log"
|
||||||
where code='EnergyConsumedC'
|
where code='EnergyConsumedC'
|
||||||
GROUP BY device_id, date
|
GROUP BY device_id, date
|
||||||
|
@ -0,0 +1,135 @@
|
|||||||
|
WITH total_energy AS (
|
||||||
|
SELECT
|
||||||
|
log.device_id,
|
||||||
|
log.event_time::date AS date,
|
||||||
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
|
MIN(log.value)::integer AS min_value,
|
||||||
|
MAX(log.value)::integer AS max_value
|
||||||
|
FROM "device-status-log" log
|
||||||
|
WHERE log.code = 'EnergyConsumed'
|
||||||
|
GROUP BY 1,2,3,4,5
|
||||||
|
),
|
||||||
|
|
||||||
|
energy_phase_A AS (
|
||||||
|
SELECT
|
||||||
|
log.device_id,
|
||||||
|
log.event_time::date AS date,
|
||||||
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
|
MIN(log.value)::integer AS min_value,
|
||||||
|
MAX(log.value)::integer AS max_value
|
||||||
|
FROM "device-status-log" log
|
||||||
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
|
GROUP BY 1,2,3,4,5
|
||||||
|
),
|
||||||
|
|
||||||
|
energy_phase_B AS (
|
||||||
|
SELECT
|
||||||
|
log.device_id,
|
||||||
|
log.event_time::date AS date,
|
||||||
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
|
MIN(log.value)::integer AS min_value,
|
||||||
|
MAX(log.value)::integer AS max_value
|
||||||
|
FROM "device-status-log" log
|
||||||
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
|
GROUP BY 1,2,3,4,5
|
||||||
|
),
|
||||||
|
|
||||||
|
energy_phase_C AS (
|
||||||
|
SELECT
|
||||||
|
log.device_id,
|
||||||
|
log.event_time::date AS date,
|
||||||
|
EXTRACT(HOUR FROM log.event_time) AS hour,
|
||||||
|
TO_CHAR(log.event_time, 'MM-YYYY') AS event_month,
|
||||||
|
EXTRACT(YEAR FROM log.event_time)::int AS event_year,
|
||||||
|
MIN(log.value)::integer AS min_value,
|
||||||
|
MAX(log.value)::integer AS max_value
|
||||||
|
FROM "device-status-log" log
|
||||||
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
|
GROUP BY 1,2,3,4,5
|
||||||
|
)
|
||||||
|
, final_data as (
|
||||||
|
SELECT
|
||||||
|
t.device_id,
|
||||||
|
t.date,
|
||||||
|
t.event_year::text,
|
||||||
|
t.event_month,
|
||||||
|
t.hour,
|
||||||
|
(t.max_value - t.min_value) AS energy_consumed_kW,
|
||||||
|
(a.max_value - a.min_value) AS energy_consumed_A,
|
||||||
|
(b.max_value - b.min_value) AS energy_consumed_B,
|
||||||
|
(c.max_value - c.min_value) AS energy_consumed_C
|
||||||
|
FROM total_energy t
|
||||||
|
JOIN energy_phase_A a ON t.device_id = a.device_id AND t.date = a.date AND t.hour = a.hour
|
||||||
|
JOIN energy_phase_B b ON t.device_id = b.device_id AND t.date = b.date AND t.hour = b.hour
|
||||||
|
JOIN energy_phase_C c ON t.device_id = c.device_id AND t.date = c.date AND t.hour = c.hour
|
||||||
|
ORDER BY 1,2)
|
||||||
|
|
||||||
|
|
||||||
|
INSERT INTO public."power-clamp-energy-consumed-daily"(
|
||||||
|
device_uuid,
|
||||||
|
energy_consumed_kw,
|
||||||
|
energy_consumed_a,
|
||||||
|
energy_consumed_b,
|
||||||
|
energy_consumed_c,
|
||||||
|
date
|
||||||
|
)
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
device_id,
|
||||||
|
SUM(CAST(energy_consumed_kw AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_a AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_b AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_c AS NUMERIC))::VARCHAR,
|
||||||
|
date
|
||||||
|
FROM final_data
|
||||||
|
GROUP BY device_id, date;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
INSERT INTO public."power-clamp-energy-consumed-hourly"(
|
||||||
|
device_uuid,
|
||||||
|
energy_consumed_kw,
|
||||||
|
energy_consumed_a,
|
||||||
|
energy_consumed_b,
|
||||||
|
energy_consumed_c,
|
||||||
|
date,
|
||||||
|
hour
|
||||||
|
)
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
device_id,
|
||||||
|
SUM(CAST(energy_consumed_kw AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_a AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_b AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_c AS NUMERIC))::VARCHAR,
|
||||||
|
date,
|
||||||
|
hour
|
||||||
|
FROM final_data
|
||||||
|
GROUP BY 1,6,7
|
||||||
|
|
||||||
|
|
||||||
|
INSERT INTO public."power-clamp-energy-consumed-monthly"(
|
||||||
|
device_uuid,
|
||||||
|
energy_consumed_kw,
|
||||||
|
energy_consumed_a,
|
||||||
|
energy_consumed_b,
|
||||||
|
energy_consumed_c,
|
||||||
|
month
|
||||||
|
)
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
device_id,
|
||||||
|
SUM(CAST(energy_consumed_kw AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_a AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_b AS NUMERIC))::VARCHAR,
|
||||||
|
SUM(CAST(energy_consumed_c AS NUMERIC))::VARCHAR,
|
||||||
|
TO_CHAR(date, 'MM-YYYY')
|
||||||
|
FROM final_data
|
||||||
|
GROUP BY 1,6;
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import nodemailer from 'nodemailer';
|
import * as nodemailer from 'nodemailer';
|
||||||
import Mail from 'nodemailer/lib/mailer';
|
import Mail from 'nodemailer/lib/mailer';
|
||||||
import { BatchEmailData } from './batch-email.interface';
|
import { BatchEmailData } from './batch-email.interface';
|
||||||
import { SingleEmailData } from './single-email.interface';
|
import { SingleEmailData } from './single-email.interface';
|
||||||
|
@ -3,11 +3,11 @@ import { IsNotEmpty, IsOptional, IsUUID, Matches } from 'class-validator';
|
|||||||
|
|
||||||
export class BookingRequestDto {
|
export class BookingRequestDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Month in MM/YYYY format',
|
description: 'Month in MM-YYYY format',
|
||||||
example: '07/2025',
|
example: '07-2025',
|
||||||
})
|
})
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@Matches(/^(0[1-9]|1[0-2])\/\d{4}$/, {
|
@Matches(/^(0[1-9]|1[0-2])\-\d{4}$/, {
|
||||||
message: 'Date must be in MM/YYYY format',
|
message: 'Date must be in MM/YYYY format',
|
||||||
})
|
})
|
||||||
month: string;
|
month: string;
|
||||||
|
@ -51,7 +51,7 @@ export class BookingService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findAll({ month, space }: BookingRequestDto, project: string) {
|
async findAll({ month, space }: BookingRequestDto, project: string) {
|
||||||
const [monthNumber, year] = month.split('/').map(Number);
|
const [monthNumber, year] = month.split('-').map(Number);
|
||||||
const fromDate = new Date(year, monthNumber - 1, 1);
|
const fromDate = new Date(year, monthNumber - 1, 1);
|
||||||
const toDate = new Date(year, monthNumber, 0, 23, 59, 59);
|
const toDate = new Date(year, monthNumber, 0, 23, 59, 59);
|
||||||
return this.bookingEntityRepository.find({
|
return this.bookingEntityRepository.find({
|
||||||
|
Reference in New Issue
Block a user