diff --git a/api/README.md b/api/README.md index 8941c79a2d46caeeb71189bed18aa8e9ba00451e..861524933d63c913f2d0bc10302d96e2e0741dfb 100644 --- a/api/README.md +++ b/api/README.md @@ -11,3 +11,9 @@ docker compose exec php bin/console doctrine:migrations:diff docker compose exec php bin/console doctrine:migrations:migrate ``` +(Optimisation) +Indexation de la Colonne de Date, car toutes les routes font des opérations de filtrage basées sur cette colonne. +````sql +create index date_index on sale (date); +```` + diff --git a/api/composer.json b/api/composer.json index eb987c7510418ba7b4c92e1785072c90a58bb6fb..812930e4ef38d7aa75643aa4aa8a6e4896b16db6 100644 --- a/api/composer.json +++ b/api/composer.json @@ -7,6 +7,7 @@ "ext-iconv": "*", "api-platform/core": "^3.2", "beberlei/doctrineextensions": "dev-master", + "doctrine/cache": "*", "doctrine/doctrine-bundle": "^2.7", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.12", diff --git a/api/composer.lock b/api/composer.lock index 0663f0e9673f88c6dcd17383c5b7ef4deafed07f..f2a9d6ba930bc72d2efb09f3d3685fac6c4f4b5e 100644 --- a/api/composer.lock +++ b/api/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bd6b6c41a9c7342b985895c36f7ab96c", + "content-hash": "03c3d0b50d73e40d0c29a385e97c5c6c", "packages": [ { "name": "api-platform/core", diff --git a/api/src/Service/BarChartService.php b/api/src/Service/BarChartService.php index efa52f8b842504ac9ce5f4a0c4fd22a41b5e089a..67b6ffa103de1ce0f030d42acfdabf0a780fcca7 100644 --- a/api/src/Service/BarChartService.php +++ b/api/src/Service/BarChartService.php @@ -28,44 +28,59 @@ class BarChartService $endDate = new \DateTime($input->end); $queryBuilder = $this->entityManager->createQueryBuilder(); + $output = []; - switch ($input->granularity) { - case 'day': - $groupByExpression = 's.date'; - $groupByAlias = 'date'; - $dateFormat = 'Y-m-d'; - break; - case 'month': - $groupByExpression = 'MONTH(s.date)'; - $groupByAlias = 'month'; - $dateFormat = 'Y-m'; - break; - case 'year': - $groupByExpression = 'YEAR(s.date)'; - $groupByAlias = 'year'; - $dateFormat = 'Y'; - break; - default: - throw new \InvalidArgumentException('Invalid granularity'); - } + if($input->granularity == 'month'){ + $result = $queryBuilder + ->select( 'YEAR(s.date) as year', 'MONTH(s.date) as month' ,'COUNT(s.id) as occurrences') + ->from(Sale::class, 's') + ->where('s.date BETWEEN :start AND :end') + ->setParameter('start', $startDate->format('Y-m-d')) + ->setParameter('end', $endDate->format('Y-m-d')) + ->groupBy("year","month") + ->orderBy("year") + ->getQuery() + ->getResult(); - $result = $queryBuilder - ->select("{$groupByExpression} as {$groupByAlias}", 'COUNT(s.id) as occurrences') - ->from(Sale::class, 's') - ->where('s.date BETWEEN :start AND :end') - ->setParameter('start', $startDate->format('Y-m-d')) - ->setParameter('end', $endDate->format('Y-m-d')) - ->groupBy("{$groupByAlias}") - ->orderBy("{$groupByAlias}") - ->getQuery() - ->getResult(); + foreach ($result as $row) { + $month = $row['month']; + $year = $row['year']; + $dateString = $year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT); + $output[] = new BarChartOutput($dateString, (int)$row['occurrences']); + } - $output = []; - foreach ($result as $row) { - $dateString = $row[$groupByAlias] instanceof \DateTimeInterface ? $row[$groupByAlias]->format($dateFormat) : $row[$groupByAlias]; - $output[] = new BarChartOutput($dateString, (int)$row['occurrences']); - } + } else + { + switch ($input->granularity) { + case 'day': + $groupByExpression = 's.date'; + $groupByAlias = 'date'; + $dateFormat = 'Y-m-d'; + break; + case 'year': + $groupByExpression = 'YEAR(s.date)'; + $groupByAlias = 'year'; + $dateFormat = 'Y'; + break; + default: + throw new \InvalidArgumentException('Invalid granularity'); + } + $result = $queryBuilder + ->select("{$groupByExpression} as {$groupByAlias}", 'COUNT(s.id) as occurrences') + ->from(Sale::class, 's') + ->where('s.date BETWEEN :start AND :end') + ->setParameter('start', $startDate->format('Y-m-d')) + ->setParameter('end', $endDate->format('Y-m-d')) + ->groupBy("{$groupByAlias}") + ->orderBy("{$groupByAlias}") + ->getQuery() + ->getResult(); + foreach ($result as $row) { + $dateString = $row[$groupByAlias] instanceof \DateTimeInterface ? $row[$groupByAlias]->format($dateFormat) : $row[$groupByAlias]; + $output[] = new BarChartOutput($dateString, (int)$row['occurrences']); + } + } return $output; }