Added enhancer.py and enhancer configuration

This commit is contained in:
Csaba 2023-12-11 13:13:29 +01:00
parent 05a0bcdb66
commit 3a784c230f
2 changed files with 121 additions and 0 deletions

View file

@ -0,0 +1,70 @@
import json
import time
import logging
import logging.config
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from enhancer_configuration import configure_enhancer_services
from amarillo.app.utils.container import container
from amarillo.app.models.Carpool import Carpool
from amarillo.app.utils.utils import agency_carpool_ids_from_filename
logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
logger = logging.getLogger("enhancer")
logger.info("Hello Enhancer")
configure_enhancer_services()
observer = Observer() # Watch Manager
class EventHandler(FileSystemEventHandler):
# TODO FG HB should watch for both carpools and agencies
# in data/agency, data/agencyconf, see AgencyConfService
def on_closed(self, event):
logger.info("CLOSE_WRITE: Created %s", event.src_path)
try:
with open(event.src_path, 'r', encoding='utf-8') as f:
dict = json.load(f)
carpool = Carpool(**dict)
container['carpools'].put(carpool.agency, carpool.id, carpool)
except FileNotFoundError as e:
logger.error("Carpool could not be added, as already deleted (%s)", event.src_path)
except:
logger.exception("Eventhandler on_closed encountered exception")
def on_deleted(self, event):
try:
logger.info("DELETE: Removing %s", event.src_path)
(agency_id, carpool_id) = agency_carpool_ids_from_filename(event.src_path)
container['carpools'].delete(agency_id, carpool_id)
except:
logger.exception("Eventhandler on_deleted encountered exception")
observer.schedule(EventHandler(), 'data/carpool', recursive=True)
observer.start()
import time
try:
# TODO FG Is this really needed?
cnt = 0
ENHANCER_LOG_INTERVAL_IN_S = 600
while True:
if cnt == ENHANCER_LOG_INTERVAL_IN_S:
logger.debug("Currently stored carpool ids: %s", container['carpools'].get_all_ids())
cnt = 0
time.sleep(1)
cnt += 1
finally:
observer.stop()
observer.join()
logger.info("Goodbye Enhancer")

View file

@ -0,0 +1,51 @@
# separate file so that it can be imported without initializing FastAPI
from amarillo.app.utils.container import container
import json
import logging
from glob import glob
from amarillo.app.models.Carpool import Carpool
from amarillo.app.services import stops
from amarillo.app.services import trips
from amarillo.app.services.carpools import CarpoolService
from amarillo.app.configuration import configure_services
logger = logging.getLogger(__name__)
def configure_enhancer_services():
configure_services()
logger.info("Load stops...")
stop_sources = [
{"url": "https://datahub.bbnavi.de/export/rideshare_points.geojson", "vicinity": 50},
{"url": "https://data.mfdz.de/mfdz/stops/stops_zhv.csv", "vicinity": 50},
{"url": "https://data.mfdz.de/mfdz/stops/parkings_osm.csv", "vicinity": 500},
]
stop_store = stops.StopsStore(stop_sources)
stop_store.load_stop_sources()
container['stops_store'] = stop_store
container['trips_store'] = trips.TripStore(stop_store)
container['carpools'] = CarpoolService(container['trips_store'])
logger.info("Restore carpools...")
for agency_id in container['agencies'].agencies:
for carpool_file_name in glob(f'data/carpool/{agency_id}/*.json'):
try:
with open(carpool_file_name) as carpool_file:
carpool = Carpool(**(json.load(carpool_file)))
container['carpools'].put(carpool.agency, carpool.id, carpool)
except Exception as e:
logger.warning("Issue during restore of carpool %s: %s", carpool_file_name, repr(e))
# notify carpool about carpools in trash, as delete notifications must be sent
for carpool_file_name in glob(f'data/trash/{agency_id}/*.json'):
with open(carpool_file_name) as carpool_file:
carpool = Carpool(**(json.load(carpool_file)))
container['carpools'].delete(carpool.agency, carpool.id)
logger.info("Restored carpools: %s", container['carpools'].get_all_ids())
logger.info("Starting scheduler")