Skip to content

Commit 57d2a1b

Browse files
committed
feat: use summary tables and triggers (PilotAgentsDB.PilotAgents, summaries for StatesAccounting)
1 parent b507f17 commit 57d2a1b

File tree

3 files changed

+83
-14
lines changed

3 files changed

+83
-14
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: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,18 +1049,6 @@ def getPilotMonitorWeb(self, selectDict, sortList, startItem, maxItems):
10491049

10501050
return S_OK(resultDict)
10511051

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

10651053
class PivotedPilotSummaryTable:
10661054
"""

src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,83 @@ END;
150150
//
151151

152152
DELIMITER ;
153+
154+
155+
-- summary for PilotsHistory
156+
157+
DROP TABLE IF EXISTS `PilotsHistorySummary`;
158+
CREATE TABLE IF NOT EXISTS `PilotsHistorySummary` (
159+
`GridSite` VARCHAR(128),
160+
`GridType` VARCHAR(32),
161+
`Status` VARCHAR(32),
162+
`VO` VARCHAR(128),
163+
`PilotCount` INT,
164+
PRIMARY KEY (`GridSite`,`GridType`,`Status`, `VO`),
165+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
166+
167+
168+
DELIMITER //
169+
170+
CREATE TRIGGER trg_PilotAgents_insert
171+
AFTER INSERT ON PilotAgents
172+
FOR EACH ROW
173+
BEGIN
174+
INSERT INTO PilotsHistorySummary (GridSite, GridType, Status, VO, PilotCount)
175+
VALUES (NEW.GridSite, NEW.GridType, NEW.Status, NEW.VO, 1)
176+
ON DUPLICATE KEY UPDATE PilotCount = PilotCount + 1;
177+
END;
178+
//
179+
180+
DELIMITER ;
181+
182+
DELIMITER //
183+
184+
CREATE TRIGGER trg_PilotAgents_delete
185+
AFTER DELETE ON PilotAgents
186+
FOR EACH ROW
187+
BEGIN
188+
UPDATE PilotsHistorySummary
189+
SET PilotCount = PilotCount - 1
190+
WHERE GridSite = OLD.GridSite
191+
AND GridType = OLD.GridType
192+
AND Status = OLD.Status;
193+
AND VO = OLD.VO;
194+
195+
-- Optional cleanup (remove zero rows)
196+
DELETE FROM PilotsHistorySummary
197+
WHERE PilotCount = 0;
198+
END;
199+
//
200+
201+
DELIMITER ;
202+
203+
DELIMITER //
204+
205+
CREATE TRIGGER trg_PilotAgents_update_status
206+
AFTER UPDATE ON PilotAgents
207+
FOR EACH ROW
208+
BEGIN
209+
IF OLD.Status != NEW.Status THEN
210+
211+
-- Decrease count from old status
212+
UPDATE PilotsHistorySummary
213+
SET PilotCount = PilotCount - 1
214+
WHERE GridSite = OLD.GridSite
215+
AND GridType = OLD.GridType
216+
AND Status = OLD.Status;
217+
AND VO = OLD.VO;
218+
219+
-- Delete row if count drops to zero
220+
DELETE FROM PilotsHistorySummary
221+
WHERE PilotCount = 0;
222+
223+
-- Increase count for new status
224+
INSERT INTO PilotsHistorySummary (GridSite, GridType, Status, VO, PilotCount)
225+
VALUES (NEW.GridSite, NEW.GridType, NEW.Status, NEW.VO 1)
226+
ON DUPLICATE KEY UPDATE PilotCount = PilotCount + 1;
227+
228+
END IF;
229+
END;
230+
//
231+
232+
DELIMITER ;

0 commit comments

Comments
 (0)