Skip to content

Commit e6ea073

Browse files
committed
pkp/pkp-lib#11268 Fix retrieval of preprint statistics and improved performance
1 parent de406bf commit e6ea073

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

classes/submission/Submission.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
use APP\core\Services;
2828
use APP\facades\Repo;
2929
use APP\publication\Publication;
30+
use APP\services\StatsPublicationService;
3031
use APP\statistics\StatisticsHelper;
3132
use PKP\facades\Locale;
33+
use PKP\galley\Galley;
3234
use PKP\submission\PKPSubmission;
3335

3436
class Submission extends PKPSubmission
@@ -223,27 +225,28 @@ public function getLocalizedGalleys()
223225
*/
224226
public function getTotalGalleyViews()
225227
{
226-
$fileIds = [];
227-
$publications = $this->getPublishedPublications();
228-
foreach ($publications as $publication) {
229-
foreach ($publication->getData('galleys') as $galley) {
230-
$file = $galley->getFile();
231-
if (!$galley->getRemoteUrl() && $file && !in_array($file->getId(), $fileIds)) {
232-
$fileIds[] = $file->getId();
233-
}
234-
}
228+
$submissionFileIds = collect($this->getPublishedPublications())
229+
->flatMap(fn (Publication $publication) => $publication->getData('galleys')->all())
230+
->map(fn (Galley $submissionFileId) => (int) $submissionFileId->getData('submissionFileId'))
231+
->unique()
232+
->filter(fn (int $submissionFileId) => (bool) $submissionFileId)
233+
->all();
234+
if (!count($submissionFileIds)) {
235+
return 0;
235236
}
237+
236238
$filters = [
237239
'dateStart' => StatisticsHelper::STATISTICS_EARLIEST_DATE,
238240
'dateEnd' => date('Y-m-d', strtotime('yesterday')),
239-
'contextIds' => [$this->getData('contextId')],
240-
'fileIds' => $fileIds,
241+
'contextIds' => [$this->getData('contextId')]
241242
];
242-
$metrics = Services::get('publicationStats')
243+
/** @var StatsPublicationService $publicationStats */
244+
$publicationStats = app()->get('publicationStats');
245+
return (int) $publicationStats
243246
->getQueryBuilder($filters)
244-
->getSum([])
247+
->filterBySubmissionFiles($submissionFileIds)
248+
->getSum()
245249
->value('metric');
246-
return $metrics ? $metrics : 0;
247250
}
248251

249252
/**

0 commit comments

Comments
 (0)