Added queries

This commit is contained in:
nodo kakabadze 2025-09-25 20:19:01 +04:00
parent 66c1a96a02
commit 171aaca026
5 changed files with 943 additions and 553 deletions

View File

@ -1,62 +1,28 @@
WITH Active AS ( SELECT
SELECT ae.id AS ID,
ae.id, ae.eventtime AS StartTimestamp,
ae.eventtime, CONCAT(
ae.eventid, LPAD(FLOOR(TIMESTAMPDIFF(SECOND, ae.eventtime, NOW())/3600), 2, '0'), ':',
ae.source, LPAD(FLOOR((TIMESTAMPDIFF(SECOND, ae.eventtime, NOW())%3600)/60), 2, '0'), ':',
ae.priority, LPAD( (TIMESTAMPDIFF(SECOND, ae.eventtime, NOW())%60), 2, '0')
ae.displaypath, ) AS Duration,
TIMESTAMPDIFF(SECOND, ae.eventtime, NOW()) AS duration_seconds CONCAT(REPLACE(ae.displaypath,'_','-'),' ', SUBSTRING_INDEX(ae.source,':/alm:',-1)) AS Description,
FROM alarm_events ae CASE ae.priority
WHERE ae.eventtype = 0 WHEN 0 THEN 'Diagnostic' WHEN 1 THEN 'Low' WHEN 2 THEN 'Medium'
AND NOT EXISTS ( WHEN 3 THEN 'High' WHEN 4 THEN 'Critical' ELSE 'Unknown'
SELECT 1 FROM alarm_events ae_clear END AS Priority,
WHERE ae_clear.eventid = ae.eventid CONCAT(ae.displaypath,'.HMI.Alarm.', SUBSTRING_INDEX(aed.strValue,'/',-1)) AS Tag,
AND ae_clear.eventtype = 1 SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue,'/',2),'/',-1) AS Location,
)
AND ae.displaypath NOT LIKE '%System Startup%'
AND ae.source NOT LIKE '%System Startup%'
-- Priority filter using FIND_IN_SET for comma-separated values
AND (
:priorityList IS NULL
OR :priorityList = ''
OR FIND_IN_SET(ae.priority, :priorityList) > 0
)
GROUP BY ae.id
),
SingleMyTag AS (
SELECT aed.id, aed.strValue
FROM alarm_event_data aed
WHERE aed.propname = 'myTag'
GROUP BY aed.id
)
SELECT
Active.id AS ID,
Active.eventtime AS StartTimestamp,
NULL AS EndTimestamp, -- no end time since it's still active
CONCAT(
LPAD(FLOOR(Active.duration_seconds / 3600), 2, '0'), ':',
LPAD(FLOOR((Active.duration_seconds % 3600) / 60), 2, '0'), ':',
LPAD(Active.duration_seconds % 60, 2, '0')
) AS Duration,
CONCAT(Active.displaypath, ' - ', SUBSTRING_INDEX(Active.source, ':/alm:', -1)) AS Description,
CASE Active.priority
WHEN 0 THEN 'Diagnostic'
WHEN 1 THEN 'Low'
WHEN 2 THEN 'Medium'
WHEN 3 THEN 'High'
WHEN 4 THEN 'Critical'
ELSE 'Unknown'
END AS Priority,
CONCAT(
Active.displaypath,
'.HMI.',
SUBSTRING_INDEX(aed.strValue, '/', -1)
) AS Tag,
SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue, '/', 2), '/', -1) AS Location ,
aed.strValue AS FullTag, aed.strValue AS FullTag,
Active.displaypath as Device ae.displaypath AS Device
FROM Active FROM alarm_events ae
LEFT JOIN SingleMyTag aed LEFT JOIN alarm_events clr
ON aed.id = Active.id ON clr.eventid = ae.eventid AND clr.eventtype = 1
ORDER BY Active.eventtime DESC; LEFT JOIN alarm_event_data aed
ON aed.id = ae.id AND aed.propname = 'myTag'
WHERE ae.eventtype = 0
AND clr.eventid IS NULL
AND ae.displaypath NOT LIKE '%System Startup%'
AND ae.source NOT LIKE '%System Startup%'
AND (:priorityList = '' OR FIND_IN_SET(CAST(ae.priority AS CHAR), :priorityList) > 0)
ORDER BY ae.eventtime DESC;

View File

@ -1,20 +1,3 @@
WITH Active AS (
SELECT
ae.id,
ae.eventid,
ae.priority,
aed.strValue
FROM alarm_events ae
LEFT JOIN alarm_event_data aed ON ae.id = aed.id AND aed.propname = 'myTag'
WHERE ae.eventtype = 0
AND NOT EXISTS (
SELECT 1 FROM alarm_events ae_clear
WHERE ae_clear.eventid = ae.eventid
AND ae_clear.eventtype = 1
)
AND ae.displaypath NOT LIKE '%System Startup%'
AND ae.source NOT LIKE '%System Startup%'
)
SELECT SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(strValue, '/', 2), '/', -1) AS Location, SUBSTRING_INDEX(SUBSTRING_INDEX(strValue, '/', 2), '/', -1) AS Location,
CASE priority CASE priority
@ -26,6 +9,25 @@ SELECT
ELSE 'Unknown' ELSE 'Unknown'
END AS Priority, END AS Priority,
COUNT(*) AS Count COUNT(*) AS Count
FROM Active FROM (
SELECT
ae.id,
ae.eventid,
ae.priority,
aed.strValue
FROM alarm_events ae
LEFT JOIN alarm_event_data aed
ON ae.id = aed.id
AND aed.propname = 'myTag'
WHERE ae.eventtype = 0
AND NOT EXISTS (
SELECT 1
FROM alarm_events ae_clear
WHERE ae_clear.eventid = ae.eventid
AND ae_clear.eventtype = 1
)
AND ae.displaypath NOT LIKE '%System Startup%'
AND ae.source NOT LIKE '%System Startup%'
) AS Active
GROUP BY Location, Priority GROUP BY Location, Priority
ORDER BY Location, Priority; ORDER BY Location, Priority;

View File

