MCPcopy Index your code
hub / github.com/coder/coder / insightsUserLatency

Method insightsUserLatency

coderd/insights.go:213–294  ·  view source on GitHub ↗

@Summary Get insights about user latency @ID get-insights-about-user-latency @Security CoderSessionToken @Produce json @Tags Insights @Param start_time query string true "Start time" format(date-time) @Param end_time query string true "End time" format(date-time) @Param template_ids query []string f

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

211// @Success 200 {object} codersdk.UserLatencyInsightsResponse
212// @Router /api/v2/insights/user-latency [get]
213func (api *API) insightsUserLatency(rw http.ResponseWriter, r *http.Request) {
214 ctx := r.Context()
215
216 p := httpapi.NewQueryParamParser().
217 RequiredNotEmpty("start_time").
218 RequiredNotEmpty("end_time")
219 vals := r.URL.Query()
220 var (
221 // The QueryParamParser does not preserve timezone, so we need
222 // to parse the time ourselves.
223 startTimeString = p.String(vals, "", "start_time")
224 endTimeString = p.String(vals, "", "end_time")
225 templateIDs = p.UUIDs(vals, []uuid.UUID{}, "template_ids")
226 )
227 p.ErrorExcessParams(vals)
228 if len(p.Errors) > 0 {
229 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
230 Message: "Query parameters have invalid values.",
231 Validations: p.Errors,
232 })
233 return
234 }
235
236 startTime, endTime, ok := parseInsightsStartAndEndTime(ctx, rw, time.Now(), startTimeString, endTimeString)
237 if !ok {
238 return
239 }
240
241 rows, err := api.Database.GetUserLatencyInsights(ctx, database.GetUserLatencyInsightsParams{
242 StartTime: startTime,
243 EndTime: endTime,
244 TemplateIDs: templateIDs,
245 })
246 if err != nil {
247 if httpapi.Is404Error(err) {
248 httpapi.ResourceNotFound(rw)
249 return
250 }
251 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
252 Message: "Internal error fetching user latency.",
253 Detail: err.Error(),
254 })
255 return
256 }
257
258 templateIDSet := make(map[uuid.UUID]struct{})
259 userLatencies := make([]codersdk.UserLatency, 0, len(rows))
260 for _, row := range rows {
261 for _, templateID := range row.TemplateIDs {
262 templateIDSet[templateID] = struct{}{}
263 }
264 userLatencies = append(userLatencies, codersdk.UserLatency{
265 TemplateIDs: row.TemplateIDs,
266 UserID: row.UserID,
267 Username: row.Username,
268 AvatarURL: row.AvatarURL,
269 LatencyMS: codersdk.ConnectionLatency{
270 P50: row.WorkspaceConnectionLatency50,

Callers

nothing calls this directly

Calls 13

NewQueryParamParserFunction · 0.92
WriteFunction · 0.92
Is404ErrorFunction · 0.92
ResourceNotFoundFunction · 0.92
AscendingFunction · 0.92
RequiredNotEmptyMethod · 0.80
UUIDsMethod · 0.80
ErrorExcessParamsMethod · 0.80
ContextMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected