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: Add api usage metrics for different periods #3870

Open
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

zachaysan
Copy link
Contributor

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 provides the ability to get API usage data for different billing periods. The supported new periods are the current billing period (i.e., the first day of the billing period till now), the previous billing period, and a 90-day rolling period to the present.

Part of these changes involved with a refactoring of how code is called to influxdb. The former code incorrectly attributed date_range as if it were date_start with an inverse modifier. With the former code any callers that had set a date_stop to anything other than now() the resultant range would be smaller than the interface contract implied. These issues have been addressed and subsequent calling methods have been updated.

There was also an issue that cropped up with Pydantic and Freeze Gun, which has been temporarily solved with the addition of an auto-use function in conftest.py.

How did you test this code?

Tested manually against production influxdb to verify results then also added 3 tests to ensure calling signatures to related influxdb functions.

Copy link

vercel bot commented May 1, 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 May 16, 2024 7:34pm
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 7:34pm
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 7:34pm

@github-actions github-actions bot added the api Issue related to the REST API label May 1, 2024
Copy link
Contributor

github-actions bot commented May 1, 2024

Uffizzi Ephemeral Environment deployment-51125

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

📄 View Application Logs etc.

What is Uffizzi? Learn more!

Copy link

codecov bot commented May 13, 2024

Codecov Report

Attention: Patch coverage is 99.00498% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 96.24%. Comparing base (231af3b) to head (cb37c46).

Files Patch % Lines
api/sales_dashboard/views.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3870      +/-   ##
==========================================
+ Coverage   96.18%   96.24%   +0.06%     
==========================================
  Files        1141     1142       +1     
  Lines       36507    36687     +180     
==========================================
+ Hits        35114    35310     +196     
+ Misses       1393     1377      -16     

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

Copy link
Member

@khvn26 khvn26 left a comment

Choose a reason for hiding this comment

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

LGTM in general. Didn't manage to find any flaws in logic/architecture. Commented a couple minor issues/nitpicks. Requesting changes because some of the comments point out incorrect typing.

api/app_analytics/analytics_db_service.py Outdated Show resolved Hide resolved
api/app_analytics/influxdb_wrapper.py Outdated Show resolved Hide resolved
api/app_analytics/influxdb_wrapper.py Outdated Show resolved Hide resolved
api/conftest.py Outdated Show resolved Hide resolved
api/app_analytics/serializers.py Outdated Show resolved Hide resolved
api/tests/unit/app_analytics/test_analytics_db_service.py Outdated Show resolved Hide resolved
api/app_analytics/analytics_db_service.py Show resolved Hide resolved
api/app_analytics/analytics_db_service.py Outdated Show resolved Hide resolved
api/app_analytics/analytics_db_service.py Outdated Show resolved Hide resolved
api/app_analytics/analytics_db_service.py Outdated Show resolved Hide resolved
api/app_analytics/analytics_db_service.py Show resolved Hide resolved
api/app_analytics/influxdb_wrapper.py Outdated Show resolved Hide resolved
api/app_analytics/influxdb_wrapper.py Show resolved Hide resolved
api/tests/unit/app_analytics/test_analytics_db_service.py Outdated Show resolved Hide resolved
api/tests/unit/app_analytics/test_analytics_db_service.py Outdated Show resolved Hide resolved
# get_usage_data() related period constants
CURRENT_BILLING_PERIOD = "current_billing_period"
PREVIOUS_BILLING_PERIOD = "previous_billing_period"
_90_DAY_PERIOD = "90_day_period"
Copy link
Member

Choose a reason for hiding this comment

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

Why is this prefixed with underscore (ergo, private)?

environment_id: int | None = None,
project_id: int | None = None,
period: (
Literal[
Copy link
Member

Choose a reason for hiding this comment

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

It's nicer to avoid defining the type inline here, i.e.

UsageDataPeriod = Literal[
    constants.CURRENT_BILLING_PERIOD,
    constants.PREVIOUS_BILLING_PERIOD,
    constants._90_DAY_PERIOD,
]

...

def get_usage_data(
    ...
    period: UsageDataPeriod | None = None,

Comment on lines +17 to +27
period = serializers.RegexField(
regex=r"^(%s)$"
% "|".join(
[
constants.CURRENT_BILLING_PERIOD,
constants.PREVIOUS_BILLING_PERIOD,
constants._90_DAY_PERIOD,
]
),
required=False,
)
Copy link
Member

Choose a reason for hiding this comment

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

When I offered to use RegexField initially, I was thinking more of a ^\d+[d|h|m]$ regex, as I didn't know that the periods were meant to be predefined.

Now it seems more appropriate to use ChoiceField instead (and if you define the literal type outside the method definition as suggested here, you'll be able to use typing.get_args with it to provide the choices in a DRY manner).

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants