6
6
from typing import Any , ClassVar , Literal , Protocol , TypedDict
7
7
8
8
from django .core .cache import cache
9
- from django .db .models import QuerySet
10
9
from snuba_sdk import Op
11
10
12
11
from sentry import release_health , tsdb
16
15
get_issue_tsdb_user_group_model ,
17
16
)
18
17
from sentry .issues .grouptype import GroupCategory , get_group_type_by_type_id
19
- from sentry .models .group import Group
20
18
from sentry .rules .conditions .event_attribute import ATTR_CHOICES
21
19
from sentry .rules .conditions .event_frequency import (
22
20
MIN_SESSIONS_TO_FIRE ,
35
33
QueryResult = dict [int , int | float ]
36
34
37
35
36
+ class GroupValues (TypedDict ):
37
+ id : int
38
+ type : int
39
+ project_id : int
40
+ project__organization_id : int
41
+
42
+
38
43
class TSDBFunction (Protocol ):
39
44
def __call__ (
40
45
self ,
@@ -61,13 +66,6 @@ class InvalidFilter(Exception):
61
66
pass
62
67
63
68
64
- class _QSTypedDict (TypedDict ):
65
- id : int
66
- type : int
67
- project_id : int
68
- project__organization_id : int
69
-
70
-
71
69
class BaseEventFrequencyQueryHandler (ABC ):
72
70
intervals : ClassVar [dict [str , tuple [str , timedelta ]]] = STANDARD_INTERVALS
73
71
@@ -154,7 +152,7 @@ def get_chunked_result(
154
152
155
153
def get_group_ids_by_category (
156
154
self ,
157
- groups : QuerySet [ Group , _QSTypedDict ],
155
+ groups : list [ GroupValues ],
158
156
) -> dict [GroupCategory , list [int ]]:
159
157
"""
160
158
Separate group ids into error group ids and generic group ids
@@ -170,7 +168,7 @@ def get_group_ids_by_category(
170
168
171
169
def get_value_from_groups (
172
170
self ,
173
- groups : QuerySet [ Group , _QSTypedDict ] | None ,
171
+ groups : list [ GroupValues ] ,
174
172
value : Literal ["id" , "project_id" , "project__organization_id" ],
175
173
) -> int | None :
176
174
result = None
@@ -270,7 +268,7 @@ def convert_filter_to_snuba_condition(
270
268
@abstractmethod
271
269
def batch_query (
272
270
self ,
273
- group_ids : set [ int ],
271
+ groups : list [ GroupValues ],
274
272
start : datetime ,
275
273
end : datetime ,
276
274
environment_id : int | None ,
@@ -285,7 +283,7 @@ def batch_query(
285
283
def get_rate_bulk (
286
284
self ,
287
285
duration : timedelta ,
288
- group_ids : set [ int ],
286
+ groups : list [ GroupValues ],
289
287
environment_id : int | None ,
290
288
current_time : datetime ,
291
289
comparison_interval : timedelta | None ,
@@ -306,7 +304,7 @@ def get_rate_bulk(
306
304
307
305
with self .disable_consistent_snuba_mode (duration ):
308
306
result = self .batch_query (
309
- group_ids = group_ids ,
307
+ groups = groups ,
310
308
start = start ,
311
309
end = end ,
312
310
environment_id = environment_id ,
@@ -325,16 +323,13 @@ def get_rate_bulk(
325
323
class EventFrequencyQueryHandler (BaseEventFrequencyQueryHandler ):
326
324
def batch_query (
327
325
self ,
328
- group_ids : set [ int ],
326
+ groups : list [ GroupValues ],
329
327
start : datetime ,
330
328
end : datetime ,
331
329
environment_id : int | None ,
332
330
filters : list [QueryFilter ] | None = None ,
333
331
) -> QueryResult :
334
332
batch_sums : QueryResult = defaultdict (int )
335
- groups = Group .objects .filter (id__in = group_ids ).values (
336
- "id" , "type" , "project_id" , "project__organization_id"
337
- )
338
333
category_group_ids = self .get_group_ids_by_category (groups )
339
334
organization_id = self .get_value_from_groups (groups , "project__organization_id" )
340
335
@@ -371,16 +366,13 @@ def batch_query(
371
366
class EventUniqueUserFrequencyQueryHandler (BaseEventFrequencyQueryHandler ):
372
367
def batch_query (
373
368
self ,
374
- group_ids : set [ int ],
369
+ groups : list [ GroupValues ],
375
370
start : datetime ,
376
371
end : datetime ,
377
372
environment_id : int | None ,
378
373
filters : list [QueryFilter ] | None = None ,
379
374
) -> QueryResult :
380
375
batch_sums : QueryResult = defaultdict (int )
381
- groups = Group .objects .filter (id__in = group_ids ).values (
382
- "id" , "type" , "project_id" , "project__organization_id"
383
- )
384
376
category_group_ids = self .get_group_ids_by_category (groups )
385
377
organization_id = self .get_value_from_groups (groups , "project__organization_id" )
386
378
@@ -442,16 +434,13 @@ def get_session_interval(self, session_count: int, duration: timedelta) -> int |
442
434
443
435
def batch_query (
444
436
self ,
445
- group_ids : set [ int ],
437
+ groups : list [ GroupValues ],
446
438
start : datetime ,
447
439
end : datetime ,
448
440
environment_id : int | None ,
449
441
filters : list [QueryFilter ] | None = None ,
450
442
) -> QueryResult :
451
443
batch_percents : QueryResult = {}
452
- groups = Group .objects .filter (id__in = group_ids ).values (
453
- "id" , "type" , "project_id" , "project__organization_id"
454
- )
455
444
category_group_ids = self .get_group_ids_by_category (groups )
456
445
project_id = self .get_value_from_groups (groups , "project_id" )
457
446
0 commit comments