|
8 | 8 | Integer,
|
9 | 9 | String,
|
10 | 10 | Text,
|
| 11 | + UniqueConstraint, |
11 | 12 | )
|
12 | 13 | from sqlalchemy.orm import declarative_base
|
13 | 14 |
|
@@ -156,3 +157,129 @@ class JobCommands(JobDBBase):
|
156 | 157 | status = Column("Status", String(32), default="Received")
|
157 | 158 | reception_time = Column("ReceptionTime", DateTime, primary_key=True)
|
158 | 159 | execution_time = NullColumn("ExecutionTime", DateTime)
|
| 160 | + |
| 161 | + |
| 162 | +class JobsHistorySummary(JobDBBase): |
| 163 | + __tablename__ = "JobsHistorySummary" |
| 164 | + id = Column("ID", Integer, primary_key=True, autoincrement=True) |
| 165 | + status = Column("Status", String(32)) |
| 166 | + site = Column("Site", String(100)) |
| 167 | + owner = Column("Owner", String(64)) |
| 168 | + owner_group = Column("OwnerGroup", String(128)) |
| 169 | + vo = Column("VO", String(64)) |
| 170 | + job_group = Column("JobGroup", String(32)) |
| 171 | + job_type = Column("JobType", String(32)) |
| 172 | + application_status = Column("ApplicationStatus", String(255)) |
| 173 | + minor_status = Column("MinorStatus", String(128)) |
| 174 | + job_count = Column("JobCount", Integer, default=0) |
| 175 | + reschedule_sum = Column("RescheduleSum", Integer, default=0) |
| 176 | + |
| 177 | + __table_args__ = ( |
| 178 | + UniqueConstraint( |
| 179 | + "Status", |
| 180 | + "Site", |
| 181 | + "Owner", |
| 182 | + "OwnerGroup", # TODO: OwnerGroup(32) |
| 183 | + "VO", |
| 184 | + "JobGroup", |
| 185 | + "JobType", |
| 186 | + "ApplicationStatus", # TODO: ApplicationStatus(128) |
| 187 | + "MinorStatus", |
| 188 | + name="uq_summary", |
| 189 | + ), |
| 190 | + ) |
| 191 | + |
| 192 | + |
| 193 | +# TODO: |
| 194 | + |
| 195 | +# DELIMITER // |
| 196 | + |
| 197 | +# CREATE TRIGGER trg_Jobs_insert |
| 198 | +# AFTER INSERT ON Jobs |
| 199 | +# FOR EACH ROW |
| 200 | +# BEGIN |
| 201 | +# INSERT INTO JobsHistorySummary ( |
| 202 | +# Status, Site, Owner, OwnerGroup, VO, JobGroup, JobType, |
| 203 | +# ApplicationStatus, MinorStatus, JobCount, RescheduleSum |
| 204 | +# ) |
| 205 | +# VALUES ( |
| 206 | +# NEW.Status, NEW.Site, NEW.Owner, NEW.OwnerGroup, NEW.VO, |
| 207 | +# NEW.JobGroup, NEW.JobType, NEW.ApplicationStatus, |
| 208 | +# NEW.MinorStatus, 1, NEW.RescheduleCounter |
| 209 | +# ) |
| 210 | +# ON DUPLICATE KEY UPDATE JobCount = JobCount + 1, |
| 211 | +# RescheduleSum = RescheduleSum + NEW.RescheduleCounter; |
| 212 | +# END; |
| 213 | +# // |
| 214 | + |
| 215 | +# CREATE TRIGGER trg_Jobs_delete |
| 216 | +# AFTER DELETE ON Jobs |
| 217 | +# FOR EACH ROW |
| 218 | +# BEGIN |
| 219 | +# UPDATE JobsHistorySummary |
| 220 | +# SET JobCount = JobCount - 1, RescheduleSum = RescheduleSum - OLD.RescheduleCounter |
| 221 | +# WHERE Status = OLD.Status |
| 222 | +# AND Site = OLD.Site |
| 223 | +# AND Owner = OLD.Owner |
| 224 | +# AND OwnerGroup = OLD.OwnerGroup |
| 225 | +# AND VO = OLD.VO |
| 226 | +# AND JobGroup = OLD.JobGroup |
| 227 | +# AND JobType = OLD.JobType |
| 228 | +# AND ApplicationStatus = OLD.ApplicationStatus |
| 229 | +# AND MinorStatus = OLD.MinorStatus; |
| 230 | + |
| 231 | +# -- Remove zero rows |
| 232 | +# DELETE FROM JobsHistorySummary |
| 233 | +# WHERE JobCount = 0 |
| 234 | +# AND Status = OLD.Status |
| 235 | +# AND Site = OLD.Site |
| 236 | +# AND Owner = OLD.Owner |
| 237 | +# AND OwnerGroup = OLD.OwnerGroup |
| 238 | +# AND VO = OLD.VO |
| 239 | +# AND JobGroup = OLD.JobGroup |
| 240 | +# AND JobType = OLD.JobType |
| 241 | +# AND ApplicationStatus = OLD.ApplicationStatus |
| 242 | +# AND MinorStatus = OLD.MinorStatus; |
| 243 | +# END; |
| 244 | +# // |
| 245 | + |
| 246 | +# CREATE TRIGGER trg_Jobs_update_status |
| 247 | +# AFTER UPDATE ON Jobs |
| 248 | +# FOR EACH ROW |
| 249 | +# BEGIN |
| 250 | +# IF OLD.Status != NEW.Status THEN |
| 251 | + |
| 252 | +# -- Decrease count from old status |
| 253 | +# UPDATE JobsHistorySummary |
| 254 | +# SET JobCount = JobCount - 1, RescheduleSum = RescheduleSum - OLD.RescheduleCounter |
| 255 | +# WHERE Status = OLD.Status |
| 256 | +# AND Site = OLD.Site |
| 257 | +# AND Owner = OLD.Owner |
| 258 | +# AND OwnerGroup = OLD.OwnerGroup |
| 259 | +# AND VO = OLD.VO |
| 260 | +# AND JobGroup = OLD.JobGroup |
| 261 | +# AND JobType = OLD.JobType |
| 262 | +# AND ApplicationStatus = OLD.ApplicationStatus |
| 263 | +# AND MinorStatus = OLD.MinorStatus; |
| 264 | + |
| 265 | +# -- Delete row if count drops to zero |
| 266 | +# DELETE FROM JobsHistorySummary WHERE JobCount = 0; |
| 267 | + |
| 268 | +# -- Increase count for new status |
| 269 | +# INSERT INTO JobsHistorySummary ( |
| 270 | +# Status, Site, Owner, OwnerGroup, JobGroup, VO, |
| 271 | +# JobType, ApplicationStatus, MinorStatus, JobCount, RescheduleSum |
| 272 | +# ) |
| 273 | +# VALUES ( |
| 274 | +# NEW.Status, NEW.Site, NEW.Owner, NEW.OwnerGroup, NEW.JobGroup, |
| 275 | +# NEW.VO, NEW.JobType, NEW.ApplicationStatus, NEW.MinorStatus, |
| 276 | +# 1, NEW.RescheduleCounter |
| 277 | +# ) |
| 278 | +# ON DUPLICATE KEY UPDATE JobCount = JobCount + 1, |
| 279 | +# RescheduleSum = RescheduleSum + NEW.RescheduleCounter; |
| 280 | + |
| 281 | +# END IF; |
| 282 | +# END; |
| 283 | +# // |
| 284 | + |
| 285 | +# DELIMITER ; |
0 commit comments