-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add GQL mutation to store event metrics (#620)
- Loading branch information
1 parent
6efefaf
commit 1787236
Showing
12 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
codecov_auth/commands/owner/interactors/store_codecov_metric.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
|
||
from shared.django_apps.codecov_metrics.service.codecov_metrics import ( | ||
UserOnboardingMetricsService, | ||
) | ||
|
||
from codecov.commands.base import BaseInteractor | ||
from codecov.commands.exceptions import Unauthenticated, ValidationError | ||
from codecov.db import sync_to_async | ||
from codecov_auth.models import Owner | ||
|
||
|
||
class StoreCodecovMetricInteractor(BaseInteractor): | ||
@sync_to_async | ||
def execute(self, org_username: str, event: str, json_string: str) -> None: | ||
current_org = Owner.objects.filter( | ||
username=org_username, service=self.service | ||
).first() | ||
if not current_org: | ||
raise ValidationError("Cannot find owner record in the database") | ||
|
||
try: | ||
payload = json.loads(json_string) | ||
except json.JSONDecodeError: | ||
raise ValidationError("Invalid JSON string") | ||
|
||
UserOnboardingMetricsService.create_user_onboarding_metric( | ||
org_id=current_org.pk, | ||
event=event, | ||
payload=payload, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
graphql_api/tests/mutation/test_store_codecov_metrics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from django.test import TransactionTestCase | ||
from shared.django_apps.codecov_metrics.models import UserOnboardingLifeCycleMetrics | ||
|
||
from codecov_auth.tests.factories import OwnerFactory | ||
from graphql_api.tests.helper import GraphQLTestHelper | ||
|
||
query = """ | ||
mutation($input: StoreEventMetricsInput!) { | ||
storeEventMetric(input: $input) { | ||
error { | ||
__typename | ||
... on ResolverError { | ||
message | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
class StoreEventMetricMutationTest(GraphQLTestHelper, TransactionTestCase): | ||
def _request(self, org_username: str, event: str, json_payload: str, owner=None): | ||
return self.gql_request( | ||
query, | ||
variables={ | ||
"input": { | ||
"orgUsername": org_username, | ||
"eventName": event, | ||
"jsonPayload": json_payload, | ||
} | ||
}, | ||
owner=owner, | ||
) | ||
|
||
def setUp(self): | ||
self.owner = OwnerFactory(username="codecov-user") | ||
|
||
def test_unauthenticated(self): | ||
response = self._request( | ||
org_username="codecov-user", | ||
event="VISITED_PAGE", | ||
json_payload='{"key": "value"}', | ||
) | ||
assert response == { | ||
"storeEventMetric": { | ||
"error": { | ||
"__typename": "UnauthenticatedError", | ||
"message": "You are not authenticated", | ||
} | ||
} | ||
} | ||
|
||
def test_authenticated_inserts_into_db(self): | ||
response = self._request( | ||
org_username="codecov-user", | ||
event="VISITED_PAGE", | ||
json_payload='{"some-key": "some-value"}', | ||
owner=self.owner, | ||
) | ||
metric = UserOnboardingLifeCycleMetrics.objects.filter( | ||
event="VISITED_PAGE" | ||
).first() | ||
self.assertIsNotNone(metric) | ||
self.assertEqual(metric.additional_data, {"some-key": "some-value"}) | ||
|
||
def test_invalid_org(self): | ||
response = self._request( | ||
org_username="invalid_org", | ||
event="VISITED_PAGE", | ||
json_payload='{"key": "value"}', | ||
owner=self.owner, | ||
) | ||
assert response == { | ||
"storeEventMetric": { | ||
"error": { | ||
"__typename": "ValidationError", | ||
"message": "Cannot find owner record in the database", | ||
} | ||
} | ||
} | ||
|
||
def test_invalid_event(self): | ||
self._request( | ||
org_username="codecov-user", | ||
event="INVALID_EVENT", | ||
json_payload='{"key": "value"}', | ||
owner=self.owner, | ||
) | ||
metric = UserOnboardingLifeCycleMetrics.objects.filter( | ||
event="INVALID_EVENT" | ||
).first() | ||
self.assertIsNone(metric) | ||
|
||
def test_invalid_json_string(self): | ||
response = self._request( | ||
org_username="codecov-user", | ||
event="VISITED_PAGE", | ||
json_payload="invalid-json", | ||
owner=self.owner, | ||
) | ||
assert response == { | ||
"storeEventMetric": { | ||
"error": { | ||
"__typename": "ValidationError", | ||
"message": "Invalid JSON string", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from graphql_api.helpers.ariadne import ariadne_load_local_graphql | ||
|
||
from .store_event_metrics import error_store_event_metrics, resolve_store_event_metrics | ||
|
||
gql_store_event_metrics = ariadne_load_local_graphql( | ||
__file__, "store_event_metrics.graphql" | ||
) |
11 changes: 11 additions & 0 deletions
11
graphql_api/types/mutation/store_event_metrics/store_event_metrics.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
union StoreEventMetricsError = UnauthenticatedError | ValidationError | ||
|
||
type StoreEventMetricsPayload { | ||
error: StoreEventMetricsError | ||
} | ||
|
||
input StoreEventMetricsInput { | ||
orgUsername: String! | ||
eventName: String! | ||
jsonPayload: String # The input expects a serialized json string | ||
} |
22 changes: 22 additions & 0 deletions
22
graphql_api/types/mutation/store_event_metrics/store_event_metrics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from ariadne import UnionType | ||
|
||
from codecov_auth.commands.owner import OwnerCommands | ||
from graphql_api.helpers.mutation import ( | ||
require_authenticated, | ||
resolve_union_error_type, | ||
wrap_error_handling_mutation, | ||
) | ||
|
||
|
||
@wrap_error_handling_mutation | ||
@require_authenticated | ||
async def resolve_store_event_metrics(_, info, input) -> None: | ||
command: OwnerCommands = info.context["executor"].get_command("owner") | ||
await command.store_codecov_metric( | ||
input.get("orgUsername"), input.get("eventName"), input.get("jsonPayload") | ||
) | ||
return None | ||
|
||
|
||
error_store_event_metrics = UnionType("StoreEventMetricsError") | ||
error_store_event_metrics.type_resolver(resolve_union_error_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters