(rw http.ResponseWriter, r *http.Request, templateIDs []uuid.UUID)
| 44 | } |
| 45 | |
| 46 | func (api *API) returnDAUsInternal(rw http.ResponseWriter, r *http.Request, templateIDs []uuid.UUID) { |
| 47 | ctx := r.Context() |
| 48 | |
| 49 | p := httpapi.NewQueryParamParser() |
| 50 | vals := r.URL.Query() |
| 51 | tzOffset := p.Int(vals, 0, "tz_offset") |
| 52 | p.ErrorExcessParams(vals) |
| 53 | if len(p.Errors) > 0 { |
| 54 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 55 | Message: "Query parameters have invalid values.", |
| 56 | Validations: p.Errors, |
| 57 | }) |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | loc := time.FixedZone("", tzOffset*3600) |
| 62 | // If the time is 14:01 or 14:31, we still want to include all the |
| 63 | // data between 14:00 and 15:00. Our rollups buckets are 30 minutes |
| 64 | // so this works nicely. It works just as well for 23:59 as well. |
| 65 | nextHourInLoc := time.Now().In(loc).Truncate(time.Hour).Add(time.Hour) |
| 66 | // Always return 60 days of data (2 months). |
| 67 | sixtyDaysAgo := nextHourInLoc.In(loc).Truncate(24*time.Hour).AddDate(0, 0, -60) |
| 68 | |
| 69 | rows, err := api.Database.GetTemplateInsightsByInterval(ctx, database.GetTemplateInsightsByIntervalParams{ |
| 70 | StartTime: sixtyDaysAgo, |
| 71 | EndTime: nextHourInLoc, |
| 72 | IntervalDays: 1, |
| 73 | TemplateIDs: templateIDs, |
| 74 | }) |
| 75 | if err != nil { |
| 76 | if httpapi.Is404Error(err) { |
| 77 | httpapi.ResourceNotFound(rw) |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 82 | Message: "Internal error fetching DAUs.", |
| 83 | Detail: err.Error(), |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | resp := codersdk.DAUsResponse{ |
| 88 | TZHourOffset: tzOffset, |
| 89 | Entries: make([]codersdk.DAUEntry, 0, len(rows)), |
| 90 | } |
| 91 | for _, row := range rows { |
| 92 | resp.Entries = append(resp.Entries, codersdk.DAUEntry{ |
| 93 | Date: row.StartTime.In(loc).Format(time.DateOnly), |
| 94 | Amount: int(row.ActiveUsers), |
| 95 | }) |
| 96 | } |
| 97 | httpapi.Write(ctx, rw, http.StatusOK, resp) |
| 98 | } |
| 99 | |
| 100 | // @Summary Get insights about user activity |
| 101 | // @ID get-insights-about-user-activity |
no test coverage detected