Skip to content

Commit cd106ec

Browse files
committed
activity on restore
1 parent 501e9f1 commit cd106ec

File tree

8 files changed

+123
-26
lines changed

8 files changed

+123
-26
lines changed

appinfo/info.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
</settings>
5454

5555
<activity>
56-
<!-- <settings>-->
57-
<!-- <setting>OCA\Backup\Activity\GlobalSetting</setting>-->
58-
<!-- </settings>-->
56+
<settings>
57+
<setting>OCA\Backup\Activity\GlobalSetting</setting>
58+
</settings>
5959
<filters>
6060
<filter>OCA\Backup\Activity\Filter</filter>
6161
</filters>

lib/Activity/Filter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434

3535
use OCA\Backup\AppInfo\Application;
36+
use OCA\Backup\Service\ActivityService;
3637
use OCP\Activity\IFilter;
3738
use OCP\IL10N;
3839
use OCP\IURLGenerator;
@@ -95,7 +96,7 @@ public function getPriority(): int {
9596
* @return string[]
9697
*/
9798
public function filterTypes(array $types): array {
98-
return $types;
99+
return array_merge($types, [ActivityService::TYPE_GLOBAL]);
99100
}
100101

101102

lib/Activity/Provider.php

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ public function __construct(
9292
*/
9393
public function parse($lang, IEvent $event, IEvent $previousEvent = null): IEvent {
9494
$params = $event->getSubjectParameters();
95-
\OC::$server->getLogger()->log(3, json_encode($params));
9695
$this->initActivityParser($event, $params);
97-
9896
$this->setIcon($event);
9997

10098
if ($event->getType() !== ActivityService::TYPE_GLOBAL) {
@@ -105,6 +103,15 @@ public function parse($lang, IEvent $event, IEvent $previousEvent = null): IEven
105103
case ActivityService::CREATE;
106104
$this->parseCreate($event, $params);
107105
break;
106+
107+
case ActivityService::RESTORE;
108+
$this->parseRestore($event, $params);
109+
break;
110+
111+
case ActivityService::RESTORE_FILE;
112+
$this->parseRestoreFile($event, $params);
113+
break;
114+
108115
}
109116

110117
return $event;
@@ -146,26 +153,63 @@ private function setIcon(IEvent $event): void {
146153
* @param array $params
147154
*/
148155
private function parseCreate(IEvent $activity, array $params): void {
149-
$params['downtime'] = $this->getDateDiff(
150-
$this->getInt('duration', $params),
151-
0,
152-
false,
153-
[
154-
'seconds' => $this->l10n->t('seconds'),
155-
'minutes' => $this->l10n->t('minutes'),
156-
'hours' => $this->l10n->t('hours'),
157-
'days' => $this->l10n->t('days')
158-
]
159-
);
156+
$params['type'] = ($this->getBool('complete', $params)) ?
157+
$this->l10n->t('complete') :
158+
$this->l10n->t('partial');
159+
160+
try {
161+
$params['downtime'] = $this->getDateDiff(
162+
$this->getInt('duration', $params),
163+
0,
164+
false,
165+
[
166+
'seconds' => $this->l10n->t('seconds'),
167+
'minutes' => $this->l10n->t('minutes'),
168+
'hours' => $this->l10n->t('hours'),
169+
'days' => $this->l10n->t('days')
170+
]
171+
);
172+
} catch (\Exception $e) {
173+
}
174+
160175
$this->parseSimpleEvent(
161176
$activity,
162-
'A new restoring point have been generated, ' .
177+
'A new restoring point ({type}) have been generated, ' .
163178
'requiring maintenance mode for {downtime}. (id: {id}, status: {status})',
164179
$params
165180
);
166181
}
167182

168183

184+
/**
185+
* @param IEvent $activity
186+
* @param array $params
187+
*/
188+
private function parseRestore(IEvent $activity, array $params): void {
189+
$this->readableRewing($params);
190+
191+
$this->parseSimpleEvent(
192+
$activity,
193+
'Your system have been fully restored based on a restoring point from {date} (estimated rewind: {rewind})',
194+
$params
195+
);
196+
}
197+
198+
199+
/**
200+
* @param IEvent $activity
201+
* @param array $params
202+
*/
203+
private function parseRestoreFile(IEvent $activity, array $params): void {
204+
$this->readableRewing($params);
205+
206+
$this->parseSimpleEvent(
207+
$activity,
208+
'The file {file} have restored based on a restoring point from {date} (estimated rewind: {rewind})',
209+
$params
210+
);
211+
}
212+
169213
/**
170214
* @param IEvent $activity
171215
* @param string $global
@@ -192,4 +236,27 @@ protected function setSubject(IEvent $event, string $line) {
192236
$event->setRichSubject($line);
193237
}
194238

239+
240+
/**
241+
* @param array $params
242+
*/
243+
private function readableRewing(array &$params): void {
244+
$params['date'] = date('Y-m-d H:i:s', $this->getInt('date', $params));
245+
246+
try {
247+
$params['rewind'] = $this->getDateDiff(
248+
$this->getInt('rewind', $params),
249+
0,
250+
false,
251+
[
252+
'seconds' => $this->l10n->t('seconds'),
253+
'minutes' => $this->l10n->t('minutes'),
254+
'hours' => $this->l10n->t('hours'),
255+
'days' => $this->l10n->t('days')
256+
]
257+
);
258+
} catch (\Exception $e) {
259+
}
260+
}
261+
195262
}

lib/AppInfo/Application.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
namespace OCA\Backup\AppInfo;
3333

3434

35-
use OCA\Backup\Activity\GlobalSetting;
35+
use OCA\Backup\Activity\Filter;
3636
use OCA\Backup\Handlers\WebfingerHandler;
3737
use OCA\Backup\Listeners\NodeEvent;
3838
use OCP\Activity\IManager as IActivityManager;
@@ -43,6 +43,8 @@
4343
use OCP\Files\Events\Node\NodeCreatedEvent;
4444
use OCP\Files\Events\Node\NodeRenamedEvent;
4545
use OCP\Files\Events\Node\NodeWrittenEvent;
46+
use OCP\IGroupManager;
47+
use OCP\IUserSession;
4648

4749

4850
require_once __DIR__ . '/../../vendor/autoload.php';
@@ -88,10 +90,6 @@ public function register(IRegistrationContext $context): void {
8890
* @param IBootContext $context
8991
*/
9092
public function boot(IBootContext $context): void {
91-
/** @var IActivityManager $activityManager */
92-
$activityManager = $context->getServerContainer()->get(IActivityManager::class);
93-
94-
$activityManager->registerSetting(GlobalSetting::class);
9593
}
9694

9795
}

lib/Command/PointRestore.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use OCA\Backup\Model\RestoringData;
5050
use OCA\Backup\Model\RestoringHealth;
5151
use OCA\Backup\Model\RestoringPoint;
52+
use OCA\Backup\Service\ActivityService;
5253
use OCA\Backup\Service\ChunkService;
5354
use OCA\Backup\Service\ConfigService;
5455
use OCA\Backup\Service\FilesService;
@@ -89,6 +90,9 @@ class PointRestore extends Base {
8990
/** @var RestoreService */
9091
private $restoreService;
9192

93+
/** @var ActivityService */
94+
private $activityService;
95+
9296
/** @var ConfigService */
9397
private $configService;
9498

@@ -110,6 +114,7 @@ class PointRestore extends Base {
110114
* @param ChunkService $chunkService
111115
* @param FilesService $filesService
112116
* @param RestoreService $restoreService
117+
* @param ActivityService $activityService
113118
* @param ConfigService $configService
114119
* @param OutputService $outputService
115120
*/
@@ -118,6 +123,7 @@ public function __construct(
118123
ChunkService $chunkService,
119124
FilesService $filesService,
120125
RestoreService $restoreService,
126+
ActivityService $activityService,
121127
ConfigService $configService,
122128
OutputService $outputService
123129
) {
@@ -127,6 +133,7 @@ public function __construct(
127133
$this->chunkService = $chunkService;
128134
$this->filesService = $filesService;
129135
$this->restoreService = $restoreService;
136+
$this->activityService = $activityService;
130137
$this->configService = $configService;
131138
$this->outputService = $outputService;
132139
}
@@ -226,6 +233,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
226233
$this->restorePointComplete($point);
227234
$this->configService->maintenanceMode(false);
228235

236+
$this->activityService->newActivity(
237+
ActivityService::RESTORE,
238+
[
239+
'id' => $point->getId(),
240+
'date' => $point->getDate(),
241+
'rewind' => time() - $point->getDate()
242+
]
243+
);
244+
229245
return 0;
230246
}
231247

@@ -263,7 +279,8 @@ public function restorePointComplete(RestoringPoint $point): void {
263279

264280
foreach ($data->getChunks() as $chunk) {
265281
$this->output->write(
266-
' > Chunk: ' . $chunk->getPath() . $chunk->getFilename() . ' (' . $chunk->getCount() . ' files) '
282+
' > Chunk: ' . $chunk->getPath() . $chunk->getFilename() . ' (' . $chunk->getCount()
283+
. ' files) '
267284
);
268285

269286
try {
@@ -375,6 +392,16 @@ private function restoreUniqueFile(
375392
$this->chunkService->restoreUniqueFile($point, $chunk, $root, $file->getName());
376393
$this->output->writeln('<info>ok</info>');
377394

395+
$this->activityService->newActivity(
396+
ActivityService::RESTORE_FILE,
397+
[
398+
'id' => $point->getId(),
399+
'file' => $file->getName(),
400+
'date' => $point->getDate(),
401+
'rewind' => time() - $point->getDate()
402+
]
403+
);
404+
378405
// include restored file in next incremental backup
379406
$changedFile = new ChangedFile($file->getName());
380407
$this->filesService->changedFile($changedFile);

lib/Service/ActivityService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class ActivityService {
4949

5050
const TYPE_GLOBAL = 'backup_global';
5151
const CREATE = 'backup_create';
52+
const RESTORE = 'backup_restore';
53+
const RESTORE_FILE = 'backup_restore_file';
5254

5355
const LIMIT_TO_GROUP = 'admin';
5456

lib/Service/ChunkService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,11 @@ public function searchFileInChunk(RestoringPoint $point, RestoringChunk $chunk,
830830
$this->listFilesFromChunk($point, $chunk);
831831
}
832832

833+
$search = strtolower($search);
833834
return array_filter(
834835
array_map(
835836
function (ArchiveFile $file) use ($search): ?ArchiveFile {
836-
if (strpos($file->getName(), $search) !== false) {
837+
if (strpos(strtolower($file->getName()), $search) !== false) {
837838
return $file;
838839
}
839840

lib/Service/PointService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ public function create(bool $complete): RestoringPoint {
247247
'backup_create', [
248248
'id' => $point->getId(),
249249
'duration' => time() - $initTime,
250-
'status' => $point->getStatus()
250+
'status' => $point->getStatus(),
251+
'complete' => $complete
251252
]
252253
);
253254

0 commit comments

Comments
 (0)