updated alarm view. displaying plc tag. correctly fetching alarms data for history table #129

Merged
gigi.mamaladze merged 10 commits from kakhadze into main 2025-06-23 12:06:30 +00:00
5 changed files with 1276 additions and 4010 deletions

View File

@ -0,0 +1,60 @@
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
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
FROM Active
LEFT JOIN SingleMyTag aed
ON aed.id = Active.id
ORDER BY Active.eventtime DESC;

View File

@ -0,0 +1,40 @@
{
"scope": "DG",
"version": 2,
"restricted": false,
"overridable": true,
"files": [
"query.sql"
],
"attributes": {
"useMaxReturnSize": false,
"autoBatchEnabled": false,
"fallbackValue": "",
"maxReturnSize": 100,
"cacheUnit": "SEC",
"type": "Query",
"enabled": true,
"cacheAmount": 1,
"cacheEnabled": false,
"database": "MariaDB",
"fallbackEnabled": false,
"lastModificationSignature": "d4eef6a8194fc16fb2061c0ffe057909fb37ddedbe4fd6e4f2416cc6050f6209",
"permissions": [
{
"zone": "default",
"role": ""
}
],
"lastModification": {
"actor": "admin",
"timestamp": "2025-06-23T10:38:45Z"
},
"parameters": [
{
"type": "Parameter",
"identifier": "priorityList",
"sqlType": 7
}
]
}
}

View File

@ -1,17 +1,46 @@
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`,
Active.eventtime AS StartTimestamp,
Clear.eventtime AS EndTimestamp,
CONCAT(
LPAD(FLOOR(duration_seconds / 3600), 2, '0'), ':',
LPAD(FLOOR((duration_seconds % 3600) / 60), 2, '0'), ':',
LPAD(duration_seconds % 60, 2, '0')
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(
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(Active.source, '/HMI/ALARMST', 1), ':/tag:', -1), '/', -1),
' - ',
SUBSTRING_INDEX(Active.source, ':/alm:', -1)
) AS Description,
CONCAT(Active.displaypath, ' - ', SUBSTRING_INDEX(Active.source, ':/alm:', -1)) AS Description,
CASE Active.priority
WHEN 0 THEN 'Diagnostic'
WHEN 1 THEN 'Low'
@ -20,21 +49,21 @@ SELECT
WHEN 4 THEN 'Critical'
ELSE 'Unknown'
END AS Priority,
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(Active.source, '/HMI/ALARMST', 1), ':/tag:', -1), '/', -1) AS Tag,
SUBSTRING_INDEX(SUBSTRING_INDEX(Active.source, '/tag:', -1), '/', 1) AS MCM
FROM (
SELECT
ae.id,
ae.eventtime,
ae.eventid,
ae.source,
ae.priority,
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
) AS Active
LEFT JOIN alarm_events Clear
ON Active.eventid = Clear.eventid AND Clear.eventtype = 1
CONCAT(
Active.displaypath,
'.HMI.',
SUBSTRING_INDEX(aed.strValue, '/', -1)
) AS Tag,
SUBSTRING_INDEX(SUBSTRING_INDEX(aed.strValue, '/', 2), '/', -1) AS Location
FROM Active
LEFT JOIN SingleClear Clear
ON Active.eventid = Clear.eventid
LEFT JOIN SingleMyTag aed
ON aed.id = Active.id
ORDER BY Active.eventtime DESC;

View File

@ -0,0 +1,60 @@
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'
END AS Priority,
-- First and last seen times for this alarm
MIN(Active.eventtime) AS FirstTimestamp,
MAX(Active.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')
) AS Duration,
-- Total number of activations
COUNT(*) AS Count
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 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;