src/Controller/DashboardController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Alert;
  4. use App\Entity\AlertProduct;
  5. use App\Entity\Competitor;
  6. use App\Entity\CompetitorProduct;
  7. use App\Entity\PriceScraped;
  8. use DateInterval;
  9. use DateTime;
  10. use http\Env\Request;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class DashboardController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/", name="home")
  19.      */
  20.     public function index(): Response
  21.     {
  22.         $em $this->getDoctrine()->getManager();
  23.         $interval $this->lastScrapedCard();
  24.         $nbErrors $this->countProductError();
  25.         $monitoredProduct $em->getRepository(CompetitorProduct::class)->findMonitoredProducts();
  26.         $number_prices_scrapped $this->countPriceScrap();
  27.         $compScrap $this->countCompScrap();
  28.         if($monitoredProduct)
  29.             $monitoredProduct count($monitoredProduct);
  30.         else
  31.             $monitoredProduct 0;
  32.         return $this->render('dashboard/index.html.twig', [
  33.             'interval' => $interval,
  34.             'nbErrors' => $nbErrors,
  35.             'monitoredProduct' => $monitoredProduct,
  36.             'pricesScrapped' => $number_prices_scrapped,
  37.             'compScrap' => $compScrap
  38.         ]);
  39.     }
  40.     public function lastScrapedCard(): string
  41.     {
  42.         $em $this->getDoctrine()->getManager();
  43.         $idLast =  $em->getRepository(PriceScraped::class)->findLastScrap();
  44.         $lastScrap $em->getRepository(PriceScraped::class)->findOneBy(['id' => $idLast[0][1]]);
  45.         $dateLastScrap $lastScrap->getDate();
  46.         $date = new DateTime('now');
  47.         $intervalDate $dateLastScrap->diff($date);
  48.         $interval = ($intervalDate->h)*60 +  $intervalDate->i;
  49.         if($intervalDate->|| $intervalDate->5){
  50.             return '<p class="uk-margin-remove-bottom uk-padding-small uk-padding-remove-bottom"><i class="fas fa-exclamation-triangle fa-xl uk-margin-small-right" style="color: orange"></i>Attention, il y a peut-être un caillou dans l\'engrenage :</p>
  51.                     <p class="uk-padding-small uk-padding-remove-top uk-margin-small-left">dernier prix relevé il y a '.$interval.' minutes</p>';
  52.         }else{
  53.             return '<p class="uk-padding-small"><i class="fas fa-check-circle fa-xl uk-margin-small-right" style="color: green"></i>La routourne tourne rond, dernier prix relevé à '.$dateLastScrap->format('h:i').' sur 
  54.             '.$em->getRepository(Competitor::class)->findOneBy(['competitor_id' => $lastScrap->getCompetitorId()])->getBrand().'</p>';
  55.         }
  56.     }
  57.     public function countProductError(): string
  58.     {
  59.         $em $this->getDoctrine()->getManager();
  60.         $date = new DateTime('now');
  61.         $interval $date->sub(new DateInterval('P7D'));
  62.         $products $em->getRepository(PriceScraped::class)->findProductError($interval);
  63.         if ($products)
  64.             $nb_errors '<p class="uk-padding-small"><i class="fas fa-exclamation-triangle fa-xl uk-margin-small-right" style="color: orange"></i>'.count($products).' Produits n\'ont pas pu être mis à jour</p>';
  65.         else
  66.             $nb_errors '';
  67.         return $nb_errors;
  68.     }
  69.     public function countPriceScrap(){
  70.         $em $this->getDoctrine()->getManager();
  71.         $date = new DateTime('now');
  72.         $intervalDate $date->sub(new DateInterval('P7D'));
  73.         $pricesScrapped $em->getRepository(PriceScraped::class)->findPriceScrap($intervalDate);
  74.         if($pricesScrapped)
  75.             return count($pricesScrapped);
  76.         else
  77.             return 0;
  78.     }
  79.     /**
  80.      * count the number of competitor activated
  81.      * @return int
  82.      */
  83.     public function countCompScrap(){
  84.         $em $this->getDoctrine()->getManager();
  85.         $date = new DateTime('now');
  86.         $intervalDate $date->sub(new DateInterval('P7D'));
  87.         $compScrap $em->getRepository(PriceScraped::class)->findCompScrap($intervalDate);
  88.         if($compScrap)
  89.             return count($compScrap);
  90.         else
  91.             return 0;
  92.     }
  93.     /**
  94.      * construct table with all alerts
  95.      * @Route("/load/dashboardTable", name="dashboard_table")
  96.      */
  97.     public function constructTable(\Symfony\Component\HttpFoundation\Request $request){
  98.         $data $request->request->all();
  99.         $em $this->getDoctrine()->getManager();
  100.         $alerts $em->getRepository(Alert::class)->findAll();
  101.         foreach($alerts as $alert){
  102.             $alert_table['icon'] = '<p class="uk-align-center">'.$alert->getIconLink().'</p>';
  103.             $alert_table['alert_level'] = '<p style="text-align: center">'.$alert->getAlertLevel().'</p>';
  104.             $alert_table['name'] = '<p class="uk-align-center">'.$alert->getMessage().'</p>';
  105.             $alerted_products $em->getRepository(AlertProduct::class)->findBy(['alert_level' => $alert->getAlertLevel()]);
  106.             if($alerted_products){
  107.                 $number_products count($alerted_products);
  108.             }else
  109.                 $number_products 0;
  110.             $alert_table['number_products'] = '<p class="uk-align-center">'.$number_products.'</p>';
  111.             $alert_table['list_products'] = '<a href="https://pricedemo.romapps.com/comparativeTable/'.$alert->getAlertLevel().'" 
  112.                                                 class="uk-button uk-button-primary uk-margin-small-left uk-align-center">Voir produits</a>';
  113.             $alerts_table[] = $alert_table;
  114.         }
  115.         $json_alerts_table json_encode($alerts_table);
  116.         $returnResponse = new JsonResponse($json_alerts_table);
  117.         $returnResponse->setJson($json_alerts_table);
  118.         return $returnResponse;
  119.     }
  120. }