Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with scheduled queries #7111

Merged
merged 4 commits into from
Oct 29, 2024

Conversation

ezraodio1
Copy link
Contributor

@ezraodio1 ezraodio1 commented Aug 6, 2024

What type of PR is this?

  • Refactor
  • Feature
  • Bug Fix
  • New Query Runner (Data Source)
  • New Alert Destination
  • Other

Description

There's currently an issue in Redash where some queries fail to refresh on their schedule. This happens when the query has no latest_query_data, which makes retrieved_at = None, which makes it so the query never gets added to outdated_queries. This PR fixes that.

The problem can be reproduced by running the unit test that's part of this PR (without making the fix first, of course)

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)

How is this tested?

  • Unit tests (pytest, jest)
  • E2E Tests (Cypress)
  • Manually
  • N/A

Related Tickets & Documents

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

eradman
eradman previously approved these changes Sep 20, 2024
Copy link
Collaborator

@eradman eradman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this manually, seems to check out

@justinclift
Copy link
Member

@arikfr Any idea if this will interact with the scheduled queries hash issue you were looking at yesterday?

redash/models/__init__.py Outdated Show resolved Hide resolved
Comment on lines 613 to 614
if all(value is None for value in query.schedule.values()):
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a little while since I looked at this, but I think this is the answer:

The only way to check whether a query doesn't have a refresh schedule is to ensure that everything in query.schedule.values() is None. For example, interval, until, time are all keys in the query.schedule. It's possible to have a query refresh schedule when just one of these keys is not None.

If a query doesn't have a refresh schedule, then we can just continue to the next query in the loop.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a query does not have a schedule then the schedule value is null (or empty). It's possible there is some edge case scenario that might keep schedule object with empty values, but I would assume it's not common?

To keep the code maintainable I would rather avoid this change in this PR and if you still feel it's necessary let's bring it back in a separate one and have the discussion there (and potentially add tests for this case).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is worth double checking. I could be mistaken, but I remember this edge case popping up during my testing. Perhaps if a query had a schedule and then that schedule was later removed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like we'll need to create a follow up PR to this one if/when we merge this. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found out that we have an explicit test case for this (schedule object with all values being None). I don't know if this is to cover some case that won't happen anymore, but just to be on the safe side I kept this logic + added a comment to explain.

Copy link
Member

@arikfr arikfr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last review. Please let us know if you have the bandwidth to apply the changes. Thanks!

Comment on lines 613 to 614
if all(value is None for value in query.schedule.values()):
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a query does not have a schedule then the schedule value is null (or empty). It's possible there is some edge case scenario that might keep schedule object with empty values, but I would assume it's not common?

To keep the code maintainable I would rather avoid this change in this PR and if you still feel it's necessary let's bring it back in a separate one and have the discussion there (and potentially add tests for this case).

redash/models/__init__.py Outdated Show resolved Hide resolved
@ezraodio1
Copy link
Contributor Author

I lost access to the VM that I was using over the summer for Redash dev work unfortunately, so I think it's easiest if someone else applies the changes.

@justinclift
Copy link
Member

justinclift commented Oct 9, 2024

@ezraodio1 If you need a new VM for doing Redash dev work on, then I'm happy to spin one up for you. Would that be useful? 😄

@ezraodio1
Copy link
Contributor Author

I'm taking a tough course load right, so I'm pretty busy. I'd definitely be interested after this semester ends, though. I'll try to spin up a new VM for myself.

@justinclift
Copy link
Member

No worries. Keep it in mind as an option if/when it'd be useful.

Good luck with your course load, hopefully it all goes really well. 😄

@justinclift
Copy link
Member

@arikfr Should we merge this PR as-is, or do you want to modify it a bit first? Asking because @ezraodio1 doesn't have the bandwidth to make changes at present.

@justinclift
Copy link
Member

@arikfr Should this be merged now? 😄

Copy link

codecov bot commented Oct 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.14%. Comparing base (a2611b8) to head (5dd129c).
Report is 34 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7111      +/-   ##
==========================================
- Coverage   64.15%   64.14%   -0.01%     
==========================================
  Files         162      162              
  Lines       13295    13341      +46     
  Branches     1883     1893      +10     
==========================================
+ Hits         8529     8558      +29     
- Misses       4432     4450      +18     
+ Partials      334      333       -1     
Files with missing lines Coverage Δ
redash/models/__init__.py 91.93% <100.00%> (+0.08%) ⬆️

... and 3 files with indirect coverage changes

Copy link
Member

@justinclift justinclift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eric already approved the previous revision of this. 😄

@justinclift justinclift enabled auto-merge (squash) October 29, 2024 10:17
@justinclift justinclift merged commit 673ba76 into getredash:master Oct 29, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants