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
ae.id,
ae.eventtime,
ae.eventid,
ae.source,
ae.priority,
ae.displaypath,
TIMESTAMPDIFF(SECOND, ae.eventtime, NOW()) AS duration_seconds
FROM alarm_events ae
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%'
-- 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
ae.id AS ID,
ae.eventtime AS StartTimestamp,
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')
LPAD(FLOOR(TIMESTAMPDIFF(SECOND, ae.eventtime, NOW())/3600), 2, '0'), ':',
LPAD(FLOOR((TIMESTAMPDIFF(SECOND, ae.eventtime, NOW())%3600)/60), 2, '0'), ':',
LPAD( (TIMESTAMPDIFF(SECOND, ae.eventtime, NOW())%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'
CONCAT(REPLACE(ae.displaypath,'_','-'),' ', SUBSTRING_INDEX(ae.source,':/alm:',-1)) AS Description,
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,
CONCAT(
Active.displaypath,
'.HMI.',
SUBSTRING_INDEX(aed.strValue, '/', -1)
) AS Tag,
CONCAT(ae.displaypath,'.HMI.Alarm.', SUBSTRING_INDEX(aed.strValue,'/',-1)) AS Tag,
SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue,'/',2),'/',-1) AS Location,
aed.strValue AS FullTag,
Active.displaypath as Device
FROM Active
LEFT JOIN SingleMyTag aed
ON aed.id = Active.id
ORDER BY Active.eventtime DESC;
ae.displaypath AS Device
FROM alarm_events ae
LEFT JOIN alarm_events clr
ON clr.eventid = ae.eventid AND clr.eventtype = 1
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
SUBSTRING_INDEX(SUBSTRING_INDEX(strValue, '/', 2), '/', -1) AS Location,
CASE priority
@ -26,6 +9,25 @@ SELECT
ELSE 'Unknown'
END AS Priority,
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
ORDER BY Location, Priority;

View File

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

View File

@ -1,64 +1,42 @@
SELECT
CONCAT(Active.displaypath, ' - ', SUBSTRING_INDEX(Active.source, ':/alm:', -1)) AS Description,
SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue, '/', 2), '/', -1) AS Location,
-- Formatted OPC-style tag
CONCAT(
Active.displaypath,
'.HMI.',
SUBSTRING_INDEX(aed.strValue, '/', -1)
) 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'
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,
-- First and last seen times for this alarm
MIN(Active.eventtime) AS FirstTimestamp,
MAX(Active.eventtime) AS LastTimestamp,
MIN(ae.eventtime) AS FirstTimestamp,
MAX(ae.eventtime) AS LastTimestamp,
-- Total duration summed from each active-clear pair
CONCAT(
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')
-- total duration, formatted HH:MM:SS
DATE_FORMAT(
SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND, ae.eventtime, COALESCE(clr.clear_time, NOW())))),
'%H:%i:%s'
) AS Duration,
-- Total number of activations
COUNT(*) AS Count,
-- Newly added columns
aed.strValue AS FullTag,
Active.displaypath AS Device
ae.displaypath AS Device
FROM (
SELECT
ae.id,
ae.source,
ae.eventid,
ae.eventtime,
ae.priority,
ae.displaypath,
TIMESTAMPDIFF(SECOND, ae.eventtime, COALESCE(ae_clear.eventtime, NOW())) AS duration_seconds
FROM alarm_events ae
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 (
-- get earliest clear per event
SELECT eventid, MIN(eventtime) AS clear_time
FROM alarm_events
WHERE eventtype = 1
GROUP BY eventid
) clr ON clr.eventid = ae.eventid
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;
ON aed.id = ae.id AND aed.propname = 'myTag'
WHERE ae.eventtype = 0
AND COALESCE(ae.displaypath,'') NOT LIKE '%System Startup%'
AND COALESCE(ae.source,'') NOT LIKE '%System Startup%'
GROUP BY
ae.source, ae.displaypath, aed.strValue
ORDER BY
FirstTimestamp DESC, MIN(ae.id) DESC;