Skip to content

Commit

Permalink
Merge pull request #1728 from bozana/9666
Browse files Browse the repository at this point in the history
pkp/pkp-lib#9666 COUNTER R5 TSV reports
  • Loading branch information
bozana authored Oct 16, 2024
2 parents d2f267e + 7bc4611 commit 3d1fd39
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 17 deletions.
5 changes: 3 additions & 2 deletions api/v1/stats/sushi/StatsSushiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Symfony\Component\HttpFoundation\StreamedResponse;

class StatsSushiController extends \PKP\API\v1\stats\sushi\PKPStatsSushiController
{
Expand All @@ -44,7 +45,7 @@ public function getGroupRoutes(): void
* A customizable report detailing activity at the press level
* that allows the user to apply filters and select other configuration options for the report.
*/
public function getReportsTR(Request $illuminateRequest): JsonResponse
public function getReportsTR(Request $illuminateRequest): JsonResponse|StreamedResponse
{
return $this->getReportResponse(new TR(), $illuminateRequest);
}
Expand All @@ -53,7 +54,7 @@ public function getReportsTR(Request $illuminateRequest): JsonResponse
* COUNTER 'Book Usage by Access Type' [TR_B3].
* This is a Standard View of Title Master Report that reports on book usage showing all applicable Metric_Types broken down by Access_Type.
*/
public function getReportsTRB3(Request $illuminateRequest): JsonResponse
public function getReportsTRB3(Request $illuminateRequest): JsonResponse|StreamedResponse
{
return $this->getReportResponse(new TR_B3(), $illuminateRequest);
}
Expand Down
52 changes: 52 additions & 0 deletions classes/components/forms/counter/CounterReportForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @file classes/components/form/counter/CounterReportForm.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CounterReportForm
*
* @ingroup classes_controllers_form
*
* @brief A form for setting a counter report
*/

namespace APP\components\forms\counter;

use APP\sushi\PR;
use APP\sushi\PR_P1;
use APP\sushi\TR;
use APP\sushi\TR_B3;
use PKP\components\forms\counter\PKPCounterReportForm;

class CounterReportForm extends PKPCounterReportForm
{
public function setReportFields(): void
{
$formFieldsPR = PR::getReportSettingsFormFields();
$this->reportFields['PR'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsPR);

$formFieldsPR_P1 = PR_P1::getReportSettingsFormFields();
$this->reportFields['PR_P1'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsPR_P1);

$formFieldsTR = TR::getReportSettingsFormFields();
$this->reportFields['TR'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsTR);

$formFieldsTR_B3 = TR_B3::getReportSettingsFormFields();
$this->reportFields['TR_B3'] = array_map(function ($field) {
$field->groupId = 'default';
return $field;
}, $formFieldsTR_B3);
}
}
142 changes: 136 additions & 6 deletions classes/sushi/PR.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace APP\sushi;

use Illuminate\Support\Collection;
use PKP\components\forms\FieldOptions;
use PKP\statistics\PKPStatisticsHelper;
use PKP\sushi\CounterR5Report;

Expand Down Expand Up @@ -81,7 +83,8 @@ public function getSupportedParams(): array
'data_type',
'access_method',
'attributes_to_show',
'granularity'
'granularity',
'_', // for ajax requests
];
}

Expand Down Expand Up @@ -132,10 +135,9 @@ public function getSupportedAttributes(): array
];
}

/**
* Get report items
*/
public function getReportItems(): array

/** Get DB query results for the report */
protected function getQueryResults(): Collection
{
$params['contextIds'] = [$this->context->getId()];
$params['institutionId'] = $this->customerId;
Expand Down Expand Up @@ -167,6 +169,13 @@ public function getReportItems(): array
'Data' => __('sushi.exception.3030', ['beginDate' => $this->beginDate, 'endDate' => $this->endDate])
]);
}
return $results;
}

/** Get report items */
public function getReportItems(): array
{
$results = $this->getQueryResults();

// There is only one platform, so there will be only one report item
$item['Platform'] = $this->platformName;
Expand Down Expand Up @@ -200,7 +209,6 @@ public function getReportItems(): array
$metrics['Unique_Item_Investigations'] = $result->metric_book_investigations_unique + $result->metric_chapter_investigations_unique;
$metrics['Total_Item_Requests'] = $result->metric_book_requests + $result->metric_chapter_requests;
$metrics['Unique_Item_Requests'] = $result->metric_book_requests_unique + $result->metric_chapter_requests_unique;
$metrics['Total_Item_Requests'] = $result->metric_book_requests + $result->metric_chapter_requests;
$metrics['Unique_Title_Investigations'] = $result->metric_title_investigations_unique;
$metrics['Unique_Title_Requests'] = $result->metric_title_requests_unique;
// filter here by requested metric types
Expand All @@ -219,4 +227,126 @@ public function getReportItems(): array
$items = [$item];
return $items;
}

/** Get TSV report column names */
public function getTSVColumnNames(): array
{
$columnRow = ['Platform'];
if (in_array('Data_Type', $this->attributesToShow)) {
array_push($columnRow, 'Data_Type');
}
if (in_array('Access_Method', $this->attributesToShow)) {
array_push($columnRow, 'Access_Method');
}
array_push($columnRow, 'Metric_Type', 'Reporting_Period_Total');
if ($this->granularity == 'Month') {
$period = $this->getMonthlyDatePeriod();
foreach ($period as $dt) {
array_push($columnRow, $dt->format('M-Y'));
}
}
return [$columnRow];
}

