PHRAS-3823 add column permalink and option in report command (#4288)

This commit is contained in:
Aina Sitraka
2023-03-30 13:15:45 +03:00
committed by GitHub
parent faf179681a
commit 2a271f458e
5 changed files with 68 additions and 4 deletions

View File

@@ -37,7 +37,7 @@ abstract class AbstractReportCommand extends Command
->addOption('email', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY ,'emails to send the report')
->addOption('dmin', null, InputOption::VALUE_REQUIRED, 'minimum date yyyy-mm-dd')
->addOption('dmax', null, InputOption::VALUE_REQUIRED, 'maximum date yyyy-mm-dd, until today if not set')
->addOption('range', null, InputOption::VALUE_REQUIRED, "period range until now eg: <info>'10 days', '2 weeks', '6 months', ' 1 year'</info>")
->addOption('range', null, InputOption::VALUE_REQUIRED, "period range until now eg: <info>'10 days', '2 weeks', '6 months', '2 years'</info>")
;
}

View File

@@ -17,9 +17,12 @@ class DataboxContentCommand extends AbstractReportCommand
->setDescription('BETA - Get all databox records')
->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('field', 'f', InputOption::VALUE_REQUIRED| InputOption::VALUE_IS_ARRAY, 'The field name to include in the report, get all available report field if not defined')
->addOption('permalink', 'p', InputOption::VALUE_REQUIRED, 'the subdefinition name to retrieve permalink if exist')
->setHelp(
"eg: bin/report databox:content --databox_id 2 --email 'admin@alchemy.fr' --dmin '2022-12-01' --dmax '2023-01-01' \n"
. "\<DMIN> \<DMAX> date filter on the updated_on (moddate of table record)"
);
}
@@ -63,6 +66,7 @@ class DataboxContentCommand extends AbstractReportCommand
]
))
->setCollIds($collIds)
->setPermalink($input->getOption('permalink'))
;
}
}

View File

@@ -2,7 +2,6 @@
namespace Alchemy\Phrasea\Command\Report;
use Alchemy\Phrasea\Report\Report;
use Alchemy\Phrasea\Report\ReportDownloads;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@@ -20,6 +19,7 @@ class DownloadsCommand extends AbstractReportCommand
->setDescription('BETA - Get all downloads report')
->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 ""')
->setHelp(
"eg: bin/report downloads:all --databox_id 2 --email 'admin@alchemy.fr' --dmin '2022-12-01' --dmax '2023-01-01' --type 'user' \n"
@@ -37,6 +37,7 @@ class DownloadsCommand extends AbstractReportCommand
{
$type = $input->getOption('type');
$collectionIds = $input->getOption('collection_id');
$permalink = $input->getOption('permalink');
if (!empty($type) && !in_array($type, self::TYPES)) {
$output->writeln("<error>wrong '--type' option (--help for available value)</error>");
@@ -44,6 +45,12 @@ class DownloadsCommand extends AbstractReportCommand
return 1;
}
if (!empty($permalink) && $type == 'user') {
$output->writeln("<error>--permalink is not used with type=user </error>");
return 1;
}
$databox = $this->findDbOr404($this->sbasId);
$collIds = [];
@@ -71,6 +78,7 @@ class DownloadsCommand extends AbstractReportCommand
))
->setAppKey($this->container['conf']->get(['main', 'key']))
->setCollIds($collIds)
->setPermalink($permalink)
;
}
}

View File

@@ -22,6 +22,7 @@ class ReportDownloads extends Report
private $acl;
private $collIds = null;
private $permalink = null;
/* those vars will be set once by computeVars() */
private $name = null;
@@ -33,6 +34,11 @@ class ReportDownloads extends Report
public function getColumnTitles()
{
$this->computeVars();
// only for group downloads all and download by record
if (($this->parms['group'] === null || $this->parms['group'] == 'record') && !empty($this->permalink)) {
$this->columnTitles[] = 'permalink_' . $this->permalink;
}
return $this->columnTitles;
}
@@ -69,11 +75,30 @@ class ReportDownloads extends Report
return $this;
}
public function setPermalink($permalink)
{
$this->permalink = $permalink;
return $this;
}
public function getAllRows($callback)
{
$this->computeVars();
$stmt = $this->databox->get_connection()->executeQuery($this->sql, []);
while (($row = $stmt->fetch())) {
// only for group downloads all and download by record
if (($this->parms['group'] === null || $this->parms['group'] == 'record') && !empty($this->permalink)) {
try {
$record = $this->databox->get_record($row['record_id']);
$permalinkUrl = $record->get_subdef($this->permalink)->get_permalink()->get_url()->__toString();
} catch (\Exception $e) {
// the record or subdef is not found
$permalinkUrl = '';
}
$row['permalink_' . $this->permalink] = $permalinkUrl;
}
$callback($row);
}
$stmt->closeCursor();

View File

@@ -23,11 +23,16 @@ class ReportRecords extends Report
private $sqlColSelect = null;
private $columnTitles = null;
private $keyName = null;
private $permalink = null;
public function getColumnTitles()
{
$this->computeVars();
if (!empty($this->permalink)) {
$this->columnTitles[] = 'permalink_' . $this->permalink;
}
return $this->columnTitles;
}
@@ -57,6 +62,13 @@ class ReportRecords extends Report
return $this;
}
public function setPermalink($permalink)
{
$this->permalink = $permalink;
return $this;
}
public function getAllRows($callback)
{
$this->computeVars();
@@ -71,7 +83,7 @@ class ReportRecords extends Report
$stmt->closeCursor();
if($row && !is_null($row['from']) && !is_null($row['to'])) {
$sql = "SELECT r.record_id, c.asciiname, r.moddate AS updated_on, r.credate AS created_on, r.mime, r.type, r.originalname,\n"
$sql = "SELECT r.record_id, c.asciiname, r.credate AS created_on, r.moddate AS updated_on, r.mime, r.type, r.originalname, r.status, r.sha256, r.uuid,\n"
. $this->sqlColSelect . "\n"
. "FROM (`record` AS `r` LEFT JOIN `coll` AS `c` USING(`coll_id`)) LEFT JOIN `metadatas` AS `m` USING(`record_id`)\n"
. "WHERE " . $this->sqlWhere . "\n"
@@ -82,6 +94,21 @@ class ReportRecords extends Report
$rows = $stmt->fetchAll();
$stmt->closeCursor();
foreach($rows as $row) {
if (!empty($this->permalink)) {
try {
$record = $this->databox->get_record($row['record_id']);
$permalinkUrl = $record->get_subdef($this->permalink)->get_permalink()->get_url()->__toString();
} catch (\Exception $e) {
// the record or subdef is not found
$permalinkUrl = '';
} catch (\Throwable $e) {
// there is no permalink created ???
$permalinkUrl = '';
}
$row['permalink_' . $this->permalink] = $permalinkUrl;
}
$callback($row);
$lastRid = $row['record_id'];
}
@@ -101,7 +128,7 @@ class ReportRecords extends Report
// pivot-like query on metadata fields
$this->sqlColSelect = [];
$this->columnTitles = ['record_id', 'collection', 'updated_on', 'created_on', 'mime', 'type', 'originalname'];
$this->columnTitles = ['record_id', 'collection', 'created_on', 'updated_on', 'mime', 'type', 'originalname', 'status', 'sha256', 'uuid'];
foreach($this->getDatabox()->get_meta_structure() as $field) {
// skip the fields that can't be reported
if(!$field->is_report() || (isset($this->acl) && $field->isBusiness() && !$this->acl->can_see_business_fields($this->getDatabox()))) {