From 0d696c83991db4d730b2b4d216a1398681a435cb Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Wed, 24 Aug 2022 07:23:47 +0200 Subject: [PATCH] www: Add try catch to avoid exception on missing database --- www/src/Controller/HomeController.php | 66 ++++++++++++-------- www/src/Controller/TodayController.php | 83 +++++++++++++++++--------- www/templates/menu.html.twig | 7 +-- www/templates/stats.html.twig | 4 +- 4 files changed, 103 insertions(+), 57 deletions(-) diff --git a/www/src/Controller/HomeController.php b/www/src/Controller/HomeController.php index ce4c2cb..19248d1 100644 --- a/www/src/Controller/HomeController.php +++ b/www/src/Controller/HomeController.php @@ -1,4 +1,5 @@ connection = $connection; + $this->logger = $logger; } /** @@ -27,16 +32,14 @@ class HomeController extends AbstractController "charts" => $this->last_chart_generated(), ]); } - + /** * @Route("/about", name="about") * @Route("/{_locale<%app.supported_locales%>}/about", name="about_i18n") */ public function about() { - return $this->render('about/index.html.twig', [ - - ]); + return $this->render('about/index.html.twig', []); } private function get_stats() @@ -47,40 +50,55 @@ class HomeController extends AbstractController return $stats; } - private function get_most_recorded_species() + private function get_most_recorded_species() { + $species = []; $sql = "SELECT `scientific_name`, `common_name`, COUNT(*) AS contact_count FROM `taxon` INNER JOIN `observation` ON `taxon`.`taxon_id` = `observation`.`taxon_id` ORDER BY `contact_count` DESC LIMIT 1"; - $stmt = $this->connection->prepare($sql); - $result = $stmt->executeQuery(); - $species = $result->fetchAllAssociative(); - return $species[0]; + try { + $stmt = $this->connection->prepare($sql); + $result = $stmt->executeQuery(); + $species = $result->fetchAllAssociative()[0]; + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } + return $species; } - private function get_last_recorded_species() + private function get_last_recorded_species() { + $species = []; $sql = "SELECT `scientific_name`, `common_name`, `date`, `audio_file`, `confidence` FROM `observation` INNER JOIN `taxon` ON `observation`.`taxon_id` = `taxon`.`taxon_id` ORDER BY `date` DESC LIMIT 1"; - $stmt = $this->connection->prepare($sql); - $result = $stmt->executeQuery(); - $species = $result->fetchAllAssociative(); - return $species[0]; + try { + $stmt = $this->connection->prepare($sql); + $result = $stmt->executeQuery(); + $species = $result->fetchAllAssociative()[0]; + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } + return $species; } - private function last_chart_generated() { - + private function last_chart_generated() + { $files = glob($this->getParameter('kernel.project_dir') . '/../var/charts/*.png'); - usort($files, function($a, $b) { - return filemtime($a) - filemtime($b); - }); - $last_chart = basename(array_pop($files)); - return $last_chart; + if (count($files) > 0) { + usort($files, function ($a, $b) { + return filemtime($a) - filemtime($b); + }); + + $last_chart = basename(array_pop($files)); + return $last_chart; + } else { + $this->logger->info("No charts found"); + return ""; + } } - -} \ No newline at end of file +} diff --git a/www/src/Controller/TodayController.php b/www/src/Controller/TodayController.php index f33b554..36586a6 100644 --- a/www/src/Controller/TodayController.php +++ b/www/src/Controller/TodayController.php @@ -6,15 +6,20 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use App\AppBundle\ConnectionObservations; +use Psr\Log\LoggerInterface; class TodayController extends AbstractController -{ private ConnectionObservations $connection; +{ + private ConnectionObservations $connection; + private LoggerInterface $logger; - public function __construct(ConnectionObservations $connection) + public function __construct(ConnectionObservations $connection, LoggerInterface $logger) { $this->connection = $connection; + $this->logger = $logger; } + /** * @Route("/today", name="today") * @Route("/{_locale<%app.supported_locales%>}/today", name="today_i18n") @@ -88,28 +93,38 @@ class TodayController extends AbstractController private function recorded_species_by_date($date) { + $recorded_species = []; $sql = "SELECT `taxon`.`taxon_id`, `scientific_name`, `common_name`, COUNT(*) AS `contact_count`, MAX(`confidence`) AS max_confidence FROM observation INNER JOIN taxon ON observation.taxon_id = taxon.taxon_id WHERE strftime('%Y-%m-%d', `observation`.`date`) = :date GROUP BY observation.taxon_id"; - $stmt = $this->connection->prepare($sql); - $stmt->bindValue(':date', $date); - $result = $stmt->executeQuery(); - return $result->fetchAllAssociative(); + try { + $stmt = $this->connection->prepare($sql); + $stmt->bindValue(':date', $date); + $result = $stmt->executeQuery(); + $recorded_species = $result->fetchAllAssociative(); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } + return $recorded_species; } private function recorded_species_by_id_and_date($id, $date) { /* Get taxon even if there is no record this date */ $sql = "SELECT * FROM `taxon` WHERE `taxon_id` = :id"; - $stmt = $this->connection->prepare($sql); - $stmt->bindValue(':id', $id); - $result = $stmt->executeQuery(); - $taxon = $result->fetchAllAssociative()[0]; - if (!$taxon) { - return []; + $taxon = []; + $stat = []; + $records = []; + try { + $stmt = $this->connection->prepare($sql); + $stmt->bindValue(':id', $id); + $result = $stmt->executeQuery(); + $taxon = $result->fetchAllAssociative()[0]; + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); } /* Get daily stats */ $sql = "SELECT COUNT(*) AS `contact_count`, MAX(`confidence`) AS `max_confidence` @@ -118,33 +133,47 @@ class TodayController extends AbstractController ON `taxon`.`taxon_id` = `observation`.`taxon_id` WHERE strftime('%Y-%m-%d', `observation`.`date`) = :date AND `observation`.`taxon_id` = :id"; - $stmt = $this->connection->prepare($sql); - $stmt->bindValue(':id', $id); - $stmt->bindValue(':date', $date); - $result = $stmt->executeQuery(); - $stat = $result->fetchAllAssociative(); + try { + $stmt = $this->connection->prepare($sql); + $stmt->bindValue(':id', $id); + $stmt->bindValue(':date', $date); + $result = $stmt->executeQuery(); + $stat = $result->fetchAllAssociative(); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } $sql = "SELECT * FROM `observation` WHERE `taxon_id` = :id AND strftime('%Y-%m-%d', `observation`.`date`) = :date ORDER BY `observation`.`date` ASC"; - $stmt = $this->connection->prepare($sql); - $stmt->bindValue(':id', $id); - $stmt->bindValue(':date', $date); - $result = $stmt->executeQuery(); - $records = $result->fetchAllAssociative(); + try { + $stmt = $this->connection->prepare($sql); + $stmt->bindValue(':id', $id); + $stmt->bindValue(':date', $date); + $result = $stmt->executeQuery(); + $records = $result->fetchAllAssociative(); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } return array("taxon" => $taxon, "stat" => $stat, "records" => $records); } private function best_confidence_today($id, $date) { + $best_confidence = 0; $sql = "SELECT MAX(`confidence`) AS confidence FROM `observation` WHERE strftime('%Y-%m-%d', `observation`.`date`) = :date AND `taxon_id` = :id"; - $stmt = $this->connection->prepare($sql); - $stmt->bindValue(':id', $id); - $stmt->bindValue(':date', $date); - $result = $stmt->executeQuery(); - return $result->fetchAllAssociative(); + try { + $stmt = $this->connection->prepare($sql); + $stmt->bindValue(':id', $id); + $stmt->bindValue(':date', $date); + $result = $stmt->executeQuery(); + $result->fetchAllAssociative(); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } + return $best_confidence; } } \ No newline at end of file diff --git a/www/templates/menu.html.twig b/www/templates/menu.html.twig index 2c5b162..bfec8b9 100644 --- a/www/templates/menu.html.twig +++ b/www/templates/menu.html.twig @@ -41,10 +41,9 @@