- WIP on Cronard tasks
- Added some minors features to the API web interface
This commit is contained in:
1 parent
2d560a0857
commit
a54930b6c6
10 files changed
+105
-31
No files matched your search
+43
-11
@@ -2,32 +2,64 @@
|
||||
|
||||
namespace Lean\Api\Cronard;
|
||||
|
||||
use CSSLSJ\TransportScolaire\Api\{Controller\Api, Entity, Form, Lib};
|
||||
use Notes\Breadcrumb\Attribute\Method\Breadcrumb;
|
||||
use Lean\Api\{Controller, Entity, Form, Lib};
|
||||
use DI\Attribute\Inject;
|
||||
use Notes\Cronard\Attribute\Method\Cronard;
|
||||
use Notes\Route\Attribute\Method\Route;
|
||||
use Notes\Route\Attribute\Object\Route as RouteObj;
|
||||
use Notes\Security\Attribute\Taxus;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
|
||||
use Storage\{Cookie, Session};
|
||||
use TheBugs\Email\MailerInterface;
|
||||
use function CSSLSJ\TransportScolaire\Api\View\{form, route};
|
||||
use ZipArchive;
|
||||
|
||||
##[Taxus(Entity\UserTypeEnum::Developer)]
|
||||
#[RouteObj(base: "/lean/cronard")]
|
||||
class System {
|
||||
use Lib\ControllerTrait;
|
||||
|
||||
#[Route("/", name: "lean-api.cronard:documentation", method: "GET", description: "Route de documentation pour cette section")]
|
||||
protected int $weekRetention = 4;
|
||||
|
||||
#[Inject]
|
||||
protected ContainerInterface $container;
|
||||
|
||||
#[Route("/", name: "lean-api.cronard:documentation", method: "GET", description: "Documentation route for ")]
|
||||
public function documentation(ServerRequestInterface $request, array $arguments) : ResponseInterface
|
||||
{
|
||||
return $this->renderMarkdown(getenv("PROJECT_PATH") . "/meta/docs/cronard.md", [ ], []);
|
||||
return $this->renderMarkdown(LEAN_API_PROJECT_PATH . "/meta/docs/cronard.md", [ ], []);
|
||||
}
|
||||
|
||||
#[Cronard('0 0 1 * *')]
|
||||
#[Route("/clean/request-logs", name: "lean-api.cronard:clean-request-logs", method: ["DELETE"], description: "Fait le ménage automatiquement des logs de requêtes datant de plus d'un an")]
|
||||
#[Cronard(cron: '30 3 1 * *', method: 'DELETE')]
|
||||
#[Route("/clean/request-logs", name: "lean-api.cronard:clean-request-logs", method: ["DELETE"], description: "Fait le ménage automatiquement des logs de requêtes")]
|
||||
public function clean_request_logs(ServerRequestInterface $request, array $arguments): ResponseInterface
|
||||
{
|
||||
# return $this->outputSearchResult($results);
|
||||
$date = new Entity\Field\Datetime()->sub(new \DateInterval("P{$this->weekRetention}W"));
|
||||
|
||||
$count = Entity\Security\ApiRequestLog::repository()
|
||||
->where(Entity\Security\ApiRequestLog::field('createdAt'), $date, '<=')
|
||||
->count();
|
||||
|
||||
if ($count) {
|
||||
$table = Entity\Security\ApiRequestLog::resolveEntity()->tableName();
|
||||
$zipPath = getenv('LOGS_PATH') . DIRECTORY_SEPARATOR . sprintf("lean-api.%s-%s.zip", $table, date('Ymm_Hi'));
|
||||
$dbPath = $this->container->get('config')['ulmus']['connections'][Entity\Security\ApiRequestLog::repository()->adapter->name]['path'];
|
||||
|
||||
$zip = new ZipArchive();
|
||||
$zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
|
||||
|
||||
$sqlDump = shell_exec(sprintf("sqlite3 %s \".dump %s\"", $dbPath, $table));
|
||||
$zip->addFromString('lean-api.sqlite3', $sqlDump);
|
||||
$zip->close();
|
||||
|
||||
$count = Entity\Security\ApiRequestLog::repository()
|
||||
->where(Entity\Security\ApiRequestLog::field('createdAt', false), $date, '<=')
|
||||
->deleteAll();
|
||||
|
||||
return $this->renderJson([
|
||||
'data' => [
|
||||
'count' => $count
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->renderNothing();
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class ApiActionLog implements EntityInterface, \JsonSerializable
|
||||
public int $id;
|
||||
|
||||
#[Field\ForeignKey(name: "object_id"), EntityField(field: "object_id")]
|
||||
public int $objectId;
|
||||
public int|array $objectId;
|
||||
|
||||
#[Field(name: "object_type"), EntityField(field: "object_type")]
|
||||
public string $objectType;
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Lean\Api\Form\Security;
|
||||
use CSSLSJ\TransportScolaire\Api\{Entity};
|
||||
use Lean\Api\Attribute\ContextField;
|
||||
use Lean\Api\Lib\FormContext;
|
||||
use Ulmus\Attribute\IndexTypeEnum;
|
||||
use Ulmus\Entity\EntityInterface;
|
||||
use Ulmus\Event\Entity\DatasetFill;
|
||||
use Ulmus\Ulmus;
|
||||
@@ -21,7 +22,7 @@ class ActionLogContext extends FormContext
|
||||
public string $method;
|
||||
|
||||
#[ContextField(mandatory: true)]
|
||||
public int $object_id;
|
||||
public int|array $object_id;
|
||||
|
||||
#[ContextField(mandatory: true)]
|
||||
public string $object_type;
|
||||
@@ -60,14 +61,24 @@ class ActionLogContext extends FormContext
|
||||
public function initializeEntity(\Lean\Api\Entity\Security\ApiActionLog $actionLog) : void
|
||||
{
|
||||
if (isset($this->entity) && $this->entity->isLoaded()) {
|
||||
if ($pk = array_key_first($this->entity::resolveEntity()->getPrimaryKeyField())) {
|
||||
$this->object_type = $this->entity::class;
|
||||
|
||||
if (isset($this->datasetFillEvent->data)) {
|
||||
$this->data = $this->datasetFillEvent->data;
|
||||
}
|
||||
|
||||
$pk = $this->entity::resolveEntity()->getPrimaryKeyField();
|
||||
|
||||
if ($pk) {
|
||||
$pk = array_key_first($this->entity::resolveEntity()->getPrimaryKeyField());
|
||||
|
||||
if (isset($this->entity->$pk)) {
|
||||
$this->object_id = $this->entity->$pk;
|
||||
$this->object_type = $this->entity::class;
|
||||
|
||||
if (isset($this->datasetFillEvent->data)) {
|
||||
$this->data = $this->datasetFillEvent->data;
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif ($index = $this->entity::resolveEntity()->getCompoundKeyFields()) {
|
||||
if ($index->type === IndexTypeEnum::Primary) {
|
||||
$this->object_id = array_combine($index->column, array_map(fn($e) => $this->entity->$e ?? null, $index->column));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user