1
+ from __future__ import annotations
2
+
1
3
from django .db import transaction
2
4
from django .utils import timezone
3
5
from drf_spectacular .utils import extend_schema
@@ -41,7 +43,15 @@ class MonitorCheckInDetailsEndpoint(Endpoint):
41
43
42
44
# TODO(dcramer): this code needs shared with other endpoints as its security focused
43
45
# 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
+ ):
45
55
try :
46
56
monitor = Monitor .objects .get (guid = monitor_id )
47
57
except Monitor .DoesNotExist :
@@ -51,6 +61,10 @@ def convert_args(self, request: Request, monitor_id, checkin_id, *args, **kwargs
51
61
if project .status != ProjectStatus .VISIBLE :
52
62
raise ResourceDoesNotExist
53
63
64
+ if organization_slug :
65
+ if project .organization .slug != organization_slug :
66
+ return self .respond_invalid ()
67
+
54
68
if hasattr (request .auth , "project_id" ) and project .id != request .auth .project_id :
55
69
return self .respond (status = 400 )
56
70
@@ -102,12 +116,7 @@ def convert_args(self, request: Request, monitor_id, checkin_id, *args, **kwargs
102
116
)
103
117
def get (self , request : Request , project , monitor , checkin ) -> Response :
104
118
"""
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.
111
120
112
121
You may use `latest` for the `checkin_id` parameter in order to retrieve
113
122
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:
128
137
request = MonitorCheckInValidator ,
129
138
responses = {
130
139
200 : inline_sentry_response_serializer (
131
- "MonitorCheckIn2 " , MonitorCheckInSerializerResponse
140
+ "MonitorCheckIn " , MonitorCheckInSerializerResponse
132
141
),
133
142
208 : RESPONSE_ALREADY_REPORTED ,
134
143
400 : RESPONSE_BAD_REQUEST ,
135
144
401 : RESPONSE_UNAUTHORIZED ,
136
145
403 : RESPONSE_FORBIDDEN ,
137
146
404 : RESPONSE_NOTFOUND ,
138
147
},
139
- examples = [
140
- # OpenApiExample()
141
- ],
142
148
)
143
149
def put (self , request : Request , project , monitor , checkin ) -> Response :
144
150
"""
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.
147
154
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.
151
156
152
157
You may use `latest` for the `checkin_id` parameter in order to retrieve
153
158
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:
165
170
166
171
current_datetime = timezone .now ()
167
172
params = {"date_updated" : current_datetime }
173
+ if "status" in result :
174
+ params ["status" ] = getattr (CheckInStatus , result ["status" ].upper ())
175
+
168
176
if "duration" in result :
169
177
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 :
171
180
duration = int ((current_datetime - checkin .date_added ).total_seconds () * 1000 )
172
181
params ["duration" ] = duration
173
182
174
- if "status" in result :
175
- params ["status" ] = getattr (CheckInStatus , result ["status" ].upper ())
176
-
177
183
with transaction .atomic ():
178
184
checkin .update (** params )
179
185
if checkin .status == CheckInStatus .ERROR :
0 commit comments