2424from sentry .snuba .utils import build_query_strings
2525from sentry .users .models .user import User
2626from sentry .users .services .user import RpcUser
27+ from sentry .workflow_engine .endpoints .serializers .detector_serializer import (
28+ DetectorSerializerResponse ,
29+ )
2730from sentry .workflow_engine .models import AlertRuleDetector
2831
2932CRASH_FREE_SESSIONS = "percentage(sessions_crashed, sessions) AS _crash_rate_alert_aggregate"
@@ -134,6 +137,7 @@ def fetch_metric_issue_open_periods(
134137 time_period : Mapping [str , str ],
135138 user : User | RpcUser | None = None ,
136139) -> list [Any ]:
140+ detector_id = open_period_identifier
137141 try :
138142 if features .has (
139143 "organizations:workflow-engine-single-process-metric-issues" ,
@@ -147,23 +151,36 @@ def fetch_metric_issue_open_periods(
147151 # open_period_identifier is a metric detector ID -> get the alert rule ID
148152 open_period_identifier = alert_rule_detector .alert_rule_id
149153
150- resp = client .get (
151- auth = ApiKey (organization_id = organization .id , scope_list = ["org:read" ]),
152- user = user ,
153- path = f"/organizations/{ organization .slug } /incidents/" ,
154- # TODO(iamrajjoshi): Use the correct endpoint and update the params
155- params = {
156- "alertRule" : open_period_identifier ,
157- "expand" : "activities" ,
158- "includeSnapshots" : True ,
159- "project" : - 1 ,
160- ** time_period ,
161- },
162- )
154+ if features .has (
155+ "organizations:new-metric-issue-charts" ,
156+ organization ,
157+ ):
158+ resp = client .get (
159+ auth = ApiKey (organization_id = organization .id , scope_list = ["org:read" ]),
160+ user = user ,
161+ path = f"/organizations/{ organization .slug } /open-periods/" ,
162+ params = {
163+ "detectorId" : detector_id ,
164+ ** time_period ,
165+ },
166+ )
167+ else :
168+ resp = client .get (
169+ auth = ApiKey (organization_id = organization .id , scope_list = ["org:read" ]),
170+ user = user ,
171+ path = f"/organizations/{ organization .slug } /incidents/" ,
172+ params = {
173+ "alertRule" : open_period_identifier ,
174+ "expand" : "activities" ,
175+ "includeSnapshots" : True ,
176+ "project" : - 1 ,
177+ ** time_period ,
178+ },
179+ )
163180 # TODO (mifu67): temporary log that I'm going to remove after debugging. Get the data for old and new
164- if organization .slug == "sentry" or organization . slug == "demo " :
181+ if organization .slug == "mf-test-n7 " :
165182 logger .info (
166- "fetching metric issue incidents " ,
183+ "fetching metric issue open periods " ,
167184 extra = {
168185 "organization_id" : organization .id ,
169186 "open_period_id" : open_period_identifier ,
@@ -193,18 +210,30 @@ def build_metric_alert_chart(
193210 user : User | RpcUser | None = None ,
194211 size : ChartSize | None = None ,
195212 subscription : QuerySubscription | None = None ,
213+ detector_serialized_response : DetectorSerializerResponse | None = None ,
196214) -> str | None :
197215 """
198216 Builds the dataset required for metric alert chart the same way the frontend would
199217 """
200218 dataset = Dataset (snuba_query .dataset )
201219 query_type = SnubaQuery .Type (snuba_query .type )
202220 is_crash_free_alert = query_type == SnubaQuery .Type .CRASH_RATE
203- style = (
204- ChartType .SLACK_METRIC_ALERT_SESSIONS
205- if is_crash_free_alert
206- else ChartType .SLACK_METRIC_ALERT_EVENTS
221+ using_new_charts = features .has (
222+ "organizations:new-metric-issue-charts" ,
223+ organization ,
207224 )
225+ if is_crash_free_alert :
226+ style = (
227+ ChartType .SLACK_METRIC_DETECTOR_SESSIONS
228+ if using_new_charts
229+ else ChartType .SLACK_METRIC_ALERT_SESSIONS
230+ )
231+ else :
232+ style = (
233+ ChartType .SLACK_METRIC_DETECTOR_EVENTS
234+ if using_new_charts
235+ else ChartType .SLACK_METRIC_ALERT_EVENTS
236+ )
208237
209238 if open_period_context :
210239 time_period = incident_date_range (
@@ -220,17 +249,32 @@ def build_metric_alert_chart(
220249 "start" : period_start .strftime (TIME_FORMAT ),
221250 "end" : timezone .now ().strftime (TIME_FORMAT ),
222251 }
223-
224- chart_data = {
225- "rule" : alert_rule_serialized_response ,
226- "selectedIncident" : selected_incident_serialized ,
227- "incidents" : fetch_metric_issue_open_periods (
228- organization ,
229- alert_context .action_identifier_id ,
230- time_period ,
231- user ,
232- ),
233- }
252+ if features .has (
253+ "organizations:new-metric-issue-charts" ,
254+ organization ,
255+ ):
256+ # TODO(mifu67): create detailed serializer for open period, pass here.
257+ # But we don't need it to render the chart, so it's fine for now.
258+ chart_data_detector = {
259+ "detector" : detector_serialized_response ,
260+ "open_periods" : fetch_metric_issue_open_periods (
261+ organization ,
262+ alert_context .action_identifier_id ,
263+ time_period ,
264+ user ,
265+ ),
266+ }
267+ else :
268+ chart_data_alert_rule = {
269+ "rule" : alert_rule_serialized_response ,
270+ "selectedIncident" : selected_incident_serialized ,
271+ "incidents" : fetch_metric_issue_open_periods (
272+ organization ,
273+ alert_context .action_identifier_id ,
274+ time_period ,
275+ user ,
276+ ),
277+ }
234278
235279 allow_mri = features .has (
236280 "organizations:insights-alerts" ,
@@ -264,6 +308,7 @@ def build_metric_alert_chart(
264308 )
265309 )
266310
311+ chart_data = {}
267312 query_params = {
268313 ** env_params ,
269314 ** time_period ,
@@ -303,6 +348,10 @@ def build_metric_alert_chart(
303348 )
304349
305350 try :
351+ if using_new_charts :
352+ chart_data .update (chart_data_detector )
353+ else :
354+ chart_data .update (chart_data_alert_rule )
306355 return charts .generate_chart (style , chart_data , size = size )
307356 except RuntimeError as exc :
308357 logger .error (
0 commit comments