/** Get TSV report rows */
public function getTSVReportItems(): array
{
$results = $this->getQueryResults();

// get total numbers for every metric type
$metricsTotal['Total_Item_Investigations'] = $results->pluck('metric_book_investigations')->sum() + $results->pluck('metric_chapter_investigations')->sum();
$metricsTotal['Unique_Item_Investigations'] = $results->pluck('metric_book_investigations_unique')->sum() + $results->pluck('metric_chapter_investigations_unique')->sum();
$metricsTotal['Total_Item_Requests'] = $results->pluck('metric_book_requests')->sum() + $results->pluck('metric_chapter_requests')->sum();
$metricsTotal['Unique_Item_Requests'] = $results->pluck('metric_book_requests_unique')->sum() + $results->pluck('metric_chapter_requests_unique')->sum();
$metricsTotal['Unique_Title_Investigations'] = $results->pluck('metric_title_investigations_unique')->sum();
$metricsTotal['Unique_Title_Requests'] = $results->pluck('metric_title_requests_unique')->sum();

$resultRows = [];
// filter here by requested metric types
foreach ($this->metricTypes as $metricType) {
// if the total numbers for the given metric type > 0
if ($metricsTotal[$metricType] > 0) {
// construct the result row
$resultRow = [];
array_push($resultRow, $this->platformName); // Platform
if (in_array('Data_Type', $this->attributesToShow)) {
array_push($resultRow, self::DATA_TYPE); // Data_Type
}
if (in_array('Access_Method', $this->attributesToShow)) {
array_push($resultRow, self::ACCESS_METHOD); // Access_Method
}
array_push($resultRow, $metricType); // Metric_Type
array_push($resultRow, $metricsTotal[$metricType]); // Reporting_Period_Total
if ($this->granularity == 'Month') { // metrics for each month in the given period
$period = $this->getMonthlyDatePeriod();
foreach ($period as $dt) {
$month = $dt->format('Ym');
$result = $results->firstWhere('month', '=', $month);
if ($result === null) {
array_push($resultRow, '0');
} else {
$metrics['Total_Item_Investigations'] = $result->metric_book_investigations + $result->metric_chapter_investigations;
$metrics['Unique_Item_Investigations'] = $result->metric_book_investigations_unique + $result->metric_chapter_investigations_unique;
$metrics['Total_Item_Requests'] = $result->metric_book_requests + $result->metric_chapter_requests;
$metrics['Unique_Item_Requests'] = $result->metric_book_requests_unique + $result->metric_chapter_requests_unique;
$metrics['Unique_Title_Investigations'] = $result->metric_title_investigations_unique;
$metrics['Unique_Title_Requests'] = $result->metric_title_requests_unique;
array_push($resultRow, $metrics[$metricType]);
}
}
}
$resultRows[] = $resultRow;
}
}

return $resultRows;
}

/** Get report specific form fields */
public static function getReportSettingsFormFields(): array
{
$formFields = parent::getCommonReportSettingsFormFields();

$metricTypes = [
'Total_Item_Investigations',
'Unique_Item_Investigations',
'Total_Item_Requests',
'Unique_Item_Requests',
'Unique_Title_Investigations',
'Unique_Title_Requests'
];
$metricTypeOptions = [];
foreach ($metricTypes as $metricType) {
$metricTypeOptions[] = ['value' => $metricType, 'label' => $metricType];
}
$formFields[] = new FieldOptions('metric_type', [
'label' => __('manager.statistics.counterR5Report.settings.metricType'),
'options' => $metricTypeOptions,
'value' => $metricTypes,
'groupId' => 'default',
]);

$attributesToShow = ['Data_Type', 'Access_Method'];
$attributesToShowOptions = [];
foreach ($attributesToShow as $attributeToShow) {
$attributesToShowOptions[] = ['value' => $attributeToShow, 'label' => $attributeToShow];
}
$formFields[] = new FieldOptions('attributes_to_show', [
'label' => __('manager.statistics.counterR5Report.settings.attributesToShow'),
'options' => $attributesToShowOptions,
'value' => [],
'groupId' => 'default',
]);

$formFields[] = new FieldOptions('granularity', [
'label' => __('manager.statistics.counterR5Report.settings.excludeMonthlyDetails'),
'options' => [
['value' => true, 'label' => __('manager.statistics.counterR5Report.settings.excludeMonthlyDetails')],
],
'value' => false,
'groupId' => 'default',
]);

return $formFields;
}
}
9 changes: 8 additions & 1 deletion classes/sushi/PR_P1.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function getSupportedParams(): array
'customer_id',
'begin_date',
'end_date',
'platform'
'platform',
'_', // for ajax requests
];
}

Expand Down Expand Up @@ -114,4 +115,10 @@ public function setAttributes(array $attributes): void
{
$this->attributes = [];
}

/** Get report specific form fields */
public static function getReportSettingsFormFields(): array
{
return parent::getCommonReportSettingsFormFields();
}
}
Loading

0 comments on commit 3d1fd39

Please sign in to comment.