From 94c3bb73b3f52bfa80b1a73808c0c9603d011e24 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 9 Jun 2025 19:26:36 -0500 Subject: [PATCH] fix(notifications): changes to help improve the downtime check for greater than x only while active --- .../controller/notifications/downTimeCheck.ts | 60 ++----------------- .../utils/processNotifications.ts | 12 +++- .../utils/views/downTimeCheck.hbs | 4 +- .../querys/notifications/downtimecheck.ts | 45 ++++++++++++++ 4 files changed, 63 insertions(+), 58 deletions(-) create mode 100644 server/services/sqlServer/querys/notifications/downtimecheck.ts diff --git a/server/services/notifications/controller/notifications/downTimeCheck.ts b/server/services/notifications/controller/notifications/downTimeCheck.ts index b8214a7..b1afb39 100644 --- a/server/services/notifications/controller/notifications/downTimeCheck.ts +++ b/server/services/notifications/controller/notifications/downTimeCheck.ts @@ -7,6 +7,7 @@ import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { createLog } from "../../../logger/logger.js"; import { sendEmail } from "../sendMail.js"; import { query } from "../../../sqlServer/prodSqlServer.js"; +import { downTimeCheck } from "../../../sqlServer/querys/notifications/downtimecheck.js"; export interface DownTime { downTimeId?: number; @@ -28,58 +29,9 @@ export default async function reprintLabelMonitor(notifyData: any) { } // console.log(data.secondarySetting[0].duration); - let dQuery = ` - SELECT - [IdHistoryStillstandsereignis] as downTimeId - ,DATEDIFF(MINUTE,b.[Startzeit], b.[Endzeit]) as totalDuration - --, b.[IdMaschine] - ,x.[Bezeichnung] as machineAlias - --,b.[IdStillstandsGrund], - , c.CTO_Code - ,c.Downtime_Description - --,b.[IdFehlermerkmal], - ,case when g.DT_Group_Desc is null then 'Not assigned yet' else g.DT_Group_Desc end as groupDesc - ,b.[Bemerkung] as remark - ,CONVERT(VARCHAR, CAST(b.[Startzeit] AS DATETIME), 100) dtStart - ,CONVERT(VARCHAR, CAST(b.[Endzeit] AS DATETIME), 100) dtEnd - - FROM Alplaprod_test1.[dbo].[T_HistoryStillstandsereignis] (nolock)b - - --get the machine info - left join - Alplaprod_test1.[dbo].[T_Maschine] (nolock)x - on b.IdMaschine = x.IdMaschine - - -- add in the cto codes - left join - Alplaprod_test1.[dbo].[V_MES_Downtime_Reasons] (nolock)c - on b.IdStillstandsGrund = c.Local_Downtime_ID - - left join - Alplaprod_test1.[dbo].[V_MES_Downtime_Characteristics] (nolock)g - on b.IdFehlermerkmal = g.Local_DT_Characteristic_Id - - - where DATEDIFF(MINUTE,b.[Startzeit],b.[Endzeit]) > ${ - notifyData.notifiySettings - ? notifyData.notifiySettings?.duration - : 10 - } - and b.[Startzeit] > getDate() - ${ - notifyData.notifiySettings - ? notifyData.notifiySettings?.daysInPast - : 10 - } --adding this date check in so we dont get everything possible - and c.CTO_Code not like 'a%' - and c.CTO_Code not like 'b%' - and c.CTO_Code not like 'c%' - and c.CTO_Code not like 'd%' - and c.CTO_Code not like 'e%' - and c.CTO_Code not like 'f%' - and c.CTO_Code not like 'y%' - order by IdHistoryStillstandsereignis desc - `; - + let dQuery = downTimeCheck + .replace("[dtDuration]", notifyData.notifiySettings?.duration) + .replace("[daysInPast]", notifyData.notifiySettings?.daysInPast); //console.log(query); let downTime: any = []; //DownTime[]; try { @@ -87,8 +39,8 @@ export default async function reprintLabelMonitor(notifyData: any) { //console.log(labels.length); downTime = res.data; if ( - downTime.length > 0 && - downTime[0]?.downTimeId > notifyData.notifiySettings.prodID + downTime.length > 0 + // && downTime[0]?.downTimeId > notifyData.notifiySettings.prodID ) { //send the email :D const emailSetup = { diff --git a/server/services/notifications/utils/processNotifications.ts b/server/services/notifications/utils/processNotifications.ts index ae6fff8..be775ec 100644 --- a/server/services/notifications/utils/processNotifications.ts +++ b/server/services/notifications/utils/processNotifications.ts @@ -1,5 +1,6 @@ import { db } from "../../../../database/dbclient.js"; import { notifications } from "../../../../database/schema/notifications.js"; +import { settings } from "../../../../database/schema/settings.js"; import { tryCatch } from "../../../globalUtils/tryCatch.js"; import type { JobInfo } from "../../../types/JobInfo.js"; import { createLog } from "../../logger/logger.js"; @@ -104,7 +105,14 @@ export const startNotificationMonitor = async () => { }, 5 * 1000); }; -const createJob = (id: string, schedule: string, task: () => Promise) => { +const createJob = async ( + id: string, + schedule: string, + task: () => Promise +) => { + const { data, error } = (await tryCatch(db.select().from(settings))) as any; + + const timeZone = data.filter((n: any) => n.name === "timezone"); // Destroy existing job if it exists if (runningCrons[id]) { runningCrons[id].stop(); // Croner uses .stop() instead of .destroy() @@ -114,7 +122,7 @@ const createJob = (id: string, schedule: string, task: () => Promise) => { runningCrons[id] = new Cron( schedule, { - timezone: "America/Chicago", + timezone: timeZone[0].timezone, catch: true, // Prevents unhandled rejections }, task diff --git a/server/services/notifications/utils/views/downTimeCheck.hbs b/server/services/notifications/utils/views/downTimeCheck.hbs index 58a8fe8..747724e 100644 --- a/server/services/notifications/utils/views/downTimeCheck.hbs +++ b/server/services/notifications/utils/views/downTimeCheck.hbs @@ -12,7 +12,7 @@ - + @@ -32,7 +32,7 @@ - + {{!-- --}} {{/each}} diff --git a/server/services/sqlServer/querys/notifications/downtimecheck.ts b/server/services/sqlServer/querys/notifications/downtimecheck.ts new file mode 100644 index 0000000..40b00cd --- /dev/null +++ b/server/services/sqlServer/querys/notifications/downtimecheck.ts @@ -0,0 +1,45 @@ +export const downTimeCheck = ` +SELECT + [IdHistoryStillstandsereignis] as downTimeId + ,DATEDIFF(MINUTE,b.[Startzeit], case when b.[Endzeit] = '1900-01-01' then getdate() else b.[Endzeit] end ) as totalDuration + --, b.[IdMaschine] + ,x.[Bezeichnung] as machineAlias + --,b.[IdStillstandsGrund], + , c.CTO_Code + ,c.Downtime_Description + --,b.[IdFehlermerkmal], + ,case when g.DT_Group_Desc is null then 'Not assigned yet' else g.DT_Group_Desc end as groupDesc + ,b.[Bemerkung] as remark + ,CONVERT(VARCHAR, CAST(b.[Startzeit] AS DATETIME), 100) dtStart + ,CONVERT(VARCHAR, CAST(b.[Endzeit] AS DATETIME), 100) dtEnd + ,b.Upd_Date +FROM Alplaprod_test1.[dbo].[T_HistoryStillstandsereignis] (nolock)b + +--get the machine info +left join +Alplaprod_test1.[dbo].[T_Maschine] (nolock)x +on b.IdMaschine = x.IdMaschine + +-- add in the cto codes +left join +Alplaprod_test1.[dbo].[V_MES_Downtime_Reasons] (nolock)c +on b.IdStillstandsGrund = c.Local_Downtime_ID + +left join +Alplaprod_test1.[dbo].[V_MES_Downtime_Characteristics] (nolock)g +on b.IdFehlermerkmal = g.Local_DT_Characteristic_Id + +where DATEDIFF(MINUTE,b.[Startzeit],case when b.[Endzeit] = '1900-01-01' then getdate() else b.[Endzeit] end) > [dtDuration] + +and b.[Startzeit] > DATEADD(day, -[daysInPast], getDate()) +and b.[Endzeit] = '1900-01-01' +--adding this date check in so we dont get everything possible +and c.CTO_Code not like 'a%' +and c.CTO_Code not like 'b%' +and c.CTO_Code not like 'c%' +and c.CTO_Code not like 'd%' +and c.CTO_Code not like 'e%' +and c.CTO_Code not like 'f%' +and c.CTO_Code not like 'y%' +order by IdHistoryStillstandsereignis desc +`;
totalDurationCurrent Durration machineAlias CTO_Code Downtime_Description{{groupDesc}} {{remark}} {{dtStart}}{{dtEnd}}{{dtEnd}}