src/Repository/DetectionsRepository.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Detections;
  4. use App\Entity\Spot;
  5. use App\Entity\User;
  6. use DateInterval;
  7. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. /**
  10.  * @extends ServiceEntityRepository<Detections>
  11.  *
  12.  * @method Detections|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method Detections|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method Detections[]    findAll()
  15.  * @method Detections[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class DetectionsRepository extends ServiceEntityRepository
  18. {
  19.     public function __construct(ManagerRegistry $registry)
  20.     {
  21.         parent::__construct($registryDetections::class);
  22.     }
  23.     public function save(Detections $entitybool $flush false): void
  24.     {
  25.         $this->getEntityManager()->persist($entity);
  26.         if ($flush) {
  27.             $this->getEntityManager()->flush();
  28.         }
  29.     }
  30.     public function remove(Detections $entitybool $flush false): void
  31.     {
  32.         $this->getEntityManager()->remove($entity);
  33.         if ($flush) {
  34.             $this->getEntityManager()->flush();
  35.         }
  36.     }
  37.     // Home tops
  38.     public function getTopMediasAndSpots (bool $spot falsebool $media falseUser $userbool $consumer$month=null$dateRange null) {
  39.         $query $this  ->createQueryBuilder('d')
  40.                         ->addSelect('d.id''COUNT(d.id) as nbrUserDetections''s.name')
  41.                         ->addOrderBy('nbrUserDetections''DESC')
  42.                         ->join('App\Entity\Spot''s''WITH''d.spot = s.id');
  43.                        
  44.          //date precise
  45.          if(isset($dateRange)) {
  46.             $query      ->andWhere('d.time > :from')
  47.                         ->andWhere('d.time < :to')
  48.                         ->setParameter('from'$dateRange['from']->format('Y-m-d 00:00:00'))
  49.                         ->setParameter('to'$dateRange['to']->format('Y-m-d 23:59:59'));
  50.         }
  51.          //by media
  52.          if($media) {
  53.             $query      ->addSelect('m.id as media''m.name as mediaName''m.logo as mediaLogo''COUNT(DISTINCT(m.id))')
  54.             //! attention : this solution is not final
  55.                         ->leftJoin('App\Entity\Media''m''WITH''IDENTITY(s.media) = m.id and m.createdBy = :user')
  56.                         ->setParameter('user',$user)
  57.                         ->addGroupBy('media');
  58.                     
  59.         }
  60.          //by spot
  61.          if($spot) {
  62.             $query     // ->addSelect('d.spot')
  63.                         ->addGroupBy('d.spot');
  64.                         
  65.         }
  66.          //bymonth
  67.          if($month) {
  68.             $query      ->andWhere('MONTH(d.time) = :month')
  69.                         ->setParameter('month'$month);
  70.         }
  71.         // if Consumer ?
  72.         if ($consumer) {
  73.             $query      ->addSelect('COUNT(DISTINCT(d.uniqueId)) as nbrConsumers')
  74.                         ->addOrderBy('nbrConsumers''DESC');
  75.             }
  76.         //var_dump($query->getResult());
  77.         return $query   ->getQuery()
  78.                         ->getResult();
  79.     } 
  80.     // AVG Detections by period
  81.     public function avgDetectionByPeriod ($spot null$media null$dateInterval null$country false$region false$consumer false ) {
  82.         $query $this ->createQueryBuilder('d');
  83.         //by Country
  84.         if($region) {
  85.             $query  
  86.                     ->addSelect('d.region')
  87.                     ->addGroupBy("d.region");
  88.             //return false;
  89.         }
  90.          //by Region
  91.          if($country) {
  92.             $query  
  93.                     ->addSelect('d.country')
  94.                     ->addGroupBy("d.country");
  95.             //return false;
  96.         }
  97.         //UniqueId Consumer
  98.         if($consumer) {
  99.             $query  ->addSelect('COUNT(DISTINCT d.uniqueId) as countConsumers''COUNT(DISTINCT d.uniqueId) / DATE_DIFF(CURRENT_DATE(), :toDate) as avgConsumers');
  100.                    // ->addGroupBy("d.uniqueId");
  101.             //return false;
  102.         }
  103.         //media given
  104.         if($media) {
  105.             $query ->leftJoin('App\Entity\Spot''s''WITH''s.media = :media')
  106.             ->andWhere('d.spot = s.id')
  107.             ->setParameter('media',$media);
  108.         }
  109.         //default
  110.         $interval = new \DateInterval ($dateInterval);
  111.         $toDay = new \DateTime();
  112.         $toDate $toDay->sub($interval)->format('Y-m-d 23:59:59');
  113.         $query
  114.               ->addSelect('COUNT(d.id) as countDetections''COUNT(d.id) / DATE_DIFF(CURRENT_DATE(), :toDate) as avgDetections''DATE_DIFF(CURRENT_DATE(), :toDate) as numberDays')
  115.               ->andWhere('d.time >= :toDate')
  116.               ->setParameter('toDate'$toDate);
  117.          return $query ->getQuery()
  118.                        ->getResult() ;
  119.                      //->orderBy('d.id', 'ASC')
  120.                      //->setMaxResults(10)
  121.        }
  122.     //find os devices
  123.     //find consumers by SPOT / Media / Country / REGION / Period / MONTH / Date range / OS
  124.     public function findUniqueDevices (int $spot null$media nullbool $country false$region false$dateInterval null$month null$dateRange nullbool $os false):array
  125.     {
  126.         $query $this->createQueryBuilder('d');
  127.          //by os
  128.          if($os) {
  129.             $query->addSelect('d.id''d.os''COUNT(d.id) as nbrOsDetections')
  130.                   ->addGroupBy('d.os');
  131.         }
  132.         //date precise
  133.         if(isset($dateRange)) {
  134.             $query->andWhere('d.time > :from')
  135.                   ->andWhere('d.time < :to')
  136.                   ->setParameter('from'$dateRange['from']->format('Y-m-d 00:00:00'))
  137.                   ->setParameter('to'$dateRange['to']->format('Y-m-d 00:00:00'));
  138.         }
  139.         //spot default
  140.         if($spot) {
  141.             $query->andWhere('d.spot = :spot')
  142.             ->setParameter('spot'$spot);
  143.         }
  144.         //media given
  145.         if($media) {
  146.             $query ->leftJoin('App\Entity\Spot''s''WITH''s.media = :media')
  147.             ->andWhere('d.spot = s.id')
  148.             ->setParameter('media',$media);
  149.         }
  150.         //by country
  151.         if ($country) {
  152.             $query->addGroupBy("d.country")
  153.                   ->addSelect('d.id''d.country''COUNT(d.id) as nbrCountryDetections')
  154.                   ->addOrderBy('nbrConsumers''DESC');
  155.         }
  156.         //byregion
  157.         if ($region) {$query->addGroupBy("d.region") ->addOrderBy('nbrConsumers''DESC') ->addSelect('d.id''d.region''COUNT(d.id) as nbrRegionDetections');}
  158.         //bymonth
  159.         if($month) {
  160.             $query->andWhere('MONTH(d.time) = :month')
  161.             ->setParameter('month'$month);
  162.         }
  163.         //byperiod
  164.         if (isset($dateInterval)) {
  165.         $interval = new DateInterval ($dateInterval);
  166.         $toDay = new \DateTime();
  167.         $toDate $toDay->sub($interval)->format('Y-m-d 23:59:59');
  168.             $query->andWhere('d.time >= :toDate')
  169.                   ->setParameter('toDate'$toDate);
  170.         }
  171.         return $query->addSelect('COUNT(DISTINCT(d.uniqueId)) as nbrConsumers')
  172.                     //->addGroupBy("d.uniqueId")
  173.                      //->orderBy('d.id', 'ASC')
  174.                      //->setMaxResults(10)
  175.                      ->getQuery()
  176.                      ->getResult()
  177.                 ;
  178.     }
  179.     //find detections by SPOT / Media / Country / Period / Month / Date range / Os / device type / Conumer ?
  180.     public function findDetections (int $spot null$media nullbool $country false$region false$dateInterval null$month null$dateRange nullbool $os falsebool $consumer falsebool $device falseint $top=nullUser $user nullbool $byDay false):array
  181.     {
  182.         $query $this->createQueryBuilder('d');
  183.         //by day ?
  184.         if ($byDay) {
  185.             $query  //->andWhere('mtd.time is NOT NULL')
  186.                     ->addSelect('concat(LPAD(MONTH(d.time),2,\'0\'), \'/\', LPAD(DAY(d.time), 2, \'0\')) as dayLabel, DAY(d.time) as day ')
  187.                     //LPAD pour rajouter 0 devant les jours et mois dont les chiffres sont à 1 seul figure
  188.                     ->addGroupBy('dayLabel')
  189.                     ->orderBy('DAY(d.time)''ASC');
  190.         }
  191.         // by user
  192.         if($user) {
  193.             $query->addSelect('d.id''COUNT(d.id) as nbrUserDetections')
  194.             ->addOrderBy('nbrUserDetections''DESC')
  195.             ->leftJoin('App\Entity\Spot''s''WITH''s.createdBy = :user')
  196.             //->andWhere('d.spot = s.id')
  197.            // ->andWhere('d.createdBy = :user')
  198.             ->setParameter('user',$user);
  199.         }
  200.          //by os
  201.          if($os) {
  202.             $query->addSelect('d.id''d.os''COUNT(d.id) as nbrOsDetections')
  203.                   ->addGroupBy('d.os')
  204.                   ->addOrderBy('nbrOsDetections''DESC');
  205.                   
  206.         }
  207.         //date precise
  208.         if(isset($dateRange)) {
  209.             $query->andWhere('d.time > :from')
  210.                   ->andWhere('d.time < :to')
  211.                   ->setParameter('from'$dateRange['from']->format('Y-m-d 00:00:00'))
  212.                   ->setParameter('to'$dateRange['to']->format('Y-m-d 23:59:59'));
  213.         }
  214.         //spot default
  215.         if($spot) {
  216.             $query  ->andWhere('d.spot = :spot')
  217.                     ->setParameter('spot'$spot);
  218.         }
  219.         //media given
  220.         if($media) {
  221.             $query  ->leftJoin('App\Entity\Spot''s''WITH''s.media = :media')
  222.                     ->andWhere('d.spot = s.id')
  223.                     ->setParameter('media',$media);
  224.         }
  225.          //by device type
  226.          if ($device) {
  227.             $query->addGroupBy("d.deviceType")
  228.                 ->addSelect('d.id''d.deviceType''COUNT(d.id) as nbrDeviceDetections')
  229.                 ->addOrderBy('nbrDeviceDetections''DESC');
  230.         }
  231.         //by country
  232.         if ($country) {
  233.             $query  ->addGroupBy("d.country")
  234.                     ->addOrderBy('nbrCountryDetections''DESC')
  235.                     ->addSelect('d.id''d.country''COUNT(d.id) as nbrCountryDetections');
  236.                   
  237.         }
  238.         //byregion
  239.         if ($region) {
  240.             $query  ->addGroupBy("d.region")
  241.                     ->addOrderBy('nbrRegionDetections''DESC') ->addSelect('d.id''d.region''COUNT(d.id) as nbrRegionDetections');
  242.            
  243.         }
  244.         //bymonth
  245.         if($month) {
  246.             $query  ->andWhere('MONTH(d.time) = :month')
  247.                     ->setParameter('month'$month);
  248.         }
  249.         //byperiod
  250.         if (isset($dateInterval)) {
  251.         $interval = new DateInterval ($dateInterval);
  252.         $toDay = new \DateTime();
  253.         $toDate $toDay->sub($interval)->format('Y-m-d 00:00:00');
  254.             $query  ->andWhere('d.time >= :toDate')
  255.                     ->setParameter('toDate'$toDate);
  256.         }
  257.         //by consumer
  258.         if ($consumer) {
  259.             $query  ->addSelect('COUNT(DISTINCT(d.uniqueId)) as nbrConsumers')
  260.                     ->addOrderBy('nbrConsumers''DESC');
  261.             }
  262.          //top
  263.             if (isset($top)) {
  264.                 $query  ->setMaxResults($top);
  265.                         
  266.             }
  267.             return $query   ->getQuery()
  268.                             ->getResult();
  269.                     //->addGroupBy("d.uniqueId")
  270.                      //->orderBy('d.id', 'ASC')
  271.                      //->setMaxResults(10)
  272.                    
  273.     }
  274.     public function findDetectionsByCountry (int $spot)
  275.     {
  276.         return $this->createQueryBuilder('dc')
  277.                     ->select('dc.id''dc.country''dc.uniqueId''count(dc.id)')
  278.                     ->where('dc.spot = :spot')
  279.                     ->setParameter('spot'$spot)
  280.                     ->groupBy("dc.country")
  281.                     //->orderBy('d.id', 'ASC')
  282.                     //->setMaxResults(10)
  283.                     ->getQuery()
  284.                     ->getResult()
  285.                 ;
  286.     }
  287.     // total detections per interval
  288.     public function getMediaTotalDetectionsPeriod $media$dateInterval null$consumer falsebool $byDay false)
  289.     {
  290.         $interval = new DateInterval ($dateInterval);
  291.         $toDay = new \DateTime();
  292.         $toDate $toDay->sub($interval)->format('Y-m-d 23:59:59');
  293.         $query $this->createQueryBuilder('mtd')
  294.         ->select('COUNT(mtd.id) as nbrDetections')
  295.         ->leftJoin('App\Entity\Spot''s''WITH''s.media = :media')
  296.         ->andWhere('mtd.spot = s.id')
  297.         ->andWhere('mtd.time >= :toDate')
  298.         ->setParameters(['media' => $media'toDate'=> $toDate]);
  299.         if ($consumer) {$query->addSelect('COUNT(DISTINCT mtd.uniqueId) as nbrConsumers');}
  300.         if ($byDay) {
  301.             $query  //->andWhere('mtd.time is NOT NULL')
  302.                     ->addSelect('concat(LPAD(MONTH(mtd.time),2,\'0\'), \'/\', LPAD(DAY(mtd.time), 2, \'0\')) as dayLabel, DAY(mtd.time) as day ')
  303.                     //LPAD pour rajouter 0 devant les jours et mois dont les chiffres sont à 1 seul figure
  304.                     ->addGroupBy('dayLabel')
  305.                     ->orderBy('DAY(mtd.time)''ASC');
  306.         }
  307.       
  308.                 return $query->getQuery()->getResult();
  309.          
  310.     }
  311.     // total detections per month
  312.     public function getMediaTotalDetectionsMonth ($media$month null)
  313.     {
  314.         $query $this->createQueryBuilder('mtd')
  315.         ->select('COUNT(mtd.id)')
  316.         ->leftJoin('App\Entity\Spot''s''WITH''s.media = :media')
  317.         ->andWhere('mtd.spot = s.id')
  318.         ->andWhere('MONTH(mtd.time) = :month')
  319.         ->setParameters(['media' => $media'month'=> $month]);
  320.       
  321.         return $query->getQuery()->getSingleScalarResult();
  322.     }
  323.      // total detections per month unique devices
  324.      public function getMediaTotalConsumersMonth ($media$month null)
  325.      {
  326.          $query $this->createQueryBuilder('mtc')
  327.          ->select('COUNT(DISTINCT mtc.uniqueId)')
  328.          ->leftJoin('App\Entity\Spot''s''WITH''s.media = :media')
  329.          ->andWhere('mtc.spot = s.id')
  330.          ->andWhere('MONTH(mtc.time) = :month')
  331.       //   ->groupBy('mtc.uniqueId')
  332.          ->setParameters(['media' => $media'month'=> $month]);
  333.         if( $query->getQuery()->getOneOrNullResult() == null) {
  334.             return 0;
  335.         } else {
  336.                 return $query->getQuery()->getSingleScalarResult();
  337.             }
  338.         
  339.      }
  340. //    /**
  341. //     * @return Detections[] Returns an array of Detections objects
  342. //     */
  343. //    public function findByExampleField($value): array
  344. //    {
  345. //        return $this->createQueryBuilder('d')
  346. //            ->andWhere('d.exampleField = :val')
  347. //            ->setParameter('val', $value)
  348. //            ->orderBy('d.id', 'ASC')
  349. //            ->setMaxResults(10)
  350. //            ->getQuery()
  351. //            ->getResult()
  352. //        ;
  353. //    }
  354. //    public function findOneBySomeField($value): ?Detections
  355. //    {
  356. //        return $this->createQueryBuilder('d')
  357. //            ->andWhere('d.exampleField = :val')
  358. //            ->setParameter('val', $value)
  359. //            ->getQuery()
  360. //            ->getOneOrNullResult()
  361. //        ;
  362. //    }
  363. }