mirror of
https://forge.chapril.org/tykayn/osm-commerces
synced 2025-10-09 17:02:46 +02:00
import city followup from csv
This commit is contained in:
parent
eee5d6349a
commit
dfeaf123f4
4 changed files with 35253 additions and 1 deletions
235
src/Command/ImportCityFollowupFromCsvCommand.php
Normal file
235
src/Command/ImportCityFollowupFromCsvCommand.php
Normal file
|
@ -0,0 +1,235 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\CityFollowUp;
|
||||
use App\Entity\Stats;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:import-cityfollowup-csv',
|
||||
description: 'Importe les mesures thématiques depuis un fichier CSV pour une ville donnée'
|
||||
)]
|
||||
class ImportCityFollowupFromCsvCommand extends Command
|
||||
{
|
||||
private const CSV_DIR = '/home/poule/encrypted/stockage-syncable/www/development/html/osm-commerce-sf/counting_osm_objects/test_results';
|
||||
|
||||
public function __construct(
|
||||
private EntityManagerInterface $entityManager
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('insee', InputArgument::REQUIRED, 'Code INSEE de la ville')
|
||||
->addArgument('theme', InputArgument::REQUIRED, 'Thématique à importer (ex: borne-de-recharge, defibrillator)')
|
||||
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Simuler l\'import sans modifier la base de données');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$insee = $input->getArgument('insee');
|
||||
$theme = $input->getArgument('theme');
|
||||
$dryRun = $input->getOption('dry-run');
|
||||
|
||||
// Vérifier que la ville existe
|
||||
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee]);
|
||||
if (!$stats) {
|
||||
$io->error("Aucune ville trouvée avec le code INSEE $insee.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->title("Import des mesures thématiques pour {$stats->getName()} (INSEE: $insee) - Thème: $theme");
|
||||
|
||||
// Trouver le fichier CSV correspondant
|
||||
$csvPath = $this->findCsvFile($insee, $theme);
|
||||
if (!$csvPath) {
|
||||
$io->error("Aucun fichier CSV trouvé pour la ville $insee et la thématique $theme.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->info("Fichier CSV trouvé: $csvPath");
|
||||
|
||||
// Lire le fichier CSV
|
||||
$csvData = $this->readCsvFile($csvPath);
|
||||
if (empty($csvData)) {
|
||||
$io->error("Le fichier CSV est vide ou mal formaté.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->info(sprintf("Nombre de lignes dans le CSV: %d", count($csvData)));
|
||||
|
||||
// Récupérer les mesures existantes pour cette ville et cette thématique
|
||||
$existingMeasures = $this->getExistingMeasures($stats, $theme);
|
||||
$io->info(sprintf("Nombre de mesures existantes en base: %d", count($existingMeasures)));
|
||||
|
||||
// Comparer et importer les nouvelles mesures
|
||||
$imported = 0;
|
||||
$skipped = 0;
|
||||
$zeroSkipped = 0;
|
||||
|
||||
foreach ($csvData as $row) {
|
||||
// Ignorer les lignes avec des valeurs manquantes
|
||||
if (empty($row['date']) || empty($row['nombre_total']) || $row['nombre_total'] === '') {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Convertir la date
|
||||
try {
|
||||
$date = new \DateTime($row['date']);
|
||||
} catch (\Exception $e) {
|
||||
$io->warning("Date invalide: {$row['date']}");
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Ignorer les mesures qui valent 0
|
||||
if ((float)$row['nombre_total'] === 0.0) {
|
||||
$zeroSkipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Vérifier si la mesure existe déjà
|
||||
$dateStr = $date->format('Y-m-d');
|
||||
if (isset($existingMeasures[$dateStr])) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Créer une nouvelle mesure pour le nombre total
|
||||
if (!$dryRun) {
|
||||
$followUp = new CityFollowUp();
|
||||
$followUp->setName($this->convertThemeToCityFollowUpName($theme) . '_count')
|
||||
->setMeasure((float)$row['nombre_total'])
|
||||
->setDate($date)
|
||||
->setStats($stats);
|
||||
$this->entityManager->persist($followUp);
|
||||
|
||||
// Créer une mesure pour la complétion si disponible
|
||||
if (isset($row['pourcentage_completion']) && $row['pourcentage_completion'] !== '') {
|
||||
$followUpCompletion = new CityFollowUp();
|
||||
$followUpCompletion->setName($this->convertThemeToCityFollowUpName($theme) . '_completion')
|
||||
->setMeasure((float)$row['pourcentage_completion'])
|
||||
->setDate($date)
|
||||
->setStats($stats);
|
||||
$this->entityManager->persist($followUpCompletion);
|
||||
}
|
||||
|
||||
$imported++;
|
||||
|
||||
// Flush toutes les 50 entités pour éviter de surcharger la mémoire
|
||||
if ($imported % 50 === 0) {
|
||||
$this->entityManager->flush();
|
||||
$this->entityManager->clear(CityFollowUp::class);
|
||||
}
|
||||
} else {
|
||||
$imported++;
|
||||
}
|
||||
}
|
||||
|
||||
// Flush final
|
||||
if (!$dryRun && $imported > 0) {
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
if ($dryRun) {
|
||||
$io->success("Simulation terminée: $imported mesures seraient importées, $skipped mesures ignorées (déjà existantes), $zeroSkipped mesures ignorées (valeur zéro).");
|
||||
} else {
|
||||
$io->success("Import terminé: $imported mesures importées, $skipped mesures ignorées (déjà existantes), $zeroSkipped mesures ignorées (valeur zéro).");
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trouve le fichier CSV correspondant à la ville et à la thématique
|
||||
*/
|
||||
private function findCsvFile(string $insee, string $theme): ?string
|
||||
{
|
||||
$finder = new Finder();
|
||||
$finder->files()
|
||||
->in(self::CSV_DIR)
|
||||
->name("commune_{$insee}_{$theme}.csv");
|
||||
|
||||
if (!$finder->hasResults()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($finder as $file) {
|
||||
return $file->getRealPath();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit le fichier CSV et retourne un tableau de données
|
||||
*/
|
||||
private function readCsvFile(string $path): array
|
||||
{
|
||||
$data = [];
|
||||
if (($handle = fopen($path, "r")) !== false) {
|
||||
// Lire l'en-tête
|
||||
$header = fgetcsv($handle, 1000, ",");
|
||||
if ($header === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Lire les données
|
||||
while (($row = fgetcsv($handle, 1000, ",")) !== false) {
|
||||
$rowData = [];
|
||||
foreach ($header as $i => $key) {
|
||||
$rowData[$key] = $row[$i] ?? '';
|
||||
}
|
||||
$data[] = $rowData;
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère les mesures existantes pour cette ville et cette thématique
|
||||
*/
|
||||
private function getExistingMeasures(Stats $stats, string $theme): array
|
||||
{
|
||||
$themeName = $this->convertThemeToCityFollowUpName($theme);
|
||||
$existingMeasures = [];
|
||||
|
||||
foreach ($stats->getCityFollowUps() as $followUp) {
|
||||
if ($followUp->getName() === $themeName . '_count') {
|
||||
$date = $followUp->getDate()->format('Y-m-d');
|
||||
$existingMeasures[$date] = $followUp;
|
||||
}
|
||||
}
|
||||
|
||||
return $existingMeasures;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convertit le nom de thématique du CSV en nom utilisé dans CityFollowUp
|
||||
*/
|
||||
private function convertThemeToCityFollowUpName(string $theme): string
|
||||
{
|
||||
$themeMapping = [
|
||||
'borne-de-recharge' => 'charging_station',
|
||||
'defibrillator' => 'defibrillator',
|
||||
// Ajouter d'autres mappings si nécessaire
|
||||
];
|
||||
|
||||
return $themeMapping[$theme] ?? $theme;
|
||||
}
|
||||
}
|
|
@ -512,4 +512,4 @@ class FollowUpService
|
|||
'playground' => ['playground', 'operator'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue