Skip to content

Commit

Permalink
details & health
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtificialOwl committed Oct 11, 2021
1 parent 40e6781 commit 80cb94a
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/Command/ExternalRemove.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$storageId = (int)$input->getArgument('storage_id');

try {
$this->externalFolderRequest->getFromStorageId($storageId);
$this->externalFolderRequest->getByStorageId($storageId);
} catch (ExternalFolderNotFoundException $e) {
throw new ExternalFolderNotFoundException('Unknown external folder');
}
Expand Down
52 changes: 36 additions & 16 deletions lib/Command/PointDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,29 @@
use ArtificialOwl\MySmallPhpTools\Traits\TStringTools;
use OC\Core\Command\Base;
use OCA\Backup\Exceptions\ArchiveNotFoundException;
use OCA\Backup\Exceptions\ExternalFolderNotFoundException;
use OCA\Backup\Exceptions\RemoteInstanceException;
use OCA\Backup\Exceptions\RemoteInstanceNotFoundException;
use OCA\Backup\Exceptions\RemoteResourceNotFoundException;
use OCA\Backup\Exceptions\RestoringChunkPartNotFoundException;
use OCA\Backup\Exceptions\RestoringPointException;
use OCA\Backup\Exceptions\RestoringPointNotFoundException;
use OCA\Backup\Exceptions\RestoringPointPackException;
use OCA\Backup\Model\RestoringChunk;
use OCA\Backup\Model\RestoringData;
use OCA\Backup\Model\RestoringPoint;
use OCA\Backup\Service\ChunkService;
use OCA\Backup\Service\ExternalFolderService;
use OCA\Backup\Service\PackService;
use OCA\Backup\Service\PointService;
use OCA\Backup\Service\RemoteService;
use OCP\Files\GenericFileException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -74,6 +81,9 @@ class PointDetails extends Base {
/** @var PointService */
private $pointService;

/** @var ExternalFolderService */
private $externalFolderService;

/** @var ChunkService */
private $chunkService;

Expand All @@ -86,19 +96,22 @@ class PointDetails extends Base {
*
* @param RemoteService $remoteService
* @param PointService $pointService
* @param ExternalFolderService $externalFolderService
* @param ChunkService $chunkService
* @param PackService $packService
*/
public function __construct(
RemoteService $remoteService,
PointService $pointService,
ExternalFolderService $externalFolderService,
ChunkService $chunkService,
PackService $packService
) {
parent::__construct();

$this->remoteService = $remoteService;
$this->pointService = $pointService;
$this->externalFolderService = $externalFolderService;
$this->chunkService = $chunkService;
$this->packService = $packService;
}
Expand All @@ -113,7 +126,8 @@ protected function configure() {
$this->setName('backup:point:details')
->setDescription('Details on a restoring point')
->addArgument('pointId', InputArgument::REQUIRED, 'Id of the restoring point')
->addArgument('instance', InputArgument::OPTIONAL, 'address of the remote instance');
->addOption('remote', '', InputOption::VALUE_REQUIRED, 'address of the remote instance')
->addOption('external', '', InputOption::VALUE_REQUIRED, 'id of the external folder');
}


Expand All @@ -128,30 +142,36 @@ protected function configure() {
* @throws RemoteInstanceNotFoundException
* @throws RemoteResourceNotFoundException
* @throws RestoringPointNotFoundException
* @throws ExternalFolderNotFoundException
* @throws RestoringChunkPartNotFoundException
* @throws RestoringPointException
* @throws RestoringPointPackException
* @throws GenericFileException
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$pointId = $input->getArgument('pointId');
$instance = $input->getArgument('instance');

if ($instance) {
$point = $this->remoteService->getRestoringPoint($instance, $pointId, true);
$output->writeln(json_encode($point, JSON_PRETTY_PRINT));

return 0;
$remote = $input->getOption('remote');
$external = $input->getOption('external');

if ($remote) {
$point = $this->remoteService->getRestoringPoint($remote, $pointId, true);
} else if ($external) {
$externalFolder = $this->externalFolderService->getByStorageId((int)$external);
$point = $this->externalFolderService->getRestoringPoint($externalFolder, $pointId, true);
} else {
$point = $this->pointService->getLocalRestoringPoint($pointId);
$this->pointService->generateHealth($point, true);
// $this->pointService->initBaseFolder($point);
}


$point = $this->pointService->getLocalRestoringPoint($pointId);
$this->pointService->initBaseFolder($point);

if ($input->getOption('output') === 'json') {
$this->pointService->generateHealth($point, true);
$output->writeln(json_encode($point) . "\n");
$output->writeln(json_encode($point, JSON_PRETTY_PRINT));
// $output->writeln(json_encode($point) . "\n");
$output->writeln(json_encode($point, JSON_PRETTY_PRINT)) . "\n";

return 0;
}

echo json_encode($point);
$output = new ConsoleOutput();
$output = $output->section();

Expand All @@ -175,7 +195,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$table = new Table($output);
$table->setHeaders(['Chunk Id', 'Size', 'Count', 'Part Id', 'Checksum', 'verified']);
$table->render();

echo '-';
foreach ($data->getChunks() as $chunk) {
if ($point->isStatus(RestoringPoint::STATUS_PACKED)) {
$this->displayDetailsPacked($table, $point, $chunk);
Expand Down
87 changes: 72 additions & 15 deletions lib/Command/PointList.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Deserialize;
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
use OC\Core\Command\Base;
use OCA\Backup\Exceptions\ExternalFolderNotFoundException;
use OCA\Backup\Exceptions\RemoteInstanceException;
use OCA\Backup\Exceptions\RemoteInstanceNotFoundException;
use OCA\Backup\Exceptions\RemoteResourceNotFoundException;
use OCA\Backup\Model\ExternalFolder;
use OCA\Backup\Model\RemoteInstance;
use OCA\Backup\Model\RestoringHealth;
use OCA\Backup\Model\RestoringPoint;
use OCA\Backup\Service\ExternalFolderService;
use OCA\Backup\Service\PointService;
use OCA\Backup\Service\RemoteService;
use OCA\Backup\Service\RemoteStreamService;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -71,6 +74,9 @@ class PointList extends Base {
/** @var RemoteService */
private $remoteService;

/** @var ExternalFolderService */
private $externalFolderService;

/** @var RemoteStreamService */
private $remoteStreamService;

Expand All @@ -80,15 +86,18 @@ class PointList extends Base {
*
* @param PointService $pointService
* @param RemoteService $remoteService
* @param ExternalFolderService $externalFolderService
* @param RemoteStreamService $remoteStreamService
*/
public function __construct(
PointService $pointService,
RemoteService $remoteService,
ExternalFolderService $externalFolderService,
RemoteStreamService $remoteStreamService
) {
$this->pointService = $pointService;
$this->remoteService = $remoteService;
$this->externalFolderService = $externalFolderService;
$this->remoteStreamService = $remoteStreamService;

parent::__construct();
Expand All @@ -101,9 +110,17 @@ public function __construct(
protected function configure() {
$this->setName('backup:point:list')
->setDescription('List restoring point')
->addArgument(
'instance', InputArgument::OPTIONAL,
'list restoring point from a specific instance (or local)', ''
->addOption(
'local', '', InputOption::VALUE_NONE,
'list restoring point from local only'
)
->addOption(
'remote', '', InputOption::VALUE_REQUIRED,
'list restoring point from a remote instance (or local)', ''
)
->addOption(
'external', '', InputOption::VALUE_REQUIRED,
'list restoring point from an external folder', ''
);
}

Expand All @@ -118,13 +135,18 @@ protected function configure() {
* @throws RemoteResourceNotFoundException
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$rp = $this->getRPFromInstances($output, $input->getArgument('instance'));
$rp = $this->getRPFromInstances(
$output,
$input->getOption('local'),
$input->getOption('remote'),
$input->getOption('external')
);

$output = new ConsoleOutput();
$output = $output->section();

$table = new Table($output);
$table->setHeaders(['Restoring Point', 'Date', 'Parent', 'Instance', 'Health']);
$table->setHeaders(['Restoring Point', 'Date', 'Status', 'Parent', 'Instance', 'Health']);
$table->render();

foreach ($rp as $pointId => $item) {
Expand All @@ -139,10 +161,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$displayPointId = '<options=bold>' . $pointId . '</>';
}

$status = [];
if ($point->getStatus() === 0) {
$status[] = 'not packed';
} else {
foreach (RestoringPoint::$DEF_STATUS as $k => $v) {
if ($point->isStatus($k)) {
$status[] = $v;
}
}
}
$table->appendRow(
[
($fresh) ? $displayPointId : '',
($fresh) ? date('Y-m-d H:i:s', $point->getDate()) : '',
($fresh) ? implode(',', $status) : '',
($fresh) ? $point->getParent() : '',
$instance,
$this->displayStyleHealth($point),
Expand All @@ -160,39 +193,63 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/**
* @param OutputInterface $output
* @param string $instance
* @param bool $local
* @param string $remote
* @param string $external
*
* @return array
*/
private function getRPFromInstances(OutputInterface $output, string $instance = ''): array {
if ($instance === '') {
private function getRPFromInstances(OutputInterface $output, bool $local, string $remote, string $external
): array {
if ($local) {
$instances = [RemoteInstance::LOCAL];
} else if ($remote !== '') {
$instances = ['remote:' . $remote];
} else if ($external !== '') {
$instances = ['external:' . $external];
} else {
$instances = array_merge(
[RemoteInstance::LOCAL],
array_map(
function (RemoteInstance $remoteInstance): ?string {
return $remoteInstance->getInstance();
function (RemoteInstance $remoteInstance): string {
return 'remote:' . $remoteInstance->getInstance();
}, $this->remoteService->getOutgoing()
),
array_map(
function (ExternalFolder $externalFolder): string {
return 'external:' . $externalFolder->getStorageId();
}, $this->externalFolderService->getAll()
)
);
} else {
$instances = [$instance];
}

$points = $dates = [];
foreach ($instances as $instance) {
$output->writeln('- retreiving data from <info>' . $instance . '</info>');

$list = [];
try {
if ($instance === RemoteInstance::LOCAL) {
$list = $this->pointService->getLocalRestoringPoints();
} else {
$list = $this->remoteService->getRestoringPoints($instance);

[$source, $id] = explode(':', $instance, 2);
if ($source === 'remote') {
$list = $this->remoteService->getRestoringPoints($id);
} else if ($source === 'external') {
try {
$external = $this->externalFolderService->getByStorageId((int)$id);
$list = $this->externalFolderService->getRestoringPoints($external);
} catch (ExternalFolderNotFoundException $e) {
}
}
}
} catch (RemoteInstanceException
| RemoteInstanceNotFoundException
| RemoteResourceNotFoundException $e) {
continue;
}

foreach ($list as $item) {
$output->writeln(' > found RestoringPoint <info>' . $item->getId() . '</info>');
if (!array_key_exists($item->getId(), $points)) {
Expand Down Expand Up @@ -252,7 +309,7 @@ private function orderByDate(array $points, array $dates): array {
* @return string
*/
private function displayStyleHealth(RestoringPoint $point): string {
if ($point->getInstance() === '') {
if (!$point->hasHealth()) {
return 'not checked';
}

Expand Down
9 changes: 9 additions & 0 deletions lib/Command/SetupExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCA\Backup\Service\ConfigService;
use OCA\Backup\Service\EncryptService;
use OCA\Backup\Service\RemoteService;
use OCA\Backup\Service\RemoteStreamService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -54,6 +55,9 @@ class SetupExport extends Base {
/** @var RemoteService */
private $remoteService;

/** @var RemoteStreamService */
private $remoteStreamService;

/** @var EncryptService */
private $encryptService;

Expand All @@ -65,17 +69,20 @@ class SetupExport extends Base {
* SetupExport constructor.
*
* @param RemoteService $remoteService
* @param RemoteStreamService $remoteStreamService
* @param EncryptService $encryptService
* @param ConfigService $configService
*/
public function __construct(
RemoteService $remoteService,
RemoteStreamService $remoteStreamService,
EncryptService $encryptService,
ConfigService $configService
) {
parent::__construct();

$this->remoteService = $remoteService;
$this->remoteStreamService = $remoteStreamService;
$this->encryptService = $encryptService;
$this->configService = $configService;
}
Expand All @@ -99,6 +106,8 @@ protected function configure() {
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->remoteStreamService->getAppSignatory(true);

$setup = [
'signatory' => $this->configService->getAppValue('key_pairs'),
'remote' => $this->remoteService->getAll(true),
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/ExternalFolderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getById(int $mountId): ExternalFolder {
* @return ExternalFolder
* @throws ExternalFolderNotFoundException
*/
public function getFromStorageId(int $storageId): ExternalFolder {
public function getByStorageId(int $storageId): ExternalFolder {
$qb = $this->getExternalFolderSelectSql();
$qb->limitInt('storage_id', $storageId);

Expand Down

0 comments on commit 80cb94a

Please sign in to comment.