mirror of
https://forge.chapril.org/tykayn/osm-commerces
synced 2025-10-09 17:02:46 +02:00
follow up graphs
This commit is contained in:
parent
4b4a46c3f7
commit
ab1b9a9d3d
11 changed files with 580 additions and 41 deletions
110
src/Controller/FollowUpController.php
Normal file
110
src/Controller/FollowUpController.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Stats;
|
||||
use App\Entity\CityFollowUp;
|
||||
use App\Service\Motocultrice;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class FollowUpController extends AbstractController
|
||||
{
|
||||
#[Route('/admin/followup/{insee_code}', name: 'admin_followup')]
|
||||
public function followup(
|
||||
string $insee_code,
|
||||
Motocultrice $motocultrice,
|
||||
EntityManagerInterface $em
|
||||
): Response {
|
||||
// Récupérer la stats de la ville
|
||||
$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');
|
||||
}
|
||||
|
||||
// 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
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -3,8 +3,6 @@
|
|||
namespace App\Entity;
|
||||
|
||||
use App\Repository\CityFollowUpRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: CityFollowUpRepository::class)]
|
||||
|
@ -24,16 +22,8 @@ class CityFollowUp
|
|||
#[ORM\Column]
|
||||
private ?\DateTime $date = null;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Stats>
|
||||
*/
|
||||
#[ORM\OneToMany(targetEntity: Stats::class, mappedBy: 'cityFollowUps')]
|
||||
private Collection $stats;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->stats = new ArrayCollection();
|
||||
}
|
||||
#[ORM\ManyToOne(targetEntity: Stats::class, inversedBy: 'cityFollowUps')]
|
||||
private ?Stats $stats = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
|
@ -76,33 +66,14 @@ class CityFollowUp
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Stats>
|
||||
*/
|
||||
public function getStats(): Collection
|
||||
public function getStats(): ?Stats
|
||||
{
|
||||
return $this->stats;
|
||||
}
|
||||
|
||||
public function addStat(Stats $stat): static
|
||||
public function setStats(?Stats $stats): static
|
||||
{
|
||||
if (!$this->stats->contains($stat)) {
|
||||
$this->stats->add($stat);
|
||||
$stat->setCityFollowUps($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeStat(Stats $stat): static
|
||||
{
|
||||
if ($this->stats->removeElement($stat)) {
|
||||
// set the owning side to null (unless already changed)
|
||||
if ($stat->getCityFollowUps() === $this) {
|
||||
$stat->setCityFollowUps(null);
|
||||
}
|
||||
}
|
||||
|
||||
$this->stats = $stats;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,8 +98,11 @@ class Stats
|
|||
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2, nullable: true)]
|
||||
private ?string $budget_annuel = null;
|
||||
|
||||
#[ORM\ManyToOne(inversedBy: 'stats')]
|
||||
private ?CityFollowUp $cityFollowUps = null;
|
||||
/**
|
||||
* @var Collection<int, CityFollowUp>
|
||||
*/
|
||||
#[ORM\OneToMany(mappedBy: 'stats', targetEntity: CityFollowUp::class, cascade: ['persist', 'remove'])]
|
||||
private Collection $cityFollowUps;
|
||||
|
||||
public function getCTCurlBase(): ?string
|
||||
{
|
||||
|
@ -251,6 +254,7 @@ class Stats
|
|||
{
|
||||
$this->places = new ArrayCollection();
|
||||
$this->statsHistories = new ArrayCollection();
|
||||
$this->cityFollowUps = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
|
@ -569,15 +573,30 @@ class Stats
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function getCityFollowUps(): ?CityFollowUp
|
||||
/**
|
||||
* @return Collection<int, CityFollowUp>
|
||||
*/
|
||||
public function getCityFollowUps(): Collection
|
||||
{
|
||||
return $this->cityFollowUps;
|
||||
}
|
||||
|
||||
public function setCityFollowUps(?CityFollowUp $cityFollowUps): static
|
||||
public function addCityFollowUp(CityFollowUp $cityFollowUp): static
|
||||
{
|
||||
$this->cityFollowUps = $cityFollowUps;
|
||||
if (!$this->cityFollowUps->contains($cityFollowUp)) {
|
||||
$this->cityFollowUps->add($cityFollowUp);
|
||||
$cityFollowUp->setStats($this);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeCityFollowUp(CityFollowUp $cityFollowUp): static
|
||||
{
|
||||
if ($this->cityFollowUps->removeElement($cityFollowUp)) {
|
||||
if ($cityFollowUp->getStats() === $this) {
|
||||
$cityFollowUp->setStats(null);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
|
@ -556,4 +556,41 @@ out meta;';
|
|||
'counters' => $counters
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère la requête Overpass pour les bornes incendie et bornes de recharge
|
||||
*/
|
||||
public function get_followup_query($zone) {
|
||||
return <<<QUERY
|
||||
[out:json][timeout:60];
|
||||
area["ref:INSEE"="$zone"]->.searchArea;
|
||||
(
|
||||
nwr["emergency"="fire_hydrant"](area.searchArea);
|
||||
nwr["amenity"="charging_station"](area.searchArea);
|
||||
);
|
||||
out body;
|
||||
>;
|
||||
out skel qt;
|
||||
QUERY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les objets de suivi pour une ville (bornes incendie et bornes de recharge)
|
||||
*/
|
||||
public function followUpCity($zone) {
|
||||
$query = $this->get_followup_query($zone);
|
||||
try {
|
||||
$response = $this->client->request('POST', 'https://overpass-api.de/api/interpreter', [
|
||||
'body' => ['data' => $query],
|
||||
'timeout' => 60
|
||||
]);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new \Exception('L\'API Overpass a retourné un code de statut non-200 : ' . $response->getStatusCode());
|
||||
}
|
||||
$data = json_decode($response->getContent(), true);
|
||||
return $data['elements'] ?? [];
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue