Skip to content

Commit c480f0f

Browse files
committed
codersdk
1 parent fa85853 commit c480f0f

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

codersdk/insights.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ type UserLatency struct {
6262
LatencyMS ConnectionLatency `json:"latency_ms"`
6363
}
6464

65+
// UserActivityInsightsResponse is the response from the user activity insights
66+
// endpoint.
67+
type UserActivityInsightsResponse struct {
68+
Report UserActivityInsightsReport `json:"report"`
69+
}
70+
71+
// UserActivityInsightsReport is the report from the user activity insights
72+
// endpoint.
73+
type UserActivityInsightsReport struct {
74+
StartTime time.Time `json:"start_time" format:"date-time"`
75+
EndTime time.Time `json:"end_time" format:"date-time"`
76+
TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"`
77+
Users []UserActivity `json:"users"`
78+
}
79+
80+
// UserActivity shows the session time for a user.
81+
type UserActivity struct {
82+
TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"`
83+
UserID uuid.UUID `json:"user_id" format:"uuid"`
84+
Username string `json:"username"`
85+
AvatarURL string `json:"avatar_url" format:"uri"`
86+
Seconds int64 `json:"seconds" example:"80500"`
87+
}
88+
6589
// ConnectionLatency shows the latency for a connection.
6690
type ConnectionLatency struct {
6791
P50 float64 `json:"p50" example:"31.312"`
@@ -100,6 +124,38 @@ func (c *Client) UserLatencyInsights(ctx context.Context, req UserLatencyInsight
100124
return result, json.NewDecoder(resp.Body).Decode(&result)
101125
}
102126

127+
type UserActivityInsightsRequest struct {
128+
StartTime time.Time `json:"start_time" format:"date-time"`
129+
EndTime time.Time `json:"end_time" format:"date-time"`
130+
TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"`
131+
}
132+
133+
func (c *Client) UserActivityInsights(ctx context.Context, req UserActivityInsightsRequest) (UserActivityInsightsResponse, error) {
134+
qp := url.Values{}
135+
qp.Add("start_time", req.StartTime.Format(insightsTimeLayout))
136+
qp.Add("end_time", req.EndTime.Format(insightsTimeLayout))
137+
if len(req.TemplateIDs) > 0 {
138+
var templateIDs []string
139+
for _, id := range req.TemplateIDs {
140+
templateIDs = append(templateIDs, id.String())
141+
}
142+
qp.Add("template_ids", strings.Join(templateIDs, ","))
143+
}
144+
145+
reqURL := fmt.Sprintf("/api/v2/insights/user-activity?%s", qp.Encode())
146+
resp, err := c.Request(ctx, http.MethodGet, reqURL, nil)
147+
if err != nil {
148+
return UserActivityInsightsResponse{}, xerrors.Errorf("make request: %w", err)
149+
}
150+
defer resp.Body.Close()
151+
152+
if resp.StatusCode != http.StatusOK {
153+
return UserActivityInsightsResponse{}, ReadBodyAsError(resp)
154+
}
155+
var result UserActivityInsightsResponse
156+
return result, json.NewDecoder(resp.Body).Decode(&result)
157+
}
158+
103159
// TemplateInsightsResponse is the response from the template insights endpoint.
104160
type TemplateInsightsResponse struct {
105161
Report TemplateInsightsReport `json:"report"`

site/src/api/typesGenerated.ts

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)