Skip to content

Commit

Permalink
Fix db-update API
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziomiano committed Oct 20, 2020
1 parent a048931 commit b311668
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions app/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 11 additions & 8 deletions app/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b311668

Please sign in to comment.