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

Jinja2 based routes #1319

Merged
merged 20 commits into from
Mar 8, 2023
Merged

Jinja2 based routes #1319

merged 20 commits into from
Mar 8, 2023

Conversation

iskhakov
Copy link
Contributor

@iskhakov iskhakov commented Feb 10, 2023

What this PR does

This PR adds the new way to set up routes using jinja2 templating language

Screenshot 2023-03-06 at 22 11 13

Screenshot 2023-03-06 at 22 11 34

Which issue(s) this PR fixes

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated

@iskhakov iskhakov changed the title Iskhakov/jinja2 routes Jinja2 based routes Feb 10, 2023
@iskhakov iskhakov requested a review from maskin25 March 6, 2023 11:22
@iskhakov iskhakov marked this pull request as ready for review March 6, 2023 12:34
@iskhakov iskhakov requested review from a team March 6, 2023 12:34
Copy link
Member

@vstpme vstpme left a comment

Choose a reason for hiding this comment

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

LGTM, probably a good idea to add a couple tests on this, e.g. somewhere here:

def test_channel_filter_select_filter(make_organization, make_alert_receive_channel, make_channel_filter):

engine/apps/api/serializers/channel_filter.py Outdated Show resolved Hide resolved
migrations.AddField(
model_name='channelfilter',
name='filtering_term_type',
field=models.IntegerField(choices=[(0, 'regex'), (1, 'jinja2')], default=None, null=True),
Copy link
Member

Choose a reason for hiding this comment

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

Can we have this field non-nullable, but with default value 0? With that we can get rid of to_representation hack

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 decided not use this kind of migrations to make the release smoother for us and the oss customers (check here for more details)

Choose a reason for hiding this comment

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

I think it's valid tradeoff to experience 500's to have more consistent database and codebase. With nullable we need a lot of code to show this field in private/public API correctly and to check for null to write to that field from API.

@@ -56,22 +57,6 @@ def get_slack_channel(self, obj):
"id": obj.slack_channel_pk,
}

def validate(self, attrs):
Copy link
Member

Choose a reason for hiding this comment

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

Where jinja template is validated, when route is created/modified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was used to ensure that the routes are based on the unique regular expressions. I decided to drop uniqueness, if user wants to create exactly same route which will never get it's alert, let them do that, may be they'll want to modify it later.

@@ -176,20 +176,6 @@ def create(self, validated_data):

return instance

def validate(self, attrs):

Choose a reason for hiding this comment

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

Same question as in internal API - where jinja template is validated?

return False
if self.filtering_term is not None and self.filtering_term_type == ChannelFilter.FILTERING_TERM_TYPE_REGEX:
try:
return re.search(self.filtering_term, json.dumps(value))

Choose a reason for hiding this comment

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

json.dumps(value) is introduced here, could you please describe why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Konstantinov-Innokentii
Copy link
Member

Great idea, left comments. It will be great to support new filter term type in public api and in the terraform provider. Also, i think it is needed to provide some info in docs about jinja based routes.

),
# Adding same default value on the database level
# for database backwards-compatibility with older versions of code
AddDefaultValue(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is possible with RunSQL to insert default values, but we'll need to do that manually for each database. django_add_default_value does that for us

Choose a reason for hiding this comment

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

I didn't get that idea. Why we need AddDefaultValue, when we have default=0?

Copy link
Contributor Author

@iskhakov iskhakov Mar 8, 2023

Choose a reason for hiding this comment

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

I left the better comment in the code
for more details I recommend to read this article

@iskhakov iskhakov enabled auto-merge March 8, 2023 08:10
@iskhakov iskhakov disabled auto-merge March 8, 2023 08:10
@iskhakov iskhakov enabled auto-merge March 8, 2023 08:31
@iskhakov iskhakov disabled auto-merge March 8, 2023 08:31
@iskhakov iskhakov enabled auto-merge March 8, 2023 08:31
@iskhakov iskhakov disabled auto-merge March 8, 2023 08:41
@iskhakov iskhakov merged commit 2e63a9f into dev Mar 8, 2023
@iskhakov iskhakov deleted the iskhakov/jinja2-routes branch March 8, 2023 08:42
brojd pushed a commit that referenced this pull request Sep 18, 2024
# What this PR does

This PR adds the new way to set up routes using jinja2 templating
language

<img width="1174" alt="Screenshot 2023-03-06 at 22 11 13"
src="https://user-images.githubusercontent.com/2262529/223134053-69d43c47-bb2a-4790-a16d-767425017a76.png">
<img width="1175" alt="Screenshot 2023-03-06 at 22 11 34"
src="https://user-images.githubusercontent.com/2262529/223134070-1e5ef82f-021c-4d5d-b255-b19bb3445641.png">


## Which issue(s) this PR fixes

## Checklist

- [ ] Tests updated
- [ ] Documentation added
- [ ] `CHANGELOG.md` updated
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.

5 participants