def export_centres_stats(center_data=Path( get_conf_outputs().get("last_scans")), stats_path=get_conf_outstats().get("global")): if center_data.exists(): centres_info = get_centres_info(center_data) centres_stats = { "tout_departement": { "disponibles": 0, "total": 0, "creneaux": 0 } } tout_dep_obj = centres_stats["tout_departement"] for dep_code, dep_value in centres_info.items(): nombre_disponibles = len(dep_value["centres_disponibles"]) count = len( dep_value["centres_indisponibles"]) + nombre_disponibles creneaux = sum([ center.get("appointment_count", 0) for center in dep_value["centres_disponibles"] ]) centres_stats[dep_code] = { "disponibles": nombre_disponibles, "total": count, "creneaux": creneaux, } tout_dep_obj["disponibles"] += nombre_disponibles tout_dep_obj["total"] += count tout_dep_obj["creneaux"] += creneaux available_pct = (tout_dep_obj["disponibles"] / max(1, tout_dep_obj["total"])) * 100 logger.info("Found {0}/{1} available centers. ({2}%)".format( tout_dep_obj["disponibles"], tout_dep_obj["total"], round(available_pct, 2), )) with open(Path("data", "output", stats_path), "w") as stats_file: json.dump(centres_stats, stats_file, indent=2) if stats_path != get_conf_outstats().get("global"): return generate_stats_date(centres_stats) generate_stats_dep_date(centres_stats) generate_stats_center_types(centres_info) make_maps(centres_info)
def generate_stats_date(centres_stats): stats_path = get_conf_outstats().get("by_date") stats_data = { "dates": [], "total_centres_disponibles": [], "total_centres": [], "total_appointments": [], } try: history_rq = requests.get(f"{DATA_AUTO}{stats_path}") data = history_rq.json() if data: stats_data = data except Exception as e: logger.warning( f"Unable to fetch {DATA_AUTO}{stats_path}: generating a template file." ) pass ctz = pytz.timezone("Europe/Paris") current_time = datetime.now(tz=ctz).strftime("%Y-%m-%d %H:00:00") if current_time in stats_data["dates"]: with open(Path("data", "output", stats_path), "w") as stat_graph_file: json.dump(stats_data, stat_graph_file) logger.info(f"Stats file already updated: {stats_path}") return data_alldep = centres_stats["tout_departement"] stats_data["dates"].append(current_time) stats_data["total_centres_disponibles"].append(data_alldep["disponibles"]) stats_data["total_centres"].append(data_alldep["total"]) stats_data["total_appointments"].append(data_alldep["creneaux"]) with open(Path("data", "output", stats_path), "w") as stat_graph_file: json.dump(stats_data, stat_graph_file) logger.info(f"Updated stats file: {stats_path}")
def generate_stats_center_types(centres_info): stats_path = get_conf_outstats().get("center_types") stats_data = {"dates": [], "plateformes": {}, "center_types": {}} try: history_rq = requests.get(f"{DATA_AUTO}{stats_path}") data = history_rq.json() if data: stats_data = data except Exception as e: logger.warning( f"Unable to fetch {DATA_AUTO}{stats_path}: generating a template file." ) pass ctz = pytz.timezone("Europe/Paris") current_time = datetime.now(tz=ctz).strftime("%Y-%m-%d %H:00:00") if current_time in stats_data["dates"]: with open(f"data/output/{stats_path}", "w") as stat_graph_file: json.dump(stats_data, stat_graph_file) logger.info(f"Stats file already updated: {stats_path}") return if "center_types" not in stats_data: stats_data["center_types"] = {} stats_data["dates"].append(current_time) current_calc = compute_plateforme_data(centres_info) for plateforme in current_calc[0]: plateform_data = current_calc[0][plateforme] if plateforme not in stats_data["plateformes"]: stats_data["plateformes"][plateforme] = { "disponible": [plateform_data["disponible"]], "total": [plateform_data["total"]], "creneaux": [plateform_data["creneaux"]], } continue current_data = stats_data["plateformes"][plateforme] current_data["disponible"].append(plateform_data["disponible"]) current_data["total"].append(plateform_data["total"]) current_data["creneaux"].append(plateform_data["creneaux"]) for center_type in current_calc[1]: center_type_data = current_calc[1][center_type] if center_type not in stats_data["center_types"]: stats_data["center_types"][center_type] = { "disponible": [center_type_data["disponible"]], "total": [center_type_data["total"]], "creneaux": [center_type_data["creneaux"]], } continue current_data = stats_data["center_types"][center_type] current_data["disponible"].append(center_type_data["disponible"]) current_data["total"].append(center_type_data["total"]) current_data["creneaux"].append(center_type_data["creneaux"]) with open(f"data/output/{stats_path}", "w") as stat_graph_file: json.dump(stats_data, stat_graph_file) logger.info(f"Updated stats file: {stats_path}")
import json import logging from datetime import datetime import pytz import requests from utils.vmd_config import get_conf_outstats from utils.vmd_logger import enable_logger_for_production logger = logging.getLogger("scraper") DATA_AUTO = get_conf_outstats().get("data-auto") def compute_plateforme_data(centres_info): plateformes = {} center_types = {} for dep in centres_info: dep_data = centres_info[dep] centers = dep_data.get("centres_disponibles", {}) centers.extend(dep_data.get("centres_indisponibles", {})) for centre_dispo in centers: plateforme = centre_dispo["plateforme"] if not plateforme: plateforme = "Autre" center_type = centre_dispo["type"] if not center_type:
--input="some/file.json" # Optional (should follow the same structure as data/output/info_centres.json.)\ --output="put/it/here.json" # Optional \ --national # Optional: Whether or not to add a summary national statistic. ``` """ import argparse import json import sys from pathlib import Path from typing import Dict, List from utils.vmd_config import get_conf_outputs, get_conf_outstats _default_input = Path(get_conf_outputs().get("last_scans")) _default_output = Path(get_conf_outstats().get("chronodoses")) def count_departments_chronodoses(data: dict) -> Dict[str, int]: return { department_id: _department_chronodoses(department_data) for department_id, department_data in data.items() } def _national_doses(per_department: Dict[str, int]) -> dict: return sum(per_department.values()) def _department_chronodoses(department_data: dict) -> int: """Sums the available chronodoses for a given department.
--input="some/file.json" # Optional (should follow the same structure as data/output/info_centres.json.)\ --output="put/it/here.json" # Optional ``` """ import argparse import json import sys from functools import reduce from pathlib import Path from typing import Iterator, List, Tuple from utils.vmd_config import get_conf_outputs, get_conf_outstats _default_input = Path(get_conf_outputs().get("last_scans")) _default_output = Path(get_conf_outstats().get("by_vaccine_type")) def parse_args(args: List[str]) -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument( "--input", default=_default_input, type=Path, help= "File with the statistics per department. Should follow the same structure as info_centres.json.", ) parser.add_argument( "--output", default=_default_output, type=Path,