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

add percent changes to test results and flakes aggregates #829

Merged
merged 10 commits into from
Oct 4, 2024

Conversation

joseph-sentry
Copy link
Contributor

This adds PercentChange fields to both the TestResultsAggregates
and the FlakeAggregates. These work by getting the aggregates
for the previous historical period and comparing them with the
ones for the current historical period and getting a percentage difference.

@codecov-staging
Copy link

codecov-staging bot commented Sep 16, 2024

Codecov Report

Attention: Patch coverage is 98.71795% with 1 line in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
utils/test_results.py 97.22% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codecov-qa
Copy link

codecov-qa bot commented Sep 16, 2024

❌ 2 Tests Failed:

Tests completed Failed Passed Skipped
2339 2 2337 6
View the top 2 failed tests by shortest run time
graphql_api.tests.test_test_results_headers.TestResultTestCase test_fetch_test_result_total_runtime
Stack Traces | 0.419s run time
self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_total_runtime>

    def test_fetch_test_result_total_runtime(self) -> None:
        query = """
            query {
               owner(username: "%s") {
                    repository(name: "%s") {
                        ... on Repository {
                            testResultsAggregates {
                                totalRunTime
                            }
                        }
                    }
                 }
            }
        """ % (self.owner.username, self.repository.name)
    
>       result = self.gql_request(query, owner=self.owner)

graphql_api/tests/test_test_results_headers.py:48: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_total_runtime>
query = '\n            query {\n               owner(username: "randomOwner") {\n                    repository(name: "address...                      }\n                        }\n                    }\n                 }\n            }\n        '
provider = 'gh', owner = <Owner: Owner<github/randomOwner>>, variables = None
with_errors = False, okta_signed_in_accounts = [], impersonate_owner = False

    def gql_request(
        self,
        query,
        provider="gh",
        owner=None,
        variables=None,
        with_errors=False,
        okta_signed_in_accounts=[],
        impersonate_owner=False,
    ):
        url = f"/graphql/{provider}"
    
        if owner:
            self.client = Client()
    
            if impersonate_owner:
                staff_owner = OwnerFactory(
                    name="staff_user", service="github", user=UserFactory(is_staff=True)
                )
                self.client.cookies = SimpleCookie({"staff_user": owner.pk})
                self.client.force_login_owner(staff_owner)
            else:
                self.client.force_login_owner(owner)
    
            if okta_signed_in_accounts:
                session = self.client.session
                session[OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY] = okta_signed_in_accounts
                session.save()
    
        response = self.client.post(
            url,
            {"query": query, "variables": variables or {}},
            content_type="application/json",
        )
>       return response.json() if with_errors else response.json()["data"]
E       KeyError: 'data'

graphql_api/tests/helper.py:44: KeyError
graphql_api.tests.test_test_results_headers.TestResultTestCase test_fetch_test_result_slowest_tests_runtime
Stack Traces | 0.434s run time
self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_slowest_tests_runtime>

    def test_fetch_test_result_slowest_tests_runtime(self) -> None:
        query = """
            query {
               owner(username: "%s") {
                    repository(name: "%s") {
                        ... on Repository {
                            testResultsAggregates {
                                slowestTestsRunTime
                            }
                        }
                    }
                 }
            }
        """ % (self.owner.username, self.repository.name)
    
>       result = self.gql_request(query, owner=self.owner)

graphql_api/tests/test_test_results_headers.py:71: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_slowest_tests_runtime>
query = '\n            query {\n               owner(username: "randomOwner") {\n                    repository(name: "so") {\...                      }\n                        }\n                    }\n                 }\n            }\n        '
provider = 'gh', owner = <Owner: Owner<github/randomOwner>>, variables = None
with_errors = False, okta_signed_in_accounts = [], impersonate_owner = False

    def gql_request(
        self,
        query,
        provider="gh",
        owner=None,
        variables=None,
        with_errors=False,
        okta_signed_in_accounts=[],
        impersonate_owner=False,
    ):
        url = f"/graphql/{provider}"
    
        if owner:
            self.client = Client()
    
            if impersonate_owner:
                staff_owner = OwnerFactory(
                    name="staff_user", service="github", user=UserFactory(is_staff=True)
                )
                self.client.cookies = SimpleCookie({"staff_user": owner.pk})
                self.client.force_login_owner(staff_owner)
            else:
                self.client.force_login_owner(owner)
    
            if okta_signed_in_accounts:
                session = self.client.session
                session[OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY] = okta_signed_in_accounts
                session.save()
    
        response = self.client.post(
            url,
            {"query": query, "variables": variables or {}},
            content_type="application/json",
        )
>       return response.json() if with_errors else response.json()["data"]
E       KeyError: 'data'

graphql_api/tests/helper.py:44: KeyError

To view individual test run time comparison to the main branch, go to the Test Analytics Dashboard

Copy link

codecov-public-qa bot commented Sep 16, 2024

Test Failures Detected: Due to failing tests, we cannot provide coverage reports at this time.

❌ Failed Test Results:

Completed 2345 tests with 2 failed, 2337 passed and 6 skipped.

View the full list of failed tests

pytest

  • Class name: graphql_api.tests.test_test_results_headers.TestResultTestCase
    Test name: test_fetch_test_result_slowest_tests_runtime

    self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_slowest_tests_runtime>

    def test_fetch_test_result_slowest_tests_runtime(self) -> None:
    query = """
    query {
    owner(username: "%s") {
    repository(name: "%s") {
    ... on Repository {
    testResultsAggregates {
    slowestTestsRunTime
    }
    }
    }
    }
    }
    """ % (self.owner.username, self.repository.name)

    > result = self.gql_request(query, owner=self.owner)

    graphql_api/tests/test_test_results_headers.py:71:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_slowest_tests_runtime>
    query = '\n query {\n owner(username: "randomOwner") {\n repository(name: "so") {\... }\n }\n }\n }\n }\n '
    provider = 'gh', owner = <Owner: Owner<github/randomOwner>>, variables = None
    with_errors = False, okta_signed_in_accounts = [], impersonate_owner = False

    def gql_request(
    self,
    query,
    provider="gh",
    owner=None,
    variables=None,
    with_errors=False,
    okta_signed_in_accounts=[],
    impersonate_owner=False,
    ):
    url = f"/graphql/{provider}"

    if owner:
    self.client = Client()

    if impersonate_owner:
    staff_owner = OwnerFactory(
    name="staff_user", service="github", user=UserFactory(is_staff=True)
    )
    self.client.cookies = SimpleCookie({"staff_user": owner.pk})
    self.client.force_login_owner(staff_owner)
    else:
    self.client.force_login_owner(owner)

    if okta_signed_in_accounts:
    session = self.client.session
    session[OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY] = okta_signed_in_accounts
    session.save()

    response = self.client.post(
    url,
    {"query": query, "variables": variables or {}},
    content_type="application/json",
    )
    > return response.json() if with_errors else response.json()["data"]
    E KeyError: 'data'

    graphql_api/tests/helper.py:44: KeyError
  • Class name: graphql_api.tests.test_test_results_headers.TestResultTestCase
    Test name: test_fetch_test_result_total_runtime

    self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_total_runtime>

    def test_fetch_test_result_total_runtime(self) -> None:
    query = """
    query {
    owner(username: "%s") {
    repository(name: "%s") {
    ... on Repository {
    testResultsAggregates {
    totalRunTime
    }
    }
    }
    }
    }
    """ % (self.owner.username, self.repository.name)

    > result = self.gql_request(query, owner=self.owner)

    graphql_api/tests/test_test_results_headers.py:48:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <graphql_api.tests.test_test_results_headers.TestResultTestCase testMethod=test_fetch_test_result_total_runtime>
    query = '\n query {\n owner(username: "randomOwner") {\n repository(name: "address... }\n }\n }\n }\n }\n '
    provider = 'gh', owner = <Owner: Owner<github/randomOwner>>, variables = None
    with_errors = False, okta_signed_in_accounts = [], impersonate_owner = False

    def gql_request(
    self,
    query,
    provider="gh",
    owner=None,
    variables=None,
    with_errors=False,
    okta_signed_in_accounts=[],
    impersonate_owner=False,
    ):
    url = f"/graphql/{provider}"

    if owner:
    self.client = Client()

    if impersonate_owner:
    staff_owner = OwnerFactory(
    name="staff_user", service="github", user=UserFactory(is_staff=True)
    )
    self.client.cookies = SimpleCookie({"staff_user": owner.pk})
    self.client.force_login_owner(staff_owner)
    else:
    self.client.force_login_owner(owner)

    if okta_signed_in_accounts:
    session = self.client.session
    session[OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY] = okta_signed_in_accounts
    session.save()

    response = self.client.post(
    url,
    {"query": query, "variables": variables or {}},
    content_type="application/json",
    )
    > return response.json() if with_errors else response.json()["data"]
    E KeyError: 'data'

    graphql_api/tests/helper.py:44: KeyError

Copy link

codecov bot commented Sep 16, 2024

Codecov Report

Attention: Patch coverage is 98.71795% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.31%. Comparing base (acf749e) to head (f73c190).
Report is 5 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
utils/test_results.py 97.22% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #829      +/-   ##
==========================================
+ Coverage   96.28%   96.31%   +0.03%     
==========================================
  Files         818      818              
  Lines       18818    18975     +157     
==========================================
+ Hits        18118    18275     +157     
  Misses        700      700              
Flag Coverage Δ
unit 92.62% <98.71%> (+0.06%) ⬆️
unit-latest-uploader 92.62% <98.71%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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


flake_aggregates = ariadne_load_local_graphql(__file__, "flake_aggregates.graphql")

__all__ = ["get_current_license", "flake_aggregates_bindable"]
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need get_current_license here?

__file__, "test_results_aggregates.graphql"
)

