Browse Source

feat(discover): Update events-facets endpoint to map device class values to high, medium, low (#45579)

Updates `events-facets` endpoint to map `device.class` tag values from
`1, 2, 3` to `high, medium, low`.
edwardgou-sentry 2 years ago
parent
commit
798ec0239d

+ 15 - 0
src/sentry/api/endpoints/organization_events_facets.py

@@ -7,6 +7,7 @@ from rest_framework.response import Response
 from sentry import tagstore
 from sentry.api.base import region_silo_endpoint
 from sentry.api.bases import NoProjects, OrganizationEventsV2EndpointBase
+from sentry.search.utils import DEVICE_CLASS
 from sentry.snuba import discover
 
 
@@ -42,6 +43,7 @@ class OrganizationEventsFacetsEndpoint(OrganizationEventsV2EndpointBase):
                         "count": row.count,
                     }
                 )
+
             if "project" in resp:
                 # Replace project ids with slugs as that is what we generally expose to users
                 # and filter out projects that the user doesn't have access too.
@@ -55,4 +57,17 @@ class OrganizationEventsFacetsEndpoint(OrganizationEventsV2EndpointBase):
 
                 resp["project"]["topValues"] = filtered_values
 
+            if "device.class" in resp:
+                # Map device.class tag values to low, medium, or high
+                filtered_values = []
+                for v in resp["device.class"]["topValues"]:
+                    for key, value in DEVICE_CLASS.items():
+                        if v["value"] in value:
+                            name = key
+                            v.update({"name": name})
+                            filtered_values.append(v)
+                            continue
+
+                resp["device.class"]["topValues"] = filtered_values
+
         return Response(list(resp.values()))

+ 3 - 3
src/sentry/search/events/datasets/discover.py

@@ -1251,14 +1251,14 @@ class DiscoverDatasetConfig(DatasetConfig):
             "multiIf",
             [
                 Function(
-                    "in", [self.builder.column("tags[device.class]"), list(DEVICE_CLASS["LOW"])]
+                    "in", [self.builder.column("tags[device.class]"), list(DEVICE_CLASS["low"])]
                 ),
                 "low",
                 Function(
                     "in",
                     [
                         self.builder.column("tags[device.class]"),
-                        list(DEVICE_CLASS["MEDIUM"]),
+                        list(DEVICE_CLASS["medium"]),
                     ],
                 ),
                 "medium",
@@ -1266,7 +1266,7 @@ class DiscoverDatasetConfig(DatasetConfig):
                     "in",
                     [
                         self.builder.column("tags[device.class]"),
-                        list(DEVICE_CLASS["HIGH"]),
+                        list(DEVICE_CLASS["high"]),
                     ],
                 ),
                 "high",

+ 3 - 3
src/sentry/search/utils.py

@@ -759,7 +759,7 @@ def validate_cdc_search_filters(search_filters: Optional[Sequence[SearchFilter]]
 
 # Mapping of device class to the store corresponding tag value
 DEVICE_CLASS = {
-    "LOW": {"1"},
-    "MEDIUM": {"2"},
-    "HIGH": {"3"},
+    "low": {"1"},
+    "medium": {"2"},
+    "high": {"3"},
 }

+ 37 - 0
tests/snuba/api/endpoints/test_organization_events_facets.py

@@ -632,3 +632,40 @@ class OrganizationEventsFacetsEndpointTest(SnubaTestCase, APITestCase):
             )
 
             assert len(mock_quantize.mock_calls) == 2
+
+    def test_device_class(self):
+        self.store_event(
+            data={
+                "event_id": uuid4().hex,
+                "timestamp": self.min_ago_iso,
+                "tags": {"device.class": "1"},
+            },
+            project_id=self.project2.id,
+        )
+        self.store_event(
+            data={
+                "event_id": uuid4().hex,
+                "timestamp": self.min_ago_iso,
+                "tags": {"device.class": "2"},
+            },
+            project_id=self.project.id,
+        )
+        self.store_event(
+            data={
+                "event_id": uuid4().hex,
+                "timestamp": self.min_ago_iso,
+                "tags": {"device.class": "3"},
+            },
+            project_id=self.project.id,
+        )
+
+        with self.feature(self.features):
+            response = self.client.get(self.url, format="json")
+
+        assert response.status_code == 200, response.content
+        expected = [
+            {"count": 1, "name": "high", "value": "3"},
+            {"count": 1, "name": "medium", "value": "2"},
+            {"count": 1, "name": "low", "value": "1"},
+        ]
+        self.assert_facet(response, "device.class", expected)