From 72a90997496a32cc58d21919598e5f464aa310c8 Mon Sep 17 00:00:00 2001
From: Leo Gordon <lg4@ebi.ac.uk>
Date: Thu, 14 Jul 2011 11:47:43 +0000
Subject: [PATCH] experimental: do the job counting immediately by SQL triggers
 rather than periodically by Perl code

---
 sql/triggers.mysql  | 28 ++++++++++++++++++++++++++++
 sql/triggers.sqlite | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
 create mode 100644 sql/triggers.mysql
 create mode 100644 sql/triggers.sqlite

diff --git a/sql/triggers.mysql b/sql/triggers.mysql
new file mode 100644
index 000000000..6ebe730da
--- /dev/null
+++ b/sql/triggers.mysql
@@ -0,0 +1,28 @@
+
+CREATE TRIGGER add_job AFTER INSERT ON job
+FOR EACH ROW
+    UPDATE analysis_stats SET
+        total_job_count     = total_job_count     + 1,
+        unclaimed_job_count = unclaimed_job_count + (CASE NEW.status WHEN 'READY'  THEN 1                         ELSE 0 END),
+        done_job_count      = done_job_count      + (CASE NEW.status WHEN 'DONE'   THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
+        failed_job_count    = failed_job_count    + (CASE NEW.status WHEN 'FAILED' THEN 1                         ELSE 0 END),
+        status              = (CASE WHEN status!='BLOCKED' THEN 'LOADING' ELSE 'BLOCKED' END)
+    WHERE analysis_id = NEW.analysis_id;
+
+CREATE TRIGGER delete_job AFTER DELETE ON job
+FOR EACH ROW
+    UPDATE analysis_stats SET
+        total_job_count     = total_job_count     - 1,
+        unclaimed_job_count = unclaimed_job_count - (CASE OLD.status WHEN 'READY'  THEN 1                         ELSE 0 END),
+        done_job_count      = done_job_count      - (CASE OLD.status WHEN 'DONE'   THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
+        failed_job_count    = failed_job_count    - (CASE OLD.status WHEN 'FAILED' THEN 1                         ELSE 0 END)
+    WHERE analysis_id = OLD.analysis_id;
+
+CREATE TRIGGER update_job AFTER UPDATE ON job
+FOR EACH ROW
+    UPDATE analysis_stats SET
+        unclaimed_job_count = unclaimed_job_count + (CASE NEW.status WHEN 'READY'  THEN 1 WHEN 'CLAIMED'   THEN -1 ELSE 0 END),
+        done_job_count      = done_job_count      + (CASE NEW.status WHEN 'DONE'   THEN 1 WHEN 'PASSED_ON' THEN  1 ELSE 0 END),
+        failed_job_count    = failed_job_count    + (CASE NEW.status WHEN 'FAILED' THEN 1                          ELSE 0 END)
+    WHERE analysis_id = NEW.analysis_id;
+
diff --git a/sql/triggers.sqlite b/sql/triggers.sqlite
new file mode 100644
index 000000000..ee3ce4e57
--- /dev/null
+++ b/sql/triggers.sqlite
@@ -0,0 +1,34 @@
+
+CREATE TRIGGER add_job AFTER INSERT ON job
+FOR EACH ROW
+BEGIN
+    UPDATE analysis_stats SET
+        total_job_count     = total_job_count     + 1,
+        unclaimed_job_count = unclaimed_job_count + (CASE NEW.status WHEN 'READY'  THEN 1                         ELSE 0 END),
+        done_job_count      = done_job_count      + (CASE NEW.status WHEN 'DONE'   THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
+        failed_job_count    = failed_job_count    + (CASE NEW.status WHEN 'FAILED' THEN 1                         ELSE 0 END),
+        status              = (CASE WHEN status!='BLOCKED' THEN 'LOADING' ELSE 'BLOCKED' END)
+    WHERE analysis_id = NEW.analysis_id;
+END;
+
+CREATE TRIGGER delete_job AFTER DELETE ON job
+FOR EACH ROW
+BEGIN
+    UPDATE analysis_stats SET
+        total_job_count     = total_job_count     - 1,
+        unclaimed_job_count = unclaimed_job_count - (CASE OLD.status WHEN 'READY'  THEN 1                         ELSE 0 END),
+        done_job_count      = done_job_count      - (CASE OLD.status WHEN 'DONE'   THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
+        failed_job_count    = failed_job_count    - (CASE OLD.status WHEN 'FAILED' THEN 1                         ELSE 0 END)
+    WHERE analysis_id = OLD.analysis_id;
+END;
+
+CREATE TRIGGER update_job AFTER UPDATE ON job
+FOR EACH ROW
+BEGIN
+    UPDATE analysis_stats SET
+        unclaimed_job_count = unclaimed_job_count + (CASE NEW.status WHEN 'READY'  THEN 1 WHEN 'CLAIMED'   THEN -1 ELSE 0 END),
+        done_job_count      = done_job_count      + (CASE NEW.status WHEN 'DONE'   THEN 1 WHEN 'PASSED_ON' THEN  1 ELSE 0 END),
+        failed_job_count    = failed_job_count    + (CASE NEW.status WHEN 'FAILED' THEN 1                          ELSE 0 END)
+    WHERE analysis_id = NEW.analysis_id;
+END;
+
-- 
GitLab