mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:54:55 +00:00
Merge branch 'dev' into SP-1551-be-implement-total-energy-consumption-api-for-analytics-dashboard
This commit is contained in:
@ -51,6 +51,7 @@ import {
|
|||||||
PowerClampHourlyEntity,
|
PowerClampHourlyEntity,
|
||||||
PowerClampMonthlyEntity,
|
PowerClampMonthlyEntity,
|
||||||
} from '../modules/power-clamp/entities/power-clamp.entity';
|
} from '../modules/power-clamp/entities/power-clamp.entity';
|
||||||
|
import { PresenceSensorDailyEntity } from '../modules/presence-sensor/entities';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
@ -109,6 +110,7 @@ import {
|
|||||||
PowerClampHourlyEntity,
|
PowerClampHourlyEntity,
|
||||||
PowerClampDailyEntity,
|
PowerClampDailyEntity,
|
||||||
PowerClampMonthlyEntity,
|
PowerClampMonthlyEntity,
|
||||||
|
PresenceSensorDailyEntity,
|
||||||
],
|
],
|
||||||
namingStrategy: new SnakeNamingStrategy(),
|
namingStrategy: new SnakeNamingStrategy(),
|
||||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import { SpaceEntity } from '../../space/entities/space.entity';
|
|||||||
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
|
||||||
import { NewTagEntity } from '../../tag';
|
import { NewTagEntity } from '../../tag';
|
||||||
import { PowerClampHourlyEntity } from '../../power-clamp/entities/power-clamp.entity';
|
import { PowerClampHourlyEntity } from '../../power-clamp/entities/power-clamp.entity';
|
||||||
|
import { PresenceSensorDailyEntity } from '../../presence-sensor/entities';
|
||||||
|
|
||||||
@Entity({ name: 'device' })
|
@Entity({ name: 'device' })
|
||||||
@Unique(['deviceTuyaUuid'])
|
@Unique(['deviceTuyaUuid'])
|
||||||
@ -82,6 +83,8 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
public tag: NewTagEntity;
|
public tag: NewTagEntity;
|
||||||
@OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device)
|
@OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device)
|
||||||
powerClampHourly: PowerClampHourlyEntity[];
|
powerClampHourly: PowerClampHourlyEntity[];
|
||||||
|
@OneToMany(() => PresenceSensorDailyEntity, (sensor) => sensor.device)
|
||||||
|
presenceSensorDaily: PresenceSensorDailyEntity[];
|
||||||
constructor(partial: Partial<DeviceEntity>) {
|
constructor(partial: Partial<DeviceEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
|||||||
1
libs/common/src/modules/presence-sensor/dtos/index.ts
Normal file
1
libs/common/src/modules/presence-sensor/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './presence-sensor.dto';
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class PresenceSensorDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public deviceUuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public eventDate: string;
|
||||||
|
|
||||||
|
@IsNumber()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public CountMotionDetected: number;
|
||||||
|
|
||||||
|
@IsNumber()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public CountPresenceDetected: number;
|
||||||
|
|
||||||
|
@IsNumber()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public CountTotalPresenceDetected: number;
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
export * from './presence-sensor.entity';
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { PresenceSensorDto } from '../dtos';
|
||||||
|
import { DeviceEntity } from '../../device/entities/device.entity';
|
||||||
|
|
||||||
|
@Entity({ name: 'presence-sensor-daily-detection' })
|
||||||
|
@Unique(['deviceUuid', 'eventDate'])
|
||||||
|
export class PresenceSensorDailyEntity extends AbstractEntity<PresenceSensorDto> {
|
||||||
|
@Column({ nullable: false })
|
||||||
|
public deviceUuid: string;
|
||||||
|
|
||||||
|
@Column({ nullable: false, type: 'date' })
|
||||||
|
public eventDate: string;
|
||||||
|
|
||||||
|
@Column({ nullable: false })
|
||||||
|
public CountMotionDetected: number;
|
||||||
|
|
||||||
|
@Column({ nullable: false })
|
||||||
|
public CountPresenceDetected: number;
|
||||||
|
|
||||||
|
@Column({ nullable: false })
|
||||||
|
public CountTotalPresenceDetected: number;
|
||||||
|
|
||||||
|
@ManyToOne(() => DeviceEntity, (device) => device.presenceSensorDaily)
|
||||||
|
device: DeviceEntity;
|
||||||
|
|
||||||
|
constructor(partial: Partial<PresenceSensorDailyEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { PresenceSensorDailyEntity } from './entities/presence-sensor.entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
exports: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [TypeOrmModule.forFeature([PresenceSensorDailyEntity])],
|
||||||
|
})
|
||||||
|
export class PresenceSensorRepositoryModule {}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
export * from './presence-sensor.repository';
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { PresenceSensorDailyEntity } from '../entities';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class PresenceSensorDailyRepository extends Repository<PresenceSensorDailyEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(PresenceSensorDailyEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
WITH params AS (
|
||||||
|
SELECT
|
||||||
|
TO_DATE(NULLIF($1, ''), 'MM-YYYY') AS month,
|
||||||
|
string_to_array(NULLIF($2, ''), ',') AS device_ids
|
||||||
|
)
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
A.date,
|
||||||
|
SUM(A.energy_consumed_kW::numeric) AS total_energy_consumed_KW,
|
||||||
|
SUM(A.energy_consumed_A::numeric) AS total_energy_consumed_A,
|
||||||
|
SUM(A.energy_consumed_B::numeric) AS total_energy_consumed_B,
|
||||||
|
SUM(A.energy_consumed_C::numeric) AS total_energy_consumed_C
|
||||||
|
FROM public."power-clamp-energy-consumed-daily" AS A
|
||||||
|
JOIN public.device AS B
|
||||||
|
ON A.device_uuid::TEXT = B."uuid"::TEXT
|
||||||
|
JOIN params P ON TRUE
|
||||||
|
WHERE B."uuid"::TEXT = ANY(P.device_ids)
|
||||||
|
AND (P.month IS NULL OR date_trunc('month', A.date)= P.month)
|
||||||
|
GROUP BY A.date
|
||||||
|
ORDER BY A.date;
|
||||||
Reference in New Issue
Block a user