__all__ = ["get_current_license", "test_results_aggregates_bindable"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar story with if we need get_current_license

return obj["flake_rate"]


@flake_aggregates_bindable.field("flakeCountPercentChange")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would personally organize these in "bunches" like flakeCount together and then flakeRate together vs. all the counts and all the %'s

I could see either approach tho



@flake_aggregates_bindable.field("flakeRatePercentChange")
def resolve_flake_rate_percent_change(obj, _) -> float | None:
Copy link
Contributor

Choose a reason for hiding this comment

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

in what situation would we return None for these? Vs. returning 0

repository: Repository,
info: GraphQLResolveInfo,
):
queryset = await sync_to_async(generate_test_results_aggregates)(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can you just do a return await here for both of these resolvers?

@@ -25,6 +25,16 @@ def resolve_failure_rate(test, info) -> float | None:
return test["failure_rate"]


@test_result_bindable.field("flakeRate")
def resolve_flake_rate(test, info) -> float | None:
Copy link
Contributor

Choose a reason for hiding this comment

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

when would we want these to return None vs. 0?


@test_results_aggregates_bindable.field("slowestTestsRunTimePercentChange")
def resolve_slowest_tests_run_time_percent_change(obj, _) -> float | None:
return obj.get("slowest_tests_duration_percent_change")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: why is this called duration vs. runTime like the other? and to make resolver name



@test_results_aggregates_bindable.field("totalRunTimePercentChange")
def resolve_run_time_percent_change(obj, _) -> float | None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly when would these be None vs. 0?

commits_where_fail: list[str] | None
average_duration: float | None
def slow_test_threshold(num_tests: int):
threshold = floor(num_tests * (100 - SLOW_TEST_PERCENTILE) * 0.01)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: / 100 is a little easier to understand what we're doing here vs. * .01

def slow_test_threshold(num_tests: int):
threshold = floor(num_tests * (100 - SLOW_TEST_PERCENTILE) * 0.01)
if threshold == 0:
if num_tests < (1 / ((100 - SLOW_TEST_PERCENTILE) * 0.01)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this line needed? When would this statement be false if we're taking the floor already on line 30

repoid: int,
branch: str | None = None,
history: dt.timedelta | None = None,
parameter: GENERATE_TEST_RESULT_PARAM | None = None,
) -> QuerySet:
"""
Function that retrieves aggregated information about all tests in a given repository, for a given time range, optionally filtered by branch name.
The fields it calculates are: the test failure rate, commits where this test failed, and average duration of the test.
Copy link
Contributor

Choose a reason for hiding this comment

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

as well as last duration, flake rate, etc

branch=branch
)
totals = totals.filter(branch=branch)
print(parameter)
Copy link
Contributor

Choose a reason for hiding this comment

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

Reminder prints

test_ids = [flake.test_id for flake in flakes]

totals = totals.filter(test_id__in=test_ids)
case GENERATE_TEST_RESULT_PARAM.FAILED:
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: do cases in python automatically have break statements built in?



def percent_diff(a: int | float, b: int | float) -> int | float:
c = (a - b) / b * 100
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: just return this instead of doing variable assignment

) -> dict[str, int | float]:
diff_dict = {}

for s in ls:
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: list comprehension

) -> dict[str, float | int] | None:
repo = Repository.objects.get(repoid=repoid)
time_ago = (
(dt.datetime.now(dt.UTC) - history) if history is not None else thirty_days_ago
Copy link
Contributor

Choose a reason for hiding this comment

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

we can simplify these if else's a bit if we set History to be a const dependent on it's value being passed in OR dt.timedelta(days=30)

so then we can just do time_ago = (
(dt.datetime.now(dt.UTC) - history) and double_time_ago = time_ago -
history

WIth something like history = history or dt.timedelta(days=30)

test_rollups = test_rollups.filter(date__lte=until.date())

if len(test_rollups) == 0:
assert 0 == 2
Copy link
Contributor

Choose a reason for hiding this comment

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

reminder remove assert

@joseph-sentry joseph-sentry force-pushed the joseph/percentage-change branch 2 times, most recently from d0fdc20 to a57b8f3 Compare October 3, 2024 20:59
This adds PercentChange fields to both the TestResultsAggregates
and the FlakeAggregates. These work by getting the aggregates
for the previous historical period and comparing them with the
ones for the current historical period and getting a percentage
difference.
@joseph-sentry joseph-sentry force-pushed the joseph/percentage-change branch from a57b8f3 to 2a2d550 Compare October 3, 2024 21:07
@joseph-sentry joseph-sentry marked this pull request as ready for review October 4, 2024 13:38
@joseph-sentry joseph-sentry requested a review from a team as a code owner October 4, 2024 13:38
Copy link
Contributor

@ajay-sentry ajay-sentry left a comment

Choose a reason for hiding this comment

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

looks goooood

@joseph-sentry joseph-sentry added this pull request to the merge queue Oct 4, 2024
Merged via the queue into main with commit 16557ec Oct 4, 2024
18 of 19 checks passed
@joseph-sentry joseph-sentry deleted the joseph/percentage-change branch October 4, 2024 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants