src/Controller/HomeController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Job;
  4. use DateInterval;
  5. use DateTime;
  6. use Doctrine\ORM\NonUniqueResultException;
  7. use Doctrine\ORM\NoResultException;
  8. use Exception;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class HomeController extends AbstractController
  14. {
  15.     /**
  16.      * @var TranslatorInterface
  17.      */
  18.     private $translator;
  19.     public function __construct(TranslatorInterface $translator)
  20.     {
  21.         $this->translator $translator;
  22.     }
  23.     /**
  24.      * @Route("/", name="home.index", methods={"GET"})
  25.      * @return Response
  26.      * @throws NoResultException
  27.      * @throws NonUniqueResultException
  28.      */
  29.     public function index(): Response
  30.     {
  31.         $jobsError $this->getDoctrine()->getRepository(Job::class)->findBy([
  32.             'status' => Job::STATUS_ERROR
  33.         ], [
  34.             'createdAt' => 'DESC'
  35.         ], 5);
  36.         $lastJobs $this->getDoctrine()->getRepository(Job::class)->findBy([], [
  37.             'createdAt' => 'DESC'
  38.         ], 10);
  39.         $indicators $this->buildIndicatorData();
  40.         $jobByTypeChart $this->buildChartData();
  41.         return $this->render('home/index.html.twig', [
  42.             'controller_name' => 'HomeController',
  43.             'jobsError' => $jobsError,
  44.             'lastJobs' => $lastJobs,
  45.             'indicators' => $indicators,
  46.             'jobByTypeChart' => $jobByTypeChart
  47.         ]);
  48.     }
  49.     /**
  50.      * @return array[]
  51.      * @throws NoResultException
  52.      * @throws NonUniqueResultException
  53.      */
  54.     private function buildIndicatorData(): array
  55.     {
  56.         $jobsDone $this->getDoctrine()->getRepository(Job::class)->count([
  57.             'status' => Job::STATUS_TREATED
  58.         ]);
  59.         $jobsDoneLastDay $this->getDoctrine()->getRepository(Job::class)->findJobDoneLastDay();
  60.         $jobsError $this->getDoctrine()->getRepository(Job::class)->count([
  61.             'status' => Job::STATUS_ERROR
  62.         ]);
  63.         return [
  64.             ['title' => 'Count treatment done''class' => 'success''value' => $jobsDone'icon' => 'fa-check'],
  65.             ['title' => 'Count treatment done in last day''class' => 'warning''value' => $jobsDoneLastDay'icon' => 'fa-cogs'],
  66.             ['title' => 'Count treatment in error''class' => 'danger''value' => $jobsError'icon' => 'fa-exclamation-triangle'],
  67.         ];
  68.     }
  69.     /**
  70.      * @return array[]
  71.      * @throws Exception
  72.      */
  73.     private function buildChartData(): array
  74.     {
  75.         $treatmentVersion $this->getDoctrine()->getRepository(Job::class)->getDistinctTreatmentVersion();
  76.         $now = (new DateTime())->add(new DateInterval('P1D'));
  77.         $datas $this->getDoctrine()->getRepository(Job::class)->getLastTreatmentByVersion();
  78.         $charts = [];
  79.         foreach($datas as $data) {
  80.             $date = (new DateTime($data['jobDate']))->format('d/m');
  81.             $charts[$data['treatmentVersion']][$date] = $data['count'];
  82.         }
  83.         $count 0;
  84.         $chartConfig = [
  85.             'type' => 'line',
  86.             'data' => [
  87.                 'labels' => [],
  88.                 'datasets' => []
  89.             ],
  90.             'options' => [
  91.                 'responsive' => true,
  92.                 'title' => [
  93.                     'display' => true,
  94.                     'text' => $this->translator->trans('Count treatment by version (12 last month)')
  95.                 ],
  96.                 'scales' => [
  97.                     'yAxes' => [[
  98.                         'ticks' => [
  99.                             'stepSize' => 1,
  100.                             'beginAtZero' => true,
  101.                         ]
  102.                     ]]
  103.                 ]
  104.             ]
  105.         ];
  106.         foreach ($treatmentVersion as $key => $version) {
  107.             $dateStart = (new DateTime())->sub(new DateInterval('P30D'));
  108.             if (!isset($chartConfig['data']['datasets'][$key])) {
  109.                 $chartConfig['data']['datasets'][$key] = [];
  110.             }
  111.             //$chartConfig['data']['datasets'][$key]['label'] = $version->getVersion();
  112.             $chartConfig['data']['datasets'][$key]['label'] = $version['treatmentVersion'];
  113.             $chartConfig['data']['datasets'][$key]['fill'] = false;
  114.             $chartConfig['data']['datasets'][$key]['backgroundColor'] = $this->generateRgbColor($key);//$colors[$key] ?? $colors[0];
  115.             $chartConfig['data']['datasets'][$key]['borderColor'] = $this->generateRgbColor($key);//$colors[$key] ?? $colors[0];
  116.             if (!isset($chartConfig['data']['datasets'][$key]['data'])) {
  117.                 $chartConfig['data']['datasets'][$key]['data'] = [];
  118.             }
  119.             while ($dateStart->format('d/m') != $now->format('d/m')) {
  120.                 if ($count == 0) {
  121.                     $chartConfig['data']['labels'][] = $dateStart->format('d/m');
  122.                 }
  123.                 if (isset($charts[$version['treatmentVersion']]) && isset($charts[$version['treatmentVersion']][$dateStart->format('d/m')])) {
  124.                     $chartConfig['data']['datasets'][$key]['data'][] = (int)$charts[$version['treatmentVersion']][$dateStart->format('d/m')];
  125.                 } else {
  126.                     $chartConfig['data']['datasets'][$key]['data'][] = 0;
  127.                 }
  128.                 $dateStart $dateStart->add(new DateInterval('P1D'));
  129.             }
  130.             $count++;
  131.         }
  132.         return $chartConfig;
  133.     }
  134.     private function generateRgbColor($num)
  135.     {
  136.         $hash md5('color' $num); // modify 'color' to get a different palette
  137.         return 'rgb('.
  138.             hexdec(substr($hash02)).','.
  139.             hexdec(substr($hash22)).','.
  140.             hexdec(substr($hash42)).')'
  141.         ;
  142.         return array(
  143.             hexdec(substr($hash02)), // r
  144.             hexdec(substr($hash22)), // g
  145.             hexdec(substr($hash42))  //b
  146.         );
  147.     }
  148. }