Skip to content
Snippets Groups Projects
Verified Commit 341e38c6 authored by Damien's avatar Damien
Browse files

FEAT #12073 TIME 0:15 Refactor batch history + system actions history

parent 180f553c
No related branches found
No related tags found
No related merge requests found
...@@ -104,8 +104,8 @@ $app->delete('/baskets/{id}/groups/{groupId}', \Basket\controllers\BasketControl ...@@ -104,8 +104,8 @@ $app->delete('/baskets/{id}/groups/{groupId}', \Basket\controllers\BasketControl
$app->get('/sortedBaskets', \Basket\controllers\BasketController::class . ':getSorted'); $app->get('/sortedBaskets', \Basket\controllers\BasketController::class . ':getSorted');
$app->put('/sortedBaskets/{id}', \Basket\controllers\BasketController::class . ':updateSort'); $app->put('/sortedBaskets/{id}', \Basket\controllers\BasketController::class . ':updateSort');
//BatchHistories //BatchHistory
$app->get('/batchHistories', \History\controllers\BatchHistoryController::class . ':get'); $app->get('/batchHistory', \History\controllers\BatchHistoryController::class . ':get');
//Configurations //Configurations
$app->get('/configurations/{service}', \Configuration\controllers\ConfigurationController::class . ':getByService'); $app->get('/configurations/{service}', \Configuration\controllers\ConfigurationController::class . ':getByService');
......
...@@ -16,7 +16,6 @@ namespace History\controllers; ...@@ -16,7 +16,6 @@ namespace History\controllers;
use Group\controllers\PrivilegeController; use Group\controllers\PrivilegeController;
use History\models\BatchHistoryModel; use History\models\BatchHistoryModel;
use Respect\Validation\Validator;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;
...@@ -28,26 +27,46 @@ class BatchHistoryController ...@@ -28,26 +27,46 @@ class BatchHistoryController
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
} }
$data = $request->getQueryParams(); $queryParams = $request->getQueryParams();
$check = Validator::floatVal()->notEmpty()->validate($data['startDate']); $limit = 25;
$check = $check && Validator::floatVal()->notEmpty()->validate($data['endDate']); if (!empty($queryParams['limit']) && is_numeric($queryParams['limit'])) {
if (!$check) { $limit = (int)$queryParams['limit'];
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); }
$offset = 0;
if (!empty($queryParams['offset']) && is_numeric($queryParams['offset'])) {
$offset = (int)$queryParams['offset'];
}
$where = [];
$data = [];
if (!empty($queryParams['startDate'])) {
$where[] = 'event_date > ?';
$data[] = date('Y-m-d H:i:s', $queryParams['startDate']);
}
if (!empty($queryParams['endDate'])) {
$where[] = 'event_date < ?';
$data[] = date('Y-m-d H:i:s', $queryParams['endDate']);
} }
$maxRequestSize = 25000; $order = !in_array($queryParams['order'], ['asc', 'desc']) ? '' : $queryParams['order'];
$orderBy = !in_array($queryParams['orderBy'], ['event_date', 'module_name', 'total_processed', 'total_errors', 'info']) ? ['event_date DESC'] : ["{$queryParams['orderBy']} {$order}"];
$batchHistories = BatchHistoryModel::get([ $history = BatchHistoryModel::get([
'select' => ['event_date', 'module_name', 'total_processed', 'total_errors', 'info'], 'select' => ['event_date', 'module_name', 'total_processed', 'total_errors', 'info', 'count(1) OVER()'],
'where' => ['event_date > ?', 'event_date < ?'], 'where' => $where,
'data' => [date('Y-m-d H:i:s', $data['startDate']), date('Y-m-d H:i:s', $data['endDate'])], 'data' => $data,
'orderBy' => ['event_date DESC'], 'orderBy' => $orderBy,
'limit' => $maxRequestSize 'offset' => $offset,
'limit' => $limit
]); ]);
$limitExceeded = (count($batchHistories) == $maxRequestSize); $total = $history[0]['count'] ?? 0;
foreach ($history as $key => $value) {
unset($history[$key]['count']);
}
return $response->withJson(['batchHistories' => $batchHistories, 'limitExceeded' => $limitExceeded]); return $response->withJson(['history' => $history, 'count' => $total]);
} }
} }
...@@ -66,6 +66,7 @@ class HistoryController ...@@ -66,6 +66,7 @@ class HistoryController
$where[] = 'user_id in (?)'; $where[] = 'user_id in (?)';
$data[] = $users; $data[] = $users;
} }
if (!empty($queryParams['startDate'])) { if (!empty($queryParams['startDate'])) {
$where[] = 'event_date > ?'; $where[] = 'event_date > ?';
$data[] = date('Y-m-d H:i:s', $queryParams['startDate']); $data[] = date('Y-m-d H:i:s', $queryParams['startDate']);
...@@ -74,17 +75,19 @@ class HistoryController ...@@ -74,17 +75,19 @@ class HistoryController
$where[] = 'event_date < ?'; $where[] = 'event_date < ?';
$data[] = date('Y-m-d H:i:s', $queryParams['endDate']); $data[] = date('Y-m-d H:i:s', $queryParams['endDate']);
} }
$eventTypes = [];
if (!empty($queryParams['actions']) && is_array($queryParams['actions'])) { if (!empty($queryParams['actions']) && is_array($queryParams['actions'])) {
$actions = [];
foreach ($queryParams['actions'] as $action) { foreach ($queryParams['actions'] as $action) {
if (is_numeric($action)) { $eventTypes[] = "ACTION#{$action}";
$actions[] = "ACTION#{$action}";
} else {
$actions[] = $action;
}
} }
}
if (!empty($queryParams['systemActions']) && is_array($queryParams['systemActions'])) {
$eventTypes = array_merge($eventTypes, $queryParams['systemActions']);
}
if (!empty($eventTypes)) {
$where[] = 'event_type in (?)'; $where[] = 'event_type in (?)';
$data[] = $actions; $data[] = $eventTypes;
} }
$order = !in_array($queryParams['order'], ['asc', 'desc']) ? '' : $queryParams['order']; $order = !in_array($queryParams['order'], ['asc', 'desc']) ? '' : $queryParams['order'];
......
...@@ -19,21 +19,22 @@ use SrcCore\models\DatabaseModel; ...@@ -19,21 +19,22 @@ use SrcCore\models\DatabaseModel;
abstract class BatchHistoryModelAbstract abstract class BatchHistoryModelAbstract
{ {
public static function get(array $aArgs) public static function get(array $args)
{ {
ValidatorModel::notEmpty($aArgs, ['select']); ValidatorModel::notEmpty($args, ['select']);
ValidatorModel::arrayType($aArgs, ['select', 'where', 'data', 'orderBy']); ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy']);
ValidatorModel::intVal($aArgs, ['limit']); ValidatorModel::intVal($args, ['offset', 'limit']);
$aHistories = DatabaseModel::select([ $history = DatabaseModel::select([
'select' => $aArgs['select'], 'select' => $args['select'],
'table' => ['history_batch'], 'table' => ['history_batch'],
'where' => $aArgs['where'], 'where' => $args['where'] ?? [],
'data' => $aArgs['data'], 'data' => $args['data'] ?? [],
'order_by' => $aArgs['orderBy'], 'order_by' => $args['orderBy'] ?? [],
'limit' => $aArgs['limit'] 'offset' => $args['offset'] ?? 0,
'limit' => $args['limit'] ?? 0
]); ]);
return $aHistories; return $history;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment