LP#1672346 - Copy Statistics View
authorChris Sharp <csharp@georgialibraries.org>
Sun, 8 Oct 2017 02:44:49 +0000 (22:44 -0400)
committerChris Sharp <csharp@georgialibraries.org>
Thu, 23 Sep 2021 16:58:42 +0000 (12:58 -0400)
Certain third-party products such as collection development
management providers require copy statistics that are not
readily available in a single report.  This view adds those,
which will also benefit library staff reports generally.

Signed-off-by: Chris Sharp <csharp@georgialibraries.org>
Signed-off-by: Ruth Frasur <rfrasur@library.in.gov>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/sql/Pg/example.reporter-extension.sql

index fc6ea58..0eab159 100644 (file)
@@ -11059,6 +11059,43 @@ SELECT  usr,
                        <link field="owning_lib" reltype="has_a" key="id" map="" class="aou"/>
                </links>
        </class>
+       <class id="rcsv" controller="open-ils.reporter-store" oils_obj:fieldmapper="reporter::copy_statistics_view" oils_persist:tablename="reporter.copy_statistics_view" reporter:label="Item Statistics View">
+               <fields oils_persist:primary="copy_id">
+                       <field reporter:label="Item ID" name="copy_id" reporter:datatype="link"/>
+                       <field reporter:label="Owning Library Link" name="owning_lib_id" reporter:datatype="link"/>
+                       <field reporter:label="Consortium: Last Circulation Date" name="consortium_last_circ_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="System: Last Circulation Date" name="system_last_circ_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="Branch: Last Circulation Date" name="branch_last_circ_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="Consortium: Last Checkin Date" name="consortium_last_checkin_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="System: Last Checkin Date" name="system_last_checkin_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="Branch: Last Checkin Date" name="branch_last_checkin_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="Consortium: Last Due Date" name="consortium_last_due_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="System: Last Due Date" name="system_last_due_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="Branch: Last Due Date" name="branch_last_due_date" reporter:datatype="timestamp"/>
+                       <field reporter:label="Consortium: Month-to-Date Circulation" name="consortium_month_to_date_circ" reporter:datatype="int"/>
+                       <field reporter:label="System: Month-to-Date Circulation" name="system_month_to_date_circ" reporter:datatype="int"/>
+                       <field reporter:label="Branch: Month-to-Date Circulation" name="branch_month_to_date_circ" reporter:datatype="int"/>
+                       <field reporter:label="Consortium: Year-to-Date Circulation" name="consortium_year_to_date_circ" reporter:datatype="int"/>
+                       <field reporter:label="System: Year-to-Date Circulation" name="system_year_to_date_circ" reporter:datatype="int"/>
+                       <field reporter:label="Branch: Year-to-Date Circulation" name="branch_year_to_date_circ" reporter:datatype="int"/>
+                       <field reporter:label="Consortium: Lifetime Circulation" name="consortium_lifetime_circ" reporter:datatype="int"/>
+                       <field reporter:label="System: Lifetime Circulation" name="system_lifetime_circ" reporter:datatype="int"/>
+                       <field reporter:label="Branch: Lifetime Circulation" name="branch_lifetime_circ" reporter:datatype="int"/>
+                       <field reporter:label="Consortium: Current Title Hold Count" name="consortium_current_title_hold_count" reporter:datatype="int"/>
+                       <field reporter:label="System: Current Title Hold Count" name="system_current_title_hold_count" reporter:datatype="int"/>
+                       <field reporter:label="Branch: Current Title Hold Count" name="branch_current_title_hold_count" reporter:datatype="int"/>
+                       <field reporter:label="Consortium: Lifetime Title Hold Count" name="consortium_lifetime_holds" reporter:datatype="int"/>
+                       <field reporter:label="System: Lifetime Title Hold Count" name="system_lifetime_holds" reporter:datatype="int"/>
+                       <field reporter:label="Branch: Lifetime Title Hold Count" name="branch_lifetime_holds" reporter:datatype="int"/>
+                       <field reporter:label="Consortium: Lifetime Transits" name="consortium_lifetime_transits" reporter:datatype="int"/>
+                       <field reporter:label="System: Lifetime Outbound Transits" name="system_lifetime_transits" reporter:datatype="int"/>
+                       <field reporter:label="Branch: Lifetime Outbound Transits" name="branch_lifetime_transits" reporter:datatype="int"/>
+               </fields>
+               <links>
+                       <link field="copy_id" reltype="has_a" key="id" map="" class="acp"/>
+                       <link field="owning_lib_id" reltype="has_a" key="id" map="" class="aou"/>
+               </links>
+       </class>
        <class id="acqdf" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="acq::distribution_formula" oils_persist:tablename="acq.distribution_formula" reporter:label="Distribution Formula">
                <fields oils_persist:primary="id" oils_persist:sequence="acq.distribution_formula_id_seq">
                        <field reporter:label="Formula ID" name="id" reporter:datatype="id" reporter:selector="name" />
index 8cee578..21c9fbc 100644 (file)
@@ -316,6 +316,219 @@ CREATE OR REPLACE VIEW money.open_balance_by_usr_home_and_owning_lib AS
          FROM  money.open_circ_balance_by_usr_home_and_owning_lib x
          GROUP BY 1,2;
 
-COMMIT;
-
+CREATE OR REPLACE VIEW reporter.copy_statistics_view AS
+SELECT    acp.id as copy_id,
+       owning_lib.id as owning_lib_id,
+    -- last circulation date in corsortium
+    (SELECT COALESCE(MAX(xact_start), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+    ) AS consortium_last_circ_date,
+    -- last circulation date in system
+    (SELECT COALESCE(MAX(xact_start), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib in (
+        SELECT id
+        FROM actor.org_unit
+        WHERE parent_ou = owning_lib.parent_ou
+        )
+    ) AS system_last_circ_date,
+    -- last circulation date in branch
+    (SELECT COALESCE(MAX(xact_start), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib = owning_lib.id
+    ) AS branch_last_circ_date,
+    -- last checkin date in corsortium
+    (SELECT COALESCE(MAX(checkin_time), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+    ) AS consortium_last_checkin_date,
+    -- last checkin date in system
+    (SELECT COALESCE(MAX(checkin_time), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib in (
+        SELECT id
+        FROM actor.org_unit
+        WHERE parent_ou = owning_lib.parent_ou
+        )
+    ) AS system_last_checkin_date,
+    -- last checkin date in branch
+    (SELECT COALESCE(MAX(checkin_time), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib = owning_lib.id
+    ) AS branch_last_checkin_date,
+    -- last due date in consortium
+    (SELECT COALESCE(MAX(due_date), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+    ) AS consortium_last_due_date,
+    -- last due date in system
+    (SELECT COALESCE(MAX(due_date), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib in (
+        SELECT id
+        FROM actor.org_unit
+        WHERE parent_ou = owning_lib.parent_ou
+        )
+    ) AS system_last_due_date,
+    -- last due date in branch
+    (SELECT COALESCE(MAX(due_date), NULL)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib = owning_lib.id
+    ) AS branch_last_due_date,
+    -- month-to-date circ in corsortium
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE EXTRACT(YEAR FROM xact_start) = EXTRACT(YEAR FROM now())
+     AND EXTRACT(MONTH FROM xact_start) = EXTRACT(MONTH FROM now())
+     AND target_copy = acp.id
+    ) AS consortium_month_to_date_circ,
+    -- month-to-date circ in system
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE EXTRACT(YEAR FROM xact_start) = EXTRACT(YEAR FROM now())
+     AND EXTRACT(MONTH FROM xact_start) = EXTRACT(MONTH FROM now())
+     AND target_copy = acp.id
+     AND circ_lib in (
+       SELECT id
+       FROM actor.org_unit
+       WHERE parent_ou = owning_lib.parent_ou
+       )
+    ) AS system_month_to_date_circ,
+    -- month-to-date circ in branch
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE EXTRACT(YEAR FROM xact_start) = EXTRACT(YEAR FROM now())
+     AND EXTRACT(MONTH FROM xact_start) = EXTRACT(MONTH FROM now())
+     AND target_copy = acp.id
+     AND circ_lib = owning_lib.id
+    ) AS branch_month_to_date_circ,
+    -- year-to-date circ in consortium
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE EXTRACT(YEAR FROM xact_start) = EXTRACT(YEAR FROM now())
+     AND target_copy = acp.id
+    ) AS consortium_year_to_date_circ,
+    -- year-to-date circ in system
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE EXTRACT(YEAR FROM xact_start) = EXTRACT(YEAR FROM now())
+     AND target_copy = acp.id
+     AND circ_lib in (
+       SELECT id
+       FROM actor.org_unit
+       WHERE parent_ou = owning_lib.parent_ou
+       )
+    ) AS system_year_to_date_circ,
+    -- year-to-date circ in branch
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE EXTRACT(YEAR FROM xact_start) = EXTRACT(YEAR FROM now())
+     AND target_copy = acp.id
+     AND circ_lib = owning_lib.id
+    ) AS branch_year_to_date_circ,
+    -- lifetime circ in consortium
+    erfcc.circ_count AS consortium_lifetime_circ,
+    -- lifetime circ in system
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib in (
+        SELECT id
+        FROM actor.org_unit
+        WHERE parent_ou = owning_lib.parent_ou
+        )
+    ) AS system_lifetime_circ,
+    -- lifetime circ in branch
+    (SELECT count(*)
+     FROM action.all_circulation
+     WHERE target_copy = acp.id
+     AND circ_lib = owning_lib.id
+    ) AS branch_lifetime_circ,
+    -- current title hold count in consortium
+    (SELECT count(*)
+     FROM action.hold_request ahr
+     WHERE cancel_time IS NULL
+     AND expire_time > now()
+     AND fulfillment_time IS NULL
+     AND target = bre.id
+     AND hold_type = 'T'
+    ) AS consortium_current_title_hold_count,
+    -- current title hold count in system
+    (SELECT count(*)
+     FROM action.hold_request ahr
+     WHERE cancel_time IS NULL
+     AND expire_time > now()
+     AND fulfillment_time IS NULL
+     AND target = bre.id
+     AND hold_type = 'T'
+     AND pickup_lib IN (
+        SELECT id
+        FROM actor.org_unit
+        WHERE parent_ou = owning_lib.parent_ou
+        )
+    ) AS system_current_title_hold_count,
+    -- current title hold count in branch
+    (SELECT count(*)
+     FROM action.hold_request ahr
+     WHERE cancel_time IS NULL
+     AND expire_time > now()
+     AND fulfillment_time IS NULL
+     AND target = bre.id
+     AND hold_type = 'T'
+     AND pickup_lib = owning_lib.parent_ou
+    ) AS branch_current_title_hold_count,
+    -- consortium lifetime holds
+    (SELECT count(*)
+     FROM action.all_hold_request
+     WHERE current_copy = acp.id) AS consortium_lifetime_holds,
+    -- system lifetime holds
+    (SELECT count(*)
+     FROM action.all_hold_request
+     WHERE current_copy = acp.id
+     AND pickup_lib IN (
+        SELECT id
+        FROM actor.org_unit
+        WHERE parent_ou = owning_lib.parent_ou
+        )
+    ) AS system_lifetime_holds,
+    -- branch lifetime holds
+    (SELECT count(*)
+     FROM action.all_hold_request
+     WHERE current_copy = acp.id
+     AND pickup_lib = owning_lib.parent_ou
+    ) AS branch_lifetime_holds,
+    -- consortium lifetime transits
+    (SELECT count(*)
+     FROM action.transit_copy
+     WHERE target_copy = acp.id
+    ) AS consortium_lifetime_transits,
+    -- system lifetime transits
+    (SELECT count(*)
+     FROM action.transit_copy
+     WHERE target_copy = acp.id
+     AND source IN (
+        SELECT id
+        FROM actor.org_unit
+        WHERE parent_ou = owning_lib.parent_ou
+        )
+    ) AS system_lifetime_transits,
+    -- branch lifetime transits
+    (SELECT count(*)
+     FROM action.transit_copy
+     WHERE target_copy = acp.id
+     AND source = owning_lib.id
+    ) AS branch_lifetime_transits
+FROM    asset.copy acp
+    INNER JOIN asset.call_number acn ON (acp.call_number = acn.id)
+    INNER JOIN biblio.record_entry bre ON (acn.record = bre.id)
+    INNER JOIN actor.org_unit owning_lib ON (acn.owning_lib = owning_lib.id)
+    LEFT OUTER JOIN extend_reporter.full_circ_count erfcc ON (erfcc.id = acp.id);
 
+COMMIT;