@Summary Get insights about user activity @ID get-insights-about-user-activity @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
(rw http.ResponseWriter, r *http.Request)
| 108 | // @Success 200 {object} codersdk.UserActivityInsightsResponse |
| 109 | // @Router /api/v2/insights/user-activity [get] |
| 110 | func (api *API) insightsUserActivity(rw http.ResponseWriter, r *http.Request) { |
| 111 | ctx := r.Context() |
| 112 | |
| 113 | p := httpapi.NewQueryParamParser(). |
| 114 | RequiredNotEmpty("start_time"). |
| 115 | RequiredNotEmpty("end_time") |
| 116 | vals := r.URL.Query() |
| 117 | var ( |
| 118 | // The QueryParamParser does not preserve timezone, so we need |
| 119 | // to parse the time ourselves. |
| 120 | startTimeString = p.String(vals, "", "start_time") |
| 121 | endTimeString = p.String(vals, "", "end_time") |
| 122 | templateIDs = p.UUIDs(vals, []uuid.UUID{}, "template_ids") |
| 123 | ) |
| 124 | p.ErrorExcessParams(vals) |
| 125 | if len(p.Errors) > 0 { |
| 126 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 127 | Message: "Query parameters have invalid values.", |
| 128 | Validations: p.Errors, |
| 129 | }) |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | startTime, endTime, ok := parseInsightsStartAndEndTime(ctx, rw, time.Now(), startTimeString, endTimeString) |
| 134 | if !ok { |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | rows, err := api.Database.GetUserActivityInsights(ctx, database.GetUserActivityInsightsParams{ |
| 139 | StartTime: startTime, |
| 140 | EndTime: endTime, |
| 141 | TemplateIDs: templateIDs, |
| 142 | }) |
| 143 | if err != nil { |
| 144 | // No data is not an error. |
| 145 | if xerrors.Is(err, sql.ErrNoRows) { |
| 146 | httpapi.Write(ctx, rw, http.StatusOK, codersdk.UserActivityInsightsResponse{ |
| 147 | Report: codersdk.UserActivityInsightsReport{ |
| 148 | StartTime: startTime, |
| 149 | EndTime: endTime, |
| 150 | TemplateIDs: []uuid.UUID{}, |
| 151 | Users: []codersdk.UserActivity{}, |
| 152 | }, |
| 153 | }) |
| 154 | return |
| 155 | } |
| 156 | // Check authorization. |
| 157 | if httpapi.Is404Error(err) { |
| 158 | httpapi.ResourceNotFound(rw) |
| 159 | return |
| 160 | } |
| 161 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 162 | Message: "Internal error fetching user activity.", |
| 163 | Detail: err.Error(), |
| 164 | }) |
| 165 | return |
| 166 | } |
| 167 |
nothing calls this directly
no test coverage detected