<?php
namespace App\Repository;
use App\Entity\Detections;
use App\Entity\Spot;
use App\Entity\User;
use DateInterval;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Detections>
*
* @method Detections|null find($id, $lockMode = null, $lockVersion = null)
* @method Detections|null findOneBy(array $criteria, array $orderBy = null)
* @method Detections[] findAll()
* @method Detections[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class DetectionsRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Detections::class);
}
public function save(Detections $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Detections $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// Home tops
public function getTopMediasAndSpots (bool $spot = false, bool $media = false, User $user, bool $consumer, $month=null, $dateRange = null) {
$query = $this ->createQueryBuilder('d')
->addSelect('d.id', 'COUNT(d.id) as nbrUserDetections', 's.name')
->addOrderBy('nbrUserDetections', 'DESC')
->join('App\Entity\Spot', 's', 'WITH', 'd.spot = s.id');
//date precise
if(isset($dateRange)) {
$query ->andWhere('d.time > :from')
->andWhere('d.time < :to')
->setParameter('from', $dateRange['from']->format('Y-m-d 00:00:00'))
->setParameter('to', $dateRange['to']->format('Y-m-d 23:59:59'));
}
//by media
if($media) {
$query ->addSelect('m.id as media', 'm.name as mediaName', 'm.logo as mediaLogo', 'COUNT(DISTINCT(m.id))')
//! attention : this solution is not final
->leftJoin('App\Entity\Media', 'm', 'WITH', 'IDENTITY(s.media) = m.id and m.createdBy = :user')
->setParameter('user',$user)
->addGroupBy('media');
}
//by spot
if($spot) {
$query // ->addSelect('d.spot')
->addGroupBy('d.spot');
}
//bymonth
if($month) {
$query ->andWhere('MONTH(d.time) = :month')
->setParameter('month', $month);
}
// if Consumer ?
if ($consumer) {
$query ->addSelect('COUNT(DISTINCT(d.uniqueId)) as nbrConsumers')
->addOrderBy('nbrConsumers', 'DESC');
}
//var_dump($query->getResult());
return $query ->getQuery()
->getResult();
}
// AVG Detections by period
public function avgDetectionByPeriod ($spot = null, $media = null, $dateInterval = null, $country = false, $region = false, $consumer = false ) {
$query = $this ->createQueryBuilder('d');
//by Country
if($region) {
$query
->addSelect('d.region')
->addGroupBy("d.region");
//return false;
}
//by Region
if($country) {
$query
->addSelect('d.country')
->addGroupBy("d.country");
//return false;
}
//UniqueId Consumer
if($consumer) {
$query ->addSelect('COUNT(DISTINCT d.uniqueId) as countConsumers', 'COUNT(DISTINCT d.uniqueId) / DATE_DIFF(CURRENT_DATE(), :toDate) as avgConsumers');
// ->addGroupBy("d.uniqueId");
//return false;
}
//media given
if($media) {
$query ->leftJoin('App\Entity\Spot', 's', 'WITH', 's.media = :media')
->andWhere('d.spot = s.id')
->setParameter('media',$media);
}
//default
$interval = new \DateInterval ($dateInterval);
$toDay = new \DateTime();
$toDate = $toDay->sub($interval)->format('Y-m-d 23:59:59');
$query
->addSelect('COUNT(d.id) as countDetections', 'COUNT(d.id) / DATE_DIFF(CURRENT_DATE(), :toDate) as avgDetections', 'DATE_DIFF(CURRENT_DATE(), :toDate) as numberDays')
->andWhere('d.time >= :toDate')
->setParameter('toDate', $toDate);
return $query ->getQuery()
->getResult() ;
//->orderBy('d.id', 'ASC')
//->setMaxResults(10)
}
//find os devices
//find consumers by SPOT / Media / Country / REGION / Period / MONTH / Date range / OS
public function findUniqueDevices (int $spot = null, $media = null, bool $country = false, $region = false, $dateInterval = null, $month = null, $dateRange = null, bool $os = false):array
{
$query = $this->createQueryBuilder('d');
//by os
if($os) {
$query->addSelect('d.id', 'd.os', 'COUNT(d.id) as nbrOsDetections')
->addGroupBy('d.os');
}
//date precise
if(isset($dateRange)) {
$query->andWhere('d.time > :from')
->andWhere('d.time < :to')
->setParameter('from', $dateRange['from']->format('Y-m-d 00:00:00'))
->setParameter('to', $dateRange['to']->format('Y-m-d 00:00:00'));
}
//spot default
if($spot) {
$query->andWhere('d.spot = :spot')
->setParameter('spot', $spot);
}
//media given
if($media) {
$query ->leftJoin('App\Entity\Spot', 's', 'WITH', 's.media = :media')
->andWhere('d.spot = s.id')
->setParameter('media',$media);
}
//by country
if ($country) {
$query->addGroupBy("d.country")
->addSelect('d.id', 'd.country', 'COUNT(d.id) as nbrCountryDetections')
->addOrderBy('nbrConsumers', 'DESC');
}
//byregion
if ($region) {$query->addGroupBy("d.region") ->addOrderBy('nbrConsumers', 'DESC') ->addSelect('d.id', 'd.region', 'COUNT(d.id) as nbrRegionDetections');}
//bymonth
if($month) {
$query->andWhere('MONTH(d.time) = :month')
->setParameter('month', $month);
}
//byperiod
if (isset($dateInterval)) {
$interval = new DateInterval ($dateInterval);
$toDay = new \DateTime();
$toDate = $toDay->sub($interval)->format('Y-m-d 23:59:59');
$query->andWhere('d.time >= :toDate')
->setParameter('toDate', $toDate);
}
return $query->addSelect('COUNT(DISTINCT(d.uniqueId)) as nbrConsumers')
//->addGroupBy("d.uniqueId")
//->orderBy('d.id', 'ASC')
//->setMaxResults(10)
->getQuery()
->getResult()
;
}
//find detections by SPOT / Media / Country / Period / Month / Date range / Os / device type / Conumer ?
public function findDetections (int $spot = null, $media = null, bool $country = false, $region = false, $dateInterval = null, $month = null, $dateRange = null, bool $os = false, bool $consumer = false, bool $device = false, int $top=null, User $user = null, bool $byDay = false):array
{
$query = $this->createQueryBuilder('d');
//by day ?
if ($byDay) {
$query //->andWhere('mtd.time is NOT NULL')
->addSelect('concat(LPAD(MONTH(d.time),2,\'0\'), \'/\', LPAD(DAY(d.time), 2, \'0\')) as dayLabel, DAY(d.time) as day ')
//LPAD pour rajouter 0 devant les jours et mois dont les chiffres sont à 1 seul figure
->addGroupBy('dayLabel')
->orderBy('DAY(d.time)', 'ASC');
}
// by user
if($user) {
$query->addSelect('d.id', 'COUNT(d.id) as nbrUserDetections')
->addOrderBy('nbrUserDetections', 'DESC')
->leftJoin('App\Entity\Spot', 's', 'WITH', 's.createdBy = :user')
//->andWhere('d.spot = s.id')
// ->andWhere('d.createdBy = :user')
->setParameter('user',$user);
}
//by os
if($os) {
$query->addSelect('d.id', 'd.os', 'COUNT(d.id) as nbrOsDetections')
->addGroupBy('d.os')
->addOrderBy('nbrOsDetections', 'DESC');
}
//date precise
if(isset($dateRange)) {
$query->andWhere('d.time > :from')
->andWhere('d.time < :to')
->setParameter('from', $dateRange['from']->format('Y-m-d 00:00:00'))
->setParameter('to', $dateRange['to']->format('Y-m-d 23:59:59'));
}
//spot default
if($spot) {
$query ->andWhere('d.spot = :spot')
->setParameter('spot', $spot);
}
//media given
if($media) {
$query ->leftJoin('App\Entity\Spot', 's', 'WITH', 's.media = :media')
->andWhere('d.spot = s.id')
->setParameter('media',$media);
}
//by device type
if ($device) {
$query->addGroupBy("d.deviceType")
->addSelect('d.id', 'd.deviceType', 'COUNT(d.id) as nbrDeviceDetections')
->addOrderBy('nbrDeviceDetections', 'DESC');
}
//by country
if ($country) {
$query ->addGroupBy("d.country")
->addOrderBy('nbrCountryDetections', 'DESC')
->addSelect('d.id', 'd.country', 'COUNT(d.id) as nbrCountryDetections');
}
//byregion
if ($region) {
$query ->addGroupBy("d.region")
->addOrderBy('nbrRegionDetections', 'DESC') ->addSelect('d.id', 'd.region', 'COUNT(d.id) as nbrRegionDetections');
}
//bymonth
if($month) {
$query ->andWhere('MONTH(d.time) = :month')
->setParameter('month', $month);
}
//byperiod
if (isset($dateInterval)) {
$interval = new DateInterval ($dateInterval);
$toDay = new \DateTime();
$toDate = $toDay->sub($interval)->format('Y-m-d 00:00:00');
$query ->andWhere('d.time >= :toDate')
->setParameter('toDate', $toDate);
}
//by consumer
if ($consumer) {
$query ->addSelect('COUNT(DISTINCT(d.uniqueId)) as nbrConsumers')
->addOrderBy('nbrConsumers', 'DESC');
}
//top
if (isset($top)) {
$query ->setMaxResults($top);
}
return $query ->getQuery()
->getResult();
//->addGroupBy("d.uniqueId")
//->orderBy('d.id', 'ASC')
//->setMaxResults(10)
}
public function findDetectionsByCountry (int $spot)
{
return $this->createQueryBuilder('dc')
->select('dc.id', 'dc.country', 'dc.uniqueId', 'count(dc.id)')
->where('dc.spot = :spot')
->setParameter('spot', $spot)
->groupBy("dc.country")
//->orderBy('d.id', 'ASC')
//->setMaxResults(10)
->getQuery()
->getResult()
;
}
// total detections per interval
public function getMediaTotalDetectionsPeriod ( $media, $dateInterval = null, $consumer = false, bool $byDay = false)
{
$interval = new DateInterval ($dateInterval);
$toDay = new \DateTime();
$toDate = $toDay->sub($interval)->format('Y-m-d 23:59:59');
$query = $this->createQueryBuilder('mtd')
->select('COUNT(mtd.id) as nbrDetections')
->leftJoin('App\Entity\Spot', 's', 'WITH', 's.media = :media')
->andWhere('mtd.spot = s.id')
->andWhere('mtd.time >= :toDate')
->setParameters(['media' => $media, 'toDate'=> $toDate]);
if ($consumer) {$query->addSelect('COUNT(DISTINCT mtd.uniqueId) as nbrConsumers');}
if ($byDay) {
$query //->andWhere('mtd.time is NOT NULL')
->addSelect('concat(LPAD(MONTH(mtd.time),2,\'0\'), \'/\', LPAD(DAY(mtd.time), 2, \'0\')) as dayLabel, DAY(mtd.time) as day ')
//LPAD pour rajouter 0 devant les jours et mois dont les chiffres sont à 1 seul figure
->addGroupBy('dayLabel')
->orderBy('DAY(mtd.time)', 'ASC');
}
return $query->getQuery()->getResult();
}
// total detections per month
public function getMediaTotalDetectionsMonth ($media, $month = null)
{
$query = $this->createQueryBuilder('mtd')
->select('COUNT(mtd.id)')
->leftJoin('App\Entity\Spot', 's', 'WITH', 's.media = :media')
->andWhere('mtd.spot = s.id')
->andWhere('MONTH(mtd.time) = :month')
->setParameters(['media' => $media, 'month'=> $month]);
return $query->getQuery()->getSingleScalarResult();
}
// total detections per month unique devices
public function getMediaTotalConsumersMonth ($media, $month = null)
{
$query = $this->createQueryBuilder('mtc')
->select('COUNT(DISTINCT mtc.uniqueId)')
->leftJoin('App\Entity\Spot', 's', 'WITH', 's.media = :media')
->andWhere('mtc.spot = s.id')
->andWhere('MONTH(mtc.time) = :month')
// ->groupBy('mtc.uniqueId')
->setParameters(['media' => $media, 'month'=> $month]);
if( $query->getQuery()->getOneOrNullResult() == null) {
return 0;
} else {
return $query->getQuery()->getSingleScalarResult();
}
}
// /**
// * @return Detections[] Returns an array of Detections objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('d')
// ->andWhere('d.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('d.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Detections
// {
// return $this->createQueryBuilder('d')
// ->andWhere('d.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}