mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-03-11 08:01:44 +00:00
Compare commits
1 Commits
DATA-occup
...
DATA-daily
| Author | SHA1 | Date | |
|---|---|---|---|
| 75137da3d3 |
@ -1,18 +1,18 @@
|
||||
import { PlatformType } from '@app/common/constants/platform-type.enum';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import * as argon2 from 'argon2';
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
import { UserSessionEntity } from '../../../../common/src/modules/session/entities';
|
||||
import { UserSessionRepository } from '../../../../common/src/modules/session/repositories/session.repository';
|
||||
import { UserRepository } from '../../../../common/src/modules/user/repositories';
|
||||
import { HelperHashService } from '../../helper/services';
|
||||
import { UserRepository } from '../../../../common/src/modules/user/repositories';
|
||||
import { UserSessionRepository } from '../../../../common/src/modules/session/repositories/session.repository';
|
||||
import { UserSessionEntity } from '../../../../common/src/modules/session/entities';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
import { PlatformType } from '@app/common/constants/platform-type.enum';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
@ -40,17 +40,16 @@ export class AuthService {
|
||||
},
|
||||
relations: ['roleType', 'project'],
|
||||
});
|
||||
if (!user) {
|
||||
throw new BadRequestException('Invalid credentials');
|
||||
}
|
||||
if (
|
||||
platform === PlatformType.WEB &&
|
||||
[RoleType.SPACE_OWNER, RoleType.SPACE_MEMBER].includes(
|
||||
user.roleType.type as RoleType,
|
||||
)
|
||||
(user.roleType.type === RoleType.SPACE_OWNER ||
|
||||
user.roleType.type === RoleType.SPACE_MEMBER)
|
||||
) {
|
||||
throw new UnauthorizedException('Access denied for web platform');
|
||||
}
|
||||
if (!user) {
|
||||
throw new BadRequestException('Invalid credentials');
|
||||
}
|
||||
|
||||
if (!user.isUserVerified) {
|
||||
throw new BadRequestException('User is not verified');
|
||||
|
||||
@ -465,16 +465,7 @@ export class ControllerRoute {
|
||||
'This endpoint retrieves the terms and conditions for the application.';
|
||||
};
|
||||
};
|
||||
static WEATHER = class {
|
||||
public static readonly ROUTE = 'weather';
|
||||
|
||||
static ACTIONS = class {
|
||||
public static readonly FETCH_WEATHER_DETAILS_SUMMARY =
|
||||
'Fetch Weather Details';
|
||||
public static readonly FETCH_WEATHER_DETAILS_DESCRIPTION =
|
||||
'This endpoint retrieves the current weather details for a specified location like temperature, humidity, etc.';
|
||||
};
|
||||
};
|
||||
static PRIVACY_POLICY = class {
|
||||
public static readonly ROUTE = 'policy';
|
||||
|
||||
@ -507,21 +498,6 @@ export class ControllerRoute {
|
||||
'Get power clamp historical data';
|
||||
public static readonly GET_ENERGY_DESCRIPTION =
|
||||
'This endpoint retrieves the historical data of a power clamp device based on the provided parameters.';
|
||||
public static readonly GET_ENERGY_BY_COMMUNITY_OR_SPACE_SUMMARY =
|
||||
'Get power clamp historical data by community or space';
|
||||
public static readonly GET_ENERGY_BY_COMMUNITY_OR_SPACE_DESCRIPTION =
|
||||
'This endpoint retrieves the historical data of power clamp devices based on the provided community or space UUID.';
|
||||
};
|
||||
};
|
||||
|
||||
static Occupancy = class {
|
||||
public static readonly ROUTE = 'occupancy';
|
||||
|
||||
static ACTIONS = class {
|
||||
public static readonly GET_OCCUPANCY_HEAT_MAP_SUMMARY =
|
||||
'Get occupancy heat map data';
|
||||
public static readonly GET_OCCUPANCY_HEAT_MAP_DESCRIPTION =
|
||||
'This endpoint retrieves the occupancy heat map data based on the provided parameters.';
|
||||
};
|
||||
};
|
||||
static DEVICE = class {
|
||||
@ -633,17 +609,6 @@ export class ControllerRoute {
|
||||
'This endpoint retrieves all devices in the system.';
|
||||
};
|
||||
};
|
||||
static DEVICE_SPACE_COMMUNITY = class {
|
||||
public static readonly ROUTE = 'devices-space-community';
|
||||
|
||||
static ACTIONS = class {
|
||||
public static readonly GET_ALL_DEVICES_BY_SPACE_OR_COMMUNITY_WITH_RECURSIVE_CHILD_SUMMARY =
|
||||
'Get all devices by space or community with recursive child';
|
||||
|
||||
public static readonly GET_ALL_DEVICES_BY_SPACE_OR_COMMUNITY_WITH_RECURSIVE_CHILD_DESCRIPTION =
|
||||
'This endpoint retrieves all devices in the system by space or community with recursive child.';
|
||||
};
|
||||
};
|
||||
|
||||
static DEVICE_PERMISSION = class {
|
||||
public static readonly ROUTE = 'device-permission';
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
export enum PresenceSensorEnum {
|
||||
PRESENCE_STATE = 'presence_state',
|
||||
SENSITIVITY = 'sensitivity',
|
||||
}
|
||||
@ -51,10 +51,6 @@ import {
|
||||
PowerClampHourlyEntity,
|
||||
PowerClampMonthlyEntity,
|
||||
} from '../modules/power-clamp/entities/power-clamp.entity';
|
||||
import {
|
||||
PresenceSensorDailyDeviceEntity,
|
||||
PresenceSensorDailySpaceEntity,
|
||||
} from '../modules/presence-sensor/entities';
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRootAsync({
|
||||
@ -113,8 +109,6 @@ import {
|
||||
PowerClampHourlyEntity,
|
||||
PowerClampDailyEntity,
|
||||
PowerClampMonthlyEntity,
|
||||
PresenceSensorDailyDeviceEntity,
|
||||
PresenceSensorDailySpaceEntity,
|
||||
],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
|
||||
@ -10,7 +10,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
@ -22,7 +21,6 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
],
|
||||
controllers: [DeviceStatusFirebaseController],
|
||||
exports: [DeviceStatusFirebaseService, DeviceStatusLogRepository],
|
||||
|
||||
@ -21,8 +21,6 @@ import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { PowerClampEnergyEnum } from '@app/common/constants/power.clamp.enargy.enum';
|
||||
import { PresenceSensorEnum } from '@app/common/constants/presence.sensor.enum';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
@Injectable()
|
||||
export class DeviceStatusFirebaseService {
|
||||
private tuya: TuyaContext;
|
||||
@ -31,7 +29,6 @@ export class DeviceStatusFirebaseService {
|
||||
private readonly configService: ConfigService,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly powerClampService: PowerClampService,
|
||||
private readonly occupancyService: OccupancyService,
|
||||
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
@ -243,26 +240,6 @@ export class DeviceStatusFirebaseService {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
addDeviceStatusDto.productType === ProductType.CPS ||
|
||||
addDeviceStatusDto.productType === ProductType.WPS
|
||||
) {
|
||||
const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
|
||||
|
||||
const occupancyStatus = addDeviceStatusDto?.log?.properties?.find(
|
||||
(status) => occupancyCodes.has(status.code),
|
||||
);
|
||||
|
||||
if (occupancyStatus) {
|
||||
await this.occupancyService.updateOccupancySensorHistoricalData(
|
||||
addDeviceStatusDto.deviceUuid,
|
||||
);
|
||||
await this.occupancyService.updateOccupancySensorHistoricalDurationData(
|
||||
addDeviceStatusDto.deviceUuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the updated data
|
||||
const snapshot: DataSnapshot = await get(dataRef);
|
||||
return snapshot.val();
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
export function toDDMMYYYY(dateString?: string | null): string | null {
|
||||
if (!dateString) return null;
|
||||
|
||||
// Ensure dateString is valid format YYYY-MM-DD
|
||||
const regex = /^\d{4}-\d{2}-\d{2}$/;
|
||||
if (!regex.test(dateString)) {
|
||||
throw new Error(
|
||||
`Invalid date format: ${dateString}. Expected format is YYYY-MM-DD`,
|
||||
);
|
||||
}
|
||||
|
||||
const [year, month, day] = dateString.split('-');
|
||||
return `${day}-${month}-${year}`;
|
||||
}
|
||||
export function toMMYYYY(dateString?: string | null): string | null {
|
||||
if (!dateString) return null;
|
||||
|
||||
// Ensure dateString is valid format YYYY-MM
|
||||
const regex = /^\d{4}-\d{2}$/;
|
||||
if (!regex.test(dateString)) {
|
||||
throw new Error(
|
||||
`Invalid date format: ${dateString}. Expected format is YYYY-MM`,
|
||||
);
|
||||
}
|
||||
|
||||
const [year, month] = dateString.split('-');
|
||||
return `${month}-${year}`;
|
||||
}
|
||||
export function filterByMonth(data: any[], monthDate: string) {
|
||||
const [year, month] = monthDate.split('-').map(Number);
|
||||
|
||||
return data.filter((item) => {
|
||||
const itemDate = new Date(item.date);
|
||||
return (
|
||||
itemDate.getUTCFullYear() === year && itemDate.getUTCMonth() + 1 === month
|
||||
);
|
||||
});
|
||||
}
|
||||
@ -1,68 +0,0 @@
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { SqlLoaderService } from './sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
|
||||
@Injectable()
|
||||
export class OccupancyService {
|
||||
constructor(
|
||||
private readonly sqlLoader: SqlLoaderService,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
) {}
|
||||
async updateOccupancySensorHistoricalDurationData(
|
||||
deviceUuid: string,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const now = new Date();
|
||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
||||
const device = await this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuid },
|
||||
relations: ['spaceDevice'],
|
||||
});
|
||||
|
||||
await this.executeProcedure(
|
||||
'fact_daily_space_occupancy_duration',
|
||||
'procedure_update_daily_space_occupancy_duration',
|
||||
[dateStr, device.spaceDevice?.uuid],
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('Failed to insert or update occupancy duration data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
async updateOccupancySensorHistoricalData(deviceUuid: string): Promise<void> {
|
||||
try {
|
||||
const now = new Date();
|
||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
||||
const device = await this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuid },
|
||||
relations: ['spaceDevice'],
|
||||
});
|
||||
|
||||
await this.executeProcedure(
|
||||
'fact_space_occupancy_count',
|
||||
'procedure_update_fact_space_occupancy',
|
||||
[dateStr, device.spaceDevice?.uuid],
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('Failed to insert or update occupancy data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private async executeProcedure(
|
||||
procedureFolderName: string,
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<void> {
|
||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
||||
await this.dataSource.query(query, params);
|
||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||
}
|
||||
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
}
|
||||
}
|
||||
@ -46,15 +46,16 @@ export class PowerClampService {
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<void> {
|
||||
const query = this.loadQuery(
|
||||
'fact_device_energy_consumed',
|
||||
procedureFileName,
|
||||
);
|
||||
const query = this.loadQuery(procedureFileName);
|
||||
await this.dataSource.query(query, params);
|
||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||
}
|
||||
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
private loadQuery(fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(
|
||||
'fact_device_energy_consumed',
|
||||
fileName,
|
||||
SQL_PROCEDURES_PATH,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,7 +18,6 @@ import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
|
||||
import { NewTagEntity } from '../../tag';
|
||||
import { PowerClampHourlyEntity } from '../../power-clamp/entities/power-clamp.entity';
|
||||
import { PresenceSensorDailyDeviceEntity } from '../../presence-sensor/entities';
|
||||
|
||||
@Entity({ name: 'device' })
|
||||
@Unique(['deviceTuyaUuid'])
|
||||
@ -83,8 +82,6 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
public tag: NewTagEntity;
|
||||
@OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device)
|
||||
powerClampHourly: PowerClampHourlyEntity[];
|
||||
@OneToMany(() => PresenceSensorDailyDeviceEntity, (sensor) => sensor.device)
|
||||
presenceSensorDaily: PresenceSensorDailyDeviceEntity[];
|
||||
constructor(partial: Partial<DeviceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
||||
@ -1 +0,0 @@
|
||||
export * from './presence-sensor.dto';
|
||||
@ -1,27 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
export * from './presence-sensor.entity';
|
||||
@ -1,58 +0,0 @@
|
||||
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';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
|
||||
@Entity({ name: 'presence-sensor-daily-device-detection' })
|
||||
@Unique(['deviceUuid', 'eventDate'])
|
||||
export class PresenceSensorDailyDeviceEntity 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<PresenceSensorDailyDeviceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
@Entity({ name: 'presence-sensor-daily-space-detection' })
|
||||
@Unique(['spaceUuid', 'eventDate'])
|
||||
export class PresenceSensorDailySpaceEntity extends AbstractEntity<PresenceSensorDto> {
|
||||
@Column({ nullable: false })
|
||||
public spaceUuid: 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(() => SpaceEntity, (space) => space.presenceSensorDaily)
|
||||
space: SpaceEntity;
|
||||
|
||||
constructor(partial: Partial<PresenceSensorDailySpaceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import {
|
||||
PresenceSensorDailyDeviceEntity,
|
||||
PresenceSensorDailySpaceEntity,
|
||||
} from './entities/presence-sensor.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([
|
||||
PresenceSensorDailyDeviceEntity,
|
||||
PresenceSensorDailySpaceEntity,
|
||||
]),
|
||||
],
|
||||
})
|
||||
export class PresenceSensorRepositoryModule {}
|
||||
@ -1 +0,0 @@
|
||||
export * from './presence-sensor.repository';
|
||||
@ -1,19 +0,0 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
PresenceSensorDailyDeviceEntity,
|
||||
PresenceSensorDailySpaceEntity,
|
||||
} from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class PresenceSensorDailyDeviceRepository extends Repository<PresenceSensorDailyDeviceEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(PresenceSensorDailyDeviceEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class PresenceSensorDailySpaceRepository extends Repository<PresenceSensorDailySpaceEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(PresenceSensorDailySpaceEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
@ -12,7 +12,6 @@ export class RoleTypeEntity extends AbstractEntity<RoleTypeDto> {
|
||||
nullable: false,
|
||||
enum: Object.values(RoleType),
|
||||
})
|
||||
// why is this ts-type string not enum?
|
||||
type: string;
|
||||
@OneToMany(() => UserEntity, (inviteUser) => inviteUser.roleType, {
|
||||
nullable: true,
|
||||
|
||||
@ -10,7 +10,6 @@ import { SpaceModelEntity } from '../../space-model';
|
||||
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
||||
import { SpaceProductAllocationEntity } from './space-product-allocation.entity';
|
||||
import { SubspaceEntity } from './subspace/subspace.entity';
|
||||
import { PresenceSensorDailySpaceEntity } from '../../presence-sensor/entities';
|
||||
|
||||
@Entity({ name: 'space' })
|
||||
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||
@ -112,9 +111,6 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||
)
|
||||
public productAllocations: SpaceProductAllocationEntity[];
|
||||
|
||||
@OneToMany(() => PresenceSensorDailySpaceEntity, (sensor) => sensor.space)
|
||||
presenceSensorDaily: PresenceSensorDailySpaceEntity[];
|
||||
|
||||
constructor(partial: Partial<SpaceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
||||
@ -14,7 +14,7 @@ import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.e
|
||||
import { SubspaceProductAllocationDto } from '../../dtos/subspace-product-allocation.dto';
|
||||
|
||||
@Entity({ name: 'subspace_product_allocation' })
|
||||
// @Unique(['subspace', 'product'])
|
||||
@Unique(['subspace', 'product'])
|
||||
export class SubspaceProductAllocationEntity extends AbstractEntity<SubspaceProductAllocationDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
|
||||
@ -25,7 +25,7 @@ export class NewTagEntity extends AbstractEntity<NewTagDto> {
|
||||
name: string;
|
||||
|
||||
@ManyToOne(() => ProductEntity, (product) => product.newTags, {
|
||||
nullable: true,
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
public product: ProductEntity;
|
||||
|
||||
@ -1,39 +0,0 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
$1::uuid AS space_uuid,
|
||||
TO_DATE(NULLIF($2, ''), 'YYYY-MM') AS event_month
|
||||
)
|
||||
|
||||
SELECT
|
||||
sdp.space_uuid,
|
||||
sdp.event_date,
|
||||
sdp.good_aqi_percentage, sdp.moderate_aqi_percentage, sdp.unhealthy_sensitive_aqi_percentage, sdp.unhealthy_aqi_percentage,
|
||||
sdp.very_unhealthy_aqi_percentage, sdp.hazardous_aqi_percentage,
|
||||
sdp.daily_avg_aqi, sdp.daily_max_aqi, sdp.daily_min_aqi,
|
||||
|
||||
sdp.good_pm25_percentage, sdp.moderate_pm25_percentage, sdp.unhealthy_sensitive_pm25_percentage, sdp.unhealthy_pm25_percentage,
|
||||
sdp.very_unhealthy_pm25_percentage, sdp.hazardous_pm25_percentage,
|
||||
sdp.daily_avg_pm25, sdp.daily_max_pm25, sdp.daily_min_pm25,
|
||||
|
||||
sdp.good_pm10_percentage, sdp.moderate_pm10_percentage, sdp.unhealthy_sensitive_pm10_percentage, sdp.unhealthy_pm10_percentage,
|
||||
sdp.very_unhealthy_pm10_percentage, sdp.hazardous_pm10_percentage,
|
||||
sdp.daily_avg_pm10, sdp.daily_max_pm10, sdp.daily_min_pm10,
|
||||
|
||||
sdp.good_voc_percentage, sdp.moderate_voc_percentage, sdp.unhealthy_sensitive_voc_percentage, sdp.unhealthy_voc_percentage,
|
||||
sdp.very_unhealthy_voc_percentage, sdp.hazardous_voc_percentage,
|
||||
sdp.daily_avg_voc, sdp.daily_max_voc, sdp.daily_min_voc,
|
||||
|
||||
sdp.good_co2_percentage, sdp.moderate_co2_percentage, sdp.unhealthy_sensitive_co2_percentage, sdp.unhealthy_co2_percentage,
|
||||
sdp.very_unhealthy_co2_percentage, sdp.hazardous_co2_percentage,
|
||||
sdp.daily_avg_co2, sdp.daily_max_co2, sdp.daily_min_co2,
|
||||
|
||||
sdp.good_ch2o_percentage, sdp.moderate_ch2o_percentage, sdp.unhealthy_sensitive_ch2o_percentage, sdp.unhealthy_ch2o_percentage,
|
||||
sdp.very_unhealthy_ch2o_percentage, sdp.hazardous_ch2o_percentage,
|
||||
sdp.daily_avg_ch2o, sdp.daily_max_ch2o, sdp.daily_min_ch2o
|
||||
|
||||
FROM public."space-daily-pollutant-stats" AS sdp
|
||||
CROSS JOIN params p
|
||||
WHERE
|
||||
(p.space_uuid IS NULL OR sdp.space_uuid = p.space_uuid)
|
||||
AND (p.event_month IS NULL OR TO_CHAR(sdp.event_date, 'YYYY-MM') = TO_CHAR(p.event_month, 'YYYY-MM'))
|
||||
ORDER BY sdp.space_uuid, sdp.event_date;
|
||||
@ -1,374 +0,0 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
||||
$2::uuid AS space_id
|
||||
),
|
||||
|
||||
-- Query Pipeline Starts Here
|
||||
device_space AS (
|
||||
SELECT
|
||||
device.uuid AS device_id,
|
||||
device.space_device_uuid AS space_id,
|
||||
"device-status-log".event_time::timestamp AS event_time,
|
||||
"device-status-log".code,
|
||||
"device-status-log".value
|
||||
FROM device
|
||||
LEFT JOIN "device-status-log"
|
||||
ON device.uuid = "device-status-log".device_id
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
WHERE product.cat_name = 'hjjcy'
|
||||
),
|
||||
|
||||
average_pollutants AS (
|
||||
SELECT
|
||||
event_time::date AS event_date,
|
||||
date_trunc('hour', event_time) AS event_hour,
|
||||
space_id,
|
||||
|
||||
-- PM1
|
||||
MIN(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_min,
|
||||
AVG(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_avg,
|
||||
MAX(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_max,
|
||||
|
||||
-- PM25
|
||||
MIN(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_min,
|
||||
AVG(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_avg,
|
||||
MAX(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_max,
|
||||
|
||||
-- PM10
|
||||
MIN(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_min,
|
||||
AVG(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_avg,
|
||||
MAX(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_max,
|
||||
|
||||
-- VOC
|
||||
MIN(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_min,
|
||||
AVG(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_avg,
|
||||
MAX(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_max,
|
||||
|
||||
-- CH2O
|
||||
MIN(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_min,
|
||||
AVG(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_avg,
|
||||
MAX(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_max,
|
||||
|
||||
-- CO2
|
||||
MIN(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_min,
|
||||
AVG(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_avg,
|
||||
MAX(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_max
|
||||
|
||||
FROM device_space
|
||||
GROUP BY space_id, event_hour, event_date
|
||||
),
|
||||
|
||||
filled_pollutants AS (
|
||||
SELECT
|
||||
*,
|
||||
-- AVG
|
||||
COALESCE(pm25_avg, LAG(pm25_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_avg_f,
|
||||
COALESCE(pm10_avg, LAG(pm10_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_avg_f,
|
||||
COALESCE(voc_avg, LAG(voc_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_avg_f,
|
||||
COALESCE(co2_avg, LAG(co2_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_avg_f,
|
||||
COALESCE(ch2o_avg, LAG(ch2o_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_avg_f,
|
||||
|
||||
-- MIN
|
||||
COALESCE(pm25_min, LAG(pm25_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_min_f,
|
||||
COALESCE(pm10_min, LAG(pm10_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_min_f,
|
||||
COALESCE(voc_min, LAG(voc_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_min_f,
|
||||
COALESCE(co2_min, LAG(co2_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_min_f,
|
||||
COALESCE(ch2o_min, LAG(ch2o_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_min_f,
|
||||
|
||||
-- MAX
|
||||
COALESCE(pm25_max, LAG(pm25_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_max_f,
|
||||
COALESCE(pm10_max, LAG(pm10_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_max_f,
|
||||
COALESCE(voc_max, LAG(voc_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_max_f,
|
||||
COALESCE(co2_max, LAG(co2_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_max_f,
|
||||
COALESCE(ch2o_max, LAG(ch2o_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_max_f
|
||||
FROM average_pollutants
|
||||
),
|
||||
|
||||
hourly_results AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
event_hour,
|
||||
pm1_min, pm1_avg, pm1_max,
|
||||
pm25_min_f, pm25_avg_f, pm25_max_f,
|
||||
pm10_min_f, pm10_avg_f, pm10_max_f,
|
||||
voc_min_f, voc_avg_f, voc_max_f,
|
||||
co2_min_f, co2_avg_f, co2_max_f,
|
||||
ch2o_min_f, ch2o_avg_f, ch2o_max_f,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_min_f),
|
||||
calculate_aqi('pm10', pm10_min_f)
|
||||
) AS hourly_min_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
) AS hourly_avg_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_max_f),
|
||||
calculate_aqi('pm10', pm10_max_f)
|
||||
) AS hourly_max_aqi,
|
||||
|
||||
classify_aqi(GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
)) AS aqi_category,
|
||||
|
||||
classify_aqi(calculate_aqi('pm25',pm25_avg_f)) as pm25_category,
|
||||
classify_aqi(calculate_aqi('pm10',pm10_avg_f)) as pm10_category,
|
||||
classify_aqi(calculate_aqi('voc',voc_avg_f)) as voc_category,
|
||||
classify_aqi(calculate_aqi('co2',co2_avg_f)) as co2_category,
|
||||
classify_aqi(calculate_aqi('ch2o',ch2o_avg_f)) as ch2o_category
|
||||
|
||||
FROM filled_pollutants
|
||||
),
|
||||
|
||||
daily_category_counts AS (
|
||||
SELECT space_id, event_date, aqi_category AS category, 'aqi' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, aqi_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, pm25_category AS category, 'pm25' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, pm25_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, pm10_category AS category, 'pm10' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, pm10_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, voc_category AS category, 'voc' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, voc_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, co2_category AS category, 'co2' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, co2_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, ch2o_category AS category, 'ch2o' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, ch2o_category
|
||||
),
|
||||
|
||||
daily_totals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
SUM(category_count) AS total_count
|
||||
FROM daily_category_counts
|
||||
where pollutant = 'aqi'
|
||||
GROUP BY space_id, event_date
|
||||
),
|
||||
|
||||
-- Pivot Categories into Columns
|
||||
daily_percentages AS (
|
||||
select
|
||||
dt.space_id,
|
||||
dt.event_date,
|
||||
-- AQI CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_aqi_percentage,
|
||||
-- PM25 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm25_percentage,
|
||||
-- PM10 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm10_percentage,
|
||||
-- VOC CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_voc_percentage,
|
||||
-- CO2 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_co2_percentage,
|
||||
-- CH20 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_ch2o_percentage
|
||||
FROM daily_totals dt
|
||||
LEFT JOIN daily_category_counts dcc
|
||||
ON dt.space_id = dcc.space_id AND dt.event_date = dcc.event_date
|
||||
GROUP BY dt.space_id, dt.event_date, dt.total_count
|
||||
),
|
||||
|
||||
daily_averages AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
-- AQI
|
||||
ROUND(AVG(hourly_min_aqi)::numeric, 2) AS daily_min_aqi,
|
||||
ROUND(AVG(hourly_avg_aqi)::numeric, 2) AS daily_avg_aqi,
|
||||
ROUND(AVG(hourly_max_aqi)::numeric, 2) AS daily_max_aqi,
|
||||
-- PM25
|
||||
ROUND(AVG(pm25_min_f)::numeric, 2) AS daily_min_pm25,
|
||||
ROUND(AVG(pm25_avg_f)::numeric, 2) AS daily_avg_pm25,
|
||||
ROUND(AVG(pm25_max_f)::numeric, 2) AS daily_max_pm25,
|
||||
-- PM10
|
||||
ROUND(AVG(pm10_min_f)::numeric, 2) AS daily_min_pm10,
|
||||
ROUND(AVG(pm10_avg_f)::numeric, 2) AS daily_avg_pm10,
|
||||
ROUND(AVG(pm10_max_f)::numeric, 2) AS daily_max_pm10,
|
||||
-- VOC
|
||||
ROUND(AVG(voc_min_f)::numeric, 2) AS daily_min_voc,
|
||||
ROUND(AVG(voc_avg_f)::numeric, 2) AS daily_avg_voc,
|
||||
ROUND(AVG(voc_max_f)::numeric, 2) AS daily_max_voc,
|
||||
-- CO2
|
||||
ROUND(AVG(co2_min_f)::numeric, 2) AS daily_min_co2,
|
||||
ROUND(AVG(co2_avg_f)::numeric, 2) AS daily_avg_co2,
|
||||
ROUND(AVG(co2_max_f)::numeric, 2) AS daily_max_co2,
|
||||
-- CH2O
|
||||
ROUND(AVG(ch2o_min_f)::numeric, 2) AS daily_min_ch2o,
|
||||
ROUND(AVG(ch2o_avg_f)::numeric, 2) AS daily_avg_ch2o,
|
||||
ROUND(AVG(ch2o_max_f)::numeric, 2) AS daily_max_ch2o
|
||||
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date
|
||||
),
|
||||
|
||||
final_data as(
|
||||
SELECT
|
||||
p.space_id,
|
||||
p.event_date,
|
||||
p.good_aqi_percentage, p.moderate_aqi_percentage, p.unhealthy_sensitive_aqi_percentage, p.unhealthy_aqi_percentage, p.very_unhealthy_aqi_percentage, p.hazardous_aqi_percentage,
|
||||
a.daily_avg_aqi,a.daily_max_aqi, a.daily_min_aqi,
|
||||
p.good_pm25_percentage, p.moderate_pm25_percentage, p.unhealthy_sensitive_pm25_percentage, p.unhealthy_pm25_percentage, p.very_unhealthy_pm25_percentage, p.hazardous_pm25_percentage,
|
||||
a.daily_avg_pm25,a.daily_max_pm25, a.daily_min_pm25,
|
||||
p.good_pm10_percentage, p.moderate_pm10_percentage, p.unhealthy_sensitive_pm10_percentage, p.unhealthy_pm10_percentage, p.very_unhealthy_pm10_percentage, p.hazardous_pm10_percentage,
|
||||
a.daily_avg_pm10, a.daily_max_pm10, a.daily_min_pm10,
|
||||
p.good_voc_percentage, p.moderate_voc_percentage, p.unhealthy_sensitive_voc_percentage, p.unhealthy_voc_percentage, p.very_unhealthy_voc_percentage, p.hazardous_voc_percentage,
|
||||
a.daily_avg_voc, a.daily_max_voc, a.daily_min_voc,
|
||||
p.good_co2_percentage, p.moderate_co2_percentage, p.unhealthy_sensitive_co2_percentage, p.unhealthy_co2_percentage, p.very_unhealthy_co2_percentage, p.hazardous_co2_percentage,
|
||||
a.daily_avg_co2,a.daily_max_co2, a.daily_min_co2,
|
||||
p.good_ch2o_percentage, p.moderate_ch2o_percentage, p.unhealthy_sensitive_ch2o_percentage, p.unhealthy_ch2o_percentage, p.very_unhealthy_ch2o_percentage, p.hazardous_ch2o_percentage,
|
||||
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
||||
FROM daily_percentages p
|
||||
LEFT JOIN daily_averages a
|
||||
ON p.space_id = a.space_id AND p.event_date = a.event_date
|
||||
ORDER BY p.space_id, p.event_date)
|
||||
|
||||
|
||||
INSERT INTO public."space-daily-pollutant-stats" (
|
||||
space_uuid,
|
||||
event_date,
|
||||
good_aqi_percentage, moderate_aqi_percentage, unhealthy_sensitive_aqi_percentage, unhealthy_aqi_percentage, very_unhealthy_aqi_percentage, hazardous_aqi_percentage,
|
||||
daily_avg_aqi, daily_max_aqi, daily_min_aqi,
|
||||
good_pm25_percentage, moderate_pm25_percentage, unhealthy_sensitive_pm25_percentage, unhealthy_pm25_percentage, very_unhealthy_pm25_percentage, hazardous_pm25_percentage,
|
||||
daily_avg_pm25, daily_max_pm25, daily_min_pm25,
|
||||
good_pm10_percentage, moderate_pm10_percentage, unhealthy_sensitive_pm10_percentage, unhealthy_pm10_percentage, very_unhealthy_pm10_percentage, hazardous_pm10_percentage,
|
||||
daily_avg_pm10, daily_max_pm10, daily_min_pm10,
|
||||
good_voc_percentage, moderate_voc_percentage, unhealthy_sensitive_voc_percentage, unhealthy_voc_percentage, very_unhealthy_voc_percentage, hazardous_voc_percentage,
|
||||
daily_avg_voc, daily_max_voc, daily_min_voc,
|
||||
good_co2_percentage, moderate_co2_percentage, unhealthy_sensitive_co2_percentage, unhealthy_co2_percentage, very_unhealthy_co2_percentage, hazardous_co2_percentage,
|
||||
daily_avg_co2, daily_max_co2, daily_min_co2,
|
||||
good_ch2o_percentage, moderate_ch2o_percentage, unhealthy_sensitive_ch2o_percentage, unhealthy_ch2o_percentage, very_unhealthy_ch2o_percentage, hazardous_ch2o_percentage,
|
||||
daily_avg_ch2o, daily_max_ch2o, daily_min_ch2o
|
||||
)
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
good_aqi_percentage, moderate_aqi_percentage, unhealthy_sensitive_aqi_percentage, unhealthy_aqi_percentage, very_unhealthy_aqi_percentage, hazardous_aqi_percentage,
|
||||
daily_avg_aqi, daily_max_aqi, daily_min_aqi,
|
||||
good_pm25_percentage, moderate_pm25_percentage, unhealthy_sensitive_pm25_percentage, unhealthy_pm25_percentage, very_unhealthy_pm25_percentage, hazardous_pm25_percentage,
|
||||
daily_avg_pm25, daily_max_pm25, daily_min_pm25,
|
||||
good_pm10_percentage, moderate_pm10_percentage, unhealthy_sensitive_pm10_percentage, unhealthy_pm10_percentage, very_unhealthy_pm10_percentage, hazardous_pm10_percentage,
|
||||
daily_avg_pm10, daily_max_pm10, daily_min_pm10,
|
||||
good_voc_percentage, moderate_voc_percentage, unhealthy_sensitive_voc_percentage, unhealthy_voc_percentage, very_unhealthy_voc_percentage, hazardous_voc_percentage,
|
||||
daily_avg_voc, daily_max_voc, daily_min_voc,
|
||||
good_co2_percentage, moderate_co2_percentage, unhealthy_sensitive_co2_percentage, unhealthy_co2_percentage, very_unhealthy_co2_percentage, hazardous_co2_percentage,
|
||||
daily_avg_co2, daily_max_co2, daily_min_co2,
|
||||
good_ch2o_percentage, moderate_ch2o_percentage, unhealthy_sensitive_ch2o_percentage, unhealthy_ch2o_percentage, very_unhealthy_ch2o_percentage, hazardous_ch2o_percentage,
|
||||
daily_avg_ch2o, daily_max_ch2o, daily_min_ch2o
|
||||
FROM final_data
|
||||
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
||||
SET
|
||||
good_aqi_percentage = EXCLUDED.good_aqi_percentage,
|
||||
moderate_aqi_percentage = EXCLUDED.moderate_aqi_percentage,
|
||||
unhealthy_sensitive_aqi_percentage = EXCLUDED.unhealthy_sensitive_aqi_percentage,
|
||||
unhealthy_aqi_percentage = EXCLUDED.unhealthy_aqi_percentage,
|
||||
very_unhealthy_aqi_percentage = EXCLUDED.very_unhealthy_aqi_percentage,
|
||||
hazardous_aqi_percentage = EXCLUDED.hazardous_aqi_percentage,
|
||||
daily_avg_aqi = EXCLUDED.daily_avg_aqi,
|
||||
daily_max_aqi = EXCLUDED.daily_max_aqi,
|
||||
daily_min_aqi = EXCLUDED.daily_min_aqi,
|
||||
good_pm25_percentage = EXCLUDED.good_pm25_percentage,
|
||||
moderate_pm25_percentage = EXCLUDED.moderate_pm25_percentage,
|
||||
unhealthy_sensitive_pm25_percentage = EXCLUDED.unhealthy_sensitive_pm25_percentage,
|
||||
unhealthy_pm25_percentage = EXCLUDED.unhealthy_pm25_percentage,
|
||||
very_unhealthy_pm25_percentage = EXCLUDED.very_unhealthy_pm25_percentage,
|
||||
hazardous_pm25_percentage = EXCLUDED.hazardous_pm25_percentage,
|
||||
daily_avg_pm25 = EXCLUDED.daily_avg_pm25,
|
||||
daily_max_pm25 = EXCLUDED.daily_max_pm25,
|
||||
daily_min_pm25 = EXCLUDED.daily_min_pm25,
|
||||
good_pm10_percentage = EXCLUDED.good_pm10_percentage,
|
||||
moderate_pm10_percentage = EXCLUDED.moderate_pm10_percentage,
|
||||
unhealthy_sensitive_pm10_percentage = EXCLUDED.unhealthy_sensitive_pm10_percentage,
|
||||
unhealthy_pm10_percentage = EXCLUDED.unhealthy_pm10_percentage,
|
||||
very_unhealthy_pm10_percentage = EXCLUDED.very_unhealthy_pm10_percentage,
|
||||
hazardous_pm10_percentage = EXCLUDED.hazardous_pm10_percentage,
|
||||
daily_avg_pm10 = EXCLUDED.daily_avg_pm10,
|
||||
daily_max_pm10 = EXCLUDED.daily_max_pm10,
|
||||
daily_min_pm10 = EXCLUDED.daily_min_pm10,
|
||||
good_voc_percentage = EXCLUDED.good_voc_percentage,
|
||||
moderate_voc_percentage = EXCLUDED.moderate_voc_percentage,
|
||||
unhealthy_sensitive_voc_percentage = EXCLUDED.unhealthy_sensitive_voc_percentage,
|
||||
unhealthy_voc_percentage = EXCLUDED.unhealthy_voc_percentage,
|
||||
very_unhealthy_voc_percentage = EXCLUDED.very_unhealthy_voc_percentage,
|
||||
hazardous_voc_percentage = EXCLUDED.hazardous_voc_percentage,
|
||||
daily_avg_voc = EXCLUDED.daily_avg_voc,
|
||||
daily_max_voc = EXCLUDED.daily_max_voc,
|
||||
daily_min_voc = EXCLUDED.daily_min_voc,
|
||||
good_co2_percentage = EXCLUDED.good_co2_percentage,
|
||||
moderate_co2_percentage = EXCLUDED.moderate_co2_percentage,
|
||||
unhealthy_sensitive_co2_percentage = EXCLUDED.unhealthy_sensitive_co2_percentage,
|
||||
unhealthy_co2_percentage = EXCLUDED.unhealthy_co2_percentage,
|
||||
very_unhealthy_co2_percentage = EXCLUDED.very_unhealthy_co2_percentage,
|
||||
hazardous_co2_percentage = EXCLUDED.hazardous_co2_percentage,
|
||||
daily_avg_co2 = EXCLUDED.daily_avg_co2,
|
||||
daily_max_co2 = EXCLUDED.daily_max_co2,
|
||||
daily_min_co2 = EXCLUDED.daily_min_co2,
|
||||
good_ch2o_percentage = EXCLUDED.good_ch2o_percentage,
|
||||
moderate_ch2o_percentage = EXCLUDED.moderate_ch2o_percentage,
|
||||
unhealthy_sensitive_ch2o_percentage = EXCLUDED.unhealthy_sensitive_ch2o_percentage,
|
||||
unhealthy_ch2o_percentage = EXCLUDED.unhealthy_ch2o_percentage,
|
||||
very_unhealthy_ch2o_percentage = EXCLUDED.very_unhealthy_ch2o_percentage,
|
||||
hazardous_ch2o_percentage = EXCLUDED.hazardous_ch2o_percentage,
|
||||
daily_avg_ch2o = EXCLUDED.daily_avg_ch2o,
|
||||
daily_max_ch2o = EXCLUDED.daily_max_ch2o,
|
||||
daily_min_ch2o = EXCLUDED.daily_min_ch2o;
|
||||
|
||||
|
||||
|
||||
@ -1,367 +0,0 @@
|
||||
-- Query Pipeline Starts Here
|
||||
WITH device_space AS (
|
||||
SELECT
|
||||
device.uuid AS device_id,
|
||||
device.space_device_uuid AS space_id,
|
||||
"device-status-log".event_time::timestamp AS event_time,
|
||||
"device-status-log".code,
|
||||
"device-status-log".value
|
||||
FROM device
|
||||
LEFT JOIN "device-status-log"
|
||||
ON device.uuid = "device-status-log".device_id
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
WHERE product.cat_name = 'hjjcy'
|
||||
),
|
||||
|
||||
average_pollutants AS (
|
||||
SELECT
|
||||
event_time::date AS event_date,
|
||||
date_trunc('hour', event_time) AS event_hour,
|
||||
space_id,
|
||||
|
||||
-- PM1
|
||||
MIN(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_min,
|
||||
AVG(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_avg,
|
||||
MAX(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_max,
|
||||
|
||||
-- PM25
|
||||
MIN(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_min,
|
||||
AVG(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_avg,
|
||||
MAX(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_max,
|
||||
|
||||
-- PM10
|
||||
MIN(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_min,
|
||||
AVG(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_avg,
|
||||
MAX(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_max,
|
||||
|
||||
-- VOC
|
||||
MIN(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_min,
|
||||
AVG(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_avg,
|
||||
MAX(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_max,
|
||||
|
||||
-- CH2O
|
||||
MIN(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_min,
|
||||
AVG(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_avg,
|
||||
MAX(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_max,
|
||||
|
||||
-- CO2
|
||||
MIN(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_min,
|
||||
AVG(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_avg,
|
||||
MAX(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_max
|
||||
|
||||
FROM device_space
|
||||
GROUP BY space_id, event_hour, event_date
|
||||
),
|
||||
|
||||
filled_pollutants AS (
|
||||
SELECT
|
||||
*,
|
||||
-- AVG
|
||||
COALESCE(pm25_avg, LAG(pm25_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_avg_f,
|
||||
COALESCE(pm10_avg, LAG(pm10_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_avg_f,
|
||||
COALESCE(voc_avg, LAG(voc_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_avg_f,
|
||||
COALESCE(co2_avg, LAG(co2_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_avg_f,
|
||||
COALESCE(ch2o_avg, LAG(ch2o_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_avg_f,
|
||||
|
||||
-- MIN
|
||||
COALESCE(pm25_min, LAG(pm25_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_min_f,
|
||||
COALESCE(pm10_min, LAG(pm10_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_min_f,
|
||||
COALESCE(voc_min, LAG(voc_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_min_f,
|
||||
COALESCE(co2_min, LAG(co2_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_min_f,
|
||||
COALESCE(ch2o_min, LAG(ch2o_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_min_f,
|
||||
|
||||
-- MAX
|
||||
COALESCE(pm25_max, LAG(pm25_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_max_f,
|
||||
COALESCE(pm10_max, LAG(pm10_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_max_f,
|
||||
COALESCE(voc_max, LAG(voc_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_max_f,
|
||||
COALESCE(co2_max, LAG(co2_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_max_f,
|
||||
COALESCE(ch2o_max, LAG(ch2o_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_max_f
|
||||
FROM average_pollutants
|
||||
),
|
||||
|
||||
hourly_results AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
event_hour,
|
||||
pm1_min, pm1_avg, pm1_max,
|
||||
pm25_min_f, pm25_avg_f, pm25_max_f,
|
||||
pm10_min_f, pm10_avg_f, pm10_max_f,
|
||||
voc_min_f, voc_avg_f, voc_max_f,
|
||||
co2_min_f, co2_avg_f, co2_max_f,
|
||||
ch2o_min_f, ch2o_avg_f, ch2o_max_f,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_min_f),
|
||||
calculate_aqi('pm10', pm10_min_f)
|
||||
) AS hourly_min_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
) AS hourly_avg_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_max_f),
|
||||
calculate_aqi('pm10', pm10_max_f)
|
||||
) AS hourly_max_aqi,
|
||||
|
||||
classify_aqi(GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
)) AS aqi_category,
|
||||
|
||||
classify_aqi(calculate_aqi('pm25',pm25_avg_f)) as pm25_category,
|
||||
classify_aqi(calculate_aqi('pm10',pm10_avg_f)) as pm10_category,
|
||||
classify_aqi(calculate_aqi('voc',voc_avg_f)) as voc_category,
|
||||
classify_aqi(calculate_aqi('co2',co2_avg_f)) as co2_category,
|
||||
classify_aqi(calculate_aqi('ch2o',ch2o_avg_f)) as ch2o_category
|
||||
|
||||
FROM filled_pollutants
|
||||
),
|
||||
|
||||
daily_category_counts AS (
|
||||
SELECT space_id, event_date, aqi_category AS category, 'aqi' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, aqi_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, pm25_category AS category, 'pm25' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, pm25_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, pm10_category AS category, 'pm10' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, pm10_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, voc_category AS category, 'voc' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, voc_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, co2_category AS category, 'co2' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, co2_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, ch2o_category AS category, 'ch2o' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, ch2o_category
|
||||
),
|
||||
|
||||
daily_totals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
SUM(category_count) AS total_count
|
||||
FROM daily_category_counts
|
||||
where pollutant = 'aqi'
|
||||
GROUP BY space_id, event_date
|
||||
),
|
||||
|
||||
-- Pivot Categories into Columns
|
||||
daily_percentages AS (
|
||||
select
|
||||
dt.space_id,
|
||||
dt.event_date,
|
||||
-- AQI CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_aqi_percentage,
|
||||
-- PM25 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm25_percentage,
|
||||
-- PM10 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm10_percentage,
|
||||
-- VOC CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_voc_percentage,
|
||||
-- CO2 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_co2_percentage,
|
||||
-- CH20 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_ch2o_percentage
|
||||
FROM daily_totals dt
|
||||
LEFT JOIN daily_category_counts dcc
|
||||
ON dt.space_id = dcc.space_id AND dt.event_date = dcc.event_date
|
||||
GROUP BY dt.space_id, dt.event_date, dt.total_count
|
||||
),
|
||||
|
||||
daily_averages AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
-- AQI
|
||||
ROUND(AVG(hourly_min_aqi)::numeric, 2) AS daily_min_aqi,
|
||||
ROUND(AVG(hourly_avg_aqi)::numeric, 2) AS daily_avg_aqi,
|
||||
ROUND(AVG(hourly_max_aqi)::numeric, 2) AS daily_max_aqi,
|
||||
-- PM25
|
||||
ROUND(AVG(pm25_min_f)::numeric, 2) AS daily_min_pm25,
|
||||
ROUND(AVG(pm25_avg_f)::numeric, 2) AS daily_avg_pm25,
|
||||
ROUND(AVG(pm25_max_f)::numeric, 2) AS daily_max_pm25,
|
||||
-- PM10
|
||||
ROUND(AVG(pm10_min_f)::numeric, 2) AS daily_min_pm10,
|
||||
ROUND(AVG(pm10_avg_f)::numeric, 2) AS daily_avg_pm10,
|
||||
ROUND(AVG(pm10_max_f)::numeric, 2) AS daily_max_pm10,
|
||||
-- VOC
|
||||
ROUND(AVG(voc_min_f)::numeric, 2) AS daily_min_voc,
|
||||
ROUND(AVG(voc_avg_f)::numeric, 2) AS daily_avg_voc,
|
||||
ROUND(AVG(voc_max_f)::numeric, 2) AS daily_max_voc,
|
||||
-- CO2
|
||||
ROUND(AVG(co2_min_f)::numeric, 2) AS daily_min_co2,
|
||||
ROUND(AVG(co2_avg_f)::numeric, 2) AS daily_avg_co2,
|
||||
ROUND(AVG(co2_max_f)::numeric, 2) AS daily_max_co2,
|
||||
-- CH2O
|
||||
ROUND(AVG(ch2o_min_f)::numeric, 2) AS daily_min_ch2o,
|
||||
ROUND(AVG(ch2o_avg_f)::numeric, 2) AS daily_avg_ch2o,
|
||||
ROUND(AVG(ch2o_max_f)::numeric, 2) AS daily_max_ch2o
|
||||
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date
|
||||
),
|
||||
|
||||
final_data as(
|
||||
SELECT
|
||||
p.space_id,
|
||||
p.event_date,
|
||||
p.good_aqi_percentage, p.moderate_aqi_percentage, p.unhealthy_sensitive_aqi_percentage, p.unhealthy_aqi_percentage, p.very_unhealthy_aqi_percentage, p.hazardous_aqi_percentage,
|
||||
a.daily_avg_aqi,a.daily_max_aqi, a.daily_min_aqi,
|
||||
p.good_pm25_percentage, p.moderate_pm25_percentage, p.unhealthy_sensitive_pm25_percentage, p.unhealthy_pm25_percentage, p.very_unhealthy_pm25_percentage, p.hazardous_pm25_percentage,
|
||||
a.daily_avg_pm25,a.daily_max_pm25, a.daily_min_pm25,
|
||||
p.good_pm10_percentage, p.moderate_pm10_percentage, p.unhealthy_sensitive_pm10_percentage, p.unhealthy_pm10_percentage, p.very_unhealthy_pm10_percentage, p.hazardous_pm10_percentage,
|
||||
a.daily_avg_pm10, a.daily_max_pm10, a.daily_min_pm10,
|
||||
p.good_voc_percentage, p.moderate_voc_percentage, p.unhealthy_sensitive_voc_percentage, p.unhealthy_voc_percentage, p.very_unhealthy_voc_percentage, p.hazardous_voc_percentage,
|
||||
a.daily_avg_voc, a.daily_max_voc, a.daily_min_voc,
|
||||
p.good_co2_percentage, p.moderate_co2_percentage, p.unhealthy_sensitive_co2_percentage, p.unhealthy_co2_percentage, p.very_unhealthy_co2_percentage, p.hazardous_co2_percentage,
|
||||
a.daily_avg_co2,a.daily_max_co2, a.daily_min_co2,
|
||||
p.good_ch2o_percentage, p.moderate_ch2o_percentage, p.unhealthy_sensitive_ch2o_percentage, p.unhealthy_ch2o_percentage, p.very_unhealthy_ch2o_percentage, p.hazardous_ch2o_percentage,
|
||||
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
||||
FROM daily_percentages p
|
||||
LEFT JOIN daily_averages a
|
||||
ON p.space_id = a.space_id AND p.event_date = a.event_date
|
||||
ORDER BY p.space_id, p.event_date)
|
||||
|
||||
|
||||
INSERT INTO public."space-daily-pollutant-stats" (
|
||||
space_uuid,
|
||||
event_date,
|
||||
good_aqi_percentage, moderate_aqi_percentage, unhealthy_sensitive_aqi_percentage, unhealthy_aqi_percentage, very_unhealthy_aqi_percentage, hazardous_aqi_percentage,
|
||||
daily_avg_aqi, daily_max_aqi, daily_min_aqi,
|
||||
good_pm25_percentage, moderate_pm25_percentage, unhealthy_sensitive_pm25_percentage, unhealthy_pm25_percentage, very_unhealthy_pm25_percentage, hazardous_pm25_percentage,
|
||||
daily_avg_pm25, daily_max_pm25, daily_min_pm25,
|
||||
good_pm10_percentage, moderate_pm10_percentage, unhealthy_sensitive_pm10_percentage, unhealthy_pm10_percentage, very_unhealthy_pm10_percentage, hazardous_pm10_percentage,
|
||||
daily_avg_pm10, daily_max_pm10, daily_min_pm10,
|
||||
good_voc_percentage, moderate_voc_percentage, unhealthy_sensitive_voc_percentage, unhealthy_voc_percentage, very_unhealthy_voc_percentage, hazardous_voc_percentage,
|
||||
daily_avg_voc, daily_max_voc, daily_min_voc,
|
||||
good_co2_percentage, moderate_co2_percentage, unhealthy_sensitive_co2_percentage, unhealthy_co2_percentage, very_unhealthy_co2_percentage, hazardous_co2_percentage,
|
||||
daily_avg_co2, daily_max_co2, daily_min_co2,
|
||||
good_ch2o_percentage, moderate_ch2o_percentage, unhealthy_sensitive_ch2o_percentage, unhealthy_ch2o_percentage, very_unhealthy_ch2o_percentage, hazardous_ch2o_percentage,
|
||||
daily_avg_ch2o, daily_max_ch2o, daily_min_ch2o
|
||||
)
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
good_aqi_percentage, moderate_aqi_percentage, unhealthy_sensitive_aqi_percentage, unhealthy_aqi_percentage, very_unhealthy_aqi_percentage, hazardous_aqi_percentage,
|
||||
daily_avg_aqi, daily_max_aqi, daily_min_aqi,
|
||||
good_pm25_percentage, moderate_pm25_percentage, unhealthy_sensitive_pm25_percentage, unhealthy_pm25_percentage, very_unhealthy_pm25_percentage, hazardous_pm25_percentage,
|
||||
daily_avg_pm25, daily_max_pm25, daily_min_pm25,
|
||||
good_pm10_percentage, moderate_pm10_percentage, unhealthy_sensitive_pm10_percentage, unhealthy_pm10_percentage, very_unhealthy_pm10_percentage, hazardous_pm10_percentage,
|
||||
daily_avg_pm10, daily_max_pm10, daily_min_pm10,
|
||||
good_voc_percentage, moderate_voc_percentage, unhealthy_sensitive_voc_percentage, unhealthy_voc_percentage, very_unhealthy_voc_percentage, hazardous_voc_percentage,
|
||||
daily_avg_voc, daily_max_voc, daily_min_voc,
|
||||
good_co2_percentage, moderate_co2_percentage, unhealthy_sensitive_co2_percentage, unhealthy_co2_percentage, very_unhealthy_co2_percentage, hazardous_co2_percentage,
|
||||
daily_avg_co2, daily_max_co2, daily_min_co2,
|
||||
good_ch2o_percentage, moderate_ch2o_percentage, unhealthy_sensitive_ch2o_percentage, unhealthy_ch2o_percentage, very_unhealthy_ch2o_percentage, hazardous_ch2o_percentage,
|
||||
daily_avg_ch2o, daily_max_ch2o, daily_min_ch2o
|
||||
FROM final_data
|
||||
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
||||
SET
|
||||
good_aqi_percentage = EXCLUDED.good_aqi_percentage,
|
||||
moderate_aqi_percentage = EXCLUDED.moderate_aqi_percentage,
|
||||
unhealthy_sensitive_aqi_percentage = EXCLUDED.unhealthy_sensitive_aqi_percentage,
|
||||
unhealthy_aqi_percentage = EXCLUDED.unhealthy_aqi_percentage,
|
||||
very_unhealthy_aqi_percentage = EXCLUDED.very_unhealthy_aqi_percentage,
|
||||
hazardous_aqi_percentage = EXCLUDED.hazardous_aqi_percentage,
|
||||
daily_avg_aqi = EXCLUDED.daily_avg_aqi,
|
||||
daily_max_aqi = EXCLUDED.daily_max_aqi,
|
||||
daily_min_aqi = EXCLUDED.daily_min_aqi,
|
||||
good_pm25_percentage = EXCLUDED.good_pm25_percentage,
|
||||
moderate_pm25_percentage = EXCLUDED.moderate_pm25_percentage,
|
||||
unhealthy_sensitive_pm25_percentage = EXCLUDED.unhealthy_sensitive_pm25_percentage,
|
||||
unhealthy_pm25_percentage = EXCLUDED.unhealthy_pm25_percentage,
|
||||
very_unhealthy_pm25_percentage = EXCLUDED.very_unhealthy_pm25_percentage,
|
||||
hazardous_pm25_percentage = EXCLUDED.hazardous_pm25_percentage,
|
||||
daily_avg_pm25 = EXCLUDED.daily_avg_pm25,
|
||||
daily_max_pm25 = EXCLUDED.daily_max_pm25,
|
||||
daily_min_pm25 = EXCLUDED.daily_min_pm25,
|
||||
good_pm10_percentage = EXCLUDED.good_pm10_percentage,
|
||||
moderate_pm10_percentage = EXCLUDED.moderate_pm10_percentage,
|
||||
unhealthy_sensitive_pm10_percentage = EXCLUDED.unhealthy_sensitive_pm10_percentage,
|
||||
unhealthy_pm10_percentage = EXCLUDED.unhealthy_pm10_percentage,
|
||||
very_unhealthy_pm10_percentage = EXCLUDED.very_unhealthy_pm10_percentage,
|
||||
hazardous_pm10_percentage = EXCLUDED.hazardous_pm10_percentage,
|
||||
daily_avg_pm10 = EXCLUDED.daily_avg_pm10,
|
||||
daily_max_pm10 = EXCLUDED.daily_max_pm10,
|
||||
daily_min_pm10 = EXCLUDED.daily_min_pm10,
|
||||
good_voc_percentage = EXCLUDED.good_voc_percentage,
|
||||
moderate_voc_percentage = EXCLUDED.moderate_voc_percentage,
|
||||
unhealthy_sensitive_voc_percentage = EXCLUDED.unhealthy_sensitive_voc_percentage,
|
||||
unhealthy_voc_percentage = EXCLUDED.unhealthy_voc_percentage,
|
||||
very_unhealthy_voc_percentage = EXCLUDED.very_unhealthy_voc_percentage,
|
||||
hazardous_voc_percentage = EXCLUDED.hazardous_voc_percentage,
|
||||
daily_avg_voc = EXCLUDED.daily_avg_voc,
|
||||
daily_max_voc = EXCLUDED.daily_max_voc,
|
||||
daily_min_voc = EXCLUDED.daily_min_voc,
|
||||
good_co2_percentage = EXCLUDED.good_co2_percentage,
|
||||
moderate_co2_percentage = EXCLUDED.moderate_co2_percentage,
|
||||
unhealthy_sensitive_co2_percentage = EXCLUDED.unhealthy_sensitive_co2_percentage,
|
||||
unhealthy_co2_percentage = EXCLUDED.unhealthy_co2_percentage,
|
||||
very_unhealthy_co2_percentage = EXCLUDED.very_unhealthy_co2_percentage,
|
||||
hazardous_co2_percentage = EXCLUDED.hazardous_co2_percentage,
|
||||
daily_avg_co2 = EXCLUDED.daily_avg_co2,
|
||||
daily_max_co2 = EXCLUDED.daily_max_co2,
|
||||
daily_min_co2 = EXCLUDED.daily_min_co2,
|
||||
good_ch2o_percentage = EXCLUDED.good_ch2o_percentage,
|
||||
moderate_ch2o_percentage = EXCLUDED.moderate_ch2o_percentage,
|
||||
unhealthy_sensitive_ch2o_percentage = EXCLUDED.unhealthy_sensitive_ch2o_percentage,
|
||||
unhealthy_ch2o_percentage = EXCLUDED.unhealthy_ch2o_percentage,
|
||||
very_unhealthy_ch2o_percentage = EXCLUDED.very_unhealthy_ch2o_percentage,
|
||||
hazardous_ch2o_percentage = EXCLUDED.hazardous_ch2o_percentage,
|
||||
daily_avg_ch2o = EXCLUDED.daily_avg_ch2o,
|
||||
daily_max_ch2o = EXCLUDED.daily_max_ch2o,
|
||||
daily_min_ch2o = EXCLUDED.daily_min_ch2o;
|
||||
|
||||
|
||||
@ -1,171 +0,0 @@
|
||||
WITH presence_logs AS (
|
||||
SELECT
|
||||
d.space_device_uuid AS space_id,
|
||||
l.device_id,
|
||||
l.event_time,
|
||||
l.value,
|
||||
LAG(l.event_time) OVER (PARTITION BY l.device_id ORDER BY l.event_time) AS prev_time,
|
||||
LAG(l.value) OVER (PARTITION BY l.device_id ORDER BY l.event_time) AS prev_value
|
||||
FROM device d
|
||||
JOIN "device-status-log" l ON d.uuid = l.device_id
|
||||
JOIN product p ON p.uuid = d.product_device_uuid
|
||||
WHERE l.code = 'presence_state'
|
||||
AND p.cat_name = 'hps'
|
||||
),
|
||||
|
||||
raw_absence_intervals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
device_id,
|
||||
prev_time AS start_time,
|
||||
event_time AS end_time
|
||||
FROM presence_logs
|
||||
WHERE prev_value = 'none' AND prev_time IS NOT NULL
|
||||
),
|
||||
|
||||
absence_intervals AS (
|
||||
SELECT
|
||||
r.space_id,
|
||||
r.device_id,
|
||||
gs.day,
|
||||
GREATEST(r.start_time, gs.day) AS start_time,
|
||||
LEAST(r.end_time, gs.day + INTERVAL '1 day') AS end_time
|
||||
FROM raw_absence_intervals r
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT generate_series(
|
||||
date_trunc('day', r.start_time),
|
||||
date_trunc('day', r.end_time),
|
||||
INTERVAL '1 day'
|
||||
) AS day
|
||||
) gs
|
||||
WHERE GREATEST(r.start_time, gs.day) < LEAST(r.end_time, gs.day + INTERVAL '1 day')
|
||||
),
|
||||
|
||||
device_counts AS (
|
||||
SELECT space_id, day, COUNT(DISTINCT device_id) AS device_count
|
||||
FROM absence_intervals
|
||||
GROUP BY 1, 2
|
||||
),
|
||||
|
||||
timeline AS (
|
||||
SELECT
|
||||
a.space_id,
|
||||
a.day,
|
||||
a.device_id,
|
||||
a.start_time AS ts,
|
||||
1 AS is_start
|
||||
FROM absence_intervals a
|
||||
UNION ALL
|
||||
SELECT
|
||||
a.space_id,
|
||||
a.day,
|
||||
a.device_id,
|
||||
a.end_time AS ts,
|
||||
0 AS is_start
|
||||
FROM absence_intervals a
|
||||
),
|
||||
|
||||
ordered_events AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
ts,
|
||||
is_start,
|
||||
device_id,
|
||||
SUM(CASE WHEN is_start = 1 THEN 1 ELSE -1 END)
|
||||
OVER (PARTITION BY space_id, day, device_id ORDER BY ts) AS device_active
|
||||
FROM timeline
|
||||
),
|
||||
|
||||
device_state_changes AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
ts,
|
||||
SUM(CASE WHEN is_start = 1 THEN 1 ELSE -1 END)
|
||||
OVER (PARTITION BY space_id, day ORDER BY ts) AS net_active_devices
|
||||
FROM (
|
||||
SELECT DISTINCT space_id, day, ts, is_start
|
||||
FROM timeline
|
||||
) t
|
||||
),
|
||||
|
||||
absence_windows AS (
|
||||
SELECT
|
||||
dc.space_id,
|
||||
dc.day,
|
||||
ts AS start_time,
|
||||
LEAD(ts) OVER (PARTITION BY dc.space_id, dc.day ORDER BY ts) AS end_time
|
||||
FROM device_state_changes dsc
|
||||
JOIN device_counts dc ON dc.space_id = dsc.space_id AND dc.day = dsc.day
|
||||
WHERE net_active_devices = 0
|
||||
),
|
||||
|
||||
empty_periods AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
EXTRACT(EPOCH FROM (end_time - start_time)) AS unoccupied_seconds
|
||||
FROM absence_windows
|
||||
WHERE end_time IS NOT NULL
|
||||
),
|
||||
|
||||
unoccupied_summary AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
SUM(unoccupied_seconds) AS total_unoccupied_seconds
|
||||
FROM empty_periods
|
||||
GROUP BY space_id, day
|
||||
),
|
||||
|
||||
-- Include device count even for days with 0 unoccupied time
|
||||
all_days_with_devices AS (
|
||||
SELECT
|
||||
space_id,
|
||||
DATE(event_time) AS day,
|
||||
COUNT(DISTINCT device_id) AS device_count
|
||||
FROM presence_logs
|
||||
GROUP BY 1, 2
|
||||
),
|
||||
|
||||
final_occupancy AS (
|
||||
SELECT
|
||||
d.space_id,
|
||||
d.day,
|
||||
d.device_count,
|
||||
COALESCE(u.total_unoccupied_seconds, 0) AS unoccupied_seconds,
|
||||
86400 - COALESCE(u.total_unoccupied_seconds, 0) AS occupied_seconds
|
||||
FROM all_days_with_devices d
|
||||
LEFT JOIN unoccupied_summary u
|
||||
ON d.space_id = u.space_id AND d.day = u.day
|
||||
)
|
||||
, final_data as (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
device_count,
|
||||
occupied_seconds,
|
||||
ROUND(occupied_seconds / 86400.0 * 100, 2) AS occupancy_percentage
|
||||
FROM final_occupancy
|
||||
ORDER BY space_id, day)
|
||||
|
||||
|
||||
INSERT INTO public."space-daily-occupancy-duration" (
|
||||
space_uuid,
|
||||
event_date,
|
||||
occupied_seconds,
|
||||
occupancy_percentage
|
||||
)
|
||||
select space_id,
|
||||
event_date,
|
||||
occupied_seconds,
|
||||
occupancy_percentage
|
||||
FROM final_data
|
||||
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
||||
SET
|
||||
occupancy_percentage = EXCLUDED.occupancy_percentage,
|
||||
occupied_seconds = EXCLUDED.occupied_seconds;
|
||||
|
||||
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
$1::uuid AS space_uuid,
|
||||
TO_DATE(NULLIF($2, ''), 'YYYY-MM') AS event_month
|
||||
)
|
||||
|
||||
|
||||
SELECT sdo.space_uuid,
|
||||
event_date,
|
||||
occupancy_percentage,
|
||||
occupied_seconds
|
||||
FROM public."space-daily-occupancy-duration" as sdo
|
||||
JOIN params P ON true
|
||||
where (sdo.space_uuid = P.space_uuid
|
||||
OR P.event_month IS null)
|
||||
AND TO_CHAR(sdo.event_date, 'YYYY-MM') = TO_CHAR(P.event_month, 'YYYY-MM')
|
||||
ORDER BY sdo.space_uuid, sdo.event_date;
|
||||
@ -1,180 +0,0 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
||||
$2::uuid AS space_id
|
||||
),
|
||||
|
||||
presence_logs AS (
|
||||
SELECT
|
||||
d.space_device_uuid AS space_id,
|
||||
l.device_id,
|
||||
l.event_time,
|
||||
l.value,
|
||||
LAG(l.event_time) OVER (PARTITION BY l.device_id ORDER BY l.event_time) AS prev_time,
|
||||
LAG(l.value) OVER (PARTITION BY l.device_id ORDER BY l.event_time) AS prev_value
|
||||
FROM device d
|
||||
JOIN "device-status-log" l ON d.uuid = l.device_id
|
||||
JOIN product p ON p.uuid = d.product_device_uuid
|
||||
WHERE l.code = 'presence_state'
|
||||
AND p.cat_name = 'hps'
|
||||
),
|
||||
|
||||
raw_absence_intervals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
device_id,
|
||||
prev_time AS start_time,
|
||||
event_time AS end_time
|
||||
FROM presence_logs
|
||||
WHERE prev_value = 'none' AND prev_time IS NOT NULL
|
||||
),
|
||||
|
||||
absence_intervals AS (
|
||||
SELECT
|
||||
r.space_id,
|
||||
r.device_id,
|
||||
gs.day,
|
||||
GREATEST(r.start_time, gs.day) AS start_time,
|
||||
LEAST(r.end_time, gs.day + INTERVAL '1 day') AS end_time
|
||||
FROM raw_absence_intervals r
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT generate_series(
|
||||
date_trunc('day', r.start_time),
|
||||
date_trunc('day', r.end_time),
|
||||
INTERVAL '1 day'
|
||||
) AS day
|
||||
) gs
|
||||
WHERE GREATEST(r.start_time, gs.day) < LEAST(r.end_time, gs.day + INTERVAL '1 day')
|
||||
),
|
||||
|
||||
device_counts AS (
|
||||
SELECT space_id, day, COUNT(DISTINCT device_id) AS device_count
|
||||
FROM absence_intervals
|
||||
GROUP BY 1, 2
|
||||
),
|
||||
|
||||
timeline AS (
|
||||
SELECT
|
||||
a.space_id,
|
||||
a.day,
|
||||
a.device_id,
|
||||
a.start_time AS ts,
|
||||
1 AS is_start
|
||||
FROM absence_intervals a
|
||||
UNION ALL
|
||||
SELECT
|
||||
a.space_id,
|
||||
a.day,
|
||||
a.device_id,
|
||||
a.end_time AS ts,
|
||||
0 AS is_start
|
||||
FROM absence_intervals a
|
||||
),
|
||||
|
||||
ordered_events AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
ts,
|
||||
is_start,
|
||||
device_id,
|
||||
SUM(CASE WHEN is_start = 1 THEN 1 ELSE -1 END)
|
||||
OVER (PARTITION BY space_id, day, device_id ORDER BY ts) AS device_active
|
||||
FROM timeline
|
||||
),
|
||||
|
||||
device_state_changes AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
ts,
|
||||
SUM(CASE WHEN is_start = 1 THEN 1 ELSE -1 END)
|
||||
OVER (PARTITION BY space_id, day ORDER BY ts) AS net_active_devices
|
||||
FROM (
|
||||
SELECT DISTINCT space_id, day, ts, is_start
|
||||
FROM timeline
|
||||
) t
|
||||
),
|
||||
|
||||
absence_windows AS (
|
||||
SELECT
|
||||
dc.space_id,
|
||||
dc.day,
|
||||
ts AS start_time,
|
||||
LEAD(ts) OVER (PARTITION BY dc.space_id, dc.day ORDER BY ts) AS end_time
|
||||
FROM device_state_changes dsc
|
||||
JOIN device_counts dc ON dc.space_id = dsc.space_id AND dc.day = dsc.day
|
||||
WHERE net_active_devices = 0
|
||||
),
|
||||
|
||||
empty_periods AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
EXTRACT(EPOCH FROM (end_time - start_time)) AS unoccupied_seconds
|
||||
FROM absence_windows
|
||||
WHERE end_time IS NOT NULL
|
||||
),
|
||||
|
||||
unoccupied_summary AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
SUM(unoccupied_seconds) AS total_unoccupied_seconds
|
||||
FROM empty_periods
|
||||
GROUP BY space_id, day
|
||||
),
|
||||
|
||||
-- Include device count even for days with 0 unoccupied time
|
||||
all_days_with_devices AS (
|
||||
SELECT
|
||||
space_id,
|
||||
DATE(event_time) AS day,
|
||||
COUNT(DISTINCT device_id) AS device_count
|
||||
FROM presence_logs
|
||||
GROUP BY 1, 2
|
||||
),
|
||||
|
||||
final_occupancy AS (
|
||||
SELECT
|
||||
d.space_id,
|
||||
d.day,
|
||||
d.device_count,
|
||||
COALESCE(u.total_unoccupied_seconds, 0) AS unoccupied_seconds,
|
||||
86400 - COALESCE(u.total_unoccupied_seconds, 0) AS occupied_seconds
|
||||
FROM all_days_with_devices d
|
||||
LEFT JOIN unoccupied_summary u
|
||||
ON d.space_id = u.space_id AND d.day = u.day
|
||||
),
|
||||
|
||||
final_data as(
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
device_count,
|
||||
occupied_seconds,
|
||||
ROUND(occupied_seconds / 86400.0 * 100, 2) AS occupancy_percentage
|
||||
FROM final_occupancy s
|
||||
JOIN params p
|
||||
ON p.space_id = s.space_id
|
||||
AND p.event_date = s.event_date
|
||||
ORDER BY space_id, DAY)
|
||||
|
||||
|
||||
INSERT INTO public."space-daily-occupancy-duration" (
|
||||
space_uuid,
|
||||
event_date,
|
||||
occupied_seconds,
|
||||
occupancy_percentage
|
||||
)
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
occupied_seconds,
|
||||
occupancy_percentage
|
||||
FROM final_data
|
||||
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
||||
SET
|
||||
occupancy_percentage = EXCLUDED.occupancy_percentage,
|
||||
occupied_seconds = EXCLUDED.occupied_seconds;
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
TO_DATE(NULLIF($2, ''), 'YYYY') AS year,
|
||||
string_to_array(NULLIF($4, ''), ',') AS device_ids
|
||||
)
|
||||
|
||||
SELECT
|
||||
A.device_uuid,
|
||||
TO_CHAR(date_trunc('month', A.event_date), 'YYYY-MM') AS event_month,
|
||||
SUM(A.count_motion_detected) AS total_motion_detected,
|
||||
SUM(A.count_presence_detected) AS total_presence_detected,
|
||||
SUM(A.count_total_presence_detected) AS total_overall_presence
|
||||
FROM public."presence-sensor-daily-device-detection" AS A
|
||||
JOIN params P ON TRUE
|
||||
WHERE A.device_uuid::text = ANY(P.device_ids)
|
||||
AND (
|
||||
P.year IS NULL
|
||||
OR date_trunc('year', A.event_date) = P.year
|
||||
)
|
||||
GROUP BY 1,2
|
||||
ORDER BY 1,2;
|
||||
@ -1,20 +0,0 @@
|
||||
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;
|
||||
@ -1,8 +1,8 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
TO_DATE(NULLIF($1, ''), 'DD-MM-YYYY') AS start_date,
|
||||
TO_DATE(NULLIF($2, ''), 'DD-MM-YYYY') AS end_date,
|
||||
string_to_array(NULLIF($3, ''), ',') AS device_ids
|
||||
TO_DATE(NULLIF($2, ''), 'DD-MM-YYYY') AS start_date,
|
||||
TO_DATE(NULLIF($3, ''), 'DD-MM-YYYY') AS end_date,
|
||||
string_to_array(NULLIF($4, ''), ',') AS device_ids
|
||||
)
|
||||
|
||||
SELECT TO_CHAR(A.date, 'MM-YYYY') AS month,
|
||||
|
||||
@ -26,7 +26,6 @@ device_logs AS (
|
||||
ON device.uuid = "device-status-log".device_id
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
JOIN params P ON TRUE
|
||||
WHERE product.cat_name = 'hps'
|
||||
AND "device-status-log".code = 'presence_state'
|
||||
AND device.uuid::text = P.device_id
|
||||
@ -94,7 +93,7 @@ daily_aggregates AS (
|
||||
GROUP BY device_id, event_date
|
||||
)
|
||||
|
||||
INSERT INTO public."presence-sensor-daily-device-detection" (
|
||||
INSERT INTO public."presence-sensor-daily-detection" (
|
||||
device_uuid,
|
||||
event_date,
|
||||
count_motion_detected,
|
||||
@ -11,10 +11,9 @@ WITH params AS (
|
||||
A.count_motion_detected,
|
||||
A.count_presence_detected,
|
||||
A.count_total_presence_detected
|
||||
FROM public."presence-sensor-daily-device-detection" AS A
|
||||
FROM public."presence-sensor-daily-detection" AS A
|
||||
JOIN params P ON TRUE
|
||||
WHERE A.device_uuid::text = ANY(P.device_ids)
|
||||
AND (P.month IS NULL
|
||||
OR date_trunc('month', A.event_date) = P.month
|
||||
);
|
||||
|
||||
)
|
||||
@ -1,19 +0,0 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
$1::uuid AS space_id,
|
||||
TO_DATE(NULLIF($2, ''), 'YYYY') AS event_year
|
||||
)
|
||||
|
||||
SELECT psdsd.*
|
||||
FROM public."presence-sensor-daily-space-detection" psdsd
|
||||
JOIN params P ON true
|
||||
WHERE psdsd.space_uuid = P.space_id
|
||||
AND (
|
||||
P.event_year IS NULL
|
||||
OR TO_CHAR(psdsd.event_date, 'YYYY') = TO_CHAR(P.event_year, 'YYYY')
|
||||
)
|
||||
ORDER BY space_uuid, event_date
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,100 +0,0 @@
|
||||
WITH device_logs AS (
|
||||
SELECT
|
||||
device.uuid AS device_id,
|
||||
device.space_device_uuid AS space_id,
|
||||
"device-status-log".event_time::timestamp AS event_time,
|
||||
"device-status-log".value,
|
||||
LAG("device-status-log".value)
|
||||
OVER (PARTITION BY device.uuid ORDER BY "device-status-log".event_time) AS prev_value
|
||||
FROM device
|
||||
LEFT JOIN "device-status-log"
|
||||
ON device.uuid = "device-status-log".device_id
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
WHERE product.cat_name = 'hps'
|
||||
AND "device-status-log".code = 'presence_state'
|
||||
),
|
||||
|
||||
-- 1. All 'none' → presence or motion
|
||||
presence_transitions AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_time,
|
||||
event_time::date AS event_date,
|
||||
value
|
||||
FROM device_logs
|
||||
WHERE (value = 'motion' OR value = 'presence') AND prev_value = 'none'
|
||||
),
|
||||
|
||||
-- 2. Cluster events per space_id within 30s
|
||||
clustered_events AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_time,
|
||||
event_date,
|
||||
value,
|
||||
SUM(new_cluster_flag) OVER (PARTITION BY space_id ORDER BY event_time) AS cluster_id
|
||||
FROM (
|
||||
SELECT *,
|
||||
CASE
|
||||
WHEN event_time - LAG(event_time) OVER (PARTITION BY space_id ORDER BY event_time) > INTERVAL '30 seconds'
|
||||
THEN 1 ELSE 0
|
||||
END AS new_cluster_flag
|
||||
FROM presence_transitions
|
||||
) marked
|
||||
),
|
||||
|
||||
-- 3. Determine dominant type (motion vs presence) per cluster
|
||||
cluster_type AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
cluster_id,
|
||||
COUNT(*) FILTER (WHERE value = 'motion') AS motion_count,
|
||||
COUNT(*) FILTER (WHERE value = 'presence') AS presence_count,
|
||||
CASE
|
||||
WHEN COUNT(*) FILTER (WHERE value = 'motion') > COUNT(*) FILTER (WHERE value = 'presence') THEN 'motion'
|
||||
ELSE 'presence'
|
||||
END AS dominant_type
|
||||
FROM clustered_events
|
||||
GROUP BY space_id, event_date, cluster_id
|
||||
),
|
||||
|
||||
-- 4. Count clusters by dominant type
|
||||
summary AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
COUNT(*) FILTER (WHERE dominant_type = 'motion') AS count_motion_detected,
|
||||
COUNT(*) FILTER (WHERE dominant_type = 'presence') AS count_presence_detected,
|
||||
COUNT(*) AS count_total_presence_detected
|
||||
FROM cluster_type
|
||||
GROUP BY space_id, event_date
|
||||
)
|
||||
|
||||
-- 5. Output
|
||||
, final_table as (
|
||||
SELECT *
|
||||
FROM summary
|
||||
ORDER BY space_id, event_date)
|
||||
|
||||
|
||||
INSERT INTO public."presence-sensor-daily-space-detection" (
|
||||
space_uuid,
|
||||
event_date,
|
||||
count_motion_detected,
|
||||
count_presence_detected,
|
||||
count_total_presence_detected
|
||||
)
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
count_motion_detected,
|
||||
count_presence_detected,
|
||||
count_total_presence_detected
|
||||
FROM final_table
|
||||
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
||||
SET
|
||||
count_motion_detected = EXCLUDED.count_motion_detected,
|
||||
count_presence_detected = EXCLUDED.count_presence_detected,
|
||||
count_total_presence_detected = EXCLUDED.count_total_presence_detected;
|
||||
@ -1,113 +0,0 @@
|
||||
WITH params AS (
|
||||
SELECT
|
||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
||||
$2::uuid AS space_id
|
||||
),
|
||||
|
||||
device_logs AS (
|
||||
SELECT
|
||||
device.uuid AS device_id,
|
||||
device.space_device_uuid AS space_id,
|
||||
"device-status-log".event_time::timestamp AS event_time,
|
||||
"device-status-log".value,
|
||||
LAG("device-status-log".value)
|
||||
OVER (PARTITION BY device.uuid ORDER BY "device-status-log".event_time) AS prev_value
|
||||
FROM device
|
||||
LEFT JOIN "device-status-log"
|
||||
ON device.uuid = "device-status-log".device_id
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
WHERE product.cat_name = 'hps'
|
||||
AND "device-status-log".code = 'presence_state'
|
||||
),
|
||||
|
||||
-- 1. All 'none' → presence or motion
|
||||
presence_transitions AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_time,
|
||||
event_time::date AS event_date,
|
||||
value
|
||||
FROM device_logs
|
||||
WHERE (value = 'motion' OR value = 'presence') AND prev_value = 'none'
|
||||
),
|
||||
|
||||
-- 2. Cluster events per space_id within 30s
|
||||
clustered_events AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_time,
|
||||
event_date,
|
||||
value,
|
||||
SUM(new_cluster_flag) OVER (PARTITION BY space_id ORDER BY event_time) AS cluster_id
|
||||
FROM (
|
||||
SELECT *,
|
||||
CASE
|
||||
WHEN event_time - LAG(event_time) OVER (PARTITION BY space_id ORDER BY event_time) > INTERVAL '30 seconds'
|
||||
THEN 1 ELSE 0
|
||||
END AS new_cluster_flag
|
||||
FROM presence_transitions
|
||||
) marked
|
||||
),
|
||||
|
||||
-- 3. Determine dominant type (motion vs presence) per cluster
|
||||
cluster_type AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
cluster_id,
|
||||
COUNT(*) FILTER (WHERE value = 'motion') AS motion_count,
|
||||
COUNT(*) FILTER (WHERE value = 'presence') AS presence_count,
|
||||
CASE
|
||||
WHEN COUNT(*) FILTER (WHERE value = 'motion') > COUNT(*) FILTER (WHERE value = 'presence') THEN 'motion'
|
||||
ELSE 'presence'
|
||||
END AS dominant_type
|
||||
FROM clustered_events
|
||||
GROUP BY space_id, event_date, cluster_id
|
||||
),
|
||||
|
||||
-- 4. Count clusters by dominant type
|
||||
summary AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
COUNT(*) FILTER (WHERE dominant_type = 'motion') AS count_motion_detected,
|
||||
COUNT(*) FILTER (WHERE dominant_type = 'presence') AS count_presence_detected,
|
||||
COUNT(*) AS count_total_presence_detected
|
||||
FROM cluster_type
|
||||
GROUP BY space_id, event_date
|
||||
)
|
||||
|
||||
-- 5. Output
|
||||
, final_table as (
|
||||
SELECT summary.space_id,
|
||||
summary.event_date,
|
||||
count_motion_detected,
|
||||
count_presence_detected,
|
||||
count_total_presence_detected
|
||||
FROM summary
|
||||
JOIN params P ON true
|
||||
where summary.space_id = P.space_id
|
||||
and (P.event_date IS NULL or summary.event_date::date = P.event_date)
|
||||
ORDER BY space_id, event_date)
|
||||
|
||||
|
||||
INSERT INTO public."presence-sensor-daily-space-detection" (
|
||||
space_uuid,
|
||||
event_date,
|
||||
count_motion_detected,
|
||||
count_presence_detected,
|
||||
count_total_presence_detected
|
||||
)
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
count_motion_detected,
|
||||
count_presence_detected,
|
||||
count_total_presence_detected
|
||||
FROM final_table
|
||||
ON CONFLICT (space_uuid, event_date) DO UPDATE
|
||||
SET
|
||||
count_motion_detected = EXCLUDED.count_motion_detected,
|
||||
count_presence_detected = EXCLUDED.count_presence_detected,
|
||||
count_total_presence_detected = EXCLUDED.count_total_presence_detected;
|
||||
@ -1,362 +0,0 @@
|
||||
-- Function to calculate AQI
|
||||
CREATE OR REPLACE FUNCTION calculate_aqi(p_pollutant TEXT, concentration NUMERIC)
|
||||
RETURNS NUMERIC AS $$
|
||||
DECLARE
|
||||
c_low NUMERIC;
|
||||
c_high NUMERIC;
|
||||
i_low INT;
|
||||
i_high INT;
|
||||
BEGIN
|
||||
SELECT v.c_low, v.c_high, v.i_low, v.i_high
|
||||
INTO c_low, c_high, i_low, i_high
|
||||
FROM (
|
||||
VALUES
|
||||
-- PM2.5
|
||||
('pm25', 0.0, 12.0, 0, 50),
|
||||
('pm25', 12.1, 35.4, 51, 100),
|
||||
('pm25', 35.5, 55.4, 101, 150),
|
||||
('pm25', 55.5, 150.4, 151, 200),
|
||||
('pm25', 150.5, 250.4, 201, 300),
|
||||
('pm25', 250.5, 500.4, 301, 500),
|
||||
|
||||
-- PM10
|
||||
('pm10', 0, 54, 0, 50),
|
||||
('pm10', 55, 154, 51, 100),
|
||||
('pm10', 155, 254, 101, 150),
|
||||
('pm10', 255, 354, 151, 200),
|
||||
|
||||
-- VOC
|
||||
('voc', 0, 200, 0, 50),
|
||||
('voc', 201, 400, 51, 100),
|
||||
('voc', 401, 600, 101, 150),
|
||||
('voc', 601, 1000, 151, 200),
|
||||
|
||||
-- CH2O
|
||||
('ch2o', 0, 2, 0, 50),
|
||||
('ch2o', 2.1, 4, 51, 100),
|
||||
('ch2o', 4.1, 6, 101, 150),
|
||||
|
||||
-- CO2
|
||||
('co2', 350, 1000, 0, 50),
|
||||
('co2', 1001, 1250, 51, 100),
|
||||
('co2', 1251, 1500, 101, 150),
|
||||
('co2', 1501, 2000, 151, 200)
|
||||
) AS v(pollutant, c_low, c_high, i_low, i_high)
|
||||
WHERE v.pollutant = LOWER(p_pollutant)
|
||||
AND concentration BETWEEN v.c_low AND v.c_high
|
||||
LIMIT 1;
|
||||
|
||||
RETURN ROUND(((i_high - i_low) * (concentration - c_low) / (c_high - c_low)) + i_low);
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
|
||||
-- Function to classify AQI
|
||||
CREATE OR REPLACE FUNCTION classify_aqi(aqi NUMERIC)
|
||||
RETURNS TEXT AS $$
|
||||
BEGIN
|
||||
RETURN CASE
|
||||
WHEN aqi BETWEEN 0 AND 50 THEN 'Good'
|
||||
WHEN aqi BETWEEN 51 AND 100 THEN 'Moderate'
|
||||
WHEN aqi BETWEEN 101 AND 150 THEN 'Unhealthy for Sensitive Groups'
|
||||
WHEN aqi BETWEEN 151 AND 200 THEN 'Unhealthy'
|
||||
WHEN aqi BETWEEN 201 AND 300 THEN 'Very Unhealthy'
|
||||
WHEN aqi >= 301 THEN 'Hazardous'
|
||||
ELSE NULL
|
||||
END;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
|
||||
-- Function to convert AQI level string to number
|
||||
CREATE OR REPLACE FUNCTION level_to_numeric(level_text TEXT)
|
||||
RETURNS NUMERIC AS $$
|
||||
BEGIN
|
||||
RETURN CAST(regexp_replace(level_text, '[^0-9]', '', 'g') AS NUMERIC);
|
||||
EXCEPTION WHEN others THEN
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
|
||||
-- Query Pipeline Starts Here
|
||||
WITH device_space AS (
|
||||
SELECT
|
||||
device.uuid AS device_id,
|
||||
device.space_device_uuid AS space_id,
|
||||
"device-status-log".event_time::timestamp AS event_time,
|
||||
"device-status-log".code,
|
||||
"device-status-log".value
|
||||
FROM device
|
||||
LEFT JOIN "device-status-log"
|
||||
ON device.uuid = "device-status-log".device_id
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
WHERE product.cat_name = 'hjjcy'
|
||||
),
|
||||
|
||||
average_pollutants AS (
|
||||
SELECT
|
||||
event_time::date AS event_date,
|
||||
date_trunc('hour', event_time) AS event_hour,
|
||||
device_id,
|
||||
space_id,
|
||||
|
||||
-- PM1
|
||||
MIN(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_min,
|
||||
AVG(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_avg,
|
||||
MAX(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_max,
|
||||
|
||||
-- PM25
|
||||
MIN(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_min,
|
||||
AVG(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_avg,
|
||||
MAX(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_max,
|
||||
|
||||
-- PM10
|
||||
MIN(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_min,
|
||||
AVG(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_avg,
|
||||
MAX(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_max,
|
||||
|
||||
-- VOC
|
||||
MIN(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_min,
|
||||
AVG(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_avg,
|
||||
MAX(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_max,
|
||||
|
||||
-- CH2O
|
||||
MIN(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_min,
|
||||
AVG(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_avg,
|
||||
MAX(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_max,
|
||||
|
||||
-- CO2
|
||||
MIN(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_min,
|
||||
AVG(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_avg,
|
||||
MAX(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_max
|
||||
|
||||
FROM device_space
|
||||
GROUP BY device_id, space_id, event_hour, event_date
|
||||
),
|
||||
|
||||
filled_pollutants AS (
|
||||
SELECT
|
||||
*,
|
||||
-- AVG
|
||||
COALESCE(pm25_avg, LAG(pm25_avg) OVER (PARTITION BY device_id ORDER BY event_hour)) AS pm25_avg_f,
|
||||
COALESCE(pm10_avg, LAG(pm10_avg) OVER (PARTITION BY device_id ORDER BY event_hour)) AS pm10_avg_f,
|
||||
COALESCE(voc_avg, LAG(voc_avg) OVER (PARTITION BY device_id ORDER BY event_hour)) AS voc_avg_f,
|
||||
COALESCE(co2_avg, LAG(co2_avg) OVER (PARTITION BY device_id ORDER BY event_hour)) AS co2_avg_f,
|
||||
COALESCE(ch2o_avg, LAG(ch2o_avg) OVER (PARTITION BY device_id ORDER BY event_hour)) AS ch2o_avg_f,
|
||||
|
||||
-- MIN
|
||||
COALESCE(pm25_min, LAG(pm25_min) OVER (PARTITION BY device_id ORDER BY event_hour)) AS pm25_min_f,
|
||||
COALESCE(pm10_min, LAG(pm10_min) OVER (PARTITION BY device_id ORDER BY event_hour)) AS pm10_min_f,
|
||||
COALESCE(voc_min, LAG(voc_min) OVER (PARTITION BY device_id ORDER BY event_hour)) AS voc_min_f,
|
||||
COALESCE(co2_min, LAG(co2_min) OVER (PARTITION BY device_id ORDER BY event_hour)) AS co2_min_f,
|
||||
COALESCE(ch2o_min, LAG(ch2o_min) OVER (PARTITION BY device_id ORDER BY event_hour)) AS ch2o_min_f,
|
||||
|
||||
-- MAX
|
||||
COALESCE(pm25_max, LAG(pm25_max) OVER (PARTITION BY device_id ORDER BY event_hour)) AS pm25_max_f,
|
||||
COALESCE(pm10_max, LAG(pm10_max) OVER (PARTITION BY device_id ORDER BY event_hour)) AS pm10_max_f,
|
||||
COALESCE(voc_max, LAG(voc_max) OVER (PARTITION BY device_id ORDER BY event_hour)) AS voc_max_f,
|
||||
COALESCE(co2_max, LAG(co2_max) OVER (PARTITION BY device_id ORDER BY event_hour)) AS co2_max_f,
|
||||
COALESCE(ch2o_max, LAG(ch2o_max) OVER (PARTITION BY device_id ORDER BY event_hour)) AS ch2o_max_f
|
||||
FROM average_pollutants
|
||||
),
|
||||
|
||||
hourly_results AS (
|
||||
SELECT
|
||||
device_id,
|
||||
space_id,
|
||||
event_date,
|
||||
event_hour,
|
||||
pm1_min, pm1_avg, pm1_max,
|
||||
pm25_min_f, pm25_avg_f, pm25_max_f,
|
||||
pm10_min_f, pm10_avg_f, pm10_max_f,
|
||||
voc_min_f, voc_avg_f, voc_max_f,
|
||||
co2_min_f, co2_avg_f, co2_max_f,
|
||||
ch2o_min_f, ch2o_avg_f, ch2o_max_f,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_min_f),
|
||||
calculate_aqi('pm10', pm10_min_f)
|
||||
) AS hourly_min_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
) AS hourly_avg_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_max_f),
|
||||
calculate_aqi('pm10', pm10_max_f)
|
||||
) AS hourly_max_aqi,
|
||||
|
||||
classify_aqi(GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
)) AS aqi_category,
|
||||
|
||||
classify_aqi(calculate_aqi('pm25',pm25_avg_f)) as pm25_category,
|
||||
classify_aqi(calculate_aqi('pm10',pm10_avg_f)) as pm10_category,
|
||||
classify_aqi(calculate_aqi('voc',voc_avg_f)) as voc_category,
|
||||
classify_aqi(calculate_aqi('co2',co2_avg_f)) as co2_category,
|
||||
classify_aqi(calculate_aqi('ch2o',ch2o_avg_f)) as ch2o_category
|
||||
|
||||
FROM filled_pollutants
|
||||
),
|
||||
|
||||
daily_category_counts AS (
|
||||
SELECT device_id, space_id, event_date, aqi_category AS category, 'aqi' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY device_id, space_id, event_date, aqi_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT device_id, space_id, event_date, pm25_category AS category, 'pm25' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY device_id, space_id, event_date, pm25_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT device_id, space_id, event_date, pm10_category AS category, 'pm10' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY device_id, space_id, event_date, pm10_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT device_id, space_id, event_date, voc_category AS category, 'voc' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY device_id, space_id, event_date, voc_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT device_id, space_id, event_date, co2_category AS category, 'co2' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY device_id, space_id, event_date, co2_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT device_id, space_id, event_date, ch2o_category AS category, 'ch2o' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY device_id, space_id, event_date, ch2o_category
|
||||
),
|
||||
|
||||
daily_totals AS (
|
||||
SELECT
|
||||
device_id,
|
||||
space_id,
|
||||
event_date,
|
||||
SUM(category_count) AS total_count
|
||||
FROM daily_category_counts
|
||||
where pollutant = 'aqi'
|
||||
GROUP BY device_id, space_id, event_date
|
||||
),
|
||||
|
||||
-- Pivot Categories into Columns
|
||||
daily_percentages AS (
|
||||
select
|
||||
dt.device_id,
|
||||
dt.space_id,
|
||||
dt.event_date,
|
||||
-- AQI CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_aqi_percentage,
|
||||
-- PM25 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm25_percentage,
|
||||
-- PM10 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm10_percentage,
|
||||
-- VOC CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_voc_percentage,
|
||||
-- CO2 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_co2_percentage,
|
||||
-- CH20 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_ch2o_percentage
|
||||
FROM daily_totals dt
|
||||
LEFT JOIN daily_category_counts dcc
|
||||
ON dt.device_id = dcc.device_id AND dt.event_date = dcc.event_date
|
||||
GROUP BY dt.device_id, dt.space_id, dt.event_date, dt.total_count
|
||||
),
|
||||
|
||||
daily_averages AS (
|
||||
SELECT
|
||||
device_id,
|
||||
space_id,
|
||||
event_date,
|
||||
-- AQI
|
||||
ROUND(AVG(hourly_min_aqi)::numeric, 2) AS daily_min_aqi,
|
||||
ROUND(AVG(hourly_avg_aqi)::numeric, 2) AS daily_avg_aqi,
|
||||
ROUND(AVG(hourly_max_aqi)::numeric, 2) AS daily_max_aqi,
|
||||
-- PM25
|
||||
ROUND(AVG(pm25_min_f)::numeric, 2) AS daily_min_pm25,
|
||||
ROUND(AVG(pm25_avg_f)::numeric, 2) AS daily_avg_pm25,
|
||||
ROUND(AVG(pm25_max_f)::numeric, 2) AS daily_max_pm25,
|
||||
-- PM10
|
||||
ROUND(AVG(pm10_min_f)::numeric, 2) AS daily_min_pm10,
|
||||
ROUND(AVG(pm10_avg_f)::numeric, 2) AS daily_avg_pm10,
|
||||
ROUND(AVG(pm10_max_f)::numeric, 2) AS daily_max_pm10,
|
||||
-- VOC
|
||||
ROUND(AVG(voc_min_f)::numeric, 2) AS daily_min_voc,
|
||||
ROUND(AVG(voc_avg_f)::numeric, 2) AS daily_avg_voc,
|
||||
ROUND(AVG(voc_max_f)::numeric, 2) AS daily_max_voc,
|
||||
-- CO2
|
||||
ROUND(AVG(co2_min_f)::numeric, 2) AS daily_min_co2,
|
||||
ROUND(AVG(co2_avg_f)::numeric, 2) AS daily_avg_co2,
|
||||
ROUND(AVG(co2_max_f)::numeric, 2) AS daily_max_co2,
|
||||
-- CH2O
|
||||
ROUND(AVG(ch2o_min_f)::numeric, 2) AS daily_min_ch2o,
|
||||
ROUND(AVG(ch2o_avg_f)::numeric, 2) AS daily_avg_ch2o,
|
||||
ROUND(AVG(ch2o_max_f)::numeric, 2) AS daily_max_ch2o
|
||||
|
||||
FROM hourly_results
|
||||
GROUP BY device_id, space_id, event_date
|
||||
)
|
||||
SELECT
|
||||
p.device_id,
|
||||
p.space_id,
|
||||
p.event_date,
|
||||
p.good_aqi_percentage, p.moderate_aqi_percentage, p.unhealthy_sensitive_aqi_percentage, p.unhealthy_aqi_percentage, p.very_unhealthy_aqi_percentage, p.hazardous_aqi_percentage,
|
||||
a.daily_avg_aqi,a.daily_max_aqi, a.daily_min_aqi,
|
||||
p.good_pm25_percentage, p.moderate_pm25_percentage, p.unhealthy_sensitive_pm25_percentage, p.unhealthy_pm25_percentage, p.very_unhealthy_pm25_percentage, p.hazardous_pm25_percentage,
|
||||
a.daily_avg_pm25,a.daily_max_pm25, a.daily_min_pm25,
|
||||
p.good_pm10_percentage, p.moderate_pm10_percentage, p.unhealthy_sensitive_pm10_percentage, p.unhealthy_pm10_percentage, p.very_unhealthy_pm10_percentage, p.hazardous_pm10_percentage,
|
||||
a.daily_avg_pm10, a.daily_max_pm10, a.daily_min_pm10,
|
||||
p.good_voc_percentage, p.moderate_voc_percentage, p.unhealthy_sensitive_voc_percentage, p.unhealthy_voc_percentage, p.very_unhealthy_voc_percentage, p.hazardous_voc_percentage,
|
||||
a.daily_avg_voc, a.daily_max_voc, a.daily_min_voc,
|
||||
p.good_co2_percentage, p.moderate_co2_percentage, p.unhealthy_sensitive_co2_percentage, p.unhealthy_co2_percentage, p.very_unhealthy_co2_percentage, p.hazardous_co2_percentage,
|
||||
a.daily_avg_co2,a.daily_max_co2, a.daily_min_co2,
|
||||
p.good_ch2o_percentage, p.moderate_ch2o_percentage, p.unhealthy_sensitive_ch2o_percentage, p.unhealthy_ch2o_percentage, p.very_unhealthy_ch2o_percentage, p.hazardous_ch2o_percentage,
|
||||
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
||||
FROM daily_percentages p
|
||||
LEFT JOIN daily_averages a
|
||||
ON p.device_id = a.device_id AND p.event_date = a.event_date
|
||||
ORDER BY p.space_id, p.event_date;
|
||||
|
||||
|
||||
@ -1,275 +0,0 @@
|
||||
-- Query Pipeline Starts Here
|
||||
WITH device_space AS (
|
||||
SELECT
|
||||
device.uuid AS device_id,
|
||||
device.space_device_uuid AS space_id,
|
||||
"device-status-log".event_time::timestamp AS event_time,
|
||||
"device-status-log".code,
|
||||
"device-status-log".value
|
||||
FROM device
|
||||
LEFT JOIN "device-status-log"
|
||||
ON device.uuid = "device-status-log".device_id
|
||||
LEFT JOIN product
|
||||
ON product.uuid = device.product_device_uuid
|
||||
WHERE product.cat_name = 'hjjcy'
|
||||
),
|
||||
|
||||
average_pollutants AS (
|
||||
SELECT
|
||||
event_time::date AS event_date,
|
||||
date_trunc('hour', event_time) AS event_hour,
|
||||
space_id,
|
||||
|
||||
-- PM1
|
||||
MIN(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_min,
|
||||
AVG(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_avg,
|
||||
MAX(CASE WHEN code = 'pm1' THEN value::numeric END) AS pm1_max,
|
||||
|
||||
-- PM25
|
||||
MIN(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_min,
|
||||
AVG(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_avg,
|
||||
MAX(CASE WHEN code = 'pm25_value' THEN value::numeric END) AS pm25_max,
|
||||
|
||||
-- PM10
|
||||
MIN(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_min,
|
||||
AVG(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_avg,
|
||||
MAX(CASE WHEN code = 'pm10' THEN value::numeric END) AS pm10_max,
|
||||
|
||||
-- VOC
|
||||
MIN(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_min,
|
||||
AVG(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_avg,
|
||||
MAX(CASE WHEN code = 'voc_value' THEN value::numeric END) AS voc_max,
|
||||
|
||||
-- CH2O
|
||||
MIN(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_min,
|
||||
AVG(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_avg,
|
||||
MAX(CASE WHEN code = 'ch2o_value' THEN value::numeric END) AS ch2o_max,
|
||||
|
||||
-- CO2
|
||||
MIN(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_min,
|
||||
AVG(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_avg,
|
||||
MAX(CASE WHEN code = 'co2_value' THEN value::numeric END) AS co2_max
|
||||
|
||||
FROM device_space
|
||||
GROUP BY space_id, event_hour, event_date
|
||||
),
|
||||
|
||||
filled_pollutants AS (
|
||||
SELECT
|
||||
*,
|
||||
-- AVG
|
||||
COALESCE(pm25_avg, LAG(pm25_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_avg_f,
|
||||
COALESCE(pm10_avg, LAG(pm10_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_avg_f,
|
||||
COALESCE(voc_avg, LAG(voc_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_avg_f,
|
||||
COALESCE(co2_avg, LAG(co2_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_avg_f,
|
||||
COALESCE(ch2o_avg, LAG(ch2o_avg) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_avg_f,
|
||||
|
||||
-- MIN
|
||||
COALESCE(pm25_min, LAG(pm25_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_min_f,
|
||||
COALESCE(pm10_min, LAG(pm10_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_min_f,
|
||||
COALESCE(voc_min, LAG(voc_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_min_f,
|
||||
COALESCE(co2_min, LAG(co2_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_min_f,
|
||||
COALESCE(ch2o_min, LAG(ch2o_min) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_min_f,
|
||||
|
||||
-- MAX
|
||||
COALESCE(pm25_max, LAG(pm25_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm25_max_f,
|
||||
COALESCE(pm10_max, LAG(pm10_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS pm10_max_f,
|
||||
COALESCE(voc_max, LAG(voc_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS voc_max_f,
|
||||
COALESCE(co2_max, LAG(co2_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS co2_max_f,
|
||||
COALESCE(ch2o_max, LAG(ch2o_max) OVER (PARTITION BY space_id ORDER BY event_hour)) AS ch2o_max_f
|
||||
FROM average_pollutants
|
||||
),
|
||||
|
||||
hourly_results AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
event_hour,
|
||||
pm1_min, pm1_avg, pm1_max,
|
||||
pm25_min_f, pm25_avg_f, pm25_max_f,
|
||||
pm10_min_f, pm10_avg_f, pm10_max_f,
|
||||
voc_min_f, voc_avg_f, voc_max_f,
|
||||
co2_min_f, co2_avg_f, co2_max_f,
|
||||
ch2o_min_f, ch2o_avg_f, ch2o_max_f,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_min_f),
|
||||
calculate_aqi('pm10', pm10_min_f)
|
||||
) AS hourly_min_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
) AS hourly_avg_aqi,
|
||||
|
||||
GREATEST(
|
||||
calculate_aqi('pm25', pm25_max_f),
|
||||
calculate_aqi('pm10', pm10_max_f)
|
||||
) AS hourly_max_aqi,
|
||||
|
||||
classify_aqi(GREATEST(
|
||||
calculate_aqi('pm25', pm25_avg_f),
|
||||
calculate_aqi('pm10', pm10_avg_f)
|
||||
)) AS aqi_category,
|
||||
|
||||
classify_aqi(calculate_aqi('pm25',pm25_avg_f)) as pm25_category,
|
||||
classify_aqi(calculate_aqi('pm10',pm10_avg_f)) as pm10_category,
|
||||
classify_aqi(calculate_aqi('voc',voc_avg_f)) as voc_category,
|
||||
classify_aqi(calculate_aqi('co2',co2_avg_f)) as co2_category,
|
||||
classify_aqi(calculate_aqi('ch2o',ch2o_avg_f)) as ch2o_category
|
||||
|
||||
FROM filled_pollutants
|
||||
),
|
||||
|
||||
daily_category_counts AS (
|
||||
SELECT space_id, event_date, aqi_category AS category, 'aqi' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, aqi_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, pm25_category AS category, 'pm25' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, pm25_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, pm10_category AS category, 'pm10' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, pm10_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, voc_category AS category, 'voc' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, voc_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, co2_category AS category, 'co2' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, co2_category
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT space_id, event_date, ch2o_category AS category, 'ch2o' AS pollutant, COUNT(*) AS category_count
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date, ch2o_category
|
||||
),
|
||||
|
||||
daily_totals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
SUM(category_count) AS total_count
|
||||
FROM daily_category_counts
|
||||
where pollutant = 'aqi'
|
||||
GROUP BY space_id, event_date
|
||||
),
|
||||
|
||||
-- Pivot Categories into Columns
|
||||
daily_percentages AS (
|
||||
select
|
||||
dt.space_id,
|
||||
dt.event_date,
|
||||
-- AQI CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_aqi_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'aqi' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_aqi_percentage,
|
||||
-- PM25 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm25_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm25' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm25_percentage,
|
||||
-- PM10 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_pm10_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'pm10' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_pm10_percentage,
|
||||
-- VOC CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_voc_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'voc' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_voc_percentage,
|
||||
-- CO2 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_co2_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'co2' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_co2_percentage,
|
||||
-- CH20 CATEGORIES
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Good' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS good_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Moderate' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS moderate_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy for Sensitive Groups' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_sensitive_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Very Unhealthy' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS very_unhealthy_ch2o_percentage,
|
||||
ROUND(COALESCE(SUM(CASE WHEN dcc.category = 'Hazardous' and dcc.pollutant = 'ch2o' THEN dcc.category_count ELSE 0 END) * 100.0 / dt.total_count, 0), 2) AS hazardous_ch2o_percentage
|
||||
FROM daily_totals dt
|
||||
LEFT JOIN daily_category_counts dcc
|
||||
ON dt.space_id = dcc.space_id AND dt.event_date = dcc.event_date
|
||||
GROUP BY dt.space_id, dt.event_date, dt.total_count
|
||||
),
|
||||
|
||||
daily_averages AS (
|
||||
SELECT
|
||||
space_id,
|
||||
event_date,
|
||||
-- AQI
|
||||
ROUND(AVG(hourly_min_aqi)::numeric, 2) AS daily_min_aqi,
|
||||
ROUND(AVG(hourly_avg_aqi)::numeric, 2) AS daily_avg_aqi,
|
||||
ROUND(AVG(hourly_max_aqi)::numeric, 2) AS daily_max_aqi,
|
||||
-- PM25
|
||||
ROUND(AVG(pm25_min_f)::numeric, 2) AS daily_min_pm25,
|
||||
ROUND(AVG(pm25_avg_f)::numeric, 2) AS daily_avg_pm25,
|
||||
ROUND(AVG(pm25_max_f)::numeric, 2) AS daily_max_pm25,
|
||||
-- PM10
|
||||
ROUND(AVG(pm10_min_f)::numeric, 2) AS daily_min_pm10,
|
||||
ROUND(AVG(pm10_avg_f)::numeric, 2) AS daily_avg_pm10,
|
||||
ROUND(AVG(pm10_max_f)::numeric, 2) AS daily_max_pm10,
|
||||
-- VOC
|
||||
ROUND(AVG(voc_min_f)::numeric, 2) AS daily_min_voc,
|
||||
ROUND(AVG(voc_avg_f)::numeric, 2) AS daily_avg_voc,
|
||||
ROUND(AVG(voc_max_f)::numeric, 2) AS daily_max_voc,
|
||||
-- CO2
|
||||
ROUND(AVG(co2_min_f)::numeric, 2) AS daily_min_co2,
|
||||
ROUND(AVG(co2_avg_f)::numeric, 2) AS daily_avg_co2,
|
||||
ROUND(AVG(co2_max_f)::numeric, 2) AS daily_max_co2,
|
||||
-- CH2O
|
||||
ROUND(AVG(ch2o_min_f)::numeric, 2) AS daily_min_ch2o,
|
||||
ROUND(AVG(ch2o_avg_f)::numeric, 2) AS daily_avg_ch2o,
|
||||
ROUND(AVG(ch2o_max_f)::numeric, 2) AS daily_max_ch2o
|
||||
|
||||
FROM hourly_results
|
||||
GROUP BY space_id, event_date
|
||||
)
|
||||
SELECT
|
||||
p.space_id,
|
||||
p.event_date,
|
||||
p.good_aqi_percentage, p.moderate_aqi_percentage, p.unhealthy_sensitive_aqi_percentage, p.unhealthy_aqi_percentage, p.very_unhealthy_aqi_percentage, p.hazardous_aqi_percentage,
|
||||
a.daily_avg_aqi,a.daily_max_aqi, a.daily_min_aqi,
|
||||
p.good_pm25_percentage, p.moderate_pm25_percentage, p.unhealthy_sensitive_pm25_percentage, p.unhealthy_pm25_percentage, p.very_unhealthy_pm25_percentage, p.hazardous_pm25_percentage,
|
||||
a.daily_avg_pm25,a.daily_max_pm25, a.daily_min_pm25,
|
||||
p.good_pm10_percentage, p.moderate_pm10_percentage, p.unhealthy_sensitive_pm10_percentage, p.unhealthy_pm10_percentage, p.very_unhealthy_pm10_percentage, p.hazardous_pm10_percentage,
|
||||
a.daily_avg_pm10, a.daily_max_pm10, a.daily_min_pm10,
|
||||
p.good_voc_percentage, p.moderate_voc_percentage, p.unhealthy_sensitive_voc_percentage, p.unhealthy_voc_percentage, p.very_unhealthy_voc_percentage, p.hazardous_voc_percentage,
|
||||
a.daily_avg_voc, a.daily_max_voc, a.daily_min_voc,
|
||||
p.good_co2_percentage, p.moderate_co2_percentage, p.unhealthy_sensitive_co2_percentage, p.unhealthy_co2_percentage, p.very_unhealthy_co2_percentage, p.hazardous_co2_percentage,
|
||||
a.daily_avg_co2,a.daily_max_co2, a.daily_min_co2,
|
||||
p.good_ch2o_percentage, p.moderate_ch2o_percentage, p.unhealthy_sensitive_ch2o_percentage, p.unhealthy_ch2o_percentage, p.very_unhealthy_ch2o_percentage, p.hazardous_ch2o_percentage,
|
||||
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
||||
FROM daily_percentages p
|
||||
LEFT JOIN daily_averages a
|
||||
ON p.space_id = a.space_id AND p.event_date = a.event_date
|
||||
ORDER BY p.space_id, p.event_date;
|
||||
|
||||
|
||||
@ -1,168 +1,91 @@
|
||||
WITH presence_logs AS (
|
||||
SELECT
|
||||
d.space_device_uuid AS space_id,
|
||||
l.device_id,
|
||||
l.event_time,
|
||||
l.value,
|
||||
LAG(l.event_time) OVER (PARTITION BY l.device_id ORDER BY l.event_time) AS prev_time,
|
||||
LAG(l.value) OVER (PARTITION BY l.device_id ORDER BY l.event_time) AS prev_value
|
||||
FROM device d
|
||||
JOIN "device-status-log" l ON d.uuid = l.device_id
|
||||
JOIN product p ON p.uuid = d.product_device_uuid
|
||||
WHERE l.code = 'presence_state'
|
||||
AND p.cat_name = 'hps'
|
||||
-- Step 1: Get device presence events with previous timestamps
|
||||
WITH start_date AS (
|
||||
SELECT
|
||||
d.uuid AS device_id,
|
||||
d.space_device_uuid AS space_id,
|
||||
l.value,
|
||||
l.event_time::timestamp AS event_time,
|
||||
LAG(l.event_time::timestamp) OVER (PARTITION BY d.uuid ORDER BY l.event_time) AS prev_timestamp
|
||||
FROM device d
|
||||
LEFT JOIN "device-status-log" l
|
||||
ON d.uuid = l.device_id
|
||||
LEFT JOIN product p
|
||||
ON p.uuid = d.product_device_uuid
|
||||
WHERE p.cat_name = 'hps'
|
||||
AND l.code = 'presence_state'
|
||||
),
|
||||
|
||||
-- Intervals when device was in 'absence' (between prev_time and event_time when value='none')
|
||||
raw_absence_intervals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
device_id,
|
||||
prev_time AS start_time,
|
||||
event_time AS end_time
|
||||
FROM presence_logs
|
||||
WHERE value <> 'none'
|
||||
AND prev_value = 'none'
|
||||
AND prev_time IS NOT NULL
|
||||
-- Step 2: Identify periods when device reports "none"
|
||||
device_none_periods AS (
|
||||
SELECT
|
||||
space_id,
|
||||
device_id,
|
||||
event_time AS empty_from,
|
||||
LEAD(event_time) OVER (PARTITION BY device_id ORDER BY event_time) AS empty_until
|
||||
FROM start_date
|
||||
WHERE value = 'none'
|
||||
),
|
||||
|
||||
-- Split intervals that span multiple days into day-specific chunks
|
||||
absence_intervals AS (
|
||||
SELECT
|
||||
r.space_id,
|
||||
r.device_id,
|
||||
GREATEST(r.start_time, gs.day::timestamp) AS start_time,
|
||||
LEAST(r.end_time, (gs.day + INTERVAL '1 day' - INTERVAL '1 second')::timestamp) AS end_time
|
||||
FROM raw_absence_intervals r
|
||||
CROSS JOIN LATERAL (
|
||||
SELECT generate_series(
|
||||
date_trunc('day', r.start_time),
|
||||
date_trunc('day', r.end_time),
|
||||
INTERVAL '1 day'
|
||||
) AS day
|
||||
) gs
|
||||
WHERE GREATEST(r.start_time, gs.day::timestamp) < LEAST(r.end_time, (gs.day + INTERVAL '1 day')::timestamp)
|
||||
-- Step 3: Clip the "none" periods to the edges of each day
|
||||
clipped_device_none_periods AS (
|
||||
SELECT
|
||||
space_id,
|
||||
GREATEST(empty_from, DATE_TRUNC('day', empty_from)) AS clipped_from,
|
||||
LEAST(empty_until, DATE_TRUNC('day', empty_until) + INTERVAL '1 day') AS clipped_until
|
||||
FROM device_none_periods
|
||||
WHERE empty_until IS NOT NULL
|
||||
),
|
||||
|
||||
-- FIXED: Count devices based on presence_logs OR absence_intervals
|
||||
devices_per_day AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
COUNT(DISTINCT device_id) AS device_count
|
||||
FROM (
|
||||
-- Devices that logged events on that day
|
||||
SELECT space_id, DATE(event_time) AS day, device_id
|
||||
FROM presence_logs
|
||||
|
||||
UNION
|
||||
|
||||
-- Devices that had absence intervals on that day
|
||||
SELECT space_id, DATE(start_time) AS day, device_id
|
||||
FROM absence_intervals
|
||||
) combined
|
||||
GROUP BY space_id, day
|
||||
-- Step 4: Break multi-day periods into daily intervals
|
||||
generated_daily_intervals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
gs::date AS day,
|
||||
GREATEST(clipped_from, gs) AS interval_start,
|
||||
LEAST(clipped_until, gs + INTERVAL '1 day') AS interval_end
|
||||
FROM clipped_device_none_periods,
|
||||
LATERAL generate_series(DATE_TRUNC('day', clipped_from), DATE_TRUNC('day', clipped_until), INTERVAL '1 day') AS gs
|
||||
),
|
||||
|
||||
-- Step 5: Merge overlapping or adjacent intervals per day
|
||||
merged_intervals AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
interval_start,
|
||||
interval_end
|
||||
FROM (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
interval_start,
|
||||
interval_end,
|
||||
LAG(interval_end) OVER (PARTITION BY space_id, day ORDER BY interval_start) AS prev_end
|
||||
FROM generated_daily_intervals
|
||||
) sub
|
||||
WHERE prev_end IS NULL OR interval_start > prev_end
|
||||
),
|
||||
|
||||
-- For multi-device spaces, find all time periods when ALL devices were absent
|
||||
multi_device_unoccupied AS (
|
||||
WITH device_absence_per_day AS (
|
||||
SELECT
|
||||
a.space_id,
|
||||
DATE(a.start_time) AS day,
|
||||
a.device_id,
|
||||
a.start_time,
|
||||
a.end_time,
|
||||
d.device_count
|
||||
FROM absence_intervals a
|
||||
JOIN devices_per_day d ON a.space_id = d.space_id AND DATE(a.start_time) = d.day
|
||||
WHERE d.device_count > 1
|
||||
),
|
||||
-- Generate time slots for each day with multiple devices
|
||||
time_ranges AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
day::timestamp AS range_start,
|
||||
(day + INTERVAL '1 day')::timestamp AS range_end,
|
||||
device_count
|
||||
FROM devices_per_day
|
||||
WHERE device_count > 1
|
||||
),
|
||||
|
||||
-- Find all time periods when all devices were absent
|
||||
all_devices_absent AS (
|
||||
SELECT
|
||||
t.space_id,
|
||||
t.day,
|
||||
t.range_start,
|
||||
t.range_end,
|
||||
t.device_count,
|
||||
-- Find the latest start time of all devices' absence intervals
|
||||
MAX(a.start_time) OVER (PARTITION BY t.space_id, t.day) AS max_start_time,
|
||||
-- Find the earliest end time of all devices' absence intervals
|
||||
MIN(a.end_time) OVER (PARTITION BY t.space_id, t.day) AS min_end_time
|
||||
FROM time_ranges t
|
||||
LEFT JOIN device_absence_per_day a ON
|
||||
t.space_id = a.space_id AND
|
||||
t.day = a.day
|
||||
GROUP BY t.space_id, t.day, t.range_start, t.range_end, t.device_count, a.start_time, a.end_time
|
||||
)
|
||||
|
||||
-- Calculate total unoccupied seconds when all devices were absent
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
CASE
|
||||
WHEN max_start_time IS NULL OR min_end_time IS NULL THEN 0
|
||||
WHEN max_start_time >= min_end_time THEN 0
|
||||
ELSE EXTRACT(EPOCH FROM (LEAST(min_end_time, range_end) - GREATEST(max_start_time, range_start)))
|
||||
END AS unoccupied_seconds
|
||||
FROM all_devices_absent
|
||||
GROUP BY space_id, day, range_start, range_end, device_count, max_start_time, min_end_time
|
||||
HAVING COUNT(*) = device_count -- Only include periods when all devices were absent
|
||||
)
|
||||
,
|
||||
-- Step 6: Sum up total missing seconds (device reported "none") per day
|
||||
missing_seconds_per_day AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day AS missing_date,
|
||||
SUM(EXTRACT(EPOCH FROM (interval_end - interval_start))) AS total_missing_seconds
|
||||
FROM merged_intervals
|
||||
GROUP BY space_id, day
|
||||
),
|
||||
|
||||
-- Calculate unoccupied time for spaces with single device reporting
|
||||
single_device_unoccupied AS (
|
||||
SELECT
|
||||
a.space_id,
|
||||
DATE(a.start_time) AS day,
|
||||
SUM(EXTRACT(EPOCH FROM (a.end_time - a.start_time))) AS unoccupied_seconds
|
||||
FROM absence_intervals a
|
||||
JOIN devices_per_day d ON a.space_id = d.space_id AND DATE(a.start_time) = d.day
|
||||
WHERE d.device_count = 1
|
||||
GROUP BY a.space_id, DATE(a.start_time)
|
||||
)
|
||||
,
|
||||
|
||||
-- Combine results from both single and multi-device cases
|
||||
combined_unoccupied AS (
|
||||
SELECT space_id, day, unoccupied_seconds FROM single_device_unoccupied
|
||||
UNION ALL
|
||||
SELECT space_id, day, unoccupied_seconds FROM multi_device_unoccupied
|
||||
),
|
||||
|
||||
-- Calculate total occupied time per space per day
|
||||
daily_occupancy AS (
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
-- Total seconds in day (86400) minus unoccupied seconds
|
||||
86400 - COALESCE(SUM(unoccupied_seconds), 0) AS occupied_seconds
|
||||
FROM combined_unoccupied
|
||||
GROUP BY space_id, day
|
||||
-- Step 7: Calculate total occupied time per day (86400 - missing)
|
||||
occupied_seconds_per_day AS (
|
||||
SELECT
|
||||
space_id,
|
||||
missing_date as date,
|
||||
86400 - total_missing_seconds AS total_occupied_seconds
|
||||
FROM missing_seconds_per_day
|
||||
)
|
||||
|
||||
-- Final result
|
||||
SELECT
|
||||
space_id,
|
||||
day,
|
||||
occupied_seconds,
|
||||
-- Also include percentage for convenience
|
||||
ROUND((occupied_seconds / 86400.0) * 100, 2) AS occupancy_percentage
|
||||
FROM daily_occupancy
|
||||
ORDER BY space_id, day;
|
||||
|
||||
-- Final Output
|
||||
SELECT *
|
||||
FROM occupied_seconds_per_day
|
||||
ORDER BY 1,2;
|
||||
|
||||
@ -85,7 +85,7 @@ daily_aggregate AS (
|
||||
GROUP BY device_id, event_date
|
||||
)
|
||||
|
||||
INSERT INTO public."presence-sensor-daily-device-detection" (
|
||||
INSERT INTO public."presence-sensor-daily-detection" (
|
||||
device_uuid,
|
||||
event_date,
|
||||
count_motion_detected,
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
export function calculateAQI(pm2_5: number): number {
|
||||
const breakpoints = [
|
||||
{ pmLow: 0.0, pmHigh: 12.0, aqiLow: 0, aqiHigh: 50 },
|
||||
{ pmLow: 12.1, pmHigh: 35.4, aqiLow: 51, aqiHigh: 100 },
|
||||
{ pmLow: 35.5, pmHigh: 55.4, aqiLow: 101, aqiHigh: 150 },
|
||||
{ pmLow: 55.5, pmHigh: 150.4, aqiLow: 151, aqiHigh: 200 },
|
||||
{ pmLow: 150.5, pmHigh: 250.4, aqiLow: 201, aqiHigh: 300 },
|
||||
{ pmLow: 250.5, pmHigh: 500.4, aqiLow: 301, aqiHigh: 500 },
|
||||
];
|
||||
|
||||
const bp = breakpoints.find((b) => pm2_5 >= b.pmLow && pm2_5 <= b.pmHigh);
|
||||
if (!bp) return pm2_5 > 500.4 ? 500 : 0; // Handle out-of-range values
|
||||
|
||||
return Math.round(
|
||||
((bp.aqiHigh - bp.aqiLow) / (bp.pmHigh - bp.pmLow)) * (pm2_5 - bp.pmLow) +
|
||||
bp.aqiLow,
|
||||
);
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
import { DeviceEntity } from '../modules/device/entities';
|
||||
|
||||
export function addSpaceUuidToDevices(
|
||||
devices: DeviceEntity[],
|
||||
spaceUuid: string,
|
||||
): DeviceEntity[] {
|
||||
return devices.map((device) => {
|
||||
(device as any).spaceUuid = spaceUuid;
|
||||
return device;
|
||||
});
|
||||
}
|
||||
@ -37,14 +37,12 @@ import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
|
||||
import { HealthModule } from './health/health.module';
|
||||
|
||||
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
|
||||
import { OccupancyModule } from './occupancy/occupancy.module';
|
||||
import { WeatherModule } from './weather/weather.module';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
load: config,
|
||||
}),
|
||||
/* ThrottlerModule.forRoot({
|
||||
/* ThrottlerModule.forRoot({
|
||||
throttlers: [{ ttl: 100000, limit: 30 }],
|
||||
}), */
|
||||
WinstonModule.forRoot(winstonLoggerOptions),
|
||||
@ -79,15 +77,13 @@ import { WeatherModule } from './weather/weather.module';
|
||||
DeviceCommissionModule,
|
||||
PowerClampModule,
|
||||
HealthModule,
|
||||
OccupancyModule,
|
||||
WeatherModule,
|
||||
],
|
||||
providers: [
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: LoggingInterceptor,
|
||||
},
|
||||
/* {
|
||||
/* {
|
||||
provide: APP_GUARD,
|
||||
useClass: ThrottlerGuard,
|
||||
}, */
|
||||
|
||||
@ -18,7 +18,6 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories/project.repository';
|
||||
import { AutomationSpaceController } from './controllers/automation-space.controller';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||
@ -36,7 +35,6 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
SceneDeviceRepository,
|
||||
AutomationRepository,
|
||||
ProjectRepository,
|
||||
CommunityRepository,
|
||||
],
|
||||
exports: [AutomationService],
|
||||
})
|
||||
|
||||
@ -28,7 +28,6 @@ import {
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule],
|
||||
@ -56,7 +55,6 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
],
|
||||
exports: [],
|
||||
})
|
||||
|
||||
@ -63,7 +63,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
|
||||
@ -115,7 +114,6 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
],
|
||||
exports: [CommunityService, SpacePermissionService],
|
||||
})
|
||||
|
||||
@ -1,9 +1,4 @@
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
@ -21,9 +16,6 @@ import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
||||
import { ILike, In, Not } from 'typeorm';
|
||||
import { SpaceService } from 'src/space/services';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
||||
|
||||
@Injectable()
|
||||
export class CommunityService {
|
||||
@ -311,53 +303,4 @@ export class CommunityService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async getAllDevicesByCommunity(
|
||||
communityUuid: string,
|
||||
): Promise<DeviceEntity[]> {
|
||||
const community = await this.communityRepository.findOne({
|
||||
where: { uuid: communityUuid },
|
||||
relations: [
|
||||
'spaces',
|
||||
'spaces.children',
|
||||
'spaces.devices',
|
||||
'spaces.devices.productDevice',
|
||||
],
|
||||
});
|
||||
|
||||
if (!community) {
|
||||
throw new NotFoundException('Community not found');
|
||||
}
|
||||
|
||||
const allDevices: DeviceEntity[] = [];
|
||||
const visitedSpaceUuids = new Set<string>();
|
||||
|
||||
// Recursive fetch function with visited check
|
||||
const fetchSpaceDevices = async (space: SpaceEntity) => {
|
||||
if (visitedSpaceUuids.has(space.uuid)) return;
|
||||
visitedSpaceUuids.add(space.uuid);
|
||||
|
||||
if (space.devices?.length) {
|
||||
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
|
||||
}
|
||||
|
||||
if (space.children?.length) {
|
||||
for (const child of space.children) {
|
||||
const fullChild = await this.spaceRepository.findOne({
|
||||
where: { uuid: child.uuid },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
if (fullChild) {
|
||||
await fetchSpaceDevices(fullChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (const space of community.spaces) {
|
||||
await fetchSpaceDevices(space);
|
||||
}
|
||||
|
||||
return allDevices;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import AuthConfig from './auth.config';
|
||||
import AppConfig from './app.config';
|
||||
import JwtConfig from './jwt.config';
|
||||
import WeatherOpenConfig from './weather.open.config';
|
||||
export default [AuthConfig, AppConfig, JwtConfig, WeatherOpenConfig];
|
||||
export default [AuthConfig, AppConfig, JwtConfig];
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
export default registerAs(
|
||||
'openweather-config',
|
||||
(): Record<string, any> => ({
|
||||
OPEN_WEATHER_MAP_API_KEY: process.env.OPEN_WEATHER_MAP_API_KEY,
|
||||
WEATHER_API_URL: process.env.WEATHER_API_URL,
|
||||
}),
|
||||
);
|
||||
@ -1,52 +0,0 @@
|
||||
import { DeviceService } from '../services/device.service';
|
||||
import { Controller, Get, Query, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiQuery,
|
||||
} from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||
import { GetDevicesBySpaceOrCommunityDto } from '../dtos';
|
||||
|
||||
@ApiTags('Device Module')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
path: ControllerRoute.DEVICE_SPACE_COMMUNITY.ROUTE,
|
||||
})
|
||||
export class DeviceSpaceOrCommunityController {
|
||||
constructor(private readonly deviceService: DeviceService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(PermissionsGuard)
|
||||
@Permissions('DEVICE_VIEW')
|
||||
@Get('recursive-child')
|
||||
@ApiOperation({
|
||||
summary:
|
||||
ControllerRoute.DEVICE_SPACE_COMMUNITY.ACTIONS
|
||||
.GET_ALL_DEVICES_BY_SPACE_OR_COMMUNITY_WITH_RECURSIVE_CHILD_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.DEVICE_SPACE_COMMUNITY.ACTIONS
|
||||
.GET_ALL_DEVICES_BY_SPACE_OR_COMMUNITY_WITH_RECURSIVE_CHILD_DESCRIPTION,
|
||||
})
|
||||
@ApiQuery({
|
||||
name: 'spaceUuid',
|
||||
description: 'UUID of the Space',
|
||||
required: false,
|
||||
})
|
||||
@ApiQuery({
|
||||
name: 'communityUuid',
|
||||
description: 'UUID of the Community',
|
||||
required: false,
|
||||
})
|
||||
async getAllDevicesBySpaceOrCommunityWithChild(
|
||||
@Query() query: GetDevicesBySpaceOrCommunityDto,
|
||||
) {
|
||||
return await this.deviceService.getAllDevicesBySpaceOrCommunityWithChild(
|
||||
query,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -22,8 +22,6 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { DeviceProjectController } from './controllers/device-project.controller';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceSpaceOrCommunityController } from './controllers/device-space-community.controller';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule,
|
||||
@ -32,11 +30,7 @@ import { DeviceSpaceOrCommunityController } from './controllers/device-space-com
|
||||
DeviceRepositoryModule,
|
||||
DeviceStatusFirebaseModule,
|
||||
],
|
||||
controllers: [
|
||||
DeviceController,
|
||||
DeviceProjectController,
|
||||
DeviceSpaceOrCommunityController,
|
||||
],
|
||||
controllers: [DeviceController, DeviceProjectController],
|
||||
providers: [
|
||||
DeviceService,
|
||||
ProductRepository,
|
||||
@ -52,7 +46,6 @@ import { DeviceSpaceOrCommunityController } from './controllers/device-space-com
|
||||
SceneRepository,
|
||||
SceneDeviceRepository,
|
||||
AutomationRepository,
|
||||
CommunityRepository,
|
||||
],
|
||||
exports: [DeviceService],
|
||||
})
|
||||
|
||||
@ -1,13 +1,6 @@
|
||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import {
|
||||
IsEnum,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUUID,
|
||||
ValidateIf,
|
||||
} from 'class-validator';
|
||||
import { IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class GetDeviceBySpaceUuidDto {
|
||||
@ApiProperty({
|
||||
@ -51,24 +44,3 @@ export class GetDoorLockDevices {
|
||||
@IsOptional()
|
||||
public deviceType: DeviceTypeEnum;
|
||||
}
|
||||
export class GetDevicesBySpaceOrCommunityDto {
|
||||
@ApiProperty({
|
||||
description: 'Device Product Type',
|
||||
example: 'PC',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public productType: string;
|
||||
@IsUUID('4', { message: 'Invalid space UUID format' })
|
||||
@IsOptional()
|
||||
spaceUuid?: string;
|
||||
|
||||
@IsUUID('4', { message: 'Invalid community UUID format' })
|
||||
@IsOptional()
|
||||
communityUuid?: string;
|
||||
|
||||
@ValidateIf((o) => !o.spaceUuid && !o.communityUuid)
|
||||
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
||||
requireEither?: never; // This ensures at least one of them is provided
|
||||
}
|
||||
|
||||
@ -31,7 +31,6 @@ import {
|
||||
import {
|
||||
GetDeviceBySpaceUuidDto,
|
||||
GetDeviceLogsDto,
|
||||
GetDevicesBySpaceOrCommunityDto,
|
||||
GetDoorLockDevices,
|
||||
} from '../dtos/get.device.dto';
|
||||
import {
|
||||
@ -66,8 +65,6 @@ import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { ProjectParam } from '../dtos';
|
||||
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
|
||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceService {
|
||||
@ -83,7 +80,6 @@ export class DeviceService {
|
||||
private readonly sceneService: SceneService,
|
||||
private readonly tuyaService: TuyaService,
|
||||
private readonly projectRepository: ProjectRepository,
|
||||
private readonly communityRepository: CommunityRepository,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
@ -1726,138 +1722,4 @@ export class DeviceService {
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
}
|
||||
async getAllDevicesBySpaceOrCommunityWithChild(
|
||||
query: GetDevicesBySpaceOrCommunityDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const { spaceUuid, communityUuid, productType } = query;
|
||||
if (!spaceUuid && !communityUuid) {
|
||||
throw new BadRequestException(
|
||||
'Either spaceUuid or communityUuid must be provided',
|
||||
);
|
||||
}
|
||||
|
||||
// Get devices based on space or community
|
||||
const devices = spaceUuid
|
||||
? await this.getAllDevicesBySpace(spaceUuid)
|
||||
: await this.getAllDevicesByCommunity(communityUuid);
|
||||
|
||||
if (!devices?.length) {
|
||||
return new SuccessResponseDto({
|
||||
message: `No devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
data: [],
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
}
|
||||
|
||||
const devicesFilterd = devices.filter(
|
||||
(device) => device.productDevice?.prodType === productType,
|
||||
);
|
||||
|
||||
if (devicesFilterd.length === 0) {
|
||||
return new SuccessResponseDto({
|
||||
message: `No ${productType} devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
data: [],
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Devices fetched successfully`,
|
||||
data: devicesFilterd,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getAllDevicesBySpace(spaceUuid: string): Promise<DeviceEntity[]> {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
if (!space) {
|
||||
throw new NotFoundException('Space not found');
|
||||
}
|
||||
|
||||
const allDevices: DeviceEntity[] = [];
|
||||
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
|
||||
|
||||
// Recursive fetch function
|
||||
const fetchChildren = async (parentSpace: SpaceEntity) => {
|
||||
const children = await this.spaceRepository.find({
|
||||
where: { parent: { uuid: parentSpace.uuid } },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
for (const child of children) {
|
||||
allDevices.push(...addSpaceUuidToDevices(child.devices, child.uuid));
|
||||
|
||||
if (child.children.length > 0) {
|
||||
await fetchChildren(child);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Start recursive fetch
|
||||
await fetchChildren(space);
|
||||
|
||||
return allDevices;
|
||||
}
|
||||
async getAllDevicesByCommunity(
|
||||
communityUuid: string,
|
||||
): Promise<DeviceEntity[]> {
|
||||
const community = await this.communityRepository.findOne({
|
||||
where: { uuid: communityUuid },
|
||||
relations: [
|
||||
'spaces',
|
||||
'spaces.children',
|
||||
'spaces.devices',
|
||||
'spaces.devices.productDevice',
|
||||
],
|
||||
});
|
||||
|
||||
if (!community) {
|
||||
throw new NotFoundException('Community not found');
|
||||
}
|
||||
|
||||
const allDevices: DeviceEntity[] = [];
|
||||
const visitedSpaceUuids = new Set<string>();
|
||||
|
||||
// Recursive fetch function with visited check
|
||||
const fetchSpaceDevices = async (space: SpaceEntity) => {
|
||||
if (visitedSpaceUuids.has(space.uuid)) return;
|
||||
visitedSpaceUuids.add(space.uuid);
|
||||
|
||||
if (space.devices?.length) {
|
||||
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
|
||||
}
|
||||
|
||||
if (space.children?.length) {
|
||||
for (const child of space.children) {
|
||||
const fullChild = await this.spaceRepository.findOne({
|
||||
where: { uuid: child.uuid },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
if (fullChild) {
|
||||
await fetchSpaceDevices(fullChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (const space of community.spaces) {
|
||||
await fetchSpaceDevices(space);
|
||||
}
|
||||
|
||||
return allDevices;
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,8 +27,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
controllers: [DoorLockController],
|
||||
@ -54,8 +52,6 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
CommunityRepository,
|
||||
],
|
||||
exports: [DoorLockService],
|
||||
})
|
||||
|
||||
@ -25,8 +25,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
controllers: [GroupController],
|
||||
@ -51,8 +49,6 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
CommunityRepository,
|
||||
],
|
||||
exports: [GroupService],
|
||||
})
|
||||
|
||||
@ -83,7 +83,6 @@ import {
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, InviteUserRepositoryModule, CommunityModule],
|
||||
@ -153,7 +152,6 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
],
|
||||
exports: [InviteUserService],
|
||||
})
|
||||
|
||||
@ -1 +0,0 @@
|
||||
export * from './occupancy.controller';
|
||||
@ -1,71 +0,0 @@
|
||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiParam,
|
||||
} from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { OccupancyService } from '../services/occupancy.service';
|
||||
import {
|
||||
GetOccupancyDurationBySpaceDto,
|
||||
GetOccupancyHeatMapBySpaceDto,
|
||||
} from '../dto/get-occupancy.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SpaceParamsDto } from '../dto/occupancy-params.dto';
|
||||
|
||||
@ApiTags('Occupancy Module')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
path: ControllerRoute.Occupancy.ROUTE,
|
||||
})
|
||||
export class OccupancyController {
|
||||
constructor(private readonly occupancyService: OccupancyService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('heat-map/space/:spaceUuid')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.Occupancy.ACTIONS.GET_OCCUPANCY_HEAT_MAP_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.Occupancy.ACTIONS.GET_OCCUPANCY_HEAT_MAP_DESCRIPTION,
|
||||
})
|
||||
@ApiParam({
|
||||
name: 'spaceUuid',
|
||||
description: 'UUID of the Space',
|
||||
required: true,
|
||||
})
|
||||
async getOccupancyHeatMapDataBySpace(
|
||||
@Param() params: SpaceParamsDto,
|
||||
@Query() query: GetOccupancyHeatMapBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.occupancyService.getOccupancyHeatMapDataBySpace(
|
||||
params,
|
||||
query,
|
||||
);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('duration/space/:spaceUuid')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.Occupancy.ACTIONS.GET_OCCUPANCY_HEAT_MAP_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.Occupancy.ACTIONS.GET_OCCUPANCY_HEAT_MAP_DESCRIPTION,
|
||||
})
|
||||
@ApiParam({
|
||||
name: 'spaceUuid',
|
||||
description: 'UUID of the Space',
|
||||
required: true,
|
||||
})
|
||||
async getOccupancyDurationDataBySpace(
|
||||
@Param() params: SpaceParamsDto,
|
||||
@Query() query: GetOccupancyDurationBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.occupancyService.getOccupancyDurationDataBySpace(
|
||||
params,
|
||||
query,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Matches, IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class GetOccupancyHeatMapBySpaceDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'Input year in YYYY format to filter the data',
|
||||
example: '2025',
|
||||
required: false,
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@Matches(/^\d{4}$/, {
|
||||
message: 'Year must be in YYYY format',
|
||||
})
|
||||
year: string;
|
||||
}
|
||||
export class GetOccupancyDurationBySpaceDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'Month and year in format YYYY-MM',
|
||||
example: '2025-03',
|
||||
required: true,
|
||||
})
|
||||
@Matches(/^\d{4}-(0[1-9]|1[0-2])$/, {
|
||||
message: 'monthDate must be in YYYY-MM format',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
monthDate: string;
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
|
||||
export class SpaceParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
@IsNotEmpty()
|
||||
spaceUuid: string;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { OccupancyController } from './controllers';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from './services';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [OccupancyController],
|
||||
providers: [OccupancyService, SqlLoaderService],
|
||||
})
|
||||
export class OccupancyModule {}
|
||||
@ -1 +0,0 @@
|
||||
export * from './occupancy.service';
|
||||
@ -1,103 +0,0 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
GetOccupancyDurationBySpaceDto,
|
||||
GetOccupancyHeatMapBySpaceDto,
|
||||
} from '../dto/get-occupancy.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { SpaceParamsDto } from '../dto/occupancy-params.dto';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class OccupancyService {
|
||||
constructor(
|
||||
private readonly sqlLoader: SqlLoaderService,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async getOccupancyDurationDataBySpace(
|
||||
params: SpaceParamsDto,
|
||||
query: GetOccupancyDurationBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { monthDate } = query;
|
||||
const { spaceUuid } = params;
|
||||
|
||||
try {
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_occupancy_duration',
|
||||
'procedure_select_daily_space_occupancy_duration',
|
||||
[spaceUuid, monthDate],
|
||||
);
|
||||
const formattedData = data.map((item) => ({
|
||||
...item,
|
||||
event_date: new Date(item.event_date).toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||
}));
|
||||
return this.buildResponse(
|
||||
`Occupancy duration data fetched successfully for ${spaceUuid} space`,
|
||||
formattedData,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch occupancy duration data', {
|
||||
error,
|
||||
spaceUuid,
|
||||
});
|
||||
throw new HttpException(
|
||||
error.response?.message || 'Failed to fetch occupancy duration data',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getOccupancyHeatMapDataBySpace(
|
||||
params: SpaceParamsDto,
|
||||
query: GetOccupancyHeatMapBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { year } = query;
|
||||
const { spaceUuid } = params;
|
||||
|
||||
try {
|
||||
const data = await this.executeProcedure(
|
||||
'fact_space_occupancy_count',
|
||||
'proceduce_select_fact_space_occupancy',
|
||||
[spaceUuid, year],
|
||||
);
|
||||
const formattedData = data.map((item) => ({
|
||||
...item,
|
||||
event_date: new Date(item.event_date).toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||
}));
|
||||
return this.buildResponse(
|
||||
`Occupancy heat map data fetched successfully for ${spaceUuid} space`,
|
||||
formattedData,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch occupancy heat map data', {
|
||||
error,
|
||||
spaceUuid,
|
||||
});
|
||||
throw new HttpException(
|
||||
error.response?.message || 'Failed to fetch occupancy heat map data',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private buildResponse(message: string, data: any[]) {
|
||||
return new SuccessResponseDto({
|
||||
message,
|
||||
data,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
}
|
||||
private async executeProcedure(
|
||||
procedureFolderName: string,
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<any[]> {
|
||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
||||
return await this.dataSource.query(query, params);
|
||||
}
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
}
|
||||
}
|
||||
@ -4,21 +4,14 @@ import {
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiParam,
|
||||
ApiQuery,
|
||||
} from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { PowerClampService } from '../services/power-clamp.service';
|
||||
import {
|
||||
GetPowerClampBySpaceDto,
|
||||
GetPowerClampDto,
|
||||
} from '../dto/get-power-clamp.dto';
|
||||
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import {
|
||||
PowerClampParamsDto,
|
||||
ResourceParamsDto,
|
||||
} from '../dto/power-clamp-params.dto';
|
||||
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
|
||||
|
||||
@ApiTags('Power Clamp Module')
|
||||
@Controller({
|
||||
@ -46,34 +39,4 @@ export class PowerClampController {
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.powerClampService.getPowerClampData(params, query);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('historical')
|
||||
@ApiOperation({
|
||||
summary:
|
||||
ControllerRoute.PowerClamp.ACTIONS
|
||||
.GET_ENERGY_BY_COMMUNITY_OR_SPACE_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.PowerClamp.ACTIONS
|
||||
.GET_ENERGY_BY_COMMUNITY_OR_SPACE_DESCRIPTION,
|
||||
})
|
||||
@ApiQuery({
|
||||
name: 'spaceUuid',
|
||||
description: 'UUID of the Space',
|
||||
required: false,
|
||||
})
|
||||
@ApiQuery({
|
||||
name: 'communityUuid',
|
||||
description: 'UUID of the Community',
|
||||
required: false,
|
||||
})
|
||||
async getPowerClampDataBySpaceOrCommunity(
|
||||
@Query() params: ResourceParamsDto,
|
||||
@Query() query: GetPowerClampBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.powerClampService.getPowerClampDataBySpaceOrCommunity(
|
||||
params,
|
||||
query,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +1,5 @@
|
||||
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Transform } from 'class-transformer';
|
||||
import {
|
||||
IsOptional,
|
||||
IsDateString,
|
||||
Matches,
|
||||
IsNotEmpty,
|
||||
IsBoolean,
|
||||
} from 'class-validator';
|
||||
import { IsOptional, IsDateString, Matches } from 'class-validator';
|
||||
|
||||
export class GetPowerClampDto {
|
||||
@ApiPropertyOptional({
|
||||
@ -41,26 +33,3 @@ export class GetPowerClampDto {
|
||||
})
|
||||
year?: string;
|
||||
}
|
||||
export class GetPowerClampBySpaceDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'monthDate must be in YYYY-MM format',
|
||||
example: '2025-04',
|
||||
required: true,
|
||||
})
|
||||
@IsDateString()
|
||||
@IsNotEmpty()
|
||||
public monthDate: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
example: true,
|
||||
description: 'Whether to group results by device or not',
|
||||
required: false,
|
||||
default: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@Transform((value) => {
|
||||
return value.obj.groupByDevice === BooleanValues.TRUE;
|
||||
})
|
||||
public groupByDevice?: boolean = false;
|
||||
}
|
||||
|
||||
@ -1,23 +1,6 @@
|
||||
import { IsNotEmpty, IsOptional, IsUUID, ValidateIf } from 'class-validator';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class PowerClampParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
powerClampUuid: string;
|
||||
}
|
||||
export class SpaceParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
spaceUuid: string;
|
||||
}
|
||||
export class ResourceParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
@IsOptional()
|
||||
spaceUuid?: string;
|
||||
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
@IsOptional()
|
||||
communityUuid?: string;
|
||||
|
||||
@ValidateIf((o) => !o.spaceUuid && !o.communityUuid)
|
||||
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
||||
requireEither?: never; // This ensures at least one of them is provided
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
||||
import { PowerClampService } from './services/power-clamp.service';
|
||||
import { PowerClampController } from './controllers';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
@ -8,110 +8,16 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import {
|
||||
SpaceDeviceService,
|
||||
SpaceLinkService,
|
||||
SpaceService,
|
||||
SubspaceDeviceService,
|
||||
SubSpaceService,
|
||||
ValidationService,
|
||||
} from 'src/space/services';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import {
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
TagRepository,
|
||||
} from '@app/common/modules/space';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { TagService } from 'src/tags/services';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [PowerClampController],
|
||||
providers: [
|
||||
PowerClamp,
|
||||
PowerClampService,
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
DeviceRepository,
|
||||
SpaceDeviceService,
|
||||
TuyaService,
|
||||
ValidationService,
|
||||
DeviceService,
|
||||
SpaceRepository,
|
||||
CommunityService,
|
||||
ProjectRepository,
|
||||
CommunityRepository,
|
||||
SpaceModelRepository,
|
||||
SceneDeviceRepository,
|
||||
ProductRepository,
|
||||
DeviceStatusFirebaseService,
|
||||
SceneService,
|
||||
SpaceService,
|
||||
PowerClampService,
|
||||
DeviceStatusLogRepository,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
AutomationRepository,
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkService,
|
||||
SubSpaceService,
|
||||
TagService,
|
||||
SpaceModelService,
|
||||
SpaceProductAllocationService,
|
||||
SqlLoaderService,
|
||||
SpaceLinkRepository,
|
||||
SubspaceRepository,
|
||||
SubspaceDeviceService,
|
||||
SubspaceProductAllocationService,
|
||||
NewTagRepository,
|
||||
SubSpaceModelService,
|
||||
SpaceModelProductAllocationService,
|
||||
SpaceProductAllocationRepository,
|
||||
SubspaceProductAllocationRepository,
|
||||
TagRepository,
|
||||
SubspaceModelRepository,
|
||||
SubspaceModelProductAllocationService,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
OccupancyService,
|
||||
],
|
||||
exports: [PowerClamp],
|
||||
exports: [PowerClampService],
|
||||
})
|
||||
export class PowerClampModule {}
|
||||
|
||||
@ -1,32 +1,13 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
GetPowerClampBySpaceDto,
|
||||
GetPowerClampDto,
|
||||
} from '../dto/get-power-clamp.dto';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { GetPowerClampDto } from '../dto/get-power-clamp.dto';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import {
|
||||
PowerClampParamsDto,
|
||||
ResourceParamsDto,
|
||||
} from '../dto/power-clamp-params.dto';
|
||||
import { PowerClampParamsDto } from '../dto/power-clamp-params.dto';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { SpaceDeviceService } from 'src/space/services';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { filterByMonth, toMMYYYY } from '@app/common/helper/date-format';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PowerClampService {
|
||||
@ -35,119 +16,8 @@ export class PowerClampService {
|
||||
private readonly powerClampHourlyRepository: PowerClampHourlyRepository,
|
||||
private readonly powerClampMonthlyRepository: PowerClampMonthlyRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly spaceDeviceService: SpaceDeviceService,
|
||||
private readonly sqlLoader: SqlLoaderService,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly communityService: CommunityService,
|
||||
) {}
|
||||
|
||||
async getPowerClampDataBySpaceOrCommunity(
|
||||
params: ResourceParamsDto,
|
||||
query: GetPowerClampBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { monthDate, groupByDevice } = query;
|
||||
const { spaceUuid, communityUuid } = params;
|
||||
|
||||
try {
|
||||
// Validate we have at least one identifier
|
||||
if (!spaceUuid && !communityUuid) {
|
||||
throw new BadRequestException(
|
||||
'Either spaceUuid or communityUuid must be provided',
|
||||
);
|
||||
}
|
||||
|
||||
// Get devices based on space or community
|
||||
const devices = spaceUuid
|
||||
? await this.spaceDeviceService.getAllDevicesBySpace(spaceUuid)
|
||||
: await this.communityService.getAllDevicesByCommunity(communityUuid);
|
||||
|
||||
if (!devices?.length) {
|
||||
return this.buildResponse(
|
||||
`No devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
// Filter power clamp devices
|
||||
const powerClampDevices = devices.filter(
|
||||
(device) => device.productDevice?.prodType === ProductType.PC,
|
||||
);
|
||||
|
||||
if (powerClampDevices.length === 0) {
|
||||
return this.buildResponse(
|
||||
`No power clamp devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
const formattedMonthDate = toMMYYYY(monthDate);
|
||||
|
||||
if (groupByDevice) {
|
||||
// Handle per-device response
|
||||
const deviceDataPromises = powerClampDevices.map(async (device) => {
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_energy_consumed_procedure',
|
||||
[formattedMonthDate, device.uuid],
|
||||
);
|
||||
|
||||
const formattedData = data.map((item) => ({
|
||||
...item,
|
||||
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||
}));
|
||||
|
||||
const filteredData = monthDate
|
||||
? filterByMonth(formattedData, monthDate)
|
||||
: formattedData;
|
||||
|
||||
return {
|
||||
deviceUuid: device.uuid,
|
||||
deviceName: device.name || `Power Clamp Device`,
|
||||
data: filteredData,
|
||||
};
|
||||
});
|
||||
|
||||
const deviceDataResults = await Promise.all(deviceDataPromises);
|
||||
|
||||
return this.buildResponse(
|
||||
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
|
||||
deviceDataResults,
|
||||
);
|
||||
} else {
|
||||
// Original behavior - all devices together
|
||||
const deviceUuids = powerClampDevices.map((device) => device.uuid);
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_energy_consumed_procedure',
|
||||
[formattedMonthDate, deviceUuids.join(',')],
|
||||
);
|
||||
|
||||
// Format and filter data
|
||||
const formattedData = data.map((item) => ({
|
||||
...item,
|
||||
date: new Date(item.date).toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||
}));
|
||||
|
||||
const resultData = monthDate
|
||||
? filterByMonth(formattedData, monthDate)
|
||||
: formattedData;
|
||||
|
||||
return this.buildResponse(
|
||||
`Power clamp data fetched successfully for ${spaceUuid ? 'space' : 'community'}`,
|
||||
resultData,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching power clamp data', {
|
||||
error,
|
||||
spaceUuid,
|
||||
communityUuid,
|
||||
});
|
||||
throw new HttpException(
|
||||
error.response?.message || 'Failed to fetch power clamp data',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getPowerClampData(
|
||||
params: PowerClampParamsDto,
|
||||
query: GetPowerClampDto,
|
||||
@ -229,17 +99,4 @@ export class PowerClampService {
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
}
|
||||
private async executeProcedure(
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<any[]> {
|
||||
const query = this.loadQuery(
|
||||
'fact_space_energy_consumed',
|
||||
procedureFileName,
|
||||
);
|
||||
return await this.dataSource.query(query, params);
|
||||
}
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
@ -15,6 +13,7 @@ import {
|
||||
Res,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
@ -22,10 +21,11 @@ import {
|
||||
ApiResponse,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { Response } from 'express';
|
||||
import { CreateProjectDto, GetProjectParam } from '../dto';
|
||||
import { ListProjectsDto } from '../dto/list-project.dto';
|
||||
import { ProjectService } from '../services';
|
||||
import { CreateProjectDto, GetProjectParam } from '../dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
||||
|
||||
@ApiTags('Project Module')
|
||||
@Controller({
|
||||
@ -80,7 +80,9 @@ export class ProjectController {
|
||||
description: ControllerRoute.PROJECT.ACTIONS.LIST_PROJECTS_DESCRIPTION,
|
||||
})
|
||||
@Get()
|
||||
async list(@Query() query: ListProjectsDto): Promise<BaseResponseDto> {
|
||||
async list(
|
||||
@Query() query: PaginationRequestGetListDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return this.projectService.listProjects(query);
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
|
||||
import { PickType } from '@nestjs/swagger';
|
||||
|
||||
export class ListProjectsDto extends PickType(PaginationRequestGetListDto, [
|
||||
'page',
|
||||
'size',
|
||||
]) {}
|
||||
@ -67,7 +67,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
|
||||
const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||
|
||||
@ -125,7 +124,6 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
],
|
||||
exports: [ProjectService, CqrsModule],
|
||||
})
|
||||
|
||||
@ -16,7 +16,6 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||
@ -33,7 +32,6 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
ProjectRepository,
|
||||
SceneDeviceRepository,
|
||||
AutomationRepository,
|
||||
CommunityRepository,
|
||||
],
|
||||
exports: [SceneService],
|
||||
})
|
||||
|
||||
@ -65,7 +65,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
|
||||
const CommandHandlers = [
|
||||
PropogateUpdateSpaceModelHandler,
|
||||
@ -125,7 +124,6 @@ const CommandHandlers = [
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
],
|
||||
exports: [CqrsModule, SpaceModelService],
|
||||
})
|
||||
|
||||
@ -1,12 +1,11 @@
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { GetSpaceParam } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SpaceDeviceService } from '../services';
|
||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||
import { GetDevicesBySpaceDto } from '../dtos/device.space.dto';
|
||||
|
||||
@ApiTags('Space Module')
|
||||
@Controller({
|
||||
@ -27,8 +26,7 @@ export class SpaceDeviceController {
|
||||
@Get()
|
||||
async listDevicesInSpace(
|
||||
@Param() params: GetSpaceParam,
|
||||
@Query() query: GetDevicesBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.spaceDeviceService.listDevicesInSpace(params, query);
|
||||
return await this.spaceDeviceService.listDevicesInSpace(params);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class GetDevicesBySpaceDto {
|
||||
@ApiProperty({
|
||||
description: 'Device Product Type',
|
||||
example: 'PC',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
public productType?: string;
|
||||
}
|
||||
@ -1,11 +1,6 @@
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { GetSpaceParam } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
@ -14,11 +9,6 @@ import { ValidationService } from './space-validation.service';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { GetDevicesBySpaceDto } from '../dtos/device.space.dto';
|
||||
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceDeviceService {
|
||||
@ -26,15 +16,11 @@ export class SpaceDeviceService {
|
||||
private readonly tuyaService: TuyaService,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
) {}
|
||||
|
||||
async listDevicesInSpace(
|
||||
params: GetSpaceParam,
|
||||
query: GetDevicesBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
async listDevicesInSpace(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||
const { spaceUuid, communityUuid, projectUuid } = params;
|
||||
const { productType } = query;
|
||||
|
||||
try {
|
||||
// Validate community, project, and fetch space including devices in a single query
|
||||
const space = await this.validationService.fetchSpaceDevices(spaceUuid);
|
||||
@ -56,23 +42,7 @@ export class SpaceDeviceService {
|
||||
const detailedDevices = (await Promise.allSettled(deviceDetailsPromises))
|
||||
.filter((result) => result.status === 'fulfilled' && result.value)
|
||||
.map((result) => (result as PromiseFulfilledResult<any>).value);
|
||||
console.log('detailedDevices', detailedDevices);
|
||||
if (productType) {
|
||||
const devicesFilterd = detailedDevices.filter(
|
||||
(device) => device.productType === productType,
|
||||
);
|
||||
if (devicesFilterd.length === 0) {
|
||||
return new SuccessResponseDto({
|
||||
message: `No ${productType} devices found for ${spaceUuid ? 'space' : 'community'}`,
|
||||
data: [],
|
||||
statusCode: HttpStatus.CREATED,
|
||||
});
|
||||
}
|
||||
return new SuccessResponseDto({
|
||||
data: devicesFilterd,
|
||||
message: 'Successfully retrieved list of devices.',
|
||||
});
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
data: detailedDevices,
|
||||
message: 'Successfully retrieved list of devices.',
|
||||
@ -151,38 +121,4 @@ export class SpaceDeviceService {
|
||||
);
|
||||
return batteryStatus ? batteryStatus.value : null;
|
||||
}
|
||||
async getAllDevicesBySpace(spaceUuid: string): Promise<DeviceEntity[]> {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
if (!space) {
|
||||
throw new NotFoundException('Space not found');
|
||||
}
|
||||
|
||||
const allDevices: DeviceEntity[] = [];
|
||||
allDevices.push(...addSpaceUuidToDevices(space.devices, space.uuid));
|
||||
|
||||
// Recursive fetch function
|
||||
const fetchChildren = async (parentSpace: SpaceEntity) => {
|
||||
const children = await this.spaceRepository.find({
|
||||
where: { parent: { uuid: parentSpace.uuid } },
|
||||
relations: ['children', 'devices', 'devices.productDevice'],
|
||||
});
|
||||
|
||||
for (const child of children) {
|
||||
allDevices.push(...addSpaceUuidToDevices(child.devices, child.uuid));
|
||||
|
||||
if (child.children.length > 0) {
|
||||
await fetchChildren(child);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Start recursive fetch
|
||||
await fetchChildren(space);
|
||||
|
||||
return allDevices;
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,8 +213,8 @@ export class SpaceService {
|
||||
{ incomingConnectionDisabled: false },
|
||||
)
|
||||
.leftJoinAndSelect('space.productAllocations', 'productAllocations')
|
||||
// .leftJoinAndSelect('productAllocations.tags', 'tags')
|
||||
// .leftJoinAndSelect('productAllocations.product', 'product')
|
||||
.leftJoinAndSelect('productAllocations.tags', 'tags')
|
||||
.leftJoinAndSelect('tags.product', 'tagProduct')
|
||||
.leftJoinAndSelect(
|
||||
'space.subspaces',
|
||||
'subspaces',
|
||||
@ -225,11 +225,8 @@ export class SpaceService {
|
||||
'subspaces.productAllocations',
|
||||
'subspaceProductAllocations',
|
||||
)
|
||||
// .leftJoinAndSelect('subspaceProductAllocations.tags', 'subspaceTag')
|
||||
// .leftJoinAndSelect(
|
||||
// 'subspaceProductAllocations.product',
|
||||
// 'subspaceProduct',
|
||||
// )
|
||||
.leftJoinAndSelect('subspaceProductAllocations.tags', 'subspaceTags')
|
||||
.leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct')
|
||||
.leftJoinAndSelect('space.spaceModel', 'spaceModel')
|
||||
.where('space.community_id = :communityUuid', { communityUuid })
|
||||
.andWhere('space.spaceName != :orphanSpaceName', {
|
||||
@ -267,7 +264,9 @@ export class SpaceService {
|
||||
}),
|
||||
);
|
||||
}
|
||||
const spaceHierarchy = this.buildSpaceHierarchy(spaces);
|
||||
|
||||
const transformedSpaces = spaces.map(this.transformSpace);
|
||||
const spaceHierarchy = this.buildSpaceHierarchy(transformedSpaces);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Spaces in community ${communityUuid} successfully fetched in hierarchy`,
|
||||
@ -327,13 +326,13 @@ export class SpaceService {
|
||||
'incomingConnections.disabled = :incomingConnectionDisabled',
|
||||
{ incomingConnectionDisabled: false },
|
||||
)
|
||||
// .leftJoinAndSelect(
|
||||
// 'space.tags',
|
||||
// 'tags',
|
||||
// 'tags.disabled = :tagDisabled',
|
||||
// { tagDisabled: false },
|
||||
// )
|
||||
// .leftJoinAndSelect('tags.product', 'tagProduct')
|
||||
.leftJoinAndSelect(
|
||||
'space.tags',
|
||||
'tags',
|
||||
'tags.disabled = :tagDisabled',
|
||||
{ tagDisabled: false },
|
||||
)
|
||||
.leftJoinAndSelect('tags.product', 'tagProduct')
|
||||
.leftJoinAndSelect(
|
||||
'space.subspaces',
|
||||
'subspaces',
|
||||
@ -346,7 +345,7 @@ export class SpaceService {
|
||||
'subspaceTags.disabled = :subspaceTagsDisabled',
|
||||
{ subspaceTagsDisabled: false },
|
||||
)
|
||||
// .leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct')
|
||||
.leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct')
|
||||
.where('space.community_id = :communityUuid', { communityUuid })
|
||||
.andWhere('space.spaceName != :orphanSpaceName', {
|
||||
orphanSpaceName: ORPHAN_SPACE_NAME,
|
||||
|
||||
@ -90,7 +90,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
|
||||
export const CommandHandlers = [DisableSpaceHandler];
|
||||
|
||||
@ -167,7 +166,6 @@ export const CommandHandlers = [DisableSpaceHandler];
|
||||
PowerClampMonthlyRepository,
|
||||
PowerClampService,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
],
|
||||
exports: [SpaceService],
|
||||
})
|
||||
|
||||
@ -29,8 +29,6 @@ import {
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
||||
controllers: [VisitorPasswordController],
|
||||
@ -57,8 +55,6 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
CommunityRepository,
|
||||
],
|
||||
exports: [VisitorPasswordService],
|
||||
})
|
||||
|
||||
@ -1 +0,0 @@
|
||||
export * from './weather.controller';
|
||||
@ -1,28 +0,0 @@
|
||||
import { Controller, Get, Query } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route'; // Assuming this is where the routes are defined
|
||||
import { WeatherService } from '../services';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { GetWeatherDetailsDto } from '../dto/get.weather.dto';
|
||||
|
||||
@ApiTags('Weather Module')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
path: ControllerRoute.WEATHER.ROUTE, // use the static route constant
|
||||
})
|
||||
export class WeatherController {
|
||||
constructor(private readonly weatherService: WeatherService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.WEATHER.ACTIONS.FETCH_WEATHER_DETAILS_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.WEATHER.ACTIONS.FETCH_WEATHER_DETAILS_DESCRIPTION,
|
||||
})
|
||||
async fetchWeatherDetails(
|
||||
@Query() query: GetWeatherDetailsDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.weatherService.fetchWeatherDetails(query);
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsNumber } from 'class-validator';
|
||||
|
||||
export class GetWeatherDetailsDto {
|
||||
@ApiProperty({
|
||||
description: 'Latitude coordinate',
|
||||
example: 35.6895,
|
||||
})
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
lat: number;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Longitude coordinate',
|
||||
example: 139.6917,
|
||||
})
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
lon: number;
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
export * from './weather.service';
|
||||
@ -1,51 +0,0 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { GetWeatherDetailsDto } from '../dto/get.weather.dto';
|
||||
import { calculateAQI } from '@app/common/util/calculate.aqi';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class WeatherService {
|
||||
private readonly weatherApiUrl: string;
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly httpService: HttpService,
|
||||
) {
|
||||
this.weatherApiUrl = this.configService.get<string>('WEATHER_API_URL');
|
||||
}
|
||||
|
||||
async fetchWeatherDetails(
|
||||
query: GetWeatherDetailsDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const { lat, lon } = query;
|
||||
const weatherApiKey = this.configService.get<string>(
|
||||
'OPEN_WEATHER_MAP_API_KEY',
|
||||
);
|
||||
const url = `${this.weatherApiUrl}/current.json?key=${weatherApiKey}&q=${lat},${lon}&aqi=yes`;
|
||||
|
||||
const response = await firstValueFrom(this.httpService.get(url));
|
||||
const pm2_5 = response.data.current.air_quality.pm2_5; // Raw PM2.5 (µg/m³)
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Weather details fetched successfully`,
|
||||
data: {
|
||||
aqi: calculateAQI(pm2_5), // Converted AQI (0-500)
|
||||
temperature: response.data.current.temp_c,
|
||||
humidity: response.data.current.humidity,
|
||||
},
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`Error fetching weather data: ${error}`);
|
||||
|
||||
throw new HttpException(
|
||||
`Api can't handle these lat and lon values`,
|
||||
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { HttpModule } from '@nestjs/axios'; // <-- Import this!
|
||||
import { WeatherController } from './controllers';
|
||||
import { WeatherService } from './services';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, HttpModule],
|
||||
controllers: [WeatherController],
|
||||
providers: [WeatherService],
|
||||
})
|
||||
export class WeatherModule {}
|
||||
Reference in New Issue
Block a user