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

Grow 1299 #67

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 12 additions & 2 deletions apiserver/tests/factories/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
from .code import get_repo_workflow_run
from .incidents import get_incident
from .code import (
get_repo_workflow_run,
get_deployment,
get_pull_request,
get_pull_request_commit,
get_pull_request_event,
)
from .incidents import (
get_incident,
get_change_failure_rate_metrics,
get_org_incident_service,
)
33 changes: 33 additions & 0 deletions apiserver/tests/factories/models/code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from random import randint
from uuid import uuid4
from dora.service.deployments.models.models import (
Deployment,
DeploymentStatus,
DeploymentType,
)
from dora.utils.string import uuid4_str

from dora.store.models.code import (
PullRequestCommit,
Expand Down Expand Up @@ -141,3 +147,30 @@ def get_repo_workflow_run(
meta=meta,
html_url=html_url,
)


def get_deployment(
repo_id=None,
entity_id=None,
actor=None,
head_branch=None,
status=None,
conducted_at=None,
meta=None,
duration=None,
html_url=None,
provider=None,
):
return Deployment(
deployment_type=DeploymentType.WORKFLOW,
repo_id=repo_id or "1234567",
entity_id=entity_id or uuid4_str(),
provider=provider or "github",
actor=actor or "samad-yar-khan",
head_branch=head_branch or "master",
conducted_at=conducted_at or time_now(),
duration=duration,
status=status or DeploymentStatus.SUCCESS,
html_url=html_url or "",
meta=meta or {},
)
11 changes: 10 additions & 1 deletion apiserver/tests/factories/models/incidents.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime
from typing import List
from typing import List, Set

from voluptuous import default_factory
from dora.service.deployments.models.models import Deployment
from dora.service.incidents.models.mean_time_to_recovery import ChangeFailureRateMetrics

from dora.store.models.incidents import IncidentType, OrgIncidentService
from dora.store.models.incidents.incidents import (
Expand Down Expand Up @@ -82,3 +84,10 @@ def get_incident_org_incident_map(
incident_id: str = uuid4_str(), service_id: str = uuid4_str()
):
return IncidentOrgIncidentServiceMap(incident_id=incident_id, service_id=service_id)


def get_change_failure_rate_metrics(
failed_deployments: Set[Deployment] = None,
total_deployments: Set[Deployment] = None,
):
return ChangeFailureRateMetrics(failed_deployments, total_deployments)
200 changes: 200 additions & 0 deletions apiserver/tests/service/Incidents/test_change_failure_rate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
from datetime import timedelta
from tests.factories.models.incidents import get_change_failure_rate_metrics
from dora.service.incidents.incidents import get_incident_service
from dora.utils.time import time_now

from tests.factories.models import get_incident, get_deployment


# No incidents, no deployments
def test_get_change_failure_rate_for_no_incidents_no_deployments():
incident_service = get_incident_service()
incidents = []
deployments = []
change_failure_rate = incident_service.get_change_failure_rate_metrics(
deployments,
incidents,
)
assert change_failure_rate == get_change_failure_rate_metrics([], [])
assert change_failure_rate.change_failure_rate == 0


# No incidents, some deployments
def test_get_change_failure_rate_for_no_incidents_and_some_deployments():
incident_service = get_incident_service()
incidents = []

deployment_1 = get_deployment(conducted_at=time_now() - timedelta(days=2))
deployment_2 = get_deployment(conducted_at=time_now() - timedelta(hours=6))

deployments = [
deployment_1,
deployment_2,
]
change_failure_rate = incident_service.get_change_failure_rate_metrics(
deployments,
incidents,
)
assert change_failure_rate == get_change_failure_rate_metrics(
set(), set([deployment_2, deployment_1])
)
assert change_failure_rate.change_failure_rate == 0


# Some incidents, no deployments
def test_get_deployment_incidents_count_map_returns_empty_dict_when_given_some_incidents_no_deployments():
incident_service = get_incident_service()
incidents = [get_incident(creation_date=time_now() - timedelta(days=3))]
deployments = []
change_failure_rate = incident_service.get_change_failure_rate_metrics(
deployments,
incidents,
)
assert change_failure_rate == get_change_failure_rate_metrics(set(), set())
assert change_failure_rate.change_failure_rate == 0


# One incident between two deployments
def test_get_change_failure_rate_for_one_incidents_bw_two_deployments():
incident_service = get_incident_service()
incidents = [get_incident(creation_date=time_now() - timedelta(days=1))]

deployment_1 = get_deployment(conducted_at=time_now() - timedelta(days=2))
deployment_2 = get_deployment(conducted_at=time_now() - timedelta(hours=6))

deployments = [
deployment_1,
deployment_2,
]

change_failure_rate = incident_service.get_change_failure_rate_metrics(
deployments,
incidents,
)
assert change_failure_rate == get_change_failure_rate_metrics(
set([deployment_1]), set([deployment_2, deployment_1])
)
assert change_failure_rate.change_failure_rate == 50


# One incident before two deployments
def test_get_change_failure_rate_for_one_incidents_bef_two_deployments():
incident_service = get_incident_service()
incidents = [get_incident(creation_date=time_now() - timedelta(days=3))]

deployment_1 = get_deployment(conducted_at=time_now() - timedelta(days=2))
deployment_2 = get_deployment(conducted_at=time_now() - timedelta(hours=6))

deployments = [
deployment_1,
deployment_2,
]

change_failure_rate = incident_service.get_change_failure_rate_metrics(
deployments,
incidents,
)
assert change_failure_rate == get_change_failure_rate_metrics(
set([]), set([deployment_2, deployment_1])
)
assert change_failure_rate.change_failure_rate == 0


# One incident after two deployments
def test_get_change_failure_rate_for_one_incidents_after_two_deployments():
incident_service = get_incident_service()
incidents = [get_incident(creation_date=time_now() - timedelta(hours=1))]
deployment_1 = get_deployment(conducted_at=time_now() - timedelta(days=2))
deployment_2 = get_deployment(conducted_at=time_now() - timedelta(hours=6))

deployments = [
deployment_1,
deployment_2,
]

change_failure_rate = incident_service.get_change_failure_rate_metrics(
deployments,
incidents,
)
assert change_failure_rate == get_change_failure_rate_metrics(
set([deployment_2]), set([deployment_2, deployment_1])
)
assert change_failure_rate.change_failure_rate == 50


# Multiple incidents and deployments
def test_get_change_failure_rate_for_multi_incidents_multi_deployments():
"""
Time Line:

incident_0
deployment1
deployment_2
incident_1
deployment_3
incident_2
deployment_4
incident_3
deployment_5
incident_4
incident_5
incident_6
deployment_6

"""

incident_service = get_incident_service()

incident_0 = get_incident(creation_date=time_now() - timedelta(days=10))
incident_1 = get_incident(creation_date=time_now() - timedelta(days=5))
incident_2 = get_incident(creation_date=time_now() - timedelta(days=3))
incident_3 = get_incident(creation_date=time_now() - timedelta(hours=20))
incident_4 = get_incident(creation_date=time_now() - timedelta(hours=4))
incident_5 = get_incident(creation_date=time_now() - timedelta(hours=2))
incident_6 = get_incident(creation_date=time_now() - timedelta(hours=1))

incidents = [
incident_0,
incident_1,
incident_2,
incident_3,
incident_4,
incident_5,
incident_6,
]

deployment_1 = get_deployment(conducted_at=time_now() - timedelta(days=7))
deployment_2 = get_deployment(conducted_at=time_now() - timedelta(days=6))
deployment_3 = get_deployment(conducted_at=time_now() - timedelta(days=4))
deployment_4 = get_deployment(conducted_at=time_now() - timedelta(days=2))
deployment_5 = get_deployment(conducted_at=time_now() - timedelta(hours=6))
deployment_6 = get_deployment(conducted_at=time_now() - timedelta(minutes=30))

deployments = [
deployment_1,
deployment_2,
deployment_3,
deployment_4,
deployment_5,
deployment_6,
]

change_failure_rate = incident_service.get_change_failure_rate_metrics(
deployments,
incidents,
)

assert change_failure_rate == get_change_failure_rate_metrics(
set([deployment_2, deployment_3, deployment_4, deployment_5]),
set(
[
deployment_1,
deployment_2,
deployment_3,
deployment_4,
deployment_5,
deployment_6,
]
),
)
assert change_failure_rate.change_failure_rate == (4 / 6 * 100)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dora.service.incidents.incidents import get_incident_service
from dora.utils.time import time_now

from tests.factories.models import get_incident, get_repo_workflow_run
from tests.factories.models import get_incident, get_deployment


# No incidents, no deployments
Expand All @@ -22,8 +22,8 @@ def test_get_deployment_incidents_count_map_returns_deployment_incident_count_ma
incident_service = get_incident_service()
incidents = []
deployments = [
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=2)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(hours=6)),
get_deployment(conducted_at=time_now() - timedelta(days=2)),
get_deployment(conducted_at=time_now() - timedelta(hours=6)),
]
deployment_incidents_count_map = incident_service.get_deployment_incidents_map(
deployments,
Expand All @@ -48,8 +48,8 @@ def test_get_deployment_incidents_count_map_returns_deployment_incident_count_ma
incident_service = get_incident_service()
incidents = [get_incident(creation_date=time_now() - timedelta(days=1))]
deployments = [
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=2)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(hours=6)),
get_deployment(conducted_at=time_now() - timedelta(days=2)),
get_deployment(conducted_at=time_now() - timedelta(hours=6)),
]
deployment_incidents_count_map = incident_service.get_deployment_incidents_map(
deployments, incidents
Expand All @@ -65,8 +65,8 @@ def test_get_deployment_incidents_count_map_returns_deployment_incident_count_ma
incident_service = get_incident_service()
incidents = [get_incident(creation_date=time_now() - timedelta(days=3))]
deployments = [
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=2)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(hours=6)),
get_deployment(conducted_at=time_now() - timedelta(days=2)),
get_deployment(conducted_at=time_now() - timedelta(hours=6)),
]
deployment_incidents_count_map = incident_service.get_deployment_incidents_map(
deployments, incidents
Expand All @@ -79,8 +79,8 @@ def test_get_deployment_incidents_count_map_returns_deployment_incident_count_ma
incident_service = get_incident_service()
incidents = [get_incident(creation_date=time_now() - timedelta(hours=1))]
deployments = [
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=2)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(hours=6)),
get_deployment(conducted_at=time_now() - timedelta(days=2)),
get_deployment(conducted_at=time_now() - timedelta(hours=6)),
]
deployment_incidents_count_map = incident_service.get_deployment_incidents_map(
deployments, incidents
Expand All @@ -101,11 +101,11 @@ def test_get_deployment_incidents_count_map_returns_deployment_incident_count_ma
get_incident(creation_date=time_now() - timedelta(hours=1)),
]
deployments = [
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=7)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=6)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=4)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(days=2)),
get_repo_workflow_run(conducted_at=time_now() - timedelta(hours=6)),
get_deployment(conducted_at=time_now() - timedelta(days=7)),
get_deployment(conducted_at=time_now() - timedelta(days=6)),
get_deployment(conducted_at=time_now() - timedelta(days=4)),
get_deployment(conducted_at=time_now() - timedelta(days=2)),
get_deployment(conducted_at=time_now() - timedelta(hours=6)),
]
deployment_incidents_count_map = incident_service.get_deployment_incidents_map(
deployments, incidents
Expand Down
Loading