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(versioning): limit returned number of versions by plan #4433

Merged
merged 32 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
16c070c
Add logic to get 'subscription plan family'
matthewelwell Jul 31, 2024
1d39c3f
Restrict number of versions returned based on plan
matthewelwell Jul 31, 2024
5cf3eca
Add test
matthewelwell Jul 31, 2024
8884de4
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Aug 7, 2024
2961459
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Aug 7, 2024
04192f9
Add version limit to subscription metadata endpoint
matthewelwell Aug 7, 2024
0f976da
Update limits
matthewelwell Aug 7, 2024
5335af0
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Aug 8, 2024
ca7f632
Fix tests
matthewelwell Aug 8, 2024
83592c8
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Aug 14, 2024
abba50e
Use days instead of number of versions
matthewelwell Aug 14, 2024
8f48f7f
Delete subscription_service and replace with method on subscription m…
matthewelwell Aug 15, 2024
c12f42c
Move version limit to be handled by chargebee / subscription metadata
matthewelwell Aug 15, 2024
6900cb7
Various fixes and allow trial subscriptions to use unlimited history
matthewelwell Aug 16, 2024
d5dac66
Refactor to use a private method for readability
matthewelwell Aug 16, 2024
75c0636
Add pragma: no cover to abstract method
matthewelwell Aug 19, 2024
1feaf72
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Aug 19, 2024
c3f3866
Add tests for organisation and project viewsets which are limited on …
matthewelwell Aug 19, 2024
29ce23c
Add tests for starting / ending trial
matthewelwell Aug 19, 2024
995f78b
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Aug 20, 2024
1bc7d32
Simplify forms post merge
matthewelwell Aug 20, 2024
f9b4ab4
Ensure that data is correctly written to osic record
matthewelwell Aug 20, 2024
eff8c22
Add grandfather logic into code
matthewelwell Aug 20, 2024
33ce657
Add test and fix logic
matthewelwell Aug 21, 2024
8aee862
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Aug 21, 2024
93a95eb
Handle null VERSIONING_RELEASE_DATE
matthewelwell Sep 3, 2024
dc1107b
Fix tests
matthewelwell Sep 3, 2024
90e0f05
Add comment / refactor to facilitate contextual comment
matthewelwell Sep 3, 2024
525d516
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Sep 10, 2024
1bac612
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Sep 13, 2024
c67b429
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Oct 8, 2024
422e5df
Merge branch 'refs/heads/main' into feat(versioning)/configure-num-ve…
matthewelwell Oct 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/features/versioning/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from organisations.subscriptions.constants import SubscriptionPlanFamily

# Constants to define how many versions should be returned in the list endpoint
# based on the plan of the requesting organisation.
DEFAULT_VERSION_LIMIT = 3
# extra is used to return extra values that the FE can blur out
EXTRA = 1
VERSION_LIMIT_BY_PLAN = {
SubscriptionPlanFamily.FREE: DEFAULT_VERSION_LIMIT + EXTRA,
SubscriptionPlanFamily.START_UP: DEFAULT_VERSION_LIMIT + EXTRA,
SubscriptionPlanFamily.SCALE_UP: 5 + EXTRA,
SubscriptionPlanFamily.ENTERPRISE: None, # No limit
}
14 changes: 14 additions & 0 deletions api/features/versioning/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
from rest_framework.serializers import Serializer
from rest_framework.viewsets import GenericViewSet

from app.pagination import CustomPagination
from environments.models import Environment
from environments.permissions.constants import VIEW_ENVIRONMENT
from features.models import Feature, FeatureState
from features.serializers import (
CustomCreateSegmentOverrideFeatureStateSerializer,
)
from features.versioning.constants import (
DEFAULT_VERSION_LIMIT,
VERSION_LIMIT_BY_PLAN,
)
from features.versioning.exceptions import FeatureVersionDeleteError
from features.versioning.models import EnvironmentFeatureVersion
from features.versioning.permissions import (
Expand Down Expand Up @@ -55,6 +60,7 @@ class EnvironmentFeatureVersionViewSet(
DestroyModelMixin,
):
permission_classes = [IsAuthenticated, EnvironmentFeatureVersionPermissions]
pagination_class = CustomPagination

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -115,6 +121,14 @@ def get_queryset(self):
)
queryset = queryset.filter(_is_live=is_live)

subscription = self.environment.project.organisation.subscription
version_limit = VERSION_LIMIT_BY_PLAN.get(
subscription.subscription_plan_family, DEFAULT_VERSION_LIMIT
)

if self.action == "list" and version_limit is not None:
return queryset[:version_limit]

return queryset

def perform_create(self, serializer: Serializer) -> None:
Expand Down
7 changes: 6 additions & 1 deletion api/organisations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
SUBSCRIPTION_PAYMENT_METHODS,
TRIAL_SUBSCRIPTION_ID,
XERO,
SubscriptionPlanFamily,
)
from organisations.subscriptions.exceptions import (
SubscriptionDoesNotSupportSeatUpgrade,
Expand Down Expand Up @@ -256,7 +257,11 @@ def can_auto_upgrade_seats(self) -> bool:

@property
def is_free_plan(self) -> bool:
return self.plan == FREE_PLAN_ID
return self.subscription_plan_family == SubscriptionPlanFamily.FREE

@property
def subscription_plan_family(self) -> SubscriptionPlanFamily:
return SubscriptionPlanFamily.get_by_plan_id(self.plan)

@hook(AFTER_SAVE, when="plan", has_changed=True)
def update_api_limit_access_block(self):
Expand Down
20 changes: 20 additions & 0 deletions api/organisations/subscriptions/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,28 @@
STARTUP = "startup"
STARTUP_ANNUAL_V2 = "startup-annual-v2"
STARTUP_V2 = "startup-v2"
ENTERPRISE = "enterprise"


class SubscriptionCacheEntity(Enum):
INFLUX = "INFLUX"
CHARGEBEE = "CHARGEBEE"


class SubscriptionPlanFamily(Enum):
FREE = "FREE"
START_UP = "START_UP"
SCALE_UP = "SCALE_UP"
ENTERPRISE = "ENTERPRISE"

@classmethod
def get_by_plan_id(cls, plan_id: str) -> "SubscriptionPlanFamily":
match str(plan_id).replace("-", "").lower():
case p if p.startswith("scaleup"):
return cls.SCALE_UP
case p if p.startswith("startup"):
return cls.START_UP
case p if p.startswith("enterprise"):
return cls.ENTERPRISE
case _:
return cls.FREE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from features.models import Feature, FeatureSegment, FeatureState
from features.multivariate.models import MultivariateFeatureOption
from features.versioning.models import EnvironmentFeatureVersion
from organisations.models import Subscription
from organisations.subscriptions.constants import ENTERPRISE, STARTUP
from projects.permissions import VIEW_PROJECT
from segments.models import Segment
from tests.types import (
Expand Down Expand Up @@ -1124,3 +1126,54 @@ def test_create_new_version_delete_segment_override_updates_overrides_immediatel
get_feature_segments_response = admin_client.get(get_feature_segments_url)
assert get_feature_segments_response.status_code == status.HTTP_200_OK
assert get_feature_segments_response.json()["count"] == 0


@pytest.mark.parametrize(
"plan, versions_to_create, expected_versions_to_return",
(
(STARTUP, 10, 4),
(
ENTERPRISE,
10,
11,
), # expect 11 because of initial version created automatically
),
)
def test_list_versions_only_returns_allowed_amount_for_plan(
feature: Feature,
environment_v2_versioning: Environment,
staff_user: FFAdminUser,
staff_client: APIClient,
with_environment_permissions: WithEnvironmentPermissionsCallable,
with_project_permissions: WithProjectPermissionsCallable,
subscription: Subscription,
plan: str,
versions_to_create: int,
expected_versions_to_return: int,
) -> None:
# Given
with_environment_permissions([VIEW_ENVIRONMENT])
with_project_permissions([VIEW_PROJECT])

url = reverse(
"api-v1:versioning:environment-feature-versions-list",
args=[environment_v2_versioning.id, feature.id],
)

# Let's set the subscription plan as start up
subscription.plan = plan
subscription.save()

# and now let's create a lot more versions for the feature
for _ in range(versions_to_create):
version = EnvironmentFeatureVersion.objects.create(
environment=environment_v2_versioning, feature=feature
)
version.publish(staff_user)

# When
response = staff_client.get(url)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["count"] == expected_versions_to_return
23 changes: 23 additions & 0 deletions api/tests/unit/organisations/test_unit_organisations_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
MAX_SEATS_IN_FREE_PLAN,
TRIAL_SUBSCRIPTION_ID,
XERO,
SubscriptionPlanFamily,
)
from organisations.subscriptions.exceptions import (
SubscriptionDoesNotSupportSeatUpgrade,
Expand Down Expand Up @@ -551,3 +552,25 @@ def test_reset_of_api_notifications(organisation: Organisation) -> None:
# Then
assert OrganisationAPIUsageNotification.objects.count() == 1
assert OrganisationAPIUsageNotification.objects.first() == oapiun


@pytest.mark.parametrize(
"plan_id, expected_plan_family",
(
("free", SubscriptionPlanFamily.FREE),
("enterprise", SubscriptionPlanFamily.ENTERPRISE),
("enterprise-semiannual", SubscriptionPlanFamily.ENTERPRISE),
("scale-up", SubscriptionPlanFamily.SCALE_UP),
("scaleup", SubscriptionPlanFamily.SCALE_UP),
("scale-up-v2", SubscriptionPlanFamily.SCALE_UP),
("scale-up-v2-annual", SubscriptionPlanFamily.SCALE_UP),
("startup", SubscriptionPlanFamily.START_UP),
("start-up", SubscriptionPlanFamily.START_UP),
("start-up-v2", SubscriptionPlanFamily.START_UP),
("start-up-v2-annual", SubscriptionPlanFamily.START_UP),
),
)
def test_subscription_plan_family(
plan_id: str, expected_plan_family: SubscriptionPlanFamily
) -> None:
assert Subscription(plan=plan_id).subscription_plan_family == expected_plan_family
Loading