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)
<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"/>
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;
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;