From 9cdae30f7ef9c66b2b3a49738a737010d69d9b35 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Tue, 22 Dec 2020 02:25:56 +0100 Subject: [PATCH] Fix stupid accident dataset (remove duplicates?!?!) --- projects/project-3/backend/db_init.py | 16 ++++++++++------ projects/project-3/backend/routers/accidents.py | 9 ++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/projects/project-3/backend/db_init.py b/projects/project-3/backend/db_init.py index 51eae02..0bc2688 100644 --- a/projects/project-3/backend/db_init.py +++ b/projects/project-3/backend/db_init.py @@ -85,7 +85,8 @@ def init_database(): lon REAL, location TEXT, date TEXT, - severity TEXT + severity TEXT, + UNIQUE (lat, lon, date) )""") conn.commit() conn.close() @@ -143,7 +144,7 @@ def import_bikepoints(): def import_accidents(year): - LOG.info("Importing accidents") + LOG.info(f"Importing accidents for year {year}") conn = sqlite3.connect(DB_NAME, timeout=300) def filter_pedal_cycles(accident): @@ -155,13 +156,15 @@ def import_accidents(year): accidents = requests.get(f"https://api.tfl.gov.uk/AccidentStats/{year}").text accidents = json.loads(accidents) accidents = list(filter(filter_pedal_cycles, accidents)) - accidents = list(map(lambda a: (a['id'], a['lat'], a['lon'], a['location'], a['date'], a['severity']), accidents)) + accidents = list(map(lambda a: (a['lat'], a['lon'], a['location'], a['date'], a['severity']), accidents)) LOG.info(f"Writing {len(accidents)} bike accidents to DB") - conn.executemany("INSERT OR IGNORE INTO accidents VALUES (?, ?, ?, ?, ?, ?)", accidents) + conn.executemany("""INSERT OR IGNORE INTO + accidents(lat, lon, location, date, severity) + VALUES (?, ?, ?, ?, ?)""", accidents) conn.commit() conn.close() - LOG.info("Accidents importet") + LOG.info(f"Accidents imported for year {year}") def import_usage_stats_file(export_file: ApiExportFile): @@ -232,7 +235,8 @@ def main(): # Import Bikepoints import_bikepoints() # Import bike accidents - import_accidents(2019) + for year in range(2005, 2020): + import_accidents(year) if count_after - count_pre > 0: create_dashboard_table() diff --git a/projects/project-3/backend/routers/accidents.py b/projects/project-3/backend/routers/accidents.py index e9fe942..1706823 100644 --- a/projects/project-3/backend/routers/accidents.py +++ b/projects/project-3/backend/routers/accidents.py @@ -1,4 +1,5 @@ import logging +from enum import Enum from typing import List from fastapi import APIRouter @@ -10,10 +11,16 @@ router = APIRouter(prefix="/accidents", tags=["accidents", "local"]) LOG = logging.getLogger() +class Severity(str, Enum): + slight = "Slight" + serious = "Serious" + fatal = "Fatal" + + class Accident(BaseModel): lat: float lon: float - severity: str + severity: Severity @router.get(