From 4ea842c5f1cb88d7aab5aa7716a890f8561d3434 Mon Sep 17 00:00:00 2001 From: George Bao Lee Date: Fri, 14 Jun 2019 22:17:24 -0700 Subject: [PATCH 1/3] Add printing adjustments --- ocflib/printing/ocfprinting.sql | 58 +++++++++++++++++++++++++-------- ocflib/printing/quota.py | 7 ++-- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/ocflib/printing/ocfprinting.sql b/ocflib/printing/ocfprinting.sql index 6f06342b..a6dfeb2f 100644 --- a/ocflib/printing/ocfprinting.sql +++ b/ocflib/printing/ocfprinting.sql @@ -16,10 +16,11 @@ CREATE TABLE IF NOT EXISTS `jobs` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB; -CREATE TABLE IF NOT EXISTS `refunds` ( +CREATE TABLE IF NOT EXISTS `adjustments` ( `id` int NOT NULL AUTO_INCREMENT, `user` varchar(255) NOT NULL, `time` datetime NOT NULL, + `action` varchar(255) NOT NULL, `pages` int NOT NULL, `staffer` varchar(255) NOT NULL, `reason` varchar(510) NOT NULL, @@ -27,7 +28,7 @@ CREATE TABLE IF NOT EXISTS `refunds` ( ) ENGINE=InnoDB; CREATE INDEX `jobs_idx` ON `jobs` (`user`, `time`, `pages`); -CREATE INDEX `refunds_idx` ON `refunds` (`user`, `time`, `pages`); +CREATE INDEX `refunds_idx` ON `adjustments` (`user`, `time`, `action`, `pages`); DROP FUNCTION IF EXISTS semester_start; DELIMITER $$ @@ -52,30 +53,58 @@ CREATE VIEW jobs_today AS DROP VIEW IF EXISTS refunds_today; CREATE VIEW refunds_today AS SELECT user, SUM(pages) AS pages - FROM refunds - WHERE DATE(refunds.time) = CURDATE() + FROM adjustments + WHERE DATE(adjustments.time) = CURDATE() + AND action = "refund" + GROUP BY user; + +DROP VIEW IF EXISTS forwards_today; +CREATE VIEW forwards_today AS + SELECT user, SUM(pages) AS pages + FROM adjustments + WHERE DATE(adjustments.time) = CURDATE() + AND action = "forward" GROUP BY user; DROP VIEW IF EXISTS printed_today; CREATE VIEW `printed_today` AS SELECT jobs_today.user AS user, - COALESCE(jobs_today.pages, 0) - COALESCE(refunds_today.pages, 0) AS today + COALESCE(jobs_today.pages, 0) - COALESCE(refunds_today.pages, 0) + - COALESCE(forwards_today.pages, 0) AS today FROM jobs_today LEFT OUTER JOIN refunds_today ON jobs_today.user = refunds_today.user + LEFT OUTER JOIN forwards_today + ON jobs_today.user = forwards_today.user GROUP BY jobs_today.user UNION SELECT refunds_today.user AS user, - COALESCE(jobs_today.pages, 0) - COALESCE(refunds_today.pages, 0) AS today - FROM jobs_today - RIGHT OUTER JOIN refunds_today - ON jobs_today.user = refunds_today.user + COALESCE(jobs_today.pages, 0) - COALESCE(refunds_today.pages, 0) + - COALESCE(forwards_today.pages, 0) AS today + FROM refunds_today + LEFT OUTER JOIN jobs_today + ON refunds_today.user = jobs_today.user + LEFT OUTER JOIN forwards_today + ON refunds_today.user = forwards_today.user GROUP BY refunds_today.user + UNION + + SELECT + forwards_today.user AS user, + COALESCE(jobs_today.pages, 0) - COALESCE(refunds_today.pages, 0) + - COALESCE(forwards_today.pages, 0) AS today + FROM forwards_today + LEFT OUTER JOIN jobs_today + ON forwards_today.user = jobs_today.user + LEFT OUTER JOIN refunds_today + ON forwards_today.user = refunds_today.user + GROUP BY forwards_today.user + ORDER BY user; DROP VIEW IF EXISTS jobs_semester; @@ -88,8 +117,9 @@ CREATE VIEW jobs_semester AS DROP VIEW IF EXISTS refunds_semester; CREATE VIEW refunds_semester AS SELECT user, SUM(pages) AS pages - FROM refunds - WHERE DATE(refunds.time) >= semester_start(CURDATE()) + FROM adjustments + WHERE DATE(adjustments.time) >= semester_start(CURDATE()) + AND action = "refund" GROUP BY user; DROP VIEW IF EXISTS printed_semester; @@ -107,9 +137,9 @@ CREATE VIEW `printed_semester` AS SELECT refunds_semester.user AS user, COALESCE(jobs_semester.pages, 0) - COALESCE(refunds_semester.pages, 0) AS semester - FROM jobs_semester - RIGHT OUTER JOIN refunds_semester - ON jobs_semester.user = refunds_semester.user + FROM refunds_semester + LEFT OUTER JOIN jobs_semester + ON refunds_semester.user = jobs_semester.user GROUP BY refunds_semester.user ORDER BY user; diff --git a/ocflib/printing/quota.py b/ocflib/printing/quota.py index f8715b41..186b8575 100644 --- a/ocflib/printing/quota.py +++ b/ocflib/printing/quota.py @@ -45,10 +45,11 @@ 'filesize', )) -Refund = namedtuple('Refund', ( +Payload = namedtuple('Payload', ( 'user', 'time', 'pages', + 'action' 'staffer', 'reason', )) @@ -117,6 +118,6 @@ def add_job(c, job): c.execute(*_namedtuple_to_query('INSERT INTO jobs ({}) VALUES ({})', job)) -def add_refund(c, refund): +def add_adjustment(c, payload): """Add a new refund to the database.""" - c.execute(*_namedtuple_to_query('INSERT INTO refunds ({}) VALUES ({})', refund)) + c.execute(*_namedtuple_to_query('INSERT INTO adjustments ({}) VALUES ({})', payload)) From 7ed21ed34d41abb6544058954b3aa4f0f5b4b55f Mon Sep 17 00:00:00 2001 From: George Bao Lee Date: Fri, 14 Jun 2019 23:43:55 -0700 Subject: [PATCH 2/3] Fix style --- ocflib/printing/ocfprinting.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocflib/printing/ocfprinting.sql b/ocflib/printing/ocfprinting.sql index a6dfeb2f..fa8d30ed 100644 --- a/ocflib/printing/ocfprinting.sql +++ b/ocflib/printing/ocfprinting.sql @@ -165,4 +165,4 @@ CREATE VIEW `public_jobs` AS ORDER BY `day` DESC GRANT SELECT ON `ocfprinting`.`printed` TO 'anonymous'@'%'; -GRANT SELECT on `ocfprinting`.`public_jobs` TO 'anonymous'@'%'; +GRANT SELECT ON `ocfprinting`.`public_jobs` TO 'anonymous'@'%'; From 4f874b4d705e80c665fbffde882be9b2071ab439 Mon Sep 17 00:00:00 2001 From: Justin Zhang Date: Sun, 23 Oct 2022 15:14:53 -0700 Subject: [PATCH 3/3] Incorporate feedback for adjustments --- ocflib/printing/ocfprinting.sql | 2 +- ocflib/printing/quota.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ocflib/printing/ocfprinting.sql b/ocflib/printing/ocfprinting.sql index fa8d30ed..d054c7e4 100644 --- a/ocflib/printing/ocfprinting.sql +++ b/ocflib/printing/ocfprinting.sql @@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS `adjustments` ( `id` int NOT NULL AUTO_INCREMENT, `user` varchar(255) NOT NULL, `time` datetime NOT NULL, - `action` varchar(255) NOT NULL, + `action` enum('refund', 'forward'), `pages` int NOT NULL, `staffer` varchar(255) NOT NULL, `reason` varchar(510) NOT NULL, diff --git a/ocflib/printing/quota.py b/ocflib/printing/quota.py index 186b8575..e8bc59e6 100644 --- a/ocflib/printing/quota.py +++ b/ocflib/printing/quota.py @@ -119,5 +119,5 @@ def add_job(c, job): def add_adjustment(c, payload): - """Add a new refund to the database.""" + """Add a new adjustment to the database (either refund or forward).""" c.execute(*_namedtuple_to_query('INSERT INTO adjustments ({}) VALUES ({})', payload))