Adjust naming conventions

This commit is contained in:
Marcel Schwarz 2020-12-19 15:41:35 +01:00
parent 7704bfcc27
commit 6ee5586b74

View File

@ -7,11 +7,13 @@ from datetime import datetime
import requests import requests
DB_NAME = "bike-data.db"
logFormatter = logging.Formatter("%(asctime)-15s [%(levelname)8s] [%(threadName)s] - %(message)s") logFormatter = logging.Formatter("%(asctime)-15s [%(levelname)8s] [%(threadName)s] - %(message)s")
LOG = logging.getLogger() LOG = logging.getLogger()
LOG.setLevel(logging.DEBUG) LOG.setLevel(logging.DEBUG)
fileHandler = logging.FileHandler("importer.log") fileHandler = logging.FileHandler("db_init.log")
fileHandler.setFormatter(logFormatter) fileHandler.setFormatter(logFormatter)
LOG.addHandler(fileHandler) LOG.addHandler(fileHandler)
@ -57,7 +59,7 @@ def get_online_files_list(subdir_filter=None, file_extension_filter=None):
def init_database(): def init_database():
LOG.info("Try to create tables") LOG.info("Try to create tables")
conn = sqlite3.connect("bike-data.db", timeout=300) conn = sqlite3.connect(DB_NAME, timeout=300)
conn.execute("""CREATE TABLE IF NOT EXISTS usage_stats( conn.execute("""CREATE TABLE IF NOT EXISTS usage_stats(
rental_id INTEGER PRIMARY KEY, rental_id INTEGER PRIMARY KEY,
duration INTEGER, duration INTEGER,
@ -92,7 +94,7 @@ def init_database():
def create_indexes(): def create_indexes():
LOG.info("Try to create indexes") LOG.info("Try to create indexes")
conn = sqlite3.connect("bike-data.db", timeout=300) conn = sqlite3.connect(DB_NAME, timeout=300)
conn.execute("""CREATE INDEX IF NOT EXISTS idx_date_of_start_date conn.execute("""CREATE INDEX IF NOT EXISTS idx_date_of_start_date
ON usage_stats (date(start_date, "unixepoch"))""") ON usage_stats (date(start_date, "unixepoch"))""")
conn.commit() conn.commit()
@ -102,7 +104,7 @@ def create_indexes():
def import_bikepoints(): def import_bikepoints():
LOG.info("Importing bikepoints") LOG.info("Importing bikepoints")
conn = sqlite3.connect("bike-data.db", timeout=300) conn = sqlite3.connect(DB_NAME, timeout=300)
points = json.loads(requests.get("https://api.tfl.gov.uk/BikePoint").text) points = json.loads(requests.get("https://api.tfl.gov.uk/BikePoint").text)
points = list(map(lambda p: (p['id'], p['commonName'], p['lat'], p['lon']), points)) points = list(map(lambda p: (p['id'], p['commonName'], p['lat'], p['lon']), points))
@ -115,7 +117,7 @@ def import_bikepoints():
def import_accidents(year): def import_accidents(year):
LOG.info("Importing accidents") LOG.info("Importing accidents")
conn = sqlite3.connect("bike-data.db", timeout=300) conn = sqlite3.connect(DB_NAME, timeout=300)
def filter_pedal_cycles(accident): def filter_pedal_cycles(accident):
for vehicle in accident['vehicles']: for vehicle in accident['vehicles']:
@ -136,7 +138,7 @@ def import_accidents(year):
def import_usage_stats_file(export_file: ApiExportFile): def import_usage_stats_file(export_file: ApiExportFile):
conn = sqlite3.connect("bike-data.db", timeout=300) conn = sqlite3.connect(DB_NAME, timeout=300)
rows = conn.execute("SELECT * FROM read_files WHERE etag LIKE ?", (export_file.etag,)).fetchall() rows = conn.execute("SELECT * FROM read_files WHERE etag LIKE ?", (export_file.etag,)).fetchall()
if len(rows) != 0: if len(rows) != 0: