Skip to content

Commit

Permalink
Add scheduled queries with no latest_query_data to outdated_queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezra Odio authored and eradman committed Sep 20, 2024
1 parent 38dc31a commit f1b6be0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
22 changes: 15 additions & 7 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ def groups(self):
def should_schedule_next(previous_iteration, now, interval, time=None, day_of_week=None, failures=0):
# if time exists then interval > 23 hours (82800s)
# if day_of_week exists then interval > 6 days (518400s)
if previous_iteration is None:
return False
if time is None:
ttl = int(interval)
next_iteration = previous_iteration + datetime.timedelta(seconds=ttl)
Expand Down Expand Up @@ -608,17 +610,23 @@ def outdated_queries(cls):
if schedule_until <= now:
continue

if all(value is None for value in query.schedule.values()):
continue

retrieved_at = scheduled_queries_executions.get(query.id) or (
query.latest_query_data and query.latest_query_data.retrieved_at
)

if should_schedule_next(
retrieved_at or now,
now,
query.schedule["interval"],
query.schedule["time"],
query.schedule["day_of_week"],
query.schedule_failures,
if (
should_schedule_next(
retrieved_at,
now,
query.schedule["interval"],
query.schedule["time"],
query.schedule["day_of_week"],
query.schedule_failures,
)
or not retrieved_at
):
key = "{}:{}".format(query.query_hash, query.data_source_id)
outdated_queries[key] = query
Expand Down
14 changes: 14 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ def test_enqueues_query_only_once(self):

self.assertEqual(list(models.Query.outdated_queries()), [query2])

def test_enqueues_scheduled_query_without_latest_query_data(self):
"""
Queries with a schedule but no latest_query_data will still be reported by Query.outdated_queries()
"""
query = self.factory.create_query(
schedule=self.schedule(interval="60"),
data_source=self.factory.create_data_source(),
)

outdated_queries = models.Query.outdated_queries()
self.assertEqual(query.latest_query_data, None)
self.assertEqual(len(outdated_queries), 1)
self.assertIn(query, outdated_queries)

def test_enqueues_query_with_correct_data_source(self):
"""
Queries from different data sources will be reported by
Expand Down

0 comments on commit f1b6be0

Please sign in to comment.