1+ from __future__ import annotations
2+
13from django .db import transaction
24from django .utils import timezone
35from drf_spectacular .utils import extend_schema
@@ -41,7 +43,15 @@ class MonitorCheckInDetailsEndpoint(Endpoint):
4143
4244 # TODO(dcramer): this code needs shared with other endpoints as its security focused
4345 # TODO(dcramer): this doesnt handle is_global roles
44- def convert_args (self , request : Request , monitor_id , checkin_id , * args , ** kwargs ):
46+ def convert_args (
47+ self ,
48+ request : Request ,
49+ monitor_id ,
50+ checkin_id ,
51+ organization_slug : str | None = None ,
52+ * args ,
53+ ** kwargs ,
54+ ):
4555 try :
4656 monitor = Monitor .objects .get (guid = monitor_id )
4757 except Monitor .DoesNotExist :
@@ -51,6 +61,10 @@ def convert_args(self, request: Request, monitor_id, checkin_id, *args, **kwargs
5161 if project .status != ProjectStatus .VISIBLE :
5262 raise ResourceDoesNotExist
5363
64+ if organization_slug :
65+ if project .organization .slug != organization_slug :
66+ return self .respond_invalid ()
67+
5468 if hasattr (request .auth , "project_id" ) and project .id != request .auth .project_id :
5569 return self .respond (status = 400 )
5670
@@ -102,12 +116,7 @@ def convert_args(self, request: Request, monitor_id, checkin_id, *args, **kwargs
102116 )
103117 def get (self , request : Request , project , monitor , checkin ) -> Response :
104118 """
105- Retrieve a check-in
106- ```````````````````
107-
108- :pparam string monitor_id: the id of the monitor.
109- :pparam string checkin_id: the id of the check-in.
110- :auth: required
119+ Retrieves details for a check-in.
111120
112121 You may use `latest` for the `checkin_id` parameter in order to retrieve
113122 the most recent (by creation date) check-in which is still mutable (not marked as finished).
@@ -128,26 +137,22 @@ def get(self, request: Request, project, monitor, checkin) -> Response:
128137 request = MonitorCheckInValidator ,
129138 responses = {
130139 200 : inline_sentry_response_serializer (
131- "MonitorCheckIn2 " , MonitorCheckInSerializerResponse
140+ "MonitorCheckIn " , MonitorCheckInSerializerResponse
132141 ),
133142 208 : RESPONSE_ALREADY_REPORTED ,
134143 400 : RESPONSE_BAD_REQUEST ,
135144 401 : RESPONSE_UNAUTHORIZED ,
136145 403 : RESPONSE_FORBIDDEN ,
137146 404 : RESPONSE_NOTFOUND ,
138147 },
139- examples = [
140- # OpenApiExample()
141- ],
142148 )
143149 def put (self , request : Request , project , monitor , checkin ) -> Response :
144150 """
145- Update a check-in
146- `````````````````
151+ Updates a check-in.
152+
153+ Once a check-in is finished (indicated via an `ok` or `error` status) it can no longer be changed.
147154
148- :pparam string monitor_id: the id of the monitor.
149- :pparam string checkin_id: the id of the check-in.
150- :auth: required
155+ If you simply wish to update that the task is still running, you can simply send an empty payload.
151156
152157 You may use `latest` for the `checkin_id` parameter in order to retrieve
153158 the most recent (by creation date) check-in which is still mutable (not marked as finished).
@@ -165,15 +170,16 @@ def put(self, request: Request, project, monitor, checkin) -> Response:
165170
166171 current_datetime = timezone .now ()
167172 params = {"date_updated" : current_datetime }
173+ if "status" in result :
174+ params ["status" ] = getattr (CheckInStatus , result ["status" ].upper ())
175+
168176 if "duration" in result :
169177 params ["duration" ] = result ["duration" ]
170- else :
178+ # if a duration is not defined and we're at a finish state, calculate one
179+ elif params .get ("status" , checkin .status ) in CheckInStatus .FINISHED_VALUES :
171180 duration = int ((current_datetime - checkin .date_added ).total_seconds () * 1000 )
172181 params ["duration" ] = duration
173182
174- if "status" in result :
175- params ["status" ] = getattr (CheckInStatus , result ["status" ].upper ())
176-
177183 with transaction .atomic ():
178184 checkin .update (** params )
179185 if checkin .status == CheckInStatus .ERROR :
0 commit comments