Skip to content
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
45 changes: 27 additions & 18 deletions src/sentry/api/endpoints/prompts_activity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import calendar

from django.db import IntegrityError, transaction
from django.db.models import Q
from django.http import HttpResponse
from django.utils import timezone
from rest_framework import serializers
Expand All @@ -15,6 +16,7 @@
VALID_STATUSES = frozenset(("snoozed", "dismissed"))


# Endpoint to retrieve multiple PromptsActivity at once
class PromptsActivitySerializer(serializers.Serializer):
feature = serializers.CharField(required=True)
status = serializers.ChoiceField(choices=zip(VALID_STATUSES, VALID_STATUSES), required=True)
Expand All @@ -33,24 +35,31 @@ class PromptsActivityEndpoint(Endpoint):
def get(self, request):
"""Return feature prompt status if dismissed or in snoozed period"""

feature = request.GET.get("feature")

if not prompt_config.has(feature):
return Response({"detail": "Invalid feature name"}, status=400)

required_fields = prompt_config.required_fields(feature)
for field in required_fields:
if field not in request.GET:
return Response({"detail": 'Missing required field "%s"' % field}, status=400)

filters = {k: request.GET.get(k) for k in required_fields}

try:
result = PromptsActivity.objects.get(user=request.user, feature=feature, **filters)
except PromptsActivity.DoesNotExist:
return Response({})

return Response({"data": result.data})
features = request.GET.getlist("feature")
Copy link
Contributor

Choose a reason for hiding this comment

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

@Neo-Zhixing Nicely done using getlist here :)

if len(features) == 0:
return Response({"details": "No feature specified"}, status=400)

conditions = None
for feature in features:
if not prompt_config.has(feature):
return Response({"detail": "Invalid feature name " + feature}, status=400)

required_fields = prompt_config.required_fields(feature)
for field in required_fields:
if field not in request.GET:
return Response({"detail": 'Missing required field "%s"' % field}, status=400)
filters = {k: request.GET.get(k) for k in required_fields}
condition = Q(feature=feature, **filters)
conditions = condition if conditions is None else (conditions | condition)

result = PromptsActivity.objects.filter(conditions, user=request.user)
featuredata = {k.feature: k.data for k in result}
if len(features) == 1:
result = result.first()
data = None if result is None else result.data
return Response({"data": data, "features": featuredata})
Copy link
Contributor

Choose a reason for hiding this comment

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

@Neo-Zhixing excellent idea also setting features even though we aren't using it, it will make future changes much easier

else:
return Response({"features": featuredata})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to maintain backward compatibility with a bunch of tests that mocks the endpoint with {data: xxx}.
Long term we might want to update this so that we can support both promptsCheck and batchedPromptsCheck with a uniform behavior, regardless of the number of features requested.


def put(self, request):
serializer = PromptsActivitySerializer(data=request.data)
Expand Down
47 changes: 43 additions & 4 deletions static/app/actionCreators/prompts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ type PromptCheckParams = {
feature: string;
};

export type PromptResponseItem = {
snoozed_ts?: number;
dismissed_ts?: number;
};
export type PromptResponse = {
data?: {
snoozed_ts?: number;
dismissed_ts?: number;
};
data?: PromptResponseItem;
features?: {[key: string]: PromptResponseItem};
};

export type PromptData = null | {
Expand Down Expand Up @@ -85,3 +87,40 @@ export async function promptsCheck(
snoozedTime: data.snoozed_ts,
};
}

/**
* Get the status of many prompt
*/
export async function batchedPromptsCheck<T extends readonly string[]>(
api: Client,
features: T,
params: {organizationId: string; projectId?: string}
): Promise<{[key in T[number]]: PromptData}> {
const query = {
feature: features,
organization_id: params.organizationId,
...(params.projectId === undefined ? {} : {project_id: params.projectId}),
};

const response: PromptResponse = await api.requestPromise('/prompts-activity/', {
query,
});
const responseFeatures = response?.features;

const result: {[key in T[number]]?: PromptData} = {};
if (!responseFeatures) {
return result as {[key in T[number]]: PromptData};
}
for (const featureName of features) {
const item = responseFeatures[featureName];
if (item) {
result[featureName] = {
dismissedTime: item.dismissed_ts,
snoozedTime: item.snoozed_ts,
};
} else {
result[featureName] = null;
}
}
return result as {[key in T[number]]: PromptData};
}
3 changes: 3 additions & 0 deletions static/app/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ export enum DataCategory {
TRANSACTIONS = 'transactions',
ATTACHMENTS = 'attachments',
}

export type EventType = 'error' | 'transaction' | 'attachment';

export const DataCategoryName = {
[DataCategory.ERRORS]: 'Errors',
[DataCategory.TRANSACTIONS]: 'Transactions',
Expand Down
59 changes: 57 additions & 2 deletions tests/sentry/api/endpoints/test_prompts_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def test_invalid_feature(self):

assert resp.status_code == 400

def test_batched_invalid_feature(self):
# Invalid feature prompt name
resp = self.client.put(
self.path,
{
"organization_id": self.org.id,
"project_id": self.project.id,
"feature": ["releases", "gibberish"],
"status": "dismissed",
},
)

assert resp.status_code == 400

def test_invalid_project(self):
# Invalid project id
data = {
Expand Down Expand Up @@ -64,7 +78,7 @@ def test_dismiss(self):
}
resp = self.client.get(self.path, data)
assert resp.status_code == 200
assert resp.data == {}
assert resp.data.get("data", None) is None

self.client.put(
self.path,
Expand All @@ -89,7 +103,7 @@ def test_snooze(self):
}
resp = self.client.get(self.path, data)
assert resp.status_code == 200
assert resp.data == {}
assert resp.data.get("data", None) is None

self.client.put(
self.path,
Expand All @@ -106,3 +120,44 @@ def test_snooze(self):
assert resp.status_code == 200
assert "data" in resp.data
assert "snoozed_ts" in resp.data["data"]

def test_batched(self):
data = {
"organization_id": self.org.id,
"project_id": self.project.id,
"feature": ["releases", "alert_stream"],
}
resp = self.client.get(self.path, data)
assert resp.status_code == 200
assert resp.data["features"].get("releases", None) is None
assert resp.data["features"].get("alert_stream", None) is None

self.client.put(
self.path,
{
"organization_id": self.org.id,
"project_id": self.project.id,
"feature": "releases",
"status": "dismissed",
},
)

resp = self.client.get(self.path, data)
assert resp.status_code == 200
assert "dismissed_ts" in resp.data["features"]["releases"]
assert resp.data["features"].get("alert_stream", None) is None

self.client.put(
self.path,
{
"organization_id": self.org.id,
"project_id": self.project.id,
"feature": "alert_stream",
"status": "snoozed",
},
)

resp = self.client.get(self.path, data)
assert resp.status_code == 200
assert "dismissed_ts" in resp.data["features"]["releases"]
assert "snoozed_ts" in resp.data["features"]["alert_stream"]