Skip to content

Commit

Permalink
dont trigger again if there is a completed or deleted request
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodlz committed Oct 5, 2023
1 parent fbfb21c commit 42efe8f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
13 changes: 11 additions & 2 deletions kowalski/alert_brokers/alert_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,7 @@ def alert_sentinel_skyportal(self, alert, prv_candidates, passed_filters):
existing_requests = [
r
for r in existing_requests
if r["status"] in ["completed", "submitted"]
if r["status"] in ["completed", "submitted", "deleted"]
]
# sort by priority (highest first)
existing_requests = sorted(
Expand Down Expand Up @@ -1994,8 +1994,17 @@ def alert_sentinel_skyportal(self, alert, prv_candidates, passed_filters):
# if there is an existing request, but the priority is lower than the one we want to post,
# update the existing request with the new priority
request_to_update = existing_requests_filtered[0][1]
# if the status is completed or deleted, do not update
if request_to_update["status"] in ["completed", "deleted"]:
log(
f"Followup request for {alert['objectId']} and allocation_id {passed_filter['auto_followup']['allocation_id']} already exists on SkyPortal, but is completed or deleted, no need for update"
)
# if the status is submitted, and the new priority is higher, update
if (
passed_filter["auto_followup"]["data"]["payload"]["priority"]
request_to_update["status"] == "submitted"
and passed_filter["auto_followup"]["data"]["payload"][
"priority"
]
> request_to_update["payload"]["priority"]
):
with timer(
Expand Down
82 changes: 81 additions & 1 deletion kowalski/tests/test_alert_broker_ztf.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def test_alert_filter__user_defined_followup_with_broker(self):
assert any([g["id"] == saved_group_id for g in source["groups"]])
assert any([g["id"] == target_group_id for g in source["groups"]])

# verify that there is a follow-up request
# verify that there is no follow-up request
response = self.worker.api_skyportal(
"GET", f"/api/followup_request?sourceID={alert['objectId']}", None
)
Expand All @@ -560,6 +560,86 @@ def test_alert_filter__user_defined_followup_with_broker(self):
"DELETE", f"/api/classification/{classification_id}", None
)

# unsave the source from the group
response = self.worker.api_skyportal(
"POST",
"/api/source_groups",
{
"objId": alert["objectId"],
"unsaveGroupIds": [saved_group_id, target_group_id],
},
)
assert response.status_code == 200

# last but not least, we verify that we don't trigger the same request again, if a request has been submitted but then deleted, or is completed already
filter = filter_template(self.worker.collection_alerts)
filter["group_id"] = saved_group_id
filter["autosave"] = {
"active": True,
"comment": "Saved to BTS by BTSbot.",
}
filter["auto_followup"] = {
"active": True,
"pipeline": [
{
"$match": {
"candidate.drb": {"$gt": 0.5},
}
}
],
"allocation_id": allocation_id,
"payload": { # example payload for SEDM
"observation_type": "IFU",
"priority": 2,
"advanced": False,
},
"comment": "SEDM triggered by BTSbot",
}

passed_filters = self.worker.alert_filter__user_defined([filter], self.alert)
assert passed_filters is not None
assert len(passed_filters) == 1
assert "auto_followup" in passed_filters[0]
assert (
passed_filters[0]["auto_followup"]["data"]["payload"]["observation_type"]
== "IFU"
)

alert, prv_candidates = self.worker.alert_mongify(self.alert)
self.worker.alert_sentinel_skyportal(alert, prv_candidates, passed_filters)

# now fetch the follow-up request from SP
# we should not have any submitted follow-up requests
response = self.worker.api_skyportal(
"GET", f"/api/followup_request?sourceID={alert['objectId']}", None
)
assert response.status_code == 200
followup_requests = response.json()["data"].get("followup_requests", [])
submitted = [
f
for f in followup_requests
if (f["allocation_id"] == allocation_id and f["status"] == "submitted")
]
deleted = [
f
for f in followup_requests
if (f["allocation_id"] == allocation_id and f["status"] == "deleted")
]
assert len(submitted) == 0
assert len(deleted) > 0

# verify that it was save still
response = self.worker.api_skyportal(
"GET", f"/api/sources/{alert['objectId']}", None
)
assert response.status_code == 200
source = response.json()["data"]
assert source["id"] == "ZTF20aajcbhr"
# should be saved to Program A and Sitewide Group, but not Program B
assert any([g["id"] == saved_group_id for g in source["groups"]])
assert not any([g["id"] == target_group_id for g in source["groups"]])
assert not any([g["id"] == ignore_if_saved_group_id for g in source["groups"]])

# unsave the source from the group
response = self.worker.api_skyportal(
"POST",
Expand Down

0 comments on commit 42efe8f

Please sign in to comment.