From 6bde8304d18108a70879feacda7a1e91b617e9ef Mon Sep 17 00:00:00 2001 From: brendanrmills Date: Fri, 1 Jul 2022 10:50:39 -0700 Subject: [PATCH 1/6] added parameter for max_alerts --- tom_antares/antares.py | 14 +++++++++++++- tom_antares/tests/tests.py | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tom_antares/antares.py b/tom_antares/antares.py index 9eb933a..6692dda 100644 --- a/tom_antares/antares.py +++ b/tom_antares/antares.py @@ -130,6 +130,13 @@ class ANTARESBrokerForm(GenericQueryForm): label='Elastic Search query in JSON format', widget=forms.TextInput(attrs={'placeholder': '{"query":{}}'}), ) + max_alerts = forms.FloatField( + required=False, + label = 'Maximum number of alerts to fetch', + widget=forms.TextInput(attrs={'placeholder': 'Max Alerts'}), + min_value=0.0, + initial = 50 + ) # cone_search = ConeSearchField() # api_search_tags = forms.MultipleChoiceField(choices=get_tag_choices) @@ -221,6 +228,10 @@ def __init__(self, *args, **kwargs): 'View Tags', 'tag' ), + Fieldset( + 'Max Alerts', + 'max_alerts' + ), HTML('
'), HTML('

Advanced query

'), Fieldset( @@ -318,6 +329,7 @@ def fetch_alerts(self, parameters: dict) -> iter: mag_max = parameters.get('mag__max') elsquery = parameters.get('esquery') ztfid = parameters.get('ztfid') + max_alerts = parameters.get('max_alerts') if ztfid: query = { "query": { @@ -384,7 +396,7 @@ def fetch_alerts(self, parameters: dict) -> iter: # if ztfid: # loci = get_by_ztf_object_id(ztfid) alerts = [] - while len(alerts) < 20: + while len(alerts) < max_alerts: try: locus = next(loci) except (marshmallow.exceptions.ValidationError, StopIteration): diff --git a/tom_antares/tests/tests.py b/tom_antares/tests/tests.py index f5add54..47666ac 100644 --- a/tom_antares/tests/tests.py +++ b/tom_antares/tests/tests.py @@ -27,10 +27,18 @@ def test_fetch_alerts(self, mock_client): # NOTE: if .side_effect is going to return a list, it needs a function that returns a list mock_client.search.search.side_effect = lambda loci: iter(self.loci) expected_alert = ANTARESBroker.alert_to_dict(self.locus) - alerts = ANTARESBroker().fetch_alerts({'tag': [self.tag]}) + alerts = ANTARESBroker().fetch_alerts({'tag': [self.tag], 'max_alerts': 3}) # TODO: compare iterator length with len(self.loci) self.assertEqual(next(alerts), expected_alert) + + @mock.patch('tom_antares.antares.antares_client') + def test_fetch_alerts_max_alerts(self, mock_client): + '''Tests that the max_alerts parameter actually affects the length of the alert stream''' + mock_client.search.search.side_effect = lambda loci: iter(self.loci) + alerts = ANTARESBroker().fetch_alerts({'max_alerts': 4}) + self.assertEqual(len(list(alerts)), 4) + def test_to_target_with_horizons_targetname(self): """ From 58f220702b50eac55af0f1540f22dca26f01c8ff Mon Sep 17 00:00:00 2001 From: brendanrmills Date: Wed, 6 Jul 2022 11:49:01 -0700 Subject: [PATCH 2/6] removed blank line --- tom_antares/tests/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tom_antares/tests/tests.py b/tom_antares/tests/tests.py index 47666ac..f16209c 100644 --- a/tom_antares/tests/tests.py +++ b/tom_antares/tests/tests.py @@ -39,7 +39,6 @@ def test_fetch_alerts_max_alerts(self, mock_client): alerts = ANTARESBroker().fetch_alerts({'max_alerts': 4}) self.assertEqual(len(list(alerts)), 4) - def test_to_target_with_horizons_targetname(self): """ Test that the expected names are created. From 7b6ae5d56341ff0e4c30fa50c9b92283f9c39c44 Mon Sep 17 00:00:00 2001 From: brendanrmills Date: Wed, 6 Jul 2022 13:39:15 -0700 Subject: [PATCH 3/6] remove spaces around = --- tom_antares/antares.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tom_antares/antares.py b/tom_antares/antares.py index 6692dda..615c745 100644 --- a/tom_antares/antares.py +++ b/tom_antares/antares.py @@ -132,10 +132,10 @@ class ANTARESBrokerForm(GenericQueryForm): ) max_alerts = forms.FloatField( required=False, - label = 'Maximum number of alerts to fetch', + label='Maximum number of alerts to fetch', widget=forms.TextInput(attrs={'placeholder': 'Max Alerts'}), min_value=0.0, - initial = 50 + initial=50 ) # cone_search = ConeSearchField() From c3821bda31237627b0ecc6543e1d3e9d44b73411 Mon Sep 17 00:00:00 2001 From: brendanrmills Date: Wed, 6 Jul 2022 13:42:44 -0700 Subject: [PATCH 4/6] formatting fix --- tom_antares/tests/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tom_antares/tests/tests.py b/tom_antares/tests/tests.py index f16209c..87b6f1c 100644 --- a/tom_antares/tests/tests.py +++ b/tom_antares/tests/tests.py @@ -31,10 +31,10 @@ def test_fetch_alerts(self, mock_client): # TODO: compare iterator length with len(self.loci) self.assertEqual(next(alerts), expected_alert) - + @mock.patch('tom_antares.antares.antares_client') def test_fetch_alerts_max_alerts(self, mock_client): - '''Tests that the max_alerts parameter actually affects the length of the alert stream''' + """Tests that the max_alerts parameter actually affects the length of the alert stream""" mock_client.search.search.side_effect = lambda loci: iter(self.loci) alerts = ANTARESBroker().fetch_alerts({'max_alerts': 4}) self.assertEqual(len(list(alerts)), 4) From e00a90e6fd8fe7e36fbe8252f37864fe05265cee Mon Sep 17 00:00:00 2001 From: Joseph Chatelain <32683393+jchate6@users.noreply.github.com> Date: Fri, 24 Mar 2023 09:44:03 -0700 Subject: [PATCH 5/6] Change default max to 20 and allow for queries that have no max --- tom_antares/antares.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tom_antares/antares.py b/tom_antares/antares.py index 615c745..5a6f6ee 100644 --- a/tom_antares/antares.py +++ b/tom_antares/antares.py @@ -135,7 +135,7 @@ class ANTARESBrokerForm(GenericQueryForm): label='Maximum number of alerts to fetch', widget=forms.TextInput(attrs={'placeholder': 'Max Alerts'}), min_value=0.0, - initial=50 + initial=20 ) # cone_search = ConeSearchField() @@ -329,7 +329,7 @@ def fetch_alerts(self, parameters: dict) -> iter: mag_max = parameters.get('mag__max') elsquery = parameters.get('esquery') ztfid = parameters.get('ztfid') - max_alerts = parameters.get('max_alerts') + max_alerts = parameters.get('max_alerts', 20) if ztfid: query = { "query": { From 5e379072d8410d46c63a77d90f19afa6a58e68d1 Mon Sep 17 00:00:00 2001 From: Joseph Chatelain Date: Fri, 24 Mar 2023 13:25:34 -0700 Subject: [PATCH 6/6] force a min value for alerts. --- tom_antares/antares.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tom_antares/antares.py b/tom_antares/antares.py index 5a6f6ee..f219205 100644 --- a/tom_antares/antares.py +++ b/tom_antares/antares.py @@ -131,10 +131,9 @@ class ANTARESBrokerForm(GenericQueryForm): widget=forms.TextInput(attrs={'placeholder': '{"query":{}}'}), ) max_alerts = forms.FloatField( - required=False, label='Maximum number of alerts to fetch', widget=forms.TextInput(attrs={'placeholder': 'Max Alerts'}), - min_value=0.0, + min_value=1, initial=20 )