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
-
3
1
-- ------------------------------------------------------------------------------
4
2
--
5
3
-- Schema definition for the PilotAgentsDB database - containing the Pilots status
@@ -48,7 +46,6 @@ CREATE TABLE `PilotAgents` (
48
46
KEY ` Statuskey` (` GridSite` ,` DestinationSite` ,` Status` )
49
47
) ENGINE= InnoDB DEFAULT CHARSET= utf8mb4;
50
48
51
-
52
49
DROP TABLE IF EXISTS ` JobToPilotMapping` ;
53
50
CREATE TABLE `JobToPilotMapping ` (
54
51
` PilotID` INT (11 ) UNSIGNED NOT NULL ,
@@ -65,3 +62,77 @@ CREATE TABLE `PilotOutput` (
65
62
` StdError` MEDIUMTEXT,
66
63
PRIMARY KEY (` PilotID` )
67
64
) 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