Skip to content

Commit

Permalink
Move stale flags code to workflows module
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Jan 10, 2024
1 parent 4e26412 commit f0066f2
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 48 deletions.
1 change: 0 additions & 1 deletion api/features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class FeatureQuerySerializer(serializers.Serializer):
required=False,
help_text="Integer ID of the environment to view features in the context of.",
)
is_stale = serializers.BooleanField(required=False)

def validate_tags(self, tags):
try:
Expand Down
34 changes: 1 addition & 33 deletions api/features/tasks.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
from datetime import timedelta

from django.utils import timezone

from environments.models import Webhook
from features.models import Feature, FeatureState
from projects.models import Project
from projects.tags.models import Tag
from task_processor.decorators import register_recurring_task
from features.models import FeatureState
from webhooks.constants import WEBHOOK_DATETIME_FORMAT
from webhooks.webhooks import (
WebhookEventType,
call_environment_webhooks,
call_organisation_webhooks,
)

from .constants import STALE_FLAGS_TAG_LABEL
from .models import HistoricalFeatureState


Expand Down Expand Up @@ -87,27 +79,3 @@ def _get_feature_state_webhook_data(feature_state, previous=False):
identity_identifier=getattr(feature_state.identity, "identifier", None),
feature_segment=feature_state.feature_segment,
)


@register_recurring_task(run_every=timedelta(hours=3))
def tag_stale_flags():
feature_tag_relationships = []

for project in Project.objects.filter(environments__use_v2_feature_versioning=True):
# TODO: can we centralise this query to get stale flags (FeatureManager?)
stale_flags = project.features.exclude(tags__is_permanent=True).filter(
feature_states__environment_feature_version__created_at__lt=timezone.now()
- timedelta(days=project.stale_flags_limit_days)
)

# TODO: should we delegate each project to a separate regular task?
if stale_flags.exists():
stale_flags_tag, _ = Tag.objects.get_or_create(
label=STALE_FLAGS_TAG_LABEL, project=project, is_system_tag=True
)
feature_tag_relationships.extend(
Feature.tags.through(feature=feature, tag=stale_flags_tag)
for feature in stale_flags.exclude(tags=stale_flags_tag)
)

Feature.tags.through.objects.bulk_create(feature_tag_relationships)
14 changes: 0 additions & 14 deletions api/features/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import typing
from datetime import timedelta
from functools import reduce

from app_analytics.analytics_db_service import get_feature_evaluation_data
Expand All @@ -10,7 +9,6 @@
from django.conf import settings
from django.core.cache import caches
from django.db.models import Max, Q, QuerySet
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from drf_yasg import openapi
Expand Down Expand Up @@ -311,18 +309,6 @@ def _filter_queryset(self, queryset: QuerySet, project: Project) -> QuerySet:
if "is_archived" in query_serializer.initial_data:
queryset = queryset.filter(is_archived=query_data["is_archived"])

if "is_stale" in query_serializer.initial_data:
if query_serializer.validated_data["is_stale"] is True:
queryset = queryset.exclude(tags__is_permanent=True).filter(
last_modified_in_any_environment__lt=timezone.now()
- timedelta(days=project.stale_flags_limit_days)
)
else:
queryset = queryset.filter(
last_modified_in_any_environment__gt=timezone.now()
- timedelta(days=project.stale_flags_limit_days)
)

return queryset


Expand Down

0 comments on commit f0066f2

Please sign in to comment.