@ -1,71 +1,71 @@
WITH Active AS ( SELECT
SELECT a.id AS ID,
ae.id, a.eventtime AS StartTimestamp,
ae.eventtime, clr.eventtime AS EndTimestamp,
ae.eventid,
ae.source, -- Duration calculation (HH:MM:SS format)
ae.priority, CONCAT(
ae.displaypath, LPAD(FLOOR(TIMESTAMPDIFF(SECOND, a.eventtime, COALESCE(clr.eventtime, NOW())) / 3600), 2, '0'), ':',
TIMESTAMPDIFF(SECOND, ae.eventtime, COALESCE(ae_clear.eventtime, NOW())) AS duration_seconds LPAD(FLOOR((TIMESTAMPDIFF(SECOND, a.eventtime, COALESCE(clr.eventtime, NOW())) % 3600) / 60), 2, '0'), ':',
FROM alarm_events ae LPAD( (TIMESTAMPDIFF(SECOND, a.eventtime, COALESCE(clr.eventtime, NOW())) % 60) , 2, '0')
LEFT JOIN alarm_events ae_clear ) AS Duration,
ON ae.eventid = ae_clear.eventid AND ae_clear.eventtype = 1
WHERE ae.eventtype = 0 -- Description combining display path and alarm name
AND ae.displaypath NOT LIKE '%System Startup%' CONCAT(REPLACE(a.displaypath, '_', '-'), ' ', SUBSTRING_INDEX(a.source, ':/alm:', -1)) AS Description,
AND ae.source NOT LIKE '%System Startup%'
GROUP BY ae.id -- Ensure one row per alarm -- Priority mapping
), CASE a.priority
SingleMyTag AS ( WHEN 0 THEN 'Diagnostic'
SELECT aed.id, aed.strValue WHEN 1 THEN 'Low'
FROM alarm_event_data aed WHEN 2 THEN 'Medium'
WHERE aed.propname = 'myTag' WHEN 3 THEN 'High'
GROUP BY aed.id -- Collapse duplicates by id WHEN 4 THEN 'Critical'
), ELSE 'Unknown'
SingleClear AS ( END AS Priority,
SELECT eventid, MIN(eventtime) AS eventtime
FROM alarm_events -- Tag information
WHERE eventtype = 1 CONCAT(a.displaypath, '.HMI.Alarm.', SUBSTRING_INDEX(aed.strValue, '/', -1)) AS Tag,
GROUP BY eventid SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue, '/', 2), '/', -1) AS Location,
) aed.strValue AS FullTag,
a.displaypath AS Device
SELECT
Active.id AS ID, FROM alarm_events a
Active.eventtime AS StartTimestamp,
Clear.eventtime AS EndTimestamp, -- Join to get the earliest clear event for each alarm
LEFT JOIN (
CONCAT( SELECT eventid, MIN(eventtime) AS eventtime
LPAD(FLOOR(Active.duration_seconds / 3600), 2, '0'), ':', FROM alarm_events
LPAD(FLOOR((Active.duration_seconds % 3600) / 60), 2, '0'), ':', WHERE eventtype = 1
LPAD(Active.duration_seconds % 60, 2, '0') GROUP BY eventid
) AS Duration, ) AS clr ON clr.eventid = a.eventid
CONCAT(Active.displaypath, ' - ', SUBSTRING_INDEX(Active.source, ':/alm:', -1)) AS Description, -- Join to get additional tag data
LEFT JOIN (
CASE Active.priority SELECT id, strValue
WHEN 0 THEN 'Diagnostic' FROM alarm_event_data
WHEN 1 THEN 'Low' WHERE propname = 'myTag'
WHEN 2 THEN 'Medium' GROUP BY id
WHEN 3 THEN 'High' ) AS aed ON aed.id = a.id
WHEN 4 THEN 'Critical'
ELSE 'Unknown' WHERE
END AS Priority, -- Only active alarm events (not clear events)
a.eventtype = 0
CONCAT(
Active.displaypath, -- Exclude system startup alarms
'.HMI.', AND a.displaypath NOT LIKE '%System Startup%'
SUBSTRING_INDEX(aed.strValue, '/', -1) AND a.source NOT LIKE '%System Startup%'
) AS Tag,
-- Simple date filtering using named parameters
SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue, '/', 2), '/', -1) AS Location , AND (
aed.strValue AS FullTag, -- Case 1: Alarm was cleared within the specified time range
Active.displaypath as Device (clr.eventtime IS NOT NULL AND clr.eventtime >= :starttime AND clr.eventtime < :endtime)
OR
FROM Active -- Case 2: Alarm is still active (no clear time) and started within or before the range
(clr.eventtime IS NULL AND a.eventtime < :endtime)
LEFT JOIN SingleClear Clear )
ON Active.eventid = Clear.eventid
-- Order by end time (most recent clears first), active alarms (NULL) at top, then by ID
LEFT JOIN SingleMyTag aed ORDER BY clr.eventtime IS NULL DESC, clr.eventtime DESC, a.id DESC
ON aed.id = Active.id
-- Pagination support (100 records per page)
ORDER BY Active.eventtime DESC; LIMIT 100 OFFSET :offset;

View File

@ -1,64 +1,42 @@
SELECT SELECT
CONCAT(Active.displaypath, ' - ', SUBSTRING_INDEX(Active.source, ':/alm:', -1)) AS Description, CONCAT(COALESCE(ae.displaypath,'Unknown'), ' - ',
SUBSTRING_INDEX(COALESCE(ae.source,''), ':/alm:', -1)) AS Description,
SUBSTRING_INDEX(SUBSTRING_INDEX(COALESCE(aed.strValue,''), '/', 2), '/', -1) AS Location,
CONCAT(COALESCE(ae.displaypath,'Unknown'), '.HMI.',
SUBSTRING_INDEX(COALESCE(aed.strValue,''),'/',-1)) AS Tag,
CASE ae.priority
WHEN 0 THEN 'Diagnostic' WHEN 1 THEN 'Low' WHEN 2 THEN 'Medium'
WHEN 3 THEN 'High' WHEN 4 THEN 'Critical' ELSE 'Unknown'
END AS Priority,
SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue, '/', 2), '/', -1) AS Location, MIN(ae.eventtime) AS FirstTimestamp,
MAX(ae.eventtime) AS LastTimestamp,
-- Formatted OPC-style tag -- total duration, formatted HH:MM:SS
CONCAT( DATE_FORMAT(
Active.displaypath, SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND, ae.eventtime, COALESCE(clr.clear_time, NOW())))),
'.HMI.', '%H:%i:%s'
SUBSTRING_INDEX(aed.strValue, '/', -1) ) AS Duration,
) AS Tag,
CASE Active.priority
WHEN 0 THEN 'Diagnostic'
WHEN 1 THEN 'Low'
WHEN 2 THEN 'Medium'
WHEN 3 THEN 'High'
WHEN 4 THEN 'Critical'
ELSE 'Unknown'
END AS Priority,
-- First and last seen times for this alarm COUNT(*) AS Count,
MIN(Active.eventtime) AS FirstTimestamp,
MAX(Active.eventtime) AS LastTimestamp,
-- Total duration summed from each active-clear pair aed.strValue AS FullTag,
CONCAT( ae.displaypath AS Device
LPAD(FLOOR(SUM(Active.duration_seconds) / 3600), 2, '0'), ':',
LPAD(FLOOR((SUM(Active.duration_seconds) % 3600) / 60), 2, '0'), ':',
LPAD(SUM(Active.duration_seconds) % 60, 2, '0')
) AS Duration,
-- Total number of activations FROM alarm_events ae
COUNT(*) AS Count, LEFT JOIN (
-- get earliest clear per event
-- Newly added columns SELECT eventid, MIN(eventtime) AS clear_time
aed.strValue AS FullTag, FROM alarm_events
Active.displaypath AS Device WHERE eventtype = 1
GROUP BY eventid
FROM ( ) clr ON clr.eventid = ae.eventid
SELECT LEFT JOIN alarm_event_data aed
ae.id, ON aed.id = ae.id AND aed.propname = 'myTag'
ae.source, WHERE ae.eventtype = 0
ae.eventid, AND COALESCE(ae.displaypath,'') NOT LIKE '%System Startup%'
ae.eventtime, AND COALESCE(ae.source,'') NOT LIKE '%System Startup%'
ae.priority, GROUP BY
ae.displaypath, ae.source, ae.displaypath, aed.strValue
TIMESTAMPDIFF(SECOND, ae.eventtime, COALESCE(ae_clear.eventtime, NOW())) AS duration_seconds ORDER BY
FROM alarm_events ae FirstTimestamp DESC, MIN(ae.id) DESC;
LEFT JOIN alarm_events ae_clear
ON ae.eventid = ae_clear.eventid AND ae_clear.eventtype = 1
WHERE ae.eventtype = 0
AND ae.displaypath NOT LIKE '%System Startup%'
AND ae.source NOT LIKE '%System Startup%'
) AS Active
-- OPC tag path for building .hmi.Tag output
LEFT JOIN alarm_event_data aed
ON aed.id = Active.id AND aed.propname = 'myTag'
-- 🔹 Group by the full unique alarm key (tag + alarm name)
GROUP BY Active.source, Active.displaypath, aed.strValue
ORDER BY FirstTimestamp DESC;