<?php
namespace App\Controller;
use App\Entity\Job;
use DateInterval;
use DateTime;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class HomeController extends AbstractController
{
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @Route("/", name="home.index", methods={"GET"})
* @return Response
* @throws NoResultException
* @throws NonUniqueResultException
*/
public function index(): Response
{
$jobsError = $this->getDoctrine()->getRepository(Job::class)->findBy([
'status' => Job::STATUS_ERROR
], [
'createdAt' => 'DESC'
], 5);
$lastJobs = $this->getDoctrine()->getRepository(Job::class)->findBy([], [
'createdAt' => 'DESC'
], 10);
$indicators = $this->buildIndicatorData();
$jobByTypeChart = $this->buildChartData();
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
'jobsError' => $jobsError,
'lastJobs' => $lastJobs,
'indicators' => $indicators,
'jobByTypeChart' => $jobByTypeChart
]);
}
/**
* @return array[]
* @throws NoResultException
* @throws NonUniqueResultException
*/
private function buildIndicatorData(): array
{
$jobsDone = $this->getDoctrine()->getRepository(Job::class)->count([
'status' => Job::STATUS_TREATED
]);
$jobsDoneLastDay = $this->getDoctrine()->getRepository(Job::class)->findJobDoneLastDay();
$jobsError = $this->getDoctrine()->getRepository(Job::class)->count([
'status' => Job::STATUS_ERROR
]);
return [
['title' => 'Count treatment done', 'class' => 'success', 'value' => $jobsDone, 'icon' => 'fa-check'],
['title' => 'Count treatment done in last day', 'class' => 'warning', 'value' => $jobsDoneLastDay, 'icon' => 'fa-cogs'],
['title' => 'Count treatment in error', 'class' => 'danger', 'value' => $jobsError, 'icon' => 'fa-exclamation-triangle'],
];
}
/**
* @return array[]
* @throws Exception
*/
private function buildChartData(): array
{
$treatmentVersion = $this->getDoctrine()->getRepository(Job::class)->getDistinctTreatmentVersion();
$now = (new DateTime())->add(new DateInterval('P1D'));
$datas = $this->getDoctrine()->getRepository(Job::class)->getLastTreatmentByVersion();
$charts = [];
foreach($datas as $data) {
$date = (new DateTime($data['jobDate']))->format('d/m');
$charts[$data['treatmentVersion']][$date] = $data['count'];
}
$count = 0;
$chartConfig = [
'type' => 'line',
'data' => [
'labels' => [],
'datasets' => []
],
'options' => [
'responsive' => true,
'title' => [
'display' => true,
'text' => $this->translator->trans('Count treatment by version (12 last month)')
],
'scales' => [
'yAxes' => [[
'ticks' => [
'stepSize' => 1,
'beginAtZero' => true,
]
]]
]
]
];
foreach ($treatmentVersion as $key => $version) {
$dateStart = (new DateTime())->sub(new DateInterval('P30D'));
if (!isset($chartConfig['data']['datasets'][$key])) {
$chartConfig['data']['datasets'][$key] = [];
}
//$chartConfig['data']['datasets'][$key]['label'] = $version->getVersion();
$chartConfig['data']['datasets'][$key]['label'] = $version['treatmentVersion'];
$chartConfig['data']['datasets'][$key]['fill'] = false;
$chartConfig['data']['datasets'][$key]['backgroundColor'] = $this->generateRgbColor($key);//$colors[$key] ?? $colors[0];
$chartConfig['data']['datasets'][$key]['borderColor'] = $this->generateRgbColor($key);//$colors[$key] ?? $colors[0];
if (!isset($chartConfig['data']['datasets'][$key]['data'])) {
$chartConfig['data']['datasets'][$key]['data'] = [];
}
while ($dateStart->format('d/m') != $now->format('d/m')) {
if ($count == 0) {
$chartConfig['data']['labels'][] = $dateStart->format('d/m');
}
if (isset($charts[$version['treatmentVersion']]) && isset($charts[$version['treatmentVersion']][$dateStart->format('d/m')])) {
$chartConfig['data']['datasets'][$key]['data'][] = (int)$charts[$version['treatmentVersion']][$dateStart->format('d/m')];
} else {
$chartConfig['data']['datasets'][$key]['data'][] = 0;
}
$dateStart = $dateStart->add(new DateInterval('P1D'));
}
$count++;
}
return $chartConfig;
}
private function generateRgbColor($num)
{
$hash = md5('color' . $num); // modify 'color' to get a different palette
return 'rgb('.
hexdec(substr($hash, 0, 2)).','.
hexdec(substr($hash, 2, 2)).','.
hexdec(substr($hash, 4, 2)).')'
;
return array(
hexdec(substr($hash, 0, 2)), // r
hexdec(substr($hash, 2, 2)), // g
hexdec(substr($hash, 4, 2)) //b
);
}
}