Skip to content

Commit 999aca8

Browse files
committed
feat: use summary table and triggers (PilotAgentsDB.PilotAgents, summaries for StatesAccounting)
1 parent 9f1f318 commit 999aca8

File tree

3 files changed

+81
-24
lines changed

3 files changed

+81
-24
lines changed

src/DIRAC/WorkloadManagementSystem/Agent/StatesAccountingAgent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class StatesAccountingAgent(AgentModule):
4343
__renameFieldsMapping = {"JobType": "JobSplitType"}
4444

4545
# PilotsHistory fields
46-
__pilotsMapping = ["GridSite", "GridType", "Status", "NumOfPilots"]
46+
__pilotsMapping = ["GridSite", "GridType", "Status", "VO", "NumOfPilots"]
4747

4848
def initialize(self):
4949
"""Standard initialization"""
@@ -85,7 +85,8 @@ def execute(self):
8585
# PilotsHistory to Monitoring
8686
if "Monitoring" in self.pilotMonitoringOption:
8787
self.log.info("Committing PilotsHistory to Monitoring")
88-
result = PilotAgentsDB().getSummarySnapshot()
88+
sql = "SELECT * FROM PilotsHistorySummary ORDER BY GridSite, GridType, Status, VO;"
89+
result = PilotAgentsDB()._query(sql)
8990
now = datetime.datetime.utcnow()
9091
if not result["OK"]:
9192
self.log.error(

src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -943,15 +943,12 @@ def getPilotSummaryWeb(self, selectDict, sortList, startItem, maxItems):
943943
def getPilotMonitorSelectors(self):
944944
"""Get distinct values for the Pilot Monitor page selectors"""
945945

946-
paramNames = ["VO", "GridType", "Status", "DestinationSite", "GridSite"]
947-
948946
resultDict = {}
949-
for param in paramNames:
950-
result = self.getDistinctAttributeValues("PilotAgents", param)
951-
if result["OK"]:
952-
resultDict[param] = result["Value"]
953-
else:
947+
for param in ["VO", "Status", "DestinationSite", "GridSite"]:
948+
result = self.getDistinctAttributeValues("PilotsHistorySummary", param)
949+
if not result["OK"]:
954950
resultDict = []
951+
resultDict[param] = result["Value"]
955952

956953
return S_OK(resultDict)
957954

@@ -1048,18 +1045,6 @@ def getPilotMonitorWeb(self, selectDict, sortList, startItem, maxItems):
10481045

10491046
return S_OK(resultDict)
10501047

1051-
def getSummarySnapshot(self, requestedFields=False):
1052-
"""Get the summary snapshot for a given combination"""
1053-
if not requestedFields:
1054-
requestedFields = ["GridSite", "GridType", "Status"]
1055-
valueFields = ["COUNT(PilotID)"]
1056-
defString = ", ".join(requestedFields)
1057-
valueString = ", ".join(valueFields)
1058-
result = self._query(f"SELECT {defString}, {valueString} FROM PilotAgents GROUP BY {defString}")
1059-
if not result["OK"]:
1060-
return result
1061-
return S_OK(((requestedFields + valueFields), result["Value"]))
1062-
10631048

10641049
class PivotedPilotSummaryTable:
10651050
"""

src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
-- $Header: /tmp/libdirac/tmp.stZoy15380/dirac/DIRAC3/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql,v 1.20 2009/08/26 09:39:53 rgracian Exp $
2-
31
-- ------------------------------------------------------------------------------
42
--
53
-- Schema definition for the PilotAgentsDB database - containing the Pilots status
@@ -48,7 +46,6 @@ CREATE TABLE `PilotAgents` (
4846
KEY `Statuskey` (`GridSite`,`DestinationSite`,`Status`)
4947
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
5048

51-
5249
DROP TABLE IF EXISTS `JobToPilotMapping`;
5350
CREATE TABLE `JobToPilotMapping` (
5451
`PilotID` INT(11) UNSIGNED NOT NULL,
@@ -65,3 +62,77 @@ CREATE TABLE `PilotOutput` (
6562
`StdError` MEDIUMTEXT,
6663
PRIMARY KEY (`PilotID`)
6764
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
65+
66+
67+
-- ------------------------------------------------------------------------------
68+
-- summary table and triggers
69+
-- ------------------------------------------------------------------------------
70+
71+
-- summary for PilotsHistory
72+
73+
DROP TABLE IF EXISTS `PilotsHistorySummary`;
74+
CREATE TABLE `PilotsHistorySummary` (
75+
`GridSite` VARCHAR(128),
76+
`DestinationSite` VARCHAR(128),
77+
`Status` VARCHAR(32),
78+
`VO` VARCHAR(128),
79+
`PilotCount` INT,
80+
PRIMARY KEY (`GridSite`,`DestinationSite`,`Status`, `VO`)
81+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
82+
83+
84+
-- now the triggers
85+
86+
DELIMITER //
87+
88+
CREATE TRIGGER trg_PilotAgents_insert
89+
AFTER INSERT ON PilotAgents
90+
FOR EACH ROW
91+
BEGIN
92+
INSERT INTO PilotsHistorySummary (GridSite, DestinationSite, Status, VO, PilotCount)
93+
VALUES (NEW.GridSite, NEW.DestinationSite, NEW.Status, NEW.VO, 1)
94+
ON DUPLICATE KEY UPDATE PilotCount = PilotCount + 1;
95+
END;
96+
//
97+
98+
CREATE TRIGGER trg_PilotAgents_delete
99+
AFTER DELETE ON PilotAgents
100+
FOR EACH ROW
101+
BEGIN
102+
UPDATE PilotsHistorySummary
103+
SET PilotCount = PilotCount - 1
104+
WHERE GridSite = OLD.GridSite
105+
AND DestinationSite = OLD.DestinationSite
106+
AND Status = OLD.Status
107+
AND VO = OLD.VO;
108+
109+
-- Optional cleanup (remove zero rows)
110+
DELETE FROM PilotsHistorySummary WHERE PilotCount = 0;
111+
END;
112+
//
113+
114+
CREATE TRIGGER trg_PilotAgents_update_status
115+
AFTER UPDATE ON PilotAgents
116+
FOR EACH ROW
117+
BEGIN
118+
IF OLD.Status != NEW.Status THEN
119+
120+
-- Decrease count from old status
121+
UPDATE PilotsHistorySummary
122+
SET PilotCount = PilotCount - 1
123+
WHERE GridSite = OLD.GridSite
124+
AND DestinationSite = OLD.DestinationSite
125+
AND Status = OLD.Status
126+
AND VO = OLD.VO;
127+
128+
-- Delete row if count drops to zero
129+
DELETE FROM PilotsHistorySummary WHERE PilotCount = 0;
130+
131+
-- Increase count for new status
132+
INSERT INTO PilotsHistorySummary (GridSite, DestinationSite, Status, VO, PilotCount)
133+
VALUES (NEW.GridSite, NEW.DestinationSite, NEW.Status, NEW.VO, 1)
134+
ON DUPLICATE KEY UPDATE PilotCount = PilotCount + 1;
135+
136+
END IF;
137+
END;
138+
//

0 commit comments

Comments
 (0)