LP#1613730: Add a "Copy Count" rating function for badges
authorMike Rylander <mrylander@gmail.com>
Thu, 18 Aug 2016 16:58:25 +0000 (12:58 -0400)
committerKathy Lussier <klussier@masslnc.org>
Mon, 22 Aug 2016 17:40:20 +0000 (13:40 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/sql/Pg/220.schema.rating.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.data.copy-count-badge.sql [new file with mode: 0644]

index 460fc36..3494a7b 100644 (file)
@@ -77,7 +77,9 @@ INSERT INTO rating.popularity_parameter (id,name,func,require_horizon,require_im
     (12,'On-line Bib has attributes','rating.generic_fixed_rating_by_uri',FALSE,FALSE,FALSE),
     (13,'Bib has attributes and copies','rating.generic_fixed_rating_by_copy',FALSE,FALSE,FALSE),
     (14,'Bib has attributes and copies or URIs','rating.generic_fixed_rating_by_copy_or_uri',FALSE,FALSE,FALSE),
-    (15,'Bib has attributes','rating.generic_fixed_rating_global',FALSE,FALSE,FALSE);
+    (15,'Bib has attributes','rating.generic_fixed_rating_global',FALSE,FALSE,FALSE),
+    (16,'Copy Count','rating.copy_count',FALSE,FALSE,TRUE);
+
 
 CREATE TABLE rating.badge (
     id                      SERIAL      PRIMARY KEY,
@@ -901,5 +903,33 @@ BEGIN
 END;
 $f$ LANGUAGE PLPGSQL STRICT;
 
+CREATE OR REPLACE FUNCTION rating.copy_count(badge_id INT)
+    RETURNS TABLE (record INT, value NUMERIC) AS $f$
+DECLARE
+    badge   rating.badge_with_orgs%ROWTYPE;
+BEGIN
+
+    SELECT * INTO badge FROM rating.badge_with_orgs WHERE id = badge_id;
+
+    PERFORM rating.precalc_bibs_by_copy(badge_id);
+
+    DELETE FROM precalc_copy_filter_bib_list WHERE id NOT IN (
+        SELECT id FROM precalc_filter_bib_list
+            INTERSECT
+        SELECT id FROM precalc_bibs_by_copy_list
+    );
+    ANALYZE precalc_copy_filter_bib_list;
+
+    RETURN QUERY
+     SELECT f.id::INT AS bib,
+            COUNT(f.copy)::NUMERIC
+      FROM  precalc_copy_filter_bib_list f
+            JOIN asset.copy cp ON (f.copy = cp.id)
+            JOIN asset.call_number cn ON (cn.id = cp.call_number)
+      WHERE cn.owning_lib = ANY (badge.orgs) GROUP BY 1;
+
+END;
+$f$ LANGUAGE PLPGSQL STRICT;
+
 COMMIT;
 
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.data.copy-count-badge.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.data.copy-count-badge.sql
new file mode 100644 (file)
index 0000000..84d36da
--- /dev/null
@@ -0,0 +1,34 @@
+BEGIN;
+
+CREATE OR REPLACE FUNCTION rating.copy_count(badge_id INT)
+    RETURNS TABLE (record INT, value NUMERIC) AS $f$
+DECLARE
+    badge   rating.badge_with_orgs%ROWTYPE;
+BEGIN
+
+    SELECT * INTO badge FROM rating.badge_with_orgs WHERE id = badge_id;
+
+    PERFORM rating.precalc_bibs_by_copy(badge_id);
+
+    DELETE FROM precalc_copy_filter_bib_list WHERE id NOT IN (
+        SELECT id FROM precalc_filter_bib_list
+            INTERSECT
+        SELECT id FROM precalc_bibs_by_copy_list
+    );
+    ANALYZE precalc_copy_filter_bib_list;
+
+    RETURN QUERY
+     SELECT f.id::INT AS bib,
+            COUNT(f.copy)::NUMERIC
+      FROM  precalc_copy_filter_bib_list f
+            JOIN asset.copy cp ON (f.copy = cp.id)
+            JOIN asset.call_number cn ON (cn.id = cp.call_number)
+      WHERE cn.owning_lib = ANY (badge.orgs) GROUP BY 1;
+
+END;
+$f$ LANGUAGE PLPGSQL STRICT;
+
+INSERT INTO rating.popularity_parameter (name, func, require_percentile) VALUES ('Copy Count', 'rating.copy_count', TRUE);
+
+COMMIT;
+