Skip to content

Commit

Permalink
get latest events per provider per feature per environment
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed Jan 28, 2025
1 parent bfe1e3e commit cde95c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
8 changes: 4 additions & 4 deletions api/features/feature_health/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def get_latest_by_feature(
) -> "models.QuerySet[FeatureHealthEvent]":
return (
self.filter(feature=feature)
.order_by("provider_name", "-created_at")
.distinct("provider_name")
.order_by("provider_name", "environment_id", "-created_at")
.distinct("provider_name", "environment_id")
)

def get_latest_by_project(
Expand All @@ -89,8 +89,8 @@ def get_latest_by_project(
) -> "models.QuerySet[FeatureHealthEvent]":
return (
self.filter(feature__project=project)
.order_by("provider_name", "feature_id", "-created_at")
.distinct("provider_name", "feature_id")
.order_by("provider_name", "environment_id", "feature_id", "-created_at")
.distinct("provider_name", "environment_id", "feature_id")
)


Expand Down
35 changes: 34 additions & 1 deletion api/tests/unit/features/feature_health/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,55 @@ def test_feature_health_provider__get_audit_log_author__return_expected(
def test_feature_health_event__get_latest_by_feature__return_expected(
project: Project,
feature: Feature,
environment: Environment,
) -> None:
# Given
unrelated_feature = Feature.objects.create(
project=project, name="unrelated_feature"
)
environment_2 = Environment.objects.create(project=project, name="Environment 2")

latest_provider1_event = FeatureHealthEvent.objects.create(
feature=feature,
type="UNHEALTHY",
provider_name="provider1",
)
latest_provider1_environment_event = FeatureHealthEvent.objects.create(
feature=feature,
type="UNHEALTHY",
provider_name="provider1",
environment=environment,
)
with freeze_time(now - datetime.timedelta(hours=1)):
older_provider1_event = FeatureHealthEvent.objects.create(
feature=feature,
type="HEALTHY",
provider_name="provider1",
)
older_provider1_environment_event = FeatureHealthEvent.objects.create(
feature=feature,
type="HEALTHY",
provider_name="provider1",
environment=environment,
)
latest_provider_1_environment_2_event = FeatureHealthEvent.objects.create(
feature=feature,
type="UNHEALTHY",
provider_name="provider1",
environment=environment_2,
)
with freeze_time(now - datetime.timedelta(hours=2)):
latest_provider2_event = FeatureHealthEvent.objects.create(
feature=feature,
type="UNHEALTHY",
provider_name="provider2",
)
older_provider_1_environment_2_event = FeatureHealthEvent.objects.create(
feature=feature,
type="HEALTHY",
provider_name="provider1",
environment=environment_2,
)
unrelated_feature_event = FeatureHealthEvent.objects.create(
feature=unrelated_feature,
type="UNHEALTHY",
Expand All @@ -86,8 +112,15 @@ def test_feature_health_event__get_latest_by_feature__return_expected(
feature_health_events = [*FeatureHealthEvent.objects.get_latest_by_feature(feature)]

# Then
assert feature_health_events == [latest_provider1_event, latest_provider2_event]
assert feature_health_events == [
latest_provider1_environment_event,
latest_provider_1_environment_2_event,
latest_provider1_event,
latest_provider2_event,
]
assert older_provider1_event not in feature_health_events
assert older_provider1_environment_event not in feature_health_events
assert older_provider_1_environment_2_event not in feature_health_events
assert unrelated_feature_event not in feature_health_events


Expand Down

0 comments on commit cde95c5

Please sign in to comment.