mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-08-26 08:09:39 +00:00
Compare commits
5 Commits
task/add-p
...
217a2a8a0d
Author | SHA1 | Date | |
---|---|---|---|
217a2a8a0d | |||
84406fee1b | |||
dab09220a5 | |||
3f2c36d48b | |||
ddb205658c |
64
.github/workflows/pr-description.yml
vendored
64
.github/workflows/pr-description.yml
vendored
@ -1,64 +0,0 @@
|
|||||||
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,9 +1,24 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsOptional } from 'class-validator';
|
import { Transform } from 'class-transformer';
|
||||||
|
import { IsBoolean, IsOptional } from 'class-validator';
|
||||||
|
import { BooleanValues } from '../constants/boolean-values.enum';
|
||||||
import { IsPageRequestParam } from '../validators/is-page-request-param.validator';
|
import { IsPageRequestParam } from '../validators/is-page-request-param.validator';
|
||||||
import { IsSizeRequestParam } from '../validators/is-size-request-param.validator';
|
import { IsSizeRequestParam } from '../validators/is-size-request-param.validator';
|
||||||
|
|
||||||
export class PaginationRequestGetListDto {
|
export class PaginationRequestGetListDto {
|
||||||
|
@ApiProperty({
|
||||||
|
example: true,
|
||||||
|
description: 'include spaces',
|
||||||
|
required: false,
|
||||||
|
default: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsBoolean()
|
||||||
|
@Transform((value) => {
|
||||||
|
return value.obj.includeSpaces === BooleanValues.TRUE;
|
||||||
|
})
|
||||||
|
public includeSpaces?: boolean = false;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsPageRequestParam({
|
@IsPageRequestParam({
|
||||||
message: 'Page must be bigger than 0',
|
message: 'Page must be bigger than 0',
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
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 { ConfigService } from '@nestjs/config';
|
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 { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { firebaseDataBase } from '../../firebase.config';
|
||||||
import {
|
import {
|
||||||
Database,
|
Database,
|
||||||
DataSnapshot,
|
DataSnapshot,
|
||||||
@ -15,9 +17,7 @@ import {
|
|||||||
ref,
|
ref,
|
||||||
runTransaction,
|
runTransaction,
|
||||||
} from 'firebase/database';
|
} from 'firebase/database';
|
||||||
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
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,77 +79,64 @@ export class DeviceStatusFirebaseService {
|
|||||||
device: any;
|
device: any;
|
||||||
}[],
|
}[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
|
||||||
|
|
||||||
const allLogs = [];
|
const allLogs = [];
|
||||||
|
|
||||||
|
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine properties based on environment
|
const logs = item.log.properties.map((property) =>
|
||||||
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: device.productDevice?.uuid,
|
productId: item.log.productId,
|
||||||
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(
|
eventTime: new Date(property.time).toISOString(),
|
||||||
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 chunkSize = 300;
|
const insertLogsPromise = (async () => {
|
||||||
let insertedCount = 0;
|
const chunkSize = 300;
|
||||||
|
let insertedCount = 0;
|
||||||
|
|
||||||
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
||||||
const chunk = allLogs.slice(i, i + chunkSize);
|
const chunk = allLogs.slice(i, i + chunkSize);
|
||||||
try {
|
try {
|
||||||
const result = await this.deviceStatusLogRepository
|
const result = await this.deviceStatusLogRepository
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.insert()
|
.insert()
|
||||||
.into('device-status-log')
|
.into('device-status-log') // or use DeviceStatusLogEntity
|
||||||
.values(chunk)
|
.values(chunk)
|
||||||
.orIgnore()
|
.orIgnore() // skip duplicates
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
insertedCount += result.identifiers.length;
|
insertedCount += result.identifiers.length;
|
||||||
console.log(
|
console.log(
|
||||||
`✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`,
|
`✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Insert error (skipped chunk):', error.message);
|
console.error('❌ Insert error (skipped chunk):', error.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`);
|
console.log(
|
||||||
|
`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`,
|
||||||
|
);
|
||||||
|
})();
|
||||||
|
|
||||||
|
await insertLogsPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addDeviceStatusToFirebase(
|
async addDeviceStatusToFirebase(
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
|
||||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import * as NodeCache from 'node-cache';
|
|
||||||
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
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 { SosHandlerService } from './sos.handler.service';
|
import { SosHandlerService } from './sos.handler.service';
|
||||||
|
import * as NodeCache from 'node-cache';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TuyaWebSocketService implements OnModuleInit {
|
export class TuyaWebSocketService implements OnModuleInit {
|
||||||
@ -74,12 +74,7 @@ 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);
|
||||||
// console.log(
|
if (!Array.isArray(logData?.properties)) {
|
||||||
// `📬 Received message for device: ${devId}, status:`,
|
|
||||||
// status,
|
|
||||||
// logData,
|
|
||||||
// );
|
|
||||||
if (!Array.isArray(status)) {
|
|
||||||
this.client.ackMessage(message.messageId);
|
this.client.ackMessage(message.messageId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -167,8 +162,6 @@ 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) {
|
||||||
|
@ -0,0 +1,91 @@
|
|||||||
|
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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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;
|
@ -0,0 +1,94 @@
|
|||||||
|
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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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/100 AS min_value,
|
||||||
|
MAX(log.value)::integer/100 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-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
|
@ -5,8 +5,8 @@ WITH 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 AS max_value
|
||||||
FROM "device-status-log" log
|
FROM "device-status-log" log
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
@ -19,8 +19,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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 AS max_value
|
||||||
FROM "device-status-log" log
|
FROM "device-status-log" log
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
@ -33,8 +33,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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 AS max_value
|
||||||
FROM "device-status-log" log
|
FROM "device-status-log" log
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
@ -47,8 +47,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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 AS max_value
|
||||||
FROM "device-status-log" log
|
FROM "device-status-log" log
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
@ -71,49 +71,6 @@ JOIN energy_phase_C c ON t.device_id = c.device_id AND t.date = c.date AND t.hou
|
|||||||
ORDER BY 1,2)
|
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"(
|
INSERT INTO public."power-clamp-energy-consumed-monthly"(
|
||||||
device_uuid,
|
device_uuid,
|
||||||
energy_consumed_kw,
|
energy_consumed_kw,
|
||||||
@ -131,5 +88,4 @@ SELECT
|
|||||||
SUM(CAST(energy_consumed_c AS NUMERIC))::VARCHAR,
|
SUM(CAST(energy_consumed_c AS NUMERIC))::VARCHAR,
|
||||||
TO_CHAR(date, 'MM-YYYY')
|
TO_CHAR(date, 'MM-YYYY')
|
||||||
FROM final_data
|
FROM final_data
|
||||||
GROUP BY 1,6;
|
GROUP BY 1,6;
|
||||||
|
|
@ -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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(log.value)::integer/100 AS min_value,
|
||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer/100 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 AS min_value,
|
MIN(value)::integer/100 AS min_value,
|
||||||
MAX(value)::integer AS max_value
|
MAX(value)::integer/100 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 AS min_value,
|
MIN(value)::integer/100 AS min_value,
|
||||||
MAX(value)::integer AS max_value
|
MAX(value)::integer/100 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 AS min_value,
|
MIN(value)::integer/100 AS min_value,
|
||||||
MAX(value)::integer AS max_value
|
MAX(value)::integer/100 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 AS min_value,
|
MIN(value)::integer/100 AS min_value,
|
||||||
MAX(value)::integer AS max_value
|
MAX(value)::integer/100 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
|
||||||
|
@ -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 * as nodemailer from 'nodemailer';
|
import 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';
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
||||||
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty, OmitType } from '@nestjs/swagger';
|
||||||
import { Transform } from 'class-transformer';
|
import { Transform } from 'class-transformer';
|
||||||
import { IsBoolean, IsNotEmpty, IsOptional } from 'class-validator';
|
import { IsBoolean, IsNotEmpty, IsOptional } from 'class-validator';
|
||||||
|
|
||||||
export class BookableSpaceRequestDto extends PaginationRequestWithSearchGetListDto {
|
export class BookableSpaceRequestDto extends OmitType(
|
||||||
|
PaginationRequestWithSearchGetListDto,
|
||||||
|
['includeSpaces'],
|
||||||
|
) {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
required: false,
|
required: false,
|
||||||
|
@ -3,12 +3,12 @@ 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({
|
||||||
|
@ -17,10 +17,10 @@ import { CommunityService } from '../services/community.service';
|
|||||||
// import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
// import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
import { ProjectParam } from '../dtos';
|
import { ProjectParam } from '../dtos';
|
||||||
import { CommunityFilterDto } from '../dtos/community-filter.dto';
|
|
||||||
|
|
||||||
@ApiTags('Community Module')
|
@ApiTags('Community Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -55,7 +55,7 @@ export class CommunityController {
|
|||||||
@Get('v2')
|
@Get('v2')
|
||||||
async getCommunitiesV2(
|
async getCommunitiesV2(
|
||||||
@Param() param: ProjectParam,
|
@Param() param: ProjectParam,
|
||||||
@Query() query: CommunityFilterDto,
|
@Query() query: PaginationRequestWithSearchGetListDto,
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
return this.communityService.getCommunitiesV2(param, query);
|
return this.communityService.getCommunitiesV2(param, query);
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@ export class CommunityController {
|
|||||||
@Get()
|
@Get()
|
||||||
async getCommunities(
|
async getCommunities(
|
||||||
@Param() param: ProjectParam,
|
@Param() param: ProjectParam,
|
||||||
@Query() query: CommunityFilterDto,
|
@Query() query: PaginationRequestWithSearchGetListDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
return this.communityService.getCommunities(param, query);
|
return this.communityService.getCommunities(param, query);
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
|
||||||
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { Transform } from 'class-transformer';
|
|
||||||
import { IsBoolean, IsOptional } from 'class-validator';
|
|
||||||
|
|
||||||
export class CommunityFilterDto extends PaginationRequestWithSearchGetListDto {
|
|
||||||
@ApiProperty({
|
|
||||||
example: true,
|
|
||||||
description: 'include spaces',
|
|
||||||
required: false,
|
|
||||||
default: false,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@IsBoolean()
|
|
||||||
@Transform((value) => {
|
|
||||||
return value.obj.includeSpaces === BooleanValues.TRUE;
|
|
||||||
})
|
|
||||||
public includeSpaces?: boolean = false;
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||||
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { Transform } from 'class-transformer';
|
import { Transform } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
@ -66,7 +65,7 @@ export class GetDevicesBySpaceOrCommunityDto {
|
|||||||
requireEither?: never; // This ensures at least one of them is provided
|
requireEither?: never; // This ensures at least one of them is provided
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GetDevicesFilterDto extends PaginationRequestGetListDto {
|
export class GetDevicesFilterDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Device Type',
|
description: 'Device Type',
|
||||||
enum: DeviceTypeEnum,
|
enum: DeviceTypeEnum,
|
||||||
|
@ -7,7 +7,6 @@ import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
|||||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
|
||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
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 { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
@ -21,7 +20,6 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
|||||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
||||||
import { getPaginationResponseDto } from '@app/common/util/getPaginationResponseDto';
|
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
forwardRef,
|
forwardRef,
|
||||||
@ -102,7 +100,7 @@ export class DeviceService {
|
|||||||
|
|
||||||
async getAllDevices(
|
async getAllDevices(
|
||||||
param: ProjectParam,
|
param: ProjectParam,
|
||||||
{ deviceType, spaces, communities, page, size }: GetDevicesFilterDto,
|
{ deviceType, spaces, communities }: GetDevicesFilterDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
try {
|
try {
|
||||||
await this.validateProject(param.projectUuid);
|
await this.validateProject(param.projectUuid);
|
||||||
@ -110,12 +108,9 @@ export class DeviceService {
|
|||||||
return await this.getDoorLockDevices(param.projectUuid, {
|
return await this.getDoorLockDevices(param.projectUuid, {
|
||||||
spaces,
|
spaces,
|
||||||
communities,
|
communities,
|
||||||
deviceType,
|
|
||||||
page,
|
|
||||||
size,
|
|
||||||
});
|
});
|
||||||
} else if (!deviceType) {
|
} else if (!deviceType) {
|
||||||
const [devices, count] = await this.deviceRepository.findAndCount({
|
const devices = await this.deviceRepository.find({
|
||||||
where: {
|
where: {
|
||||||
isActive: true,
|
isActive: true,
|
||||||
spaceDevice: {
|
spaceDevice: {
|
||||||
@ -138,8 +133,6 @@ export class DeviceService {
|
|||||||
'permission.permissionType',
|
'permission.permissionType',
|
||||||
'subspace',
|
'subspace',
|
||||||
],
|
],
|
||||||
take: size ?? 10,
|
|
||||||
skip: (page ? page - 1 : 0) * (size ?? 10),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const devicesData = await Promise.allSettled(
|
const devicesData = await Promise.allSettled(
|
||||||
@ -241,14 +234,11 @@ export class DeviceService {
|
|||||||
.value,
|
.value,
|
||||||
);
|
);
|
||||||
|
|
||||||
return new PageResponse(
|
return new SuccessResponseDto({
|
||||||
{
|
message: `Devices fetched successfully`,
|
||||||
message: `Devices fetched successfully`,
|
data: fulfilledDevices,
|
||||||
data: fulfilledDevices,
|
statusCode: HttpStatus.OK,
|
||||||
statusCode: HttpStatus.OK,
|
});
|
||||||
},
|
|
||||||
getPaginationResponseDto(count, page ?? 1, size ?? 10),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
@ -1311,11 +1301,11 @@ export class DeviceService {
|
|||||||
|
|
||||||
private async getDoorLockDevices(
|
private async getDoorLockDevices(
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
{ communities, spaces, page, size }: GetDevicesFilterDto,
|
{ communities, spaces }: { spaces?: string[]; communities?: string[] },
|
||||||
) {
|
) {
|
||||||
await this.validateProject(projectUuid);
|
await this.validateProject(projectUuid);
|
||||||
|
|
||||||
const [devices, count] = await this.deviceRepository.findAndCount({
|
const devices = await this.deviceRepository.find({
|
||||||
where: {
|
where: {
|
||||||
productDevice: {
|
productDevice: {
|
||||||
prodType: ProductType.DL,
|
prodType: ProductType.DL,
|
||||||
@ -1334,8 +1324,6 @@ export class DeviceService {
|
|||||||
isActive: true,
|
isActive: true,
|
||||||
},
|
},
|
||||||
relations: ['productDevice', 'spaceDevice'],
|
relations: ['productDevice', 'spaceDevice'],
|
||||||
take: size ?? 10,
|
|
||||||
skip: (page ? page - 1 : 0) * (size ?? 10),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const devicesData = await Promise.all(
|
const devicesData = await Promise.all(
|
||||||
@ -1367,14 +1355,11 @@ export class DeviceService {
|
|||||||
(deviceData) => deviceData !== null,
|
(deviceData) => deviceData !== null,
|
||||||
);
|
);
|
||||||
|
|
||||||
return new PageResponse(
|
return new SuccessResponseDto({
|
||||||
{
|
message: 'Successfully retrieved all pass devices',
|
||||||
message: 'Successfully retrieved all pass devices',
|
data: filteredDevicesData,
|
||||||
data: filteredDevicesData,
|
statusCode: HttpStatus.OK,
|
||||||
statusCode: HttpStatus.OK,
|
});
|
||||||
},
|
|
||||||
getPaginationResponseDto(count, page ?? 1, size ?? 10),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async controlDeviceTuya(
|
private async controlDeviceTuya(
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
||||||
|
import { PickType } from '@nestjs/swagger';
|
||||||
|
|
||||||
export class ListProjectsDto extends PaginationRequestGetListDto {}
|
export class ListProjectsDto extends PickType(PaginationRequestGetListDto, [
|
||||||
|
'page',
|
||||||
|
'size',
|
||||||
|
]) {}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
@ -12,17 +11,18 @@ import {
|
|||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { ProjectParam } from 'src/community/dtos';
|
import { SpaceModelService } from '../services';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
|
||||||
import {
|
import {
|
||||||
CreateSpaceModelDto,
|
CreateSpaceModelDto,
|
||||||
LinkSpacesToModelDto,
|
LinkSpacesToModelDto,
|
||||||
SpaceModelParam,
|
SpaceModelParam,
|
||||||
UpdateSpaceModelDto,
|
UpdateSpaceModelDto,
|
||||||
} from '../dtos';
|
} from '../dtos';
|
||||||
import { SpaceModelFilterDto } from '../dtos/space-model-filter.dto';
|
import { ProjectParam } from 'src/community/dtos';
|
||||||
import { SpaceModelService } from '../services';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
||||||
|
|
||||||
@ApiTags('Space Model Module')
|
@ApiTags('Space Model Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -62,7 +62,7 @@ export class SpaceModelController {
|
|||||||
@Get()
|
@Get()
|
||||||
async listSpaceModel(
|
async listSpaceModel(
|
||||||
@Param() projectParam: ProjectParam,
|
@Param() projectParam: ProjectParam,
|
||||||
@Query() query: SpaceModelFilterDto,
|
@Query() query: PaginationRequestGetListDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
return await this.spaceModelService.list(projectParam, query);
|
return await this.spaceModelService.list(projectParam, query);
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
|
||||||
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { Transform } from 'class-transformer';
|
|
||||||
import { IsBoolean, IsOptional } from 'class-validator';
|
|
||||||
|
|
||||||
export class SpaceModelFilterDto extends PaginationRequestGetListDto {
|
|
||||||
@ApiProperty({
|
|
||||||
example: true,
|
|
||||||
description: 'include spaces',
|
|
||||||
required: false,
|
|
||||||
default: false,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@IsBoolean()
|
|
||||||
@Transform((value) => {
|
|
||||||
return value.obj.includeSpaces === BooleanValues.TRUE;
|
|
||||||
})
|
|
||||||
public includeSpaces?: boolean = false;
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
@ -11,12 +10,13 @@ import {
|
|||||||
Query,
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
|
||||||
import { SubspaceFilterDto } from 'src/space/dtos/subspace-filter.dto';
|
|
||||||
import { AddSubspaceDto, GetSpaceParam, GetSubSpaceParam } from '../../dtos';
|
|
||||||
import { SubSpaceService } from '../../services';
|
import { SubSpaceService } from '../../services';
|
||||||
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
|
import { AddSubspaceDto, GetSpaceParam, GetSubSpaceParam } from '../../dtos';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
||||||
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
|
||||||
@ApiTags('Space Module')
|
@ApiTags('Space Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -51,7 +51,7 @@ export class SubSpaceController {
|
|||||||
@Get()
|
@Get()
|
||||||
async list(
|
async list(
|
||||||
@Param() params: GetSpaceParam,
|
@Param() params: GetSpaceParam,
|
||||||
@Query() query: SubspaceFilterDto,
|
@Query() query: PaginationRequestGetListDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
return this.subSpaceService.list(params, query);
|
return this.subSpaceService.list(params, query);
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
|
||||||
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { Transform } from 'class-transformer';
|
|
||||||
import { IsBoolean, IsOptional } from 'class-validator';
|
|
||||||
|
|
||||||
export class SubspaceFilterDto extends PaginationRequestGetListDto {
|
|
||||||
@ApiProperty({
|
|
||||||
example: true,
|
|
||||||
description: 'include spaces',
|
|
||||||
required: false,
|
|
||||||
default: false,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@IsBoolean()
|
|
||||||
@Transform((value) => {
|
|
||||||
return value.obj.includeSpaces === BooleanValues.TRUE;
|
|
||||||
})
|
|
||||||
public includeSpaces?: boolean = false;
|
|
||||||
}
|
|
Reference in New Issue
Block a user