From e379a00b7fc026640f25a800bbceebe45caade19 Mon Sep 17 00:00:00 2001 From: Theophile du Laz Date: Thu, 2 May 2024 09:08:24 -0700 Subject: [PATCH] Skymap API PUT: incorrect trigger id match bugfix (#289) * Don't look for existing skymap using a None trigger id, which ended up matching skymaps incorrectly. * After finding and existing map, use it's _id to update it if needed. * Add a unique index on dateobs + localization_name to avoid duplicates if the API code fails to find an existing map (should not happen, but it's a good failsafe anyway to have that constraint in the data model). --- config.defaults.yaml | 7 +++++++ kowalski/api/api.py | 25 +++++++++++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/config.defaults.yaml b/config.defaults.yaml index 5ed15726..8a736f07 100644 --- a/config.defaults.yaml +++ b/config.defaults.yaml @@ -305,6 +305,13 @@ kowalski: - ["candidate.dmag2mass", -1] unique: false + skymaps: + - name: dateobs_-1_localization_name_1 + fields: + - ["dateobs", -1] + - ["localization_name", 1] + unique: true + filters: ZTF_alerts: # default upstream aggregation pipeline stages for filtering ZTF alerts: diff --git a/kowalski/api/api.py b/kowalski/api/api.py index ebfc904e..b56b22c8 100644 --- a/kowalski/api/api.py +++ b/kowalski/api/api.py @@ -3317,14 +3317,22 @@ async def put(self, request: web.Request) -> web.Response: { "$or": [ {"dateobs": dateobs}, - {"triggerid": triggerid}, ], }, {"localization_name": skymap["localization_name"]}, ] } + if triggerid not in [None, ""]: + query["$and"][0]["$or"].append({"triggerid": triggerid}) if len(aliases) > 0: - query["$and"][0]["$or"].append({"aliases": {"$all": aliases}}) + query["$and"][0]["$or"].append( + { + "$and": [ + {"aliases": {"$all": aliases}}, + {"aliases": {"$size": len(aliases)}}, + ] + } + ) existing_skymap = await request.app["mongo"][ config["database"]["collections"]["skymaps"] @@ -3373,18 +3381,7 @@ async def put(self, request: web.Request) -> web.Response: await request.app["mongo"][ config["database"]["collections"]["skymaps"] ].update_one( - { - "$and": [ - { - "$or": [ - {"dateobs": dateobs}, - {"triggerid": triggerid}, - {"aliases": {"$all": aliases}}, - ] - }, - {"localization_name": skymap["localization_name"]}, - ] - }, + {"_id": existing_skymap["_id"]}, {"$set": {"contours": contours}}, ) return web.json_response(