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

feat: Create api usage function #3340

Merged
merged 27 commits into from
Feb 15, 2024
Merged

feat: Create api usage function #3340

merged 27 commits into from
Feb 15, 2024

Conversation

zachaysan
Copy link
Contributor

@zachaysan zachaysan commented Jan 29, 2024

Thanks for submitting a PR! Please check the boxes below:

  • I have run pre-commit to check linting
  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

This PR enables our application to respond to organisations which are using the API close to or above our limits. A few considerations were necessary in order to get all the moving pieces together. First, additional information from Chargebee was required to see what part of the billing cycle the customer is at at the time of comparing their API use. This is then combined with the realtime data we store in Influxdb. And then code that is capable of treating yearly plans as smoothly as monthly ones is in place with relative date counting. Finally an additional model was created in order to keep track of sending notifications only once for a given threshold in a given month.

How did you test this code?

Influxdb related code was manually tested with ENV vars set to point to our development influxdb instance. In addition to that one large test is available to validate the rest of the work.

Copy link

vercel bot commented Jan 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 15, 2024 4:22pm
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 15, 2024 4:22pm
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 15, 2024 4:22pm

@codecov-commenter
Copy link

codecov-commenter commented Jan 29, 2024

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (c02562e) 95.93% compared to head (28f6a15) 95.91%.
Report is 6 commits behind head on main.

❗ Current head 28f6a15 differs from pull request most recent head 13126e4. Consider uploading reports for the commit 13126e4 to get more accurate results

Files Patch % Lines
api/app_analytics/influxdb_wrapper.py 11.11% 8 Missing ⚠️
api/organisations/tasks.py 95.45% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3340      +/-   ##
==========================================
- Coverage   95.93%   95.91%   -0.02%     
==========================================
  Files        1088     1090       +2     
  Lines       33818    33932     +114     
==========================================
+ Hits        32443    32547     +104     
- Misses       1375     1385      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

github-actions bot commented Jan 29, 2024

Uffizzi Ephemeral Environment deployment-45984

☁️ https://app.uffizzi.com/github.com/Flagsmith/flagsmith/pull/3340

📄 View Application Logs etc.

What is Uffizzi? Learn more!

@github-actions github-actions bot added the front-end Issue related to the React Front End Dashboard label Feb 12, 2024
@zachaysan
Copy link
Contributor Author

@zachaysan as discussed, I think this PR should include a small informational message on the Organisation Usage page which states the following:

These numbers show the API usage for the last 30 days, not your current billing period which may differ.

As discussed, I included it on the existing note in the page.

Copy link
Contributor

@matthewelwell matthewelwell left a comment

Choose a reason for hiding this comment

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

I've approved this as the comments I've left are only minor improvements that aren't strictly necessary.

Comment on lines 98 to 118
if matched_threshold < 100:
message = "organisations/api_usage_notification.txt"
html_message = "organisations/api_usage_notification.html"

# Since threshold < 100 only include admins.
recipient_list = list(
FFAdminUser.objects.filter(
userorganisation__organisation=organisation,
userorganisation__role=OrganisationRole.ADMIN,
).values_list("email", flat=True)
)
else:
message = "organisations/api_usage_notification_limit.txt"
html_message = "organisations/api_usage_notification_limit.html"

# Since threshold >= 100 include everyone in the organisation.
recipient_list = list(
FFAdminUser.objects.filter(
userorganisation__organisation=organisation,
).values_list("email", flat=True)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably have done:

Suggested change
if matched_threshold < 100:
message = "organisations/api_usage_notification.txt"
html_message = "organisations/api_usage_notification.html"
# Since threshold < 100 only include admins.
recipient_list = list(
FFAdminUser.objects.filter(
userorganisation__organisation=organisation,
userorganisation__role=OrganisationRole.ADMIN,
).values_list("email", flat=True)
)
else:
message = "organisations/api_usage_notification_limit.txt"
html_message = "organisations/api_usage_notification_limit.html"
# Since threshold >= 100 include everyone in the organisation.
recipient_list = list(
FFAdminUser.objects.filter(
userorganisation__organisation=organisation,
).values_list("email", flat=True)
)
recipient_list = FFAdminUser.objects.filter(
userorganisation__organisation=organisation,
)
if matched_threshold < 100:
message = "organisations/api_usage_notification.txt"
html_message = "organisations/api_usage_notification.html"
# Since threshold < 100 only include admins.
recipient_list = recipient_list.filter(
userorganisation__role=OrganisationRole.ADMIN,
)
else:
message = "organisations/api_usage_notification_limit.txt"
html_message = "organisations/api_usage_notification_limit.html"

... and then in the send_mail call you can convert to a flat list of emails:

send_mail(
    ...
    recipient_list=list(recipient_list.values_list("email", flat=True))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok that's no problem to update.

Comment on lines +298 to +307
# Now re-run the usage to make sure the notification isn't resent.
handle_api_usage_notifications()

assert (
OranisationAPIUsageNotification.objects.filter(
organisation=organisation,
).count()
== 1
)
assert OranisationAPIUsageNotification.objects.first() == api_usage_notification
Copy link
Contributor

Choose a reason for hiding this comment

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

A neat addition here might be to use the mailoutbox fixture available in pytest-django.

https://pytest-django.readthedocs.io/en/latest/helpers.html#mailoutbox

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What a cool fixture! I've updated the code to use that instead of the email send mocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API front-end Issue related to the React Front End Dashboard
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants