modified the status query

This commit is contained in:
Salijoghli 2025-09-29 16:00:23 +04:00
parent 7eec6cd773
commit 2ae3e7087b
2 changed files with 62 additions and 130 deletions

View File

@ -633,7 +633,7 @@
"enabled": true,
"rate": "3"
},
"queryPath": "Alarms-autStand/GetActiveAlarms"
"queryPath": "autStand/Alarms/GetActiveAlarms"
},
"transforms": [
{
@ -1611,7 +1611,7 @@
"enabled": true,
"rate": "3"
},
"queryPath": "Alarms-autStand/GetAlarmsWithCount"
"queryPath": "autStand/Alarms/GetAlarmsWithCount"
},
"transforms": [
{
@ -3184,7 +3184,7 @@
"$": [
"ts",
192,
1759044922618
1759143973125
],
"$ts": 1755606669646
},
@ -3192,7 +3192,7 @@
"$": [
"ts",
192,
1759044922618
1759143973125
],
"$ts": 1755608469646
}
@ -3256,7 +3256,7 @@
"enabled": true,
"rate": "3"
},
"queryPath": "Alarms-autStand/GetAlarms"
"queryPath": "autStand/Alarms/GetAlarms"
},
"transforms": [
{
@ -4334,7 +4334,7 @@
"$": [
"ts",
192,
1759044922619
1759143973124
],
"$ts": 1755606669646
},
@ -4342,7 +4342,7 @@
"$": [
"ts",
192,
1759044922618
1759143973124
],
"$ts": 1755608469646
},
@ -4601,6 +4601,7 @@
"contentStyle": {
"classes": "Background-Styles/Grey-Background"
},
"currentTabIndex": 2,
"menuType": "modern",
"style": {
"classes": "Background-Styles/Grey-Background"

View File

@ -1,135 +1,66 @@
-- Generate all lanes 101-124 excluding 102, 104, 106
WITH DesiredLanes AS (
SELECT lane_number
SELECT 101 + n AS lane_number
FROM (
SELECT 101 + n AS lane_number
FROM (
SELECT a.N + b.N * 10 AS n
FROM (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a,
(SELECT 0 AS N UNION SELECT 1 UNION SELECT 2) b
) numbers
WHERE n <= 23 -- 101 to 124
) lanes
WHERE lane_number NOT IN (102, 104, 106) -- exclude these
SELECT a.N + b.N * 10 AS n
FROM (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a,
(SELECT 0 AS N UNION SELECT 1 UNION SELECT 2) b
) numbers
WHERE n <= 23 -- 101 to 124
AND (101 + n) NOT IN (102, 104, 106)
),
AlarmData AS (
SELECT DISTINCT
ae.id AS record_id,
ae.eventid AS event_uuid,
ae.source,
ae.eventtime AS active_time,
(SELECT MIN(eventtime)
FROM alarm_events ae2
WHERE ae2.eventid = ae.eventid
AND ae2.eventtype = 1
AND ae2.eventtime >= ae.eventtime
) AS clear_time
FROM alarm_events ae
WHERE
ae.eventtime BETWEEN :startDate AND :endDate
AND ae.eventtype = 0
AND (ae.source LIKE '%Chute%' OR ae.source LIKE '%ChuteStatus%')
),
LaneMapping AS (
-- Aggregate Full alarms per lane
FullAlarms AS (
SELECT
id AS record_id,
strvalue AS tag_value,
CASE
WHEN strvalue LIKE '%Chute[%' THEN
CAST(SUBSTRING(strvalue, LOCATE('Chute[', strvalue) + 6,
LOCATE(']', strvalue, LOCATE('Chute[', strvalue)) - LOCATE('Chute[', strvalue) - 6) AS UNSIGNED)
WHEN strvalue LIKE '%Chute[_%' THEN
CAST(SUBSTRING(strvalue, LOCATE('Chute_', strvalue) + 6) AS UNSIGNED)
WHEN strvalue LIKE '%Chute%' THEN
CAST(REGEXP_SUBSTR(strvalue, '[0-9]+') AS UNSIGNED)
ELSE NULL
END AS plc_index,
CASE
WHEN strvalue LIKE '%Chute[%' THEN
CASE
WHEN CAST(SUBSTRING(strvalue, LOCATE('Chute[', strvalue) + 6,
LOCATE(']', strvalue, LOCATE('Chute[', strvalue)) - LOCATE('Chute[', strvalue) - 6) AS UNSIGNED) BETWEEN 1 AND 30
THEN 101 + (CAST(SUBSTRING(strvalue, LOCATE('Chute[', strvalue) + 6,
LOCATE(']', strvalue, LOCATE('Chute[', strvalue)) - LOCATE('Chute[', strvalue) - 6) AS UNSIGNED) - 1)
WHEN CAST(SUBSTRING(strvalue, LOCATE('Chute[', strvalue) + 6,
LOCATE(']', strvalue, LOCATE('Chute[', strvalue)) - LOCATE('Chute[', strvalue) - 6) AS UNSIGNED) BETWEEN 31 AND 60
THEN 201 + (CAST(SUBSTRING(strvalue, LOCATE('Chute[', strvalue) + 6,
LOCATE(']', strvalue, LOCATE('Chute[', strvalue)) - LOCATE('Chute[', strvalue) - 6) AS UNSIGNED) - 31)
ELSE NULL
END
WHEN strvalue LIKE '%Chute[_%' THEN
CASE
WHEN CAST(SUBSTRING(strvalue, LOCATE('Chute_', strvalue) + 6) AS UNSIGNED) BETWEEN 1 AND 30
THEN 101 + (CAST(SUBSTRING(strvalue, LOCATE('Chute_', strvalue) + 6) AS UNSIGNED) - 1)
WHEN CAST(SUBSTRING(strvalue, LOCATE('Chute_', strvalue) + 6) AS UNSIGNED) BETWEEN 31 AND 60
THEN 201 + (CAST(SUBSTRING(strvalue, LOCATE('Chute_', strvalue) + 6) AS UNSIGNED) - 31)
ELSE NULL
END
WHEN strvalue LIKE '%Chute%' THEN
CASE
WHEN CAST(REGEXP_SUBSTR(strvalue, '[0-9]+') AS UNSIGNED) BETWEEN 1 AND 30
THEN 101 + (CAST(REGEXP_SUBSTR(strvalue, '[0-9]+') AS UNSIGNED) - 1)
WHEN CAST(REGEXP_SUBSTR(strvalue, '[0-9]+') AS UNSIGNED) BETWEEN 31 AND 60
THEN 201 + (CAST(REGEXP_SUBSTR(strvalue, '[0-9]+') AS UNSIGNED) - 31)
ELSE NULL
END
ELSE NULL
END AS lane_number
FROM alarm_event_data
WHERE propname = 'Tag' AND strvalue LIKE '%Chute%'
aed.strValue AS myLocation,
COUNT(*) AS FullCount,
SUM(TIMESTAMPDIFF(SECOND, ae.eventtime, COALESCE(ae_clear.eventtime, NOW()))) AS FullDuration
FROM alarm_events ae
LEFT JOIN alarm_events ae_clear
ON ae.eventid = ae_clear.eventid AND ae_clear.eventtype = 1
JOIN alarm_event_data aed
ON aed.id = ae.id AND aed.propname = 'myLocation'
WHERE ae.eventtype = 0
AND ae.source LIKE '%Full%'
AND ae.source LIKE '%Chute%'
AND ae.eventtime BETWEEN :startDate AND :endDate
GROUP BY aed.strValue
),
JamEvents AS (
SELECT DISTINCT
lm.lane_number,
ad.event_uuid,
ad.active_time,
ad.clear_time
FROM LaneMapping lm
INNER JOIN AlarmData ad ON lm.record_id = ad.record_id
INNER JOIN alarm_event_data aed ON ad.record_id = aed.id
WHERE aed.propname = 'Jam' AND aed.strvalue = '1'
),
FullEvents AS (
SELECT DISTINCT
lm.lane_number,
ad.event_uuid,
ad.active_time,
ad.clear_time
FROM LaneMapping lm
INNER JOIN AlarmData ad ON lm.record_id = ad.record_id
INNER JOIN alarm_event_data aed ON ad.record_id = aed.id
WHERE aed.propname = 'Full' AND aed.strvalue = '1'
-- Aggregate Jammed alarms per lane
JammedAlarms AS (
SELECT
aed.strValue AS myLocation,
COUNT(*) AS JamCount,
SUM(TIMESTAMPDIFF(SECOND, ae.eventtime, COALESCE(ae_clear.eventtime, NOW()))) AS JamDuration
FROM alarm_events ae
LEFT JOIN alarm_events ae_clear
ON ae.eventid = ae_clear.eventid AND ae_clear.eventtype = 1
JOIN alarm_event_data aed
ON aed.id = ae.id AND aed.propname = 'myLocation'
WHERE ae.eventtype = 0
AND ae.source LIKE '%Jammed%'
AND ae.source LIKE '%Chute%'
AND ae.eventtime BETWEEN :startDate AND :endDate
GROUP BY aed.strValue
),
-- Combine Full and Jammed
Aggregated AS (
SELECT
CONCAT('S03_', dl.lane_number, 'CH') AS lane,
COALESCE(j.jam_count, 0) AS jam_count,
COALESCE(f.full_count, 0) AS full_count,
COALESCE(j.jam_duration_seconds, 0) AS jam_duration_seconds,
COALESCE(f.full_duration_seconds, 0) AS full_duration_seconds
dl.lane_number,
COALESCE(f.FullCount, 0) AS FullCount,
COALESCE(j.JamCount, 0) AS JamCount,
COALESCE(f.FullDuration, 0) AS FullDuration,
COALESCE(j.JamDuration, 0) AS JamDuration
FROM DesiredLanes dl
LEFT JOIN (
SELECT
lane_number,
COUNT(DISTINCT event_uuid) AS jam_count,
SUM(TIMESTAMPDIFF(SECOND, active_time, COALESCE(clear_time, NOW()))) AS jam_duration_seconds
FROM JamEvents
GROUP BY lane_number
) j ON dl.lane_number = j.lane_number
LEFT JOIN (
SELECT
lane_number,
COUNT(DISTINCT event_uuid) AS full_count,
SUM(TIMESTAMPDIFF(SECOND, active_time, COALESCE(clear_time, NOW()))) AS full_duration_seconds
FROM FullEvents
GROUP BY lane_number
) f ON dl.lane_number = f.lane_number
LEFT JOIN FullAlarms f ON f.myLocation = CONCAT('S03_CH', dl.lane_number)
LEFT JOIN JammedAlarms j ON j.myLocation = CONCAT('S03_CH', dl.lane_number)
)
SELECT
lane AS Lane,
jam_count AS JamCount,
full_count AS FullCount,
COALESCE(SEC_TO_TIME(jam_duration_seconds), '00:00:00') AS JamDuration,
COALESCE(SEC_TO_TIME(full_duration_seconds), '00:00:00') AS FullDuration
CONCAT('S03_CH', lane_number) AS Lane,
FullCount,
JamCount,
FullDuration,
JamDuration
FROM Aggregated
ORDER BY lane;
ORDER BY lane_number;