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

Add support for a default alert template #5996

Merged
merged 2 commits into from
Jul 17, 2023

Conversation

eradman
Copy link
Collaborator

@eradman eradman commented Apr 19, 2023

Description

Add support for a default alert template

  • Escape all variables by default since Mustache only has a syntax for raw values
  • Generate a generic HTML result table to allow an alert template to display any query
  • Optionally allow alert template to be defined to be set using REDASH_ALERTS_DEFAULT_MAIL_BODY_TEMPLATE

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

redash-alert-ok
redash-alert-triggered

@eradman eradman changed the title Alert email Add support for a default alert template Apr 19, 2023
@eradman eradman force-pushed the alert-email branch 2 times, most recently from 96e9306 to b6e99e0 Compare July 3, 2023 17:10
@guidopetri
Copy link
Contributor

@eradman thanks for your contribution! Would you mind rebasing on master and pushing to rerun CI checks?

@guidopetri guidopetri added the Team Review Meets PR criteria, ready for full review label Jul 6, 2023
@guidopetri
Copy link
Contributor

This LGTM! Would you mind also opening a PR for docs for REDASH_ALERTS_DEFAULT_MAIL_BODY_TEMPLATE in this repo/file?

cc @justinclift

@eradman
Copy link
Collaborator Author

eradman commented Jul 6, 2023

Certainly! Proposed an update to the docs in getredash/website#673

Copy link
Member

@junnplus junnplus left a comment

Choose a reason for hiding this comment

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

We should have test coverage for them.

@eradman
Copy link
Collaborator Author

eradman commented Jul 7, 2023

I will write some test cases update this PR

@justinclift
Copy link
Member

Awesome. 😄

@eradman
Copy link
Collaborator Author

eradman commented Jul 7, 2023

I added tests to verify that mustache variables are rendered correctly in the context of an alert. Early next week I'll review this change again and see if there are other tests that should be included.

@justinclift
Copy link
Member

Thanks @eradman, sounds like a plan. 😄

@eradman eradman force-pushed the alert-email branch 2 times, most recently from 21b08f7 to 3132e6e Compare July 10, 2023 20:06
@eradman eradman requested a review from junnplus July 10, 2023 20:09
@eradman
Copy link
Collaborator Author

eradman commented Jul 10, 2023

There does not seem to be an existing set of test cases for redash/destinations/*, so I did not write a test for the change to the email destination itself.

I would say that the mustache template rendering is covered reasonably.

redash/settings/__init__.py Outdated Show resolved Hide resolved
redash/models/__init__.py Outdated Show resolved Hide resolved
@eradman
Copy link
Collaborator Author

eradman commented Jul 13, 2023

We're getting closer: I was able to refactor this so that the query contents are rendered in the template.

Also access to MailDev was broken: we need to map port 1025 and make sure Redash uses that port to send mail. I can push this as a separate commit.

@eradman eradman requested a review from junnplus July 13, 2023 21:10
@justinclift
Copy link
Member

Also access to MailDev was broken: we need to map port 1025 and make sure Redash uses that port to send mail. I can push this as a separate commit.

As a thought for this, please make sure it's not something too hard coded. Not everyone needs to use the docker container for sending emails.

eg in the production instances I manage, we just have things going directly via SMTP2GO, no docker container needed

@eradman
Copy link
Collaborator Author

eradman commented Jul 14, 2023

As a thought for this, please make sure it's not something too hard coded. Not everyone needs to use the docker container for sending emails.

In this case, docker-compose.yml is only used for testing

# This configuration file is for the **development** setup.
# For a production example please refer to getredash/setup repository on GitHub.

@justinclift
Copy link
Member

Cool. 😄

@eradman
Copy link
Collaborator Author

eradman commented Jul 14, 2023

Fixed flake8 and rebased

@eradman
Copy link
Collaborator Author

eradman commented Jul 17, 2023

Fixed black, isort warnings

@justinclift
Copy link
Member

@eradman black still seems unhappy?

- Escape all variables by default since Mustache only has a syntax for
  raw values
- Generate a generic HTML result table to allow an alert template to
  display any query
- Optionally allow alert template to be defined to be set using
  REDASH_ALERTS_DEFAULT_MAIL_BODY_TEMPLATE
- Formatting updated by black
@eradman
Copy link
Collaborator Author

eradman commented Jul 17, 2023

Oh because I was using black-21 (from Ubuntu 22). Upgraded to black-23 ran format again

@codecov
Copy link

codecov bot commented Jul 17, 2023

Codecov Report

Merging #5996 (b02d31c) into master (1e33eee) will increase coverage by 0.06%.
The diff coverage is 80.00%.

@@            Coverage Diff             @@
##           master    #5996      +/-   ##
==========================================
+ Coverage   59.91%   59.97%   +0.06%     
==========================================
  Files         153      153              
  Lines       12462    12470       +8     
  Branches     1684     1686       +2     
==========================================
+ Hits         7466     7479      +13     
+ Misses       4790     4783       -7     
- Partials      206      208       +2     
Impacted Files Coverage Δ
redash/destinations/email.py 41.93% <0.00%> (-1.40%) ⬇️
redash/models/__init__.py 91.95% <100.00%> (+0.81%) ⬆️
redash/settings/__init__.py 98.65% <100.00%> (+<0.01%) ⬆️
redash/utils/__init__.py 71.75% <100.00%> (+0.66%) ⬆️

@justinclift
Copy link
Member

@eradman It looks like this is pretty much ready to merge yeah?

Also, is there a clear way forward for this?

Also access to MailDev was broken: we need to map port 1025 and make sure Redash uses that port to send mail. I can push this as a separate commit.

Personally speaking, I'm not bothered either way if the port mapping is a separate commitPR or in this one. There are advantages (integrated testing of both at once) and disadvantages (all commits get squashed into one) either way, so your choice I reckon. 😄

@eradman
Copy link
Collaborator Author

eradman commented Jul 17, 2023

This is ready to to go. I manually confirmed that email alerts are formatted properly and I'd say the implementation is in good shape.

I created PR #6186 with the changes to the docker port mapping. I believe if we merge that first this change will apply cleanly.

@justinclift
Copy link
Member

Cool. Just merged the other one, so I'll merge this after the CI tests all pass again. 😄

@justinclift justinclift merged commit 0bdd3bd into getredash:master Jul 17, 2023
@justinclift
Copy link
Member

Cool, all done. This seems like it'll be a useful addition for people @eradman. 😄

@eradman eradman deleted the alert-email branch July 17, 2023 20:55
@eradman
Copy link
Collaborator Author

eradman commented Jul 17, 2023

Indeed; I'm sure others will find this change useful. Thanks for mentoring this along @justinclift @guidopetri @junnplus

EmilaineBorato pushed a commit to sondautilities/sonda_d4b_redash that referenced this pull request Jul 24, 2023
- Escape all variables by default since Mustache only has a syntax for raw values
- Generate a generic HTML result table to allow an alert template to display any query
- Optionally allow alert template to be defined to be set using REDASH_ALERTS_DEFAULT_MAIL_BODY_TEMPLATE
- Formatting updated by black
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy review Feature: Alerts Frontend Team Review Meets PR criteria, ready for full review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants