1+ import { ApplicationsWithErrorsResponseDto } from "@dto/applications-dashboard-responses" ;
12import { CreateApplicationDto } from "@dto/create-application.dto" ;
23import {
34 ListAllApplicationsResponseDto ,
@@ -30,7 +31,7 @@ import { MulticastService } from "@services/chirpstack/multicast.service";
3031import { DataTargetService } from "@services/data-targets/data-target.service" ;
3132import { OrganizationService } from "@services/user-management/organization.service" ;
3233import { PermissionService } from "@services/user-management/permission.service" ;
33- import { DeleteResult , In , Repository } from "typeorm" ;
34+ import { Brackets , DeleteResult , In , Repository } from "typeorm" ;
3435
3536@Injectable ( )
3637export class ApplicationService {
@@ -52,6 +53,58 @@ export class ApplicationService {
5253 private chirpstackApplicationService : ApplicationChirpstackService
5354 ) { }
5455
56+ async countApplicationsWithError (
57+ organizationId : number ,
58+ whitelist ?: number [ ]
59+ ) : Promise < ApplicationsWithErrorsResponseDto > {
60+ const queryBuilder = this . applicationRepository
61+ . createQueryBuilder ( "app" )
62+ . leftJoin ( "app.iotDevices" , "device" )
63+ . leftJoin ( "app.belongsTo" , "organization" )
64+ . leftJoin ( "device.latestReceivedMessage" , "latestMessage" )
65+ . leftJoin ( "app.dataTargets" , "dataTargets" )
66+ . andWhere ( "app.belongsToId = :organizationId" , { organizationId : organizationId } ) ;
67+
68+ if ( whitelist && whitelist . length > 0 ) {
69+ queryBuilder . where ( "app.id IN (:...whitelist)" , { whitelist } ) ;
70+ }
71+
72+ queryBuilder . andWhere (
73+ new Brackets ( qb => {
74+ qb . where ( "dataTargets.id IS NULL" ) . orWhere ( "latestMessage.sentTime < NOW() - INTERVAL '24 HOURS'" ) ;
75+ } )
76+ ) ;
77+
78+ try {
79+ const [ result , total ] = await queryBuilder . getManyAndCount ( ) ;
80+
81+ return {
82+ withError : result . length ,
83+ total : total ,
84+ } ;
85+ } catch ( error ) {
86+ throw new Error ( "Database query failed" ) ;
87+ }
88+ }
89+ async countAllDevices ( organizationId : number , whitelist ?: number [ ] ) : Promise < number > {
90+ const queryBuilder = this . applicationRepository
91+ . createQueryBuilder ( "app" )
92+ . leftJoinAndSelect ( "app.iotDevices" , "device" )
93+ . leftJoin ( "app.dataTargets" , "dataTargets" )
94+ . where ( "app.belongsToId = :organizationId" , { organizationId } ) ;
95+
96+ if ( whitelist && whitelist . length > 0 ) {
97+ queryBuilder . andWhere ( "app.id IN (:...whitelist)" , { whitelist } ) ;
98+ }
99+
100+ const count = await queryBuilder . select ( "COUNT(device.id)" , "count" ) . getRawOne ( ) ;
101+
102+ try {
103+ return count . count ? parseInt ( count . count , 10 ) : 0 ;
104+ } catch ( error ) {
105+ throw new Error ( "Database query failed" ) ;
106+ }
107+ }
55108 async findAndCountInList (
56109 query ?: ListAllApplicationsDto ,
57110 whitelist ?: number [ ]
@@ -61,9 +114,9 @@ export class ApplicationService {
61114 const queryBuilder = this . applicationRepository
62115 . createQueryBuilder ( "app" )
63116 . leftJoinAndSelect ( "app.iotDevices" , "device" )
64- . leftJoinAndSelect ( "app.dataTargets" , "dataTarget" )
65117 . leftJoinAndSelect ( "app.belongsTo" , "organization" )
66- . leftJoinAndSelect ( "device.latestReceivedMessage" , "latestMessage" ) ;
118+ . leftJoinAndSelect ( "device.latestReceivedMessage" , "latestMessage" )
119+ . leftJoinAndSelect ( "app.dataTargets" , "dataTargets" ) ;
67120
68121 if ( whitelist && whitelist . length > 0 ) {
69122 queryBuilder . where ( "app.id IN (:...whitelist)" , { whitelist } ) ;
@@ -74,43 +127,26 @@ export class ApplicationService {
74127 }
75128
76129 if ( query . status ) {
77- console . log ( "status : " + query . status ) ;
78130 queryBuilder . andWhere ( "app.status = :status" , { status : query . status } ) ;
79131 }
80132
81133 if ( query . owner ) {
82134 queryBuilder . andWhere ( "app.owner = :owner" , { owner : query . owner } ) ;
83135 }
84136
85- if ( query . statusCheck ) {
137+ if ( query . statusCheck === "alert" ) {
86138 queryBuilder . andWhere (
87- `
88- app.id IN (
89- SELECT
90- app.id,
91- CASE
92- WHEN COUNT(DISTINCT dataTarget.id) = 0 THEN 'alert'
93- WHEN latestMessage.sentTime < NOW() - INTERVAL '24 HOURS' THEN 'alert'
94- ELSE 'stable'
95- END AS statusCheck
96- FROM application app
97- LEFT JOIN data_target dataTarget ON dataTarget.applicationId = app.id
98- LEFT JOIN device ON device.applicationId = app.id
99- LEFT JOIN latestMessage ON latestMessage.deviceId = device.id
100- WHERE app.belongsToId = :organizationId
101- GROUP BY app.id
102- HAVING
103- (
104- COUNT(DISTINCT dataTarget.id) = 0
105- OR latestMessage.sentTime < NOW() - INTERVAL '24 HOURS'
106- )
107- AND (
108- :statusCheck = 'alert'
109- OR COUNT(DISTINCT dataTarget.id) > 0
110- )
111- )
112- ` ,
113- { statusCheck : query . statusCheck , organizationId : query . organizationId }
139+ new Brackets ( qb => {
140+ qb . where ( "dataTargets.id IS NULL" ) . orWhere ( "latestMessage.sentTime < NOW() - INTERVAL '24 HOURS'" ) ;
141+ } )
142+ ) ;
143+ }
144+
145+ if ( query . statusCheck === "stable" ) {
146+ queryBuilder . andWhere (
147+ new Brackets ( qb => {
148+ qb . where ( "dataTargets.id IS NOT NULL" ) . orWhere ( "latestMessage.sentTime > NOW() - INTERVAL '24 HOURS'" ) ;
149+ } )
114150 ) ;
115151 }
116152
@@ -145,7 +181,6 @@ export class ApplicationService {
145181 count : total ,
146182 } ;
147183 } catch ( error ) {
148- console . error ( "Error executing query:" , error ) ;
149184 throw new Error ( "Database query failed" ) ;
150185 }
151186 }
@@ -252,8 +287,6 @@ export class ApplicationService {
252287 }
253288
254289 async findFilterInformation ( applicationIds : number [ ] | "admin" , organizationId : number ) {
255- console . log ( applicationIds ) ;
256-
257290 const query = this . applicationRepository
258291 . createQueryBuilder ( "application" )
259292 . leftJoinAndSelect ( "application.belongsTo" , "organization" )
0 commit comments