This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "spiral/snapshotter", | ||
"description": "Snapshot component with ability to view, delete and aggregate snapshots.", | ||
"authors": [ | ||
{ | ||
"name": "Valentin V / vvval", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"spiral/vault": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Spiral\\": "source/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
<?php | ||
namespace Spiral\Snapshotter\Controllers; | ||
|
||
use Spiral\Core\Controller; | ||
use Spiral\Http\Exceptions\ClientExceptions\ForbiddenException; | ||
use Spiral\Http\Exceptions\ClientExceptions\NotFoundException; | ||
use Spiral\Security\Traits\AuthorizesTrait; | ||
use Spiral\Translator\Traits\TranslatorTrait; | ||
use Spiral\Snapshotter\Database\Aggregation; | ||
use Spiral\Snapshotter\Database\Snapshot; | ||
use Spiral\Snapshotter\Database\Sources\AggregationSource; | ||
use Spiral\Snapshotter\Database\Sources\SnapshotSource; | ||
use Spiral\Snapshotter\Models\AggregationService; | ||
use Spiral\Snapshotter\Models\Statistics; | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: Valentin | ||
* Date: 09.02.2016 | ||
* Time: 17:47 | ||
*/ | ||
class SnapshotsController extends Controller | ||
{ | ||
use AuthorizesTrait, TranslatorTrait; | ||
|
||
const GUARD_NAMESPACE = 'keeper.vault.snapshots'; | ||
|
||
/** | ||
* @param AggregationSource $source | ||
* @param Statistics $statistics | ||
* @return mixed | ||
*/ | ||
public function indexAction(AggregationSource $source, Statistics $statistics) | ||
{ | ||
//todo filter by active (has snapshots), all - NOW filter is hardcoded | ||
//todo graph | ||
return $this->views->render('keeper:vault/snapshots/list', [ | ||
'source' => $source->findWithSnapshots()->orderBy('last_occurred_time', 'DESC'), | ||
'lastSnapshot' => $source->findLast(), | ||
'statistics' => $statistics | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param AggregationService $aggregationService | ||
* @param SnapshotSource $snapshotSource | ||
* @return mixed | ||
*/ | ||
public function editAction( | ||
$id, | ||
AggregationService $aggregationService, | ||
SnapshotSource $snapshotSource | ||
) { | ||
//todo graph | ||
/** | ||
* @var Aggregation $aggregation | ||
*/ | ||
$aggregation = $aggregationService->getSource()->findByPK($id); | ||
if (empty($aggregation)) { | ||
throw new NotFoundException; | ||
} | ||
|
||
$this->authorize('view', compact('aggregation')); | ||
|
||
return $this->views->render('keeper:vault/snapshots/aggregation', [ | ||
'source' => $snapshotSource->findStored($aggregation)->orderBy('id', 'DESC'), | ||
'aggregation' => $aggregation | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param AggregationSource $source | ||
* @return array | ||
*/ | ||
public function suppressAction($id, AggregationSource $source) | ||
{ | ||
/** | ||
* @var Aggregation $aggregation | ||
*/ | ||
$aggregation = $source->findByPK($id); | ||
if (empty($aggregation)) { | ||
throw new NotFoundException; | ||
} | ||
|
||
$this->authorize('edit', compact('aggregation')); | ||
|
||
$aggregation->setSuppression($this->input->data('suppression', false)); | ||
$source->save($aggregation); | ||
|
||
return [ | ||
'status' => 200, | ||
'message' => $this->say('Suppression status updated.') | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param SnapshotSource $source | ||
* @return mixed | ||
*/ | ||
public function snapshotAction($id, SnapshotSource $source) | ||
{ | ||
$snapshot = $source->findByPK($id); | ||
if (empty($snapshot)) { | ||
throw new NotFoundException; | ||
} | ||
|
||
$this->authorize('view', compact('snapshot')); | ||
|
||
return $this->views->render('keeper:vault/snapshots/snapshot', compact('snapshot')); | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param SnapshotSource $source | ||
* @return string | ||
*/ | ||
public function iframeAction($id, SnapshotSource $source) | ||
{ | ||
$snapshot = $source->findByPK($id); | ||
if (empty($snapshot)) { | ||
throw new NotFoundException; | ||
} | ||
|
||
$this->authorize('view', compact('snapshot')); | ||
|
||
try { | ||
return file_get_contents($snapshot->filename); | ||
} catch (\Exception $exception) { | ||
throw new NotFoundException; | ||
} | ||
} | ||
|
||
/** | ||
* @param AggregationService $aggregationService | ||
* @param AggregationSource $aggregationSource | ||
* @param SnapshotSource $snapshotSource | ||
* @return array | ||
*/ | ||
public function removeAllAction( | ||
AggregationService $aggregationService, | ||
AggregationSource $aggregationSource, | ||
SnapshotSource $snapshotSource | ||
) { | ||
$this->authorize('remove'); | ||
|
||
foreach ($aggregationSource->find() as $aggregation) { | ||
$countDeleted = 0; | ||
if (!empty($snapshotSource->findStored($aggregation)->count())) { | ||
foreach ($snapshotSource->findStored($aggregation) as $snapshot) { | ||
$countDeleted++; | ||
$snapshotSource->delete($snapshot); | ||
} | ||
} | ||
|
||
if (!empty($countDeleted)) { | ||
$aggregationService->deleteSnapshots($aggregation, $countDeleted); | ||
$aggregationSource->save($aggregation); | ||
} | ||
} | ||
|
||
return [ | ||
'status' => 200, | ||
'message' => $this->say('Snapshot deleted.'), | ||
'action' => [ | ||
'redirect' => $this->vault->uri('snapshots') | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param AggregationService $aggregationService | ||
* @param SnapshotSource $snapshotSource | ||
* @return array | ||
*/ | ||
public function removeSnapshotsAction( | ||
$id, | ||
AggregationService $aggregationService, | ||
SnapshotSource $snapshotSource | ||
) { | ||
/** | ||
* @var Aggregation $aggregation | ||
*/ | ||
$aggregation = $aggregationService->getSource()->findByPK($id); | ||
if (empty($aggregation)) { | ||
throw new NotFoundException; | ||
} | ||
|
||
$this->authorize('remove', compact('aggregation')); | ||
|
||
$countDeleted = 0; | ||
if (!empty($snapshotSource->findStored($aggregation)->count())) { | ||
foreach ($snapshotSource->findStored($aggregation) as $snapshot) { | ||
$countDeleted++; | ||
$snapshotSource->delete($snapshot); | ||
} | ||
} | ||
|
||
if (!empty($countDeleted)) { | ||
$aggregationService->deleteSnapshots($aggregation, $countDeleted); | ||
$aggregationService->getSource()->save($aggregation); | ||
} | ||
|
||
return [ | ||
'status' => 200, | ||
'message' => $this->say('Snapshot deleted.'), | ||
'action' => [ | ||
'redirect' => $this->vault->uri('snapshots:edit', ['id' => $aggregation->id]) | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $id | ||
* @param SnapshotSource $snapshotSource | ||
* @param AggregationService $aggregationService | ||
* @return array | ||
*/ | ||
public function removeSnapshotAction( | ||
$id, | ||
SnapshotSource $snapshotSource, | ||
AggregationService $aggregationService | ||
) { | ||
/** | ||
* @var Snapshot $snapshot | ||
* @var Aggregation $aggregation | ||
*/ | ||
$snapshot = $snapshotSource->findByPK($id); | ||
if (empty($snapshot)) { | ||
throw new NotFoundException; | ||
} | ||
|
||
if (!$snapshot->stored()) { | ||
throw new ForbiddenException; | ||
} | ||
|
||
$aggregation = $aggregationService->getSource()->findBySnapshot($snapshot); | ||
if (empty($aggregation)) { | ||
throw new NotFoundException; | ||
} | ||
|
||
$this->authorize('remove', compact('aggregation', 'snapshot')); | ||
|
||
$aggregationService->deleteSnapshots($aggregation, 1); | ||
$aggregationService->getSource()->save($aggregation); | ||
|
||
$snapshotSource->delete($snapshot); | ||
|
||
return [ | ||
'status' => 200, | ||
'message' => $this->say('Snapshot deleted.'), | ||
'action' => [ | ||
'redirect' => $this->vault->uri('snapshots:edit', ['id' => $aggregation->id]) | ||
] | ||
]; | ||
} | ||
} |
Oops, something went wrong.