Skip to content

Commit

Permalink
feat(backup): Add creator/owner email/username to GET /relocations/
Browse files Browse the repository at this point in the history
This makes the list a bit easier to render in the admin panel, and
minimizes the number of round trips to the server.

Issue: getsentry/team-ospo#214
  • Loading branch information
azaslavsky committed Dec 18, 2023
1 parent 0516268 commit 1ed4b90
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/sentry/api/endpoints/relocations/unpause.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ def put(self, request: Request, relocation_uuid: str) -> Response:
)

task.delay(str(relocation.uuid))
return self.respond(serialize(relocation))

return self.respond(serialize(relocation))
26 changes: 22 additions & 4 deletions src/sentry/api/serializers/models/relocation.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import Any, Mapping
from typing import Any, Mapping, MutableMapping, Sequence

from sentry.api.serializers import Serializer, register
from sentry.models.relocation import Relocation
from sentry.models.user import User
from sentry.services.hybrid_cloud.user.model import RpcUser
from sentry.services.hybrid_cloud.user.service import user_service
from sentry.utils.json import JSONData


@register(Relocation)
class RelocationSerializer(Serializer):
def serialize(
self, obj: Any, attrs: Mapping[Any, Any], user: Any, **kwargs: Any
self, obj: Relocation, attrs: Mapping[int, RpcUser], user: User, **kwargs: Any
) -> Mapping[str, JSONData]:
assert isinstance(obj, Relocation)

scheduled_at_pause_step = (
Relocation.Step(obj.scheduled_pause_at_step).name
if obj.scheduled_pause_at_step is not None
Expand All @@ -27,12 +28,17 @@ def serialize(
if obj.latest_notified is not None
else None
)

return {
"dateAdded": obj.date_added,
"dateUpdated": obj.date_updated,
"uuid": str(obj.uuid),
"creatorEmail": attrs[obj.creator_id].email,
"creatorId": str(obj.creator_id),
"creatorUsername": attrs[obj.creator_id].username,
"ownerEmail": attrs[obj.owner_id].email,
"ownerId": str(obj.owner_id),
"ownerUsername": attrs[obj.owner_id].username,
"status": Relocation.Status(obj.status).name,
"step": Relocation.Step(obj.step).name,
"failureReason": obj.failure_reason,
Expand All @@ -43,3 +49,15 @@ def serialize(
"latestNotified": latest_notified,
"latestUnclaimedEmailsSentAt": obj.latest_unclaimed_emails_sent_at,
}

def get_attrs(
self, item_list: Sequence[Relocation], user: User, **kwargs: Any
) -> MutableMapping[Relocation, Mapping[int, RpcUser]]:
user_ids = set()
for relocation in item_list:
user_ids.add(relocation.creator_id)
user_ids.add(relocation.owner_id)

users = user_service.get_many(filter=dict(user_ids=list(user_ids)))
user_map = {u.id: u for u in users}
return {r: user_map for r in item_list}
6 changes: 6 additions & 0 deletions tests/sentry/api/endpoints/relocations/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def test_good_status_in_progress(self):
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]["status"] == Relocation.Status.IN_PROGRESS.name
assert response.data[0]["creatorId"] == str(self.superuser.id)
assert response.data[0]["creatorEmail"] == str(self.superuser.email)
assert response.data[0]["creatorUsername"] == str(self.superuser.username)
assert response.data[0]["ownerId"] == str(self.owner.id)
assert response.data[0]["ownerEmail"] == str(self.owner.email)
assert response.data[0]["ownerUsername"] == str(self.owner.username)

def test_good_status_pause(self):
self.login_as(user=self.superuser, superuser=True)
Expand Down
16 changes: 16 additions & 0 deletions tests/sentry/api/serializers/test_relocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def test_in_progress(self):
assert result["dateUpdated"] == TEST_DATE_UPDATED
assert result["uuid"] == str(relocation.uuid)
assert result["creatorId"] == str(self.superuser.id)
assert result["creatorEmail"] == self.superuser.email
assert result["creatorUsername"] == self.superuser.username
assert result["ownerId"] == str(self.owner.id)
assert result["ownerEmail"] == self.owner.email
assert result["ownerUsername"] == self.owner.username
assert result["status"] == Relocation.Status.IN_PROGRESS.name
assert result["step"] == Relocation.Step.UPLOADING.name
assert not result["failureReason"]
Expand Down Expand Up @@ -74,7 +78,11 @@ def test_pause(self):
assert result["dateUpdated"] == TEST_DATE_UPDATED
assert result["uuid"] == str(relocation.uuid)
assert result["creatorId"] == str(self.superuser.id)
assert result["creatorEmail"] == self.superuser.email
assert result["creatorUsername"] == self.superuser.username
assert result["ownerId"] == str(self.owner.id)
assert result["ownerEmail"] == self.owner.email
assert result["ownerUsername"] == self.owner.username
assert result["status"] == Relocation.Status.PAUSE.name
assert result["step"] == Relocation.Step.IMPORTING.name
assert not result["failureReason"]
Expand Down Expand Up @@ -107,7 +115,11 @@ def test_success(self):
assert result["dateUpdated"] == TEST_DATE_UPDATED
assert result["uuid"] == str(relocation.uuid)
assert result["creatorId"] == str(self.superuser.id)
assert result["creatorEmail"] == self.superuser.email
assert result["creatorUsername"] == self.superuser.username
assert result["ownerId"] == str(self.owner.id)
assert result["ownerEmail"] == self.owner.email
assert result["ownerUsername"] == self.owner.username
assert result["status"] == Relocation.Status.SUCCESS.name
assert result["step"] == Relocation.Step.COMPLETED.name
assert not result["failureReason"]
Expand Down Expand Up @@ -141,7 +153,11 @@ def test_failure(self):
assert result["dateUpdated"] == TEST_DATE_UPDATED
assert result["uuid"] == str(relocation.uuid)
assert result["creatorId"] == str(self.superuser.id)
assert result["creatorEmail"] == self.superuser.email
assert result["creatorUsername"] == self.superuser.username
assert result["ownerId"] == str(self.owner.id)
assert result["ownerEmail"] == self.owner.email
assert result["ownerUsername"] == self.owner.username
assert result["status"] == Relocation.Status.FAILURE.name
assert result["step"] == Relocation.Step.VALIDATING.name
assert result["failureReason"] == "Some failure reason"
Expand Down

0 comments on commit 1ed4b90

Please sign in to comment.