Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHRAS-4077 bin/report : add group by field into download reports #4525

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions lib/Alchemy/Phrasea/Command/Report/DownloadsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class DownloadsCommand extends AbstractReportCommand
{
const TYPES = ['user', 'record'];
const TYPES = ['user', 'record', 'field'];

public function __construct()
{
Expand All @@ -20,13 +20,15 @@ public function __construct()
->addOption('type', null, InputOption::VALUE_REQUIRED, 'type of report downloads, if not defined or empty it is for all downloads')
->addOption('collection_id', 'c', InputOption::VALUE_REQUIRED| InputOption::VALUE_IS_ARRAY, 'Distant collection ID in the databox, get all available collection if not defined')
->addOption('permalink', 'p', InputOption::VALUE_REQUIRED, 'the subdefinition name to retrieve permalink if exist, available only for type record and for all downloads type ""')
->addOption('meta_struct_id', 'm', InputOption::VALUE_REQUIRED, 'the meta_struct_id of the field to do grouping, available only for type field')

->setHelp(
"eg: bin/report downloads:all --databox_id 2 --email '[email protected]' --dmin '2022-12-01' --dmax '2023-01-01' --type 'user' \n"
. "\<TYPE>type of report\n"
. "- <info>'' or not defined </info>all downloads\n"
. "- <info>'user' </info> downloads by user\n"
. "- <info>'record' </info> downloads by record\n"
. "- <info>'field' </info> downloads by field, need --meta_struct_id option\n"
);
}

Expand All @@ -38,6 +40,7 @@ protected function getReport(InputInterface $input, OutputInterface $output)
$type = $input->getOption('type');
$collectionIds = $input->getOption('collection_id');
$permalink = $input->getOption('permalink');
$metaStructId = $input->getOption('meta_struct_id');

if (!empty($type) && !in_array($type, self::TYPES)) {
$output->writeln("<error>wrong '--type' option (--help for available value)</error>");
Expand All @@ -51,6 +54,12 @@ protected function getReport(InputInterface $input, OutputInterface $output)
return 1;
}

if ($type == 'field' && empty($metaStructId)) {
$output->writeln("<error>you have to set option --meta_struct_id with type=field</error>");

return 1;
}

$databox = $this->findDbOr404($this->sbasId);
$collIds = [];

Expand All @@ -66,14 +75,21 @@ protected function getReport(InputInterface $input, OutputInterface $output)
}
}

if (!$this->isFieldExist($databox, $metaStructId)) {
$output->writeln("<error>there is no field find with this meta_struct_id!</error>");

return 1;
}

return
(new ReportActions(
$databox,
[
'dmin' => $this->dmin,
'dmax' => $this->dmax,
'group' => $type,
'anonymize' => $this->container['conf']->get(['registry', 'modules', 'anonymous-report'])
'dmin' => $this->dmin,
'dmax' => $this->dmax,
'group' => $type,
'anonymize' => $this->container['conf']->get(['registry', 'modules', 'anonymous-report']),
'meta_struct_id' => $metaStructId
]
))
->setAppKey($this->container['conf']->get(['main', 'key']))
Expand All @@ -82,4 +98,15 @@ protected function getReport(InputInterface $input, OutputInterface $output)
->setAsDownloadReport(true)
;
}

private function isFieldExist(\databox $databox, $metaStructId)
{
foreach($databox->get_meta_structure() as $field) {
if ($field->get_id() == $metaStructId) {
return true;
}
}

return false;
}
}
19 changes: 19 additions & 0 deletions lib/Alchemy/Phrasea/Report/ReportActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ private function computeVars()
;
$this->keyName = 'record_id';
break;
case 'field':
$this->name = "Downloads by field";
foreach($this->getDatabox()->get_meta_structure() as $field) {
if ($field->get_id() == $this->parms['meta_struct_id']) {
$this->columnTitles =[$field->get_name()];
}
}
$this->columnTitles[] = 'nb';

$sql = "SELECT `m`.`value`, count(`ld`.`record_id`) as `nb`\n"
. " FROM `log_docs` AS `ld` INNER JOIN `log` AS `l` ON `l`.`id`=`ld`.`log_id`\n"
. " LEFT JOIN `metadatas` AS `m` ON (`ld`.`record_id`=`m`.`record_id` AND `m`.`meta_struct_id`=". $this->parms['meta_struct_id'] .") "
. " WHERE {{GlobalFilter}}\n"
. " GROUP BY `m`.`value`\n"
. " ORDER BY `nb` DESC"
;

$this->keyName = 'value';
break;
default:
throw new InvalidArgumentException('invalid "group" argument');
break;
Expand Down
Loading