From e935423b787ed2b057219e3c94bb6e444305de5b Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Fri, 4 Aug 2023 16:32:16 +0000 Subject: [PATCH] Bug 34587: Move period total calculations to the backend to improve performance Remove the front end processing in favour of doing this in the backend and passing it in the API response Signed-off-by: Jessica Zairo Signed-off-by: Michaela Sieber Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/REST/V1/ERM/CustomReports.pm | 27 +++++++++++++++++++ Koha/Schema/Result/ErmUsageDatabase.pm | 8 ++++++ Koha/Schema/Result/ErmUsageItem.pm | 9 +++++++ .../ERM/UsageStatisticsReportsViewer.vue | 25 +++-------------- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Koha/REST/V1/ERM/CustomReports.pm b/Koha/REST/V1/ERM/CustomReports.pm index 4286887a1b..c6afff8d67 100644 --- a/Koha/REST/V1/ERM/CustomReports.pm +++ b/Koha/REST/V1/ERM/CustomReports.pm @@ -283,6 +283,8 @@ sub metric_types_report { =head3 provider_rollup_report +An endpoint to fetch all data for all providers for a given report type + =cut sub provider_rollup_report { @@ -389,7 +391,11 @@ sub provider_rollup_report { }; } +=head3 _get_data_set + +Returns the Koha object that needs to be used to fetch the data for this report. +=cut sub _get_data_set { my ($data_type) = @_; @@ -409,6 +415,13 @@ sub _get_data_set { return 0; } +=head3 _get_correct_query_param + +Returns the array of ids (or an empty array) for the data type that has been requested as a report parameter. +e.g. If it is a titles report and the user has requested titles with the ids 1,2,3, these will be fetched from the query parameters and returned as (1,2,3). + +=cut + sub _get_correct_query_param { my ( $data_type, $array_ref, $period ) = @_; @@ -428,6 +441,13 @@ sub _get_correct_query_param { return $param; } +=head3 _get_result_with_no_statistics + +Takes in an id number and a dataset. If that id number exists within the dataset then no action is needed. +If it isn't found then that means there are no statistics for that object. It is however required in the report so is returned with a blank dataset. + +=cut + sub _get_result_with_no_statistics { my ($args) = @_; @@ -466,6 +486,7 @@ sub _get_object_hash { my $provider = $args->{provider}; my $metric_type = $args->{metric_type}; my $period = $args->{period}; + my $sum = $args->{sum}; my %object_hash; if ( $data_type eq 'title' ) { @@ -527,6 +548,12 @@ sub _get_object_hash { return \%object_hash; } +=head3 _get_missing_data + +If an id is identified as missing, this piece of data is fetched and returned into _get_result_with_no_statistics for processing. + +=cut + sub _get_missing_data { my ( $data_type, $id ) = @_; diff --git a/Koha/Schema/Result/ErmUsageDatabase.pm b/Koha/Schema/Result/ErmUsageDatabase.pm index 73df6f1b75..0dd9556c5f 100644 --- a/Koha/Schema/Result/ErmUsageDatabase.pm +++ b/Koha/Schema/Result/ErmUsageDatabase.pm @@ -153,4 +153,12 @@ __PACKAGE__->belongs_to( # You can replace this text with custom code or comments, and it will be preserved on regeneration + +sub koha_object_class { + 'Koha::ERM::UsageDatabase'; +} + +sub koha_objects_class { + 'Koha::ERM::UsageDatabases'; +} 1; diff --git a/Koha/Schema/Result/ErmUsageItem.pm b/Koha/Schema/Result/ErmUsageItem.pm index 90f9345d52..e6d0bb5161 100644 --- a/Koha/Schema/Result/ErmUsageItem.pm +++ b/Koha/Schema/Result/ErmUsageItem.pm @@ -143,4 +143,13 @@ __PACKAGE__->belongs_to( # You can replace this text with custom code or comments, and it will be preserved on regeneration + +sub koha_object_class { + 'Koha::ERM::UsageItem'; +} + +sub koha_objects_class { + 'Koha::ERM::UsageItems'; +} + 1; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsReportsViewer.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsReportsViewer.vue index 608a8729ae..d553e96498 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsReportsViewer.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/UsageStatisticsReportsViewer.vue @@ -151,22 +151,8 @@ export default { orderable: true, }) column_set.push({ - title: __("Period Total"), - render: function (data, type, row, meta) { - const sum = row[`erm_usage_${data_type}s`].reduce( - (acc, obj) => { - const objSum = obj.erm_usage_muses.reduce( - (acc, mus) => { - return acc + mus.usage_count - }, - 0 - ) - return acc + objSum - }, - 0 - ) - return sum - }, + title: __("Period total"), + data: "provider_rollup_total", searchable: true, orderable: true, }) @@ -281,12 +267,7 @@ export default { if (report_type === "monthly_with_totals") { column_set.push({ title: __("Period total"), - render: function (data, type, row, meta) { - const sum = row.erm_usage_muses.reduce((acc, item) => { - return acc + item.usage_count - }, 0) - return sum - }, + name: "usage_total", searchable: true, orderable: true, }) -- 2.39.5