From b3116687f6c043d127da29af5c58e15fbdc41ac5 Mon Sep 17 00:00:00 2001 From: Fabrizio Miano Date: Tue, 20 Oct 2020 19:20:04 +0200 Subject: [PATCH] Fix db-update API --- app/api/endpoints.py | 8 ++++---- app/utils/data.py | 19 +++++++++++-------- config.py | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/app/api/endpoints.py b/app/api/endpoints.py index 6cc4162..5d74f19 100644 --- a/app/api/endpoints.py +++ b/app/api/endpoints.py @@ -42,14 +42,14 @@ def update_db(): *latest*.json file :return: dict """ - app.logger.warning("Received db update request") - response = {"ts": dt.datetime.utcnow()} + app.logger.warning("Received db-update request") + response = {"ts": dt.datetime.utcnow(), "modified_json_files": []} message = "nothing to update" try: payload = request.json - do_update, modified_files = need_update(payload) + do_update, modified_json_files = need_update(payload) if do_update: - response["modified_files"] = modified_files + response["modified_json_files"] = modified_json_files app.logger.warning("New files added. Need to update collections") update_collections() message = "collections updated" diff --git a/app/utils/data.py b/app/utils/data.py index 7968385..befd4b2 100644 --- a/app/utils/data.py +++ b/app/utils/data.py @@ -16,7 +16,7 @@ PROVINCIAL_DATA_COLLECTION, URL_NATIONAL_DATA, URL_REGIONAL_DATA, URL_PROVINCIAL_DATA, URL_LATEST_REGIONAL_DATA, URL_LATEST_PROVINCIAL_DATA, LATEST_REGIONAL_DATA_COLLECTION, LATEST_PROVINCIAL_DATA_COLLECTION, - CP_DATAFILE_MONITOR, TOTAL_SWABS_KEY + DATA_TO_MONITOR, TOTAL_SWABS_KEY ) NATIONAL_COLLECTION = mongo.db[NATIONAL_DATA_COLLECTION] @@ -544,24 +544,27 @@ def update_collections(): def need_update(payload): """ - Return a bool do_update and a list of modified_files + Return a bool do_update and a list of modified_json_files :param payload: dict :return: do_update: bool: if True will update collection, else will not - modified_files: list: last civil-protection commit modified files + modified_json_files: list: last civil-protection commit modified files """ - modified_files = [] + modified_json_files = [] commits = payload.get("commits") do_update = False if commits is not None: for c in commits: commit_modified_files = c.get("modified") if commit_modified_files is not None: - modified_files.extend(commit_modified_files) - app.logger.debug("Modified files: {}".format(modified_files)) - if any(CP_DATAFILE_MONITOR in _file for _file in modified_files): + modified_json_files.extend([ + cmf for cmf in commit_modified_files + if cmf.endswith(DATA_TO_MONITOR) + ]) + app.logger.debug("Modified JSON files: {}".format(modified_json_files)) + if len(modified_json_files) > 0: do_update = True - return do_update, modified_files + return do_update, modified_json_files def get_last_week_national_data(): diff --git a/config.py b/config.py index 2c009cc..736ea8d 100644 --- a/config.py +++ b/config.py @@ -26,7 +26,7 @@ CHART_DATE_FMT = "%d %b" UPDATE_FMT = "%d/%m/%Y %H:%M" CP_DATE_KEY = "data" -CP_DATAFILE_MONITOR = "latest.json" +DATA_TO_MONITOR = "json" NOTE_KEY = "note" RUBBISH_NOTE_REGEX = r"[a-z][a-z]-[A-Z]\w+-[0-9][0-9][0-9][0-9]" TRANSLATION_DIRNAME = "translations"