getRepository(Stats::class)->findOneBy(['zone' => $insee_code]); if (!$stats) { $this->addFlash('error', 'Aucune stats trouvée pour ce code INSEE.'); return $this->redirectToRoute('app_admin'); } // Récupérer les objets OSM $elements = $motocultrice->followUpCity($insee_code); // Séparer les objets par type $fire_hydrants = array_filter($elements, fn($el) => ($el['tags']['emergency'] ?? null) === 'fire_hydrant'); $charging_stations = array_filter($elements, fn($el) => ($el['tags']['amenity'] ?? null) === 'charging_station'); // --- Suivi du nombre d'objets --- $now = new \DateTime(); $types = [ 'fire_hydrant' => [ 'label' => 'Bornes incendie', 'objects' => $fire_hydrants ], 'charging_station' => [ 'label' => 'Bornes de recharge', 'objects' => $charging_stations ] ]; foreach ($types as $type => $data) { // Suivi du nombre $followupCount = new CityFollowUp(); $followupCount->setName($type . '_count') ->setMeasure(count($data['objects'])) ->setDate($now) ->setStats($stats); $em->persist($followupCount); // Suivi de la complétion personnalisé if ($type === 'fire_hydrant') { $completed = array_filter($data['objects'], function($el) { return !empty($el['tags']['ref'] ?? null); }); } elseif ($type === 'charging_station') { $completed = array_filter($data['objects'], function($el) { return !empty($el['tags']['charging_station:output'] ?? null) && !empty($el['tags']['capacity'] ?? null); }); } else { $completed = []; } $completion = count($data['objects']) > 0 ? round(count($completed) / count($data['objects']) * 100) : 0; $followupCompletion = new CityFollowUp(); $followupCompletion->setName($type . '_completion') ->setMeasure($completion) ->setDate($now) ->setStats($stats); $em->persist($followupCompletion); } $em->flush(); $this->addFlash('success', 'Suivi enregistré pour la ville.'); return $this->redirectToRoute('admin_followup_graph', ['insee_code' => $insee_code]); } #[Route('/admin/followup/{insee_code}/graph', name: 'admin_followup_graph')] public function followupGraph( string $insee_code, EntityManagerInterface $em ): Response { $stats = $em->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]); if (!$stats) { $this->addFlash('error', 'Aucune stats trouvée pour ce code INSEE.'); return $this->redirectToRoute('app_admin'); } $followups = $stats->getCityFollowUps(); $followups = $followups->toArray(); usort($followups, fn($a, $b) => $a->getDate() <=> $b->getDate()); // Grouper par type $series = []; foreach ($followups as $fu) { $series[$fu->getName()][] = [ 'date' => $fu->getDate()->format('c'), 'value' => $fu->getMeasure(), 'name' => $fu->getName(), ]; } return $this->render('admin/followup_graph.html.twig', [ 'stats' => $stats, 'series' => $series ]); } }