-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDashboardController.php
More file actions
196 lines (164 loc) · 8.19 KB
/
Copy pathDashboardController.php
File metadata and controls
196 lines (164 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace CodeRhapsodie\IbexaDataflowBundle\Controller;
use CodeRhapsodie\DataflowBundle\Entity\Job;
use CodeRhapsodie\DataflowBundle\Entity\ScheduledDataflow;
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\ExceptionHandlerInterface;
use CodeRhapsodie\DataflowBundle\ExceptionsHandler\NullExceptionHandler;
use CodeRhapsodie\DataflowBundle\Registry\DataflowTypeRegistryInterface;
use CodeRhapsodie\IbexaDataflowBundle\CodeRhapsodieIbexaDataflowBundle;
use CodeRhapsodie\IbexaDataflowBundle\Form\CreateOneshotType;
use CodeRhapsodie\IbexaDataflowBundle\Form\CreateScheduledType;
use CodeRhapsodie\IbexaDataflowBundle\Form\UpdateScheduledType;
use CodeRhapsodie\IbexaDataflowBundle\Gateway\ExceptionJSONDecoderAdapter;
use CodeRhapsodie\IbexaDataflowBundle\Gateway\JobGateway;
use CodeRhapsodie\IbexaDataflowBundle\Gateway\ScheduledDataflowGateway;
use CodeRhapsodie\IbexaDataflowBundle\Gateway\TransformingAdapter;
use Doctrine\DBAL\Query\QueryBuilder;
use Ibexa\Contracts\AdminUi\Controller\Controller;
use Ibexa\Contracts\Core\Ibexa;
use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute;
use Pagerfanta\Doctrine\DBAL\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route(path: '/ibexa_dataflow')]
class DashboardController extends Controller
{
public function __construct(
private readonly JobGateway $jobGateway,
private readonly ScheduledDataflowGateway $scheduledDataflowGateway,
private readonly ExceptionHandlerInterface $exceptionHandler,
private readonly DataflowTypeRegistryInterface $registry,
private readonly TranslatorInterface $translator,
) {
}
#[Route(path: '/', name: 'coderhapsodie.ibexa_dataflow.main')]
public function main(): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
$data = [
'product' => CodeRhapsodieIbexaDataflowBundle::PRODUCT_NAME,
'version' => CodeRhapsodieIbexaDataflowBundle::VERSION,
'php' => PHP_VERSION,
'ibexa' => Ibexa::VERSION,
];
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/main.html.twig', [
'link' => 'https://www.code-rhapsodie.fr/product/redirect/'.str_replace('=', '',
base64_encode(json_encode($data))
),
]);
}
public function repeating(Request $request): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
$newWorkflow = new ScheduledDataflow();
$newWorkflow->setNext((new \DateTimeImmutable())->add(new \DateInterval('PT1H')));
$form = $this->createForm(CreateScheduledType::class, $newWorkflow, [
'action' => $this->generateUrl('coderhapsodie.ibexa_dataflow.workflow.create'),
]);
$updateForm = $this->createForm(UpdateScheduledType::class);
$pager = $this->getPager($this->scheduledDataflowGateway->getListQueryForAdmin(), $request);
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/repeating.html.twig', [
'pager' => $pager,
'avg_times' => $this->jobGateway->getAverageExecutionTimes($this->extractIds($pager)),
'form' => $form->createView(),
'update_form' => $updateForm->createView(),
]);
}
#[Route(path: '/repeating', name: 'coderhapsodie.ibexa_dataflow.repeating')]
public function getRepeatingPage(Request $request): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
$pager = $this->getPager($this->scheduledDataflowGateway->getListQueryForAdmin(), $request);
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/repeating.html.twig', [
'pager' => $pager,
'avg_times' => $this->jobGateway->getAverageExecutionTimes($this->extractIds($pager)),
]);
}
public function oneshot(Request $request): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
$newOneshotJob = new Job();
$newOneshotJob->setRequestedDate((new \DateTime())->add(new \DateInterval('PT1H')));
$form = $this->createForm(CreateOneshotType::class, $newOneshotJob, [
'action' => $this->generateUrl('coderhapsodie.ibexa_dataflow.job.create'),
]);
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/oneshot.html.twig', [
'pager' => $this->getPager($this->jobGateway->getOneshotListQueryForAdmin(), $request, Job::class),
'form' => $form->createView(),
]);
}
#[Route(path: '/oneshot', name: 'coderhapsodie.ibexa_dataflow.oneshot')]
public function getOneshotPage(Request $request): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/oneshot.html.twig', [
'pager' => $this->getPager($this->jobGateway->getOneshotListQueryForAdmin(), $request, Job::class),
]);
}
#[Route(path: '/history', name: 'coderhapsodie.ibexa_dataflow.history')]
public function getHistoryPage(Request $request): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
$statusFilter = $request->query->getInt('status', JobGateway::FILTER_NONE);
$typeFilter = $request->query->getString('type');
$typeChoices = [['value' => '', 'label' => $this->translator->trans('coderhapsodie.ibexa_dataflow.history.filter.type.all')]];
foreach ($this->registry->listDataflowTypes() as $type) {
$typeChoices[] = [
'value' => $type::class,
'label' => $type->getLabel(),
];
}
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/history.html.twig', [
'pager' => $this->getPager($this->jobGateway->getListQueryForAdmin($statusFilter, $typeFilter), $request, Job::class),
'status' => $statusFilter,
'type' => $typeFilter,
'typeChoices' => $typeChoices,
]);
}
#[Route(path: '/history/schedule/{id}', name: 'coderhapsodie.ibexa_dataflow.history.workflow')]
public function getHistoryForScheduled(Request $request, int $id): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/schedule_history.html.twig', [
'id' => $id,
'pager' => $this->getPager($this->jobGateway->getListQueryForScheduleAdmin($id), $request, Job::class),
]);
}
/**
* @return array<int>
*/
private function extractIds(Pagerfanta $pager): array
{
return array_map(fn(array $item) => (int)$item['id'], $pager->getCurrentPageResults());
}
private function getPager(QueryBuilder $query, Request $request, string $class = null): Pagerfanta
{
$adapter = new ExceptionJSONDecoderAdapter(
new QueryAdapter($query, fn($queryBuilder) => $queryBuilder->select('COUNT(DISTINCT id) AS total_results')
->resetQueryPart('orderBy')
->setMaxResults(1))
);
if ($class === Job::class && !$this->exceptionHandler instanceof NullExceptionHandler) {
$adapter = new TransformingAdapter($adapter, function (array $value) {
$value['exceptions'] = $this->exceptionHandler->find((int) $value['id']);
return $value;
});
}
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(20);
$pager->setCurrentPage($request->query->getInt('page', 1));
return $pager;
}
public function dashboard(): Response
{
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
return $this->render('@ibexadesign/ibexa_dataflow/Dashboard/dashboard.html.twig', [
'jobs' => $this->jobGateway->getListPendindOrRunning(),
'counts' => $this->jobGateway->counts([Job::STATUS_PENDING, Job::STATUS_RUNNING, Job::STATUS_QUEUED]),
]);
}
}