besides queue_position, we should now have estimated_wait_time et al simplified-hpl
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Fri, 20 Apr 2012 03:42:48 +0000 (23:42 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Fri, 20 Apr 2012 03:50:17 +0000 (23:50 -0400)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/sql/Pg/090.schema.action.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.simplified-hold-pull-list.sql

index d3f758a..18d2b32 100644 (file)
@@ -4846,10 +4846,22 @@ SELECT  usr,
                                        COALESCE(au.first_given_name, ''),
                                        COALESCE(au.second_given_name, '')
                                ], ' '), E'\\s+,', ',')
-                               END AS usr_display_name,
+                       END AS usr_display_name,
                        TRIM(acnp.label || ' ' || acn.label || ' ' || acns.label)
-                               AS call_number_label
+                               AS call_number_label,
+                       ahqa.queue_position,
+                       ahqa.total_holds,
+                       ahawt.avg_wait_time,
+                       ahawt.num_potentials,
+                       action.estimate_wait_time_for_hold(
+                               ahawt.avg_wait_time,
+                               ahqa.queue_position::INTEGER,
+                               au.home_ou
+                       ) AS estimated_wait_time
                FROM action.hold_request ahr
+               JOIN action.hold_avg_wait_time ahawt ON (ahawt.hold = ahr.id)
+               JOIN action.hold_queue_approximation ahqa
+                       ON (ahqa.this_hold=ahr.id AND ahqa.other_hold=ahr.id)
                JOIN asset.copy acp ON (acp.id = ahr.current_copy)
                JOIN asset.call_number acn ON (acp.call_number = acn.id)
                JOIN asset.call_number_prefix acnp ON (acn.prefix = acnp.id)
@@ -4907,6 +4919,11 @@ SELECT  usr,
                        <field reporter:label="Copy Location Sort Order" name="copy_location_order_position" reporter:datatype="int" />
                        <field reporter:label="User Display Name" name="usr_display_name" reporter:datatype="text" />
                        <field reporter:label="Call Number Label" name="call_number_label" reporter:datatype="text" />
+                       <field reporter:label="Queue Position" name="queue_position" reporter:datatype="int" />
+                       <field reporter:label="Total Holds in Queue" name="total_holds" reporter:datatype="int" />
+                       <field reporter:label="Average Wait Time Per Hold" name="avg_wait_time" reporter:datatype="interval" />
+                       <field reporter:label="Number of Potential Copies" name="num_potentials" reporter:datatype="int" />
+                       <field reporter:label="Estimated Wait Time" name="estimated_wait_time" reporter:datatype="interval" />
                </fields>
                <links>
                        <link field="fulfillment_lib" reltype="has_a" key="id" map="" class="aou"/>
index 1af6e89..d72ea6d 100644 (file)
@@ -972,5 +972,56 @@ query-based fieldsets.
 Returns NULL if successful, or an error message if not.
 $$;
 
+-- For a given hold, get avg wait time of copies on potentials list and
+-- the number of copies on potentials list.  Estimated wait time can be
+-- calculated from these results based on queue position.
+CREATE VIEW action.hold_avg_wait_time AS
+    SELECT
+        SUM(num_potentials * avg_wait_time) / SUM(num_potentials) AS avg_wait_time,
+        SUM(num_potentials) AS num_potentials,
+        hold
+    FROM (
+        SELECT
+            COUNT(acp.id) AS num_potentials,
+            ahcm.hold,
+            COALESCE(ccm.avg_wait_time, (SELECT MAX(value) FROM (
+                SELECT value::INTERVAL FROM actor.org_unit_ancestor_setting('circ.holds.default_estimated_wait_interval', au.home_ou)
+                    UNION
+                SELECT '0 seconds'::INTERVAL
+            ) ous)) AS avg_wait_time
+        FROM action.hold_copy_map ahcm
+        JOIN action.hold_request ahr ON (ahr.id = ahcm.hold)
+        JOIN actor.usr au ON (au.id = ahr.usr)
+        JOIN asset.copy acp ON (acp.id = ahcm.target_copy)
+        LEFT JOIN config.circ_modifier ccm ON (ccm.code = acp.circ_modifier)
+        GROUP BY 2, 3, au.home_ou
+    ) x
+    GROUP by 3;
+
+
+CREATE OR REPLACE FUNCTION action.estimate_wait_time_for_hold(
+    avg_wait_time   INTERVAL,
+    queue_position  INT,
+    home_ou         INT
+) RETURNS INTERVAL AS $$
+SELECT
+    CASE WHEN min_wait > estimated_wait_time THEN
+        min_wait
+    ELSE
+        estimated_wait_time
+    END
+FROM (
+    SELECT
+        (SELECT MAX(value) FROM (
+            SELECT value::INTERVAL
+                FROM actor.org_unit_ancestor_setting(
+                    'circ.holds.min_estimated_wait_interval', $3
+                )
+            UNION
+            SELECT '0 seconds'::INTERVAL
+        ) ous) AS min_wait,
+        $1 * $2 AS estimated_wait_time
+) x
+$$ LANGUAGE SQL;
 
 COMMIT;
index 7c355dc..be40625 100644 (file)
@@ -77,4 +77,57 @@ SELECT  h1.id AS this_hold,
         JOIN action.hold_request h2 ON (h2.id = r2.id) ;
 
 -- End hold queue approximation
+
+-- For a given hold, get avg wait time of copies on potentials list and
+-- the number of copies on potentials list.  Estimated wait time can be
+-- calculated from these results based on queue position.
+CREATE VIEW action.hold_avg_wait_time AS
+    SELECT
+        SUM(num_potentials * avg_wait_time) / SUM(num_potentials) AS avg_wait_time,
+        SUM(num_potentials) AS num_potentials,
+        hold
+    FROM (
+        SELECT
+            COUNT(acp.id) AS num_potentials,
+            ahcm.hold,
+            COALESCE(ccm.avg_wait_time, (SELECT MAX(value) FROM (
+                SELECT value::INTERVAL FROM actor.org_unit_ancestor_setting('circ.holds.default_estimated_wait_interval', au.home_ou)
+                    UNION
+                SELECT '0 seconds'::INTERVAL
+            ) ous)) AS avg_wait_time
+        FROM action.hold_copy_map ahcm
+        JOIN action.hold_request ahr ON (ahr.id = ahcm.hold)
+        JOIN actor.usr au ON (au.id = ahr.usr)
+        JOIN asset.copy acp ON (acp.id = ahcm.target_copy)
+        LEFT JOIN config.circ_modifier ccm ON (ccm.code = acp.circ_modifier)
+        GROUP BY 2, 3, au.home_ou
+    ) x
+    GROUP by 3;
+
+
+CREATE OR REPLACE FUNCTION action.estimate_wait_time_for_hold(
+    avg_wait_time   INTERVAL,
+    queue_position  INT,
+    home_ou         INT
+) RETURNS INTERVAL AS $$
+SELECT
+    CASE WHEN min_wait > estimated_wait_time THEN
+        min_wait
+    ELSE
+        estimated_wait_time
+    END
+FROM (
+    SELECT
+        (SELECT MAX(value) FROM (
+            SELECT value::INTERVAL
+                FROM actor.org_unit_ancestor_setting(
+                    'circ.holds.min_estimated_wait_interval', $3
+                )
+            UNION
+            SELECT '0 seconds'::INTERVAL
+        ) ous) AS min_wait,
+        $1 * $2 AS estimated_wait_time
+) x
+$$ LANGUAGE SQL;
+
 COMMIT;