Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
183c95e
chore(public): regenerate the client from the Public API in production
Topherhindman Nov 24, 2026
d6a8749
feat(lk): lk analytics logs prints a project's logs under --user-auth
Topherhindman Nov 24, 2026
b6f184d
feat(lk): lk analytics log-patterns prints a project's log patterns
Topherhindman Nov 24, 2026
897f480
feat(lk): lk analytics insights prints a project's agent insights
Topherhindman Nov 24, 2026
1a05d61
fix(lk): review fixes for the analytics project reads
Topherhindman Nov 24, 2026
3749e3b
chore(public): regenerate the client from the Public API in production
Topherhindman Dec 1, 2026
5acb25e
fix(lk): point logs and log-patterns at --end for records over 7 days…
Topherhindman Dec 1, 2026
0f49263
fix(public): recognize an unparsable log query by the phrase that nam…
Topherhindman Dec 1, 2026
fb5c94f
test(lk): script test for the signed-in analytics project reads
Topherhindman Dec 1, 2026
396a599
fix(lk): refuse a log-patterns --limit over 200, and say logs' order
Topherhindman Dec 1, 2026
58b2eb9
fix(lk): --since sends both ends of the range from one read of the clock
Topherhindman Dec 1, 2026
989e9cf
feat(lk): session list takes RFC 3339 times and sends its end under -…
Topherhindman Dec 1, 2026
6b9d9af
fix(lk): insights takes an --agent-name with a comma whole
Topherhindman Dec 1, 2026
bd6c6b9
fix(lk): session list takes a --tag with a comma whole
Topherhindman Dec 1, 2026
4de4679
fix(lk): check --limit before it narrows to the API's int32
Topherhindman Dec 1, 2026
a6b61c4
fix(lk): refuse an int flag that doesn't fit its int32 instead of wra…
Topherhindman Dec 1, 2026
d53e077
Revert "fix(lk): session list takes a --tag with a comma whole"
Topherhindman Dec 1, 2026
b2f48d8
Revert "fix(lk): insights takes an --agent-name with a comma whole"
Topherhindman Dec 1, 2026
bba4413
test(lk): a comma splits --status, --tag and --agent-id through the r…
Topherhindman Dec 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 109 additions & 51 deletions cmd/lk/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -189,6 +190,9 @@ var (
},
},
},
analyticsLogsCommand,
analyticsLogPatternsCommand,
analyticsInsightsCommand,
},
},
}
Expand Down Expand Up @@ -253,11 +257,11 @@ func analyticsSessionListFlags() []cli.Flag {
},
&cli.StringFlag{
Name: "start",
Usage: "List sessions started on or after `YYYY-MM-DD` (UTC)",
Usage: "List sessions started on or after `TIME`: a UTC date (YYYY-MM-DD), or with --user-auth also an RFC 3339 time, such as 2026-01-02T15:04:05Z",
},
&cli.StringFlag{
Name: "end",
Usage: "List sessions started before `YYYY-MM-DD` (UTC, exclusive); requires --start",
Usage: "List sessions started before `TIME` (exclusive), a date or time as for --start; requires --start",
},
// user-auth only: the Public API is cursor-paginated and has
// filters the CLI doesn't yet send to the API-key endpoint. Hidden like
Expand Down Expand Up @@ -613,27 +617,53 @@ func buildAnalyticsListQuery(cmd *cli.Command) (url.Values, error) {
// analyticsNow is the clock --since counts back from; tests replace it.
var analyticsNow = time.Now

// analyticsWindow reads a session listing's window from --since, or else from
// --start and --end, or else defaults it to the last defaultAnalyticsWindow
// rather than leaving it to the endpoint (the Public API searches only the
// last 24 hours). A window starting at now - d leaves end zero: both
// endpoints list up to now.
// analyticsWindow reads a session listing's window for the API-key endpoint
// from --since, or else from --start and --end, or else defaults it to the last
// defaultAnalyticsWindow; publicSessionWindow reads it for the Public API. A
// window starting at now - d leaves end zero: the endpoint lists up to now,
// and it reads an end as an exclusive date, so an end of now would leave out
// today's sessions.
func analyticsWindow(cmd *cli.Command) (time.Time, time.Time, error) {
if usesDefaultWindow(cmd) {
return analyticsNow().Add(-defaultAnalyticsWindow), time.Time{}, nil
}
since := cmd.String("since")
if since == "" {
if cmd.String("since") == "" {
return validateAnalyticsDateRange(cmd.String("start"), cmd.String("end"))
}
start, _, err := sinceWindow(cmd)
return start, time.Time{}, err
}

// publicSessionWindow reads a session listing's window for the Public API, as
// analyticsWindow does for the API-key endpoint, except that --start and --end
// also take an RFC 3339 time, and a window from --since or the default sends
// its end, now, from the same read of the clock. The API refuses a window
// longer than 60 days measured to its own now, so without the end --since 60d
// would always be a little too long.
func publicSessionWindow(cmd *cli.Command) (time.Time, time.Time, error) {
if usesDefaultWindow(cmd) {
now := analyticsNow()
return now.Add(-defaultAnalyticsWindow), now, nil
}
if cmd.String("since") == "" {
return analyticsRange(cmd.String("start"), cmd.String("end"), parseTimeFlag)
}
return sinceWindow(cmd)
}

// sinceWindow reads --since: the start that long before now, and now, from
// one read of the clock, so a read that sends both ends covers exactly the
// duration. --since can't be used with --start or --end.
func sinceWindow(cmd *cli.Command) (start, now time.Time, err error) {
if cmd.IsSet("start") || cmd.IsSet("end") {
return time.Time{}, time.Time{}, errors.New("--since can't be used with --start or --end")
}
d, err := parseLookback(since)
d, err := parseLookback(cmd.String("since"))
if err != nil {
return time.Time{}, time.Time{}, err
}
return analyticsNow().Add(-d), time.Time{}, nil
now = analyticsNow()
return now.Add(-d), now, nil
}

// parseLookback parses a --since duration: a whole number of days (14d) or
Expand Down Expand Up @@ -665,29 +695,42 @@ func parseLookback(s string) (time.Duration, error) {
return d, nil
}

// validateAnalyticsDateRange parses --start and --end. Both endpoints read them
// as the half-open window [start, end), so equal dates are an empty window. A
// lone --end is rejected: each endpoint fills in a different start (the API-key
// endpoint from now, the Public API from end), and neither is what it suggests.
// validateAnalyticsDateRange parses --start and --end for the API-key
// endpoint, which reads whole UTC days, so each must be a date (see
// analyticsRange).
func validateAnalyticsDateRange(startDate, endDate string) (time.Time, time.Time, error) {
var (
start time.Time
end time.Time
err error
)
return analyticsRange(startDate, endDate, parseAnalyticsDate)
}

if startDate != "" {
start, err = time.Parse("2006-01-02", startDate)
if err != nil {
return time.Time{}, time.Time{}, fmt.Errorf("invalid start date %q, expected YYYY-MM-DD", startDate)
}
// parseAnalyticsDate parses the --start or --end (name) value s for the
// API-key endpoint: a date (YYYY-MM-DD), its UTC midnight. The endpoint reads
// only dates, so a time, which --user-auth takes, is refused saying so. Empty
// is unset, the zero time.
func parseAnalyticsDate(name, s string) (time.Time, error) {
if s == "" {
return time.Time{}, nil
}
if t, err := time.Parse(time.DateOnly, s); err == nil {
return t, nil
}
if _, err := time.Parse(time.RFC3339, s); err == nil {
return time.Time{}, fmt.Errorf("a time in --%s is only supported with --user-auth; without it, use a date (YYYY-MM-DD)", name)
}
return time.Time{}, fmt.Errorf("invalid %s date %q, expected YYYY-MM-DD", name, s)
}

if endDate != "" {
end, err = time.Parse("2006-01-02", endDate)
if err != nil {
return time.Time{}, time.Time{}, fmt.Errorf("invalid end date %q, expected YYYY-MM-DD", endDate)
}
// analyticsRange parses --start and --end with parse. Both endpoints read them
// as the half-open window [start, end), so equal values are an empty window. A
// lone --end is rejected: each endpoint fills in a different start (the API-key
// endpoint from now, the Public API from end), and neither is what it suggests.
func analyticsRange(startValue, endValue string, parse func(name, s string) (time.Time, error)) (time.Time, time.Time, error) {
start, err := parse("start", startValue)
if err != nil {
return time.Time{}, time.Time{}, err
}
end, err := parse("end", endValue)
if err != nil {
return time.Time{}, time.Time{}, err
}

if !end.IsZero() && start.IsZero() {
Expand Down Expand Up @@ -874,21 +917,24 @@ func printDefaultWindowHint() {
}

// sessionListOptions reads the session list flags for the Public API. --start
// and --end mean the same as on the API-key endpoint: UTC dates bounding when a
// session started, end exclusive. --since starts the window that long before
// now, to the second. Unknown status and sort order names fail here,
// before the project lookup, like the date and limit checks.
// and --end bound when a session started, end exclusive, as on the API-key
// endpoint, and also take an RFC 3339 time. --since starts the window that
// long before now, to the second. Unknown status and sort order names fail
// here, before the project lookup, like the date and limit checks.
func sessionListOptions(cmd *cli.Command) (public.SessionListOptions, error) {
limit := cmd.Int("limit")
limit, err := int32Flag(cmd, "limit")
if err != nil {
return public.SessionListOptions{}, err
}
if limit <= 0 {
return public.SessionListOptions{}, errors.New("limit must be greater than 0")
}
start, end, err := analyticsWindow(cmd)
start, end, err := publicSessionWindow(cmd)
if err != nil {
return public.SessionListOptions{}, err
}
opts := public.SessionListOptions{
Limit: int32(limit),
Limit: limit,
Cursor: cmd.String("cursor"),
Start: start,
End: end,
Expand Down Expand Up @@ -1159,7 +1205,7 @@ func claimFreeName(path string) (string, error) {
func sessionRecordingError(err error, projectID, sessionID, recording string) error {
label := render.RecordingLabel(recording)
if dashboardURL, ok := public.ObservabilityDisabled(err); ok {
return observabilityDisabledError(sessionID, label, dashboardURL)
return observabilityDisabledError("session "+sessionID, label, dashboardURL)
}
if public.IsNotFound(err) {
// The API answers an unknown session, or a mistyped --project, the same
Expand Down Expand Up @@ -1189,25 +1235,30 @@ func recordingNotYetError(sessionID, recording string, status *oapi.LivekitPubli
return exitError{fmt.Errorf("session %s has no %s yet: %s", sessionID, render.RecordingLabel(recording), why), exitTempFail}
}

// observabilityDisabledError says a session has nothing to read because the
// project's user data recording is off, and where an admin turns it on.
func observabilityDisabledError(sessionID, what, dashboardURL string) error {
// observabilityDisabledError says a session or the project (subject) has
// nothing to read because the project's user data recording is off, and where
// an admin turns it on.
func observabilityDisabledError(subject, what, dashboardURL string) error {
where := "in the project's settings on the LiveKit Cloud dashboard"
if dashboardURL != "" {
where = "at " + dashboardURL
}
return fmt.Errorf("session %s has no %s: user data recording is off for this project, so nothing was recorded. "+
"A project admin can turn it on %s; it records new sessions only", sessionID, what, where)
return fmt.Errorf("%s has no %s: user data recording is off for this project, so nothing was recorded. "+
"A project admin can turn it on %s; it records new sessions only", subject, what, where)
}

// pageOptions reads a session read's --limit and --cursor flags. A limit
// given that isn't positive fails here, before the project lookup; none
// leaves the limit 0, the server's default.
// given that isn't positive, or that doesn't fit the API's 32-bit page size,
// fails here, before the project lookup; none leaves the limit 0, the
// server's default.
func pageOptions(cmd *cli.Command) (public.PageOptions, error) {
limit := cmd.Int("limit")
if limit < 0 || (limit == 0 && cmd.IsSet("limit")) {
return public.PageOptions{}, errors.New("limit must be greater than 0")
}
if limit > math.MaxInt32 {
return public.PageOptions{}, fmt.Errorf("limit must be at most %d", math.MaxInt32)
}
return public.PageOptions{Limit: int32(limit), Cursor: cmd.String("cursor")}, nil
}

Expand Down Expand Up @@ -1328,7 +1379,7 @@ func emptyTranscriptReason(ctx context.Context, client *public.Client, projectID
// is what the read requires.
func sessionReadError(err error, projectID, sessionID, what string, access sessionReadAccess) error {
if dashboardURL, ok := public.ObservabilityDisabled(err); ok {
return observabilityDisabledError(sessionID, what, dashboardURL)
return observabilityDisabledError("session "+sessionID, what, dashboardURL)
}
if public.IsNotFound(err) {
return fmt.Errorf("no session %s in project %s (%w)", sessionID, projectID, err)
Expand All @@ -1347,16 +1398,23 @@ const (
projectAdminAccess
)

// sessionAPIError annotates a Public API error from a session read like
// cloudAPIError, except a permission denial: cloudAPIError suggests API-key
// credentials, which these Public-API-only reads can't use, so it says what
// the read requires instead. what names what the read returns.
// sessionAPIError annotates a Public API error from a session read as
// readAPIError does. what names what the read returns.
func sessionAPIError(err error, what string, access sessionReadAccess) error {
return readAPIError(err, "a session's "+what, access)
}

// readAPIError annotates a Public API error from a read like cloudAPIError,
// except a permission denial: cloudAPIError suggests API-key credentials,
// which these Public-API-only reads can't use, so it says what the read
// requires instead. what names what the read returns, and whose, such as "a
// session's transcript".
func readAPIError(err error, what string, access sessionReadAccess) error {
if !public.IsPermissionDenied(err) {
return cloudAPIError(err)
}
if access == projectAdminAccess {
return fmt.Errorf("%w — reading a session's %s requires being a project admin", err, what)
return fmt.Errorf("%w — reading %s requires being a project admin", err, what)
}
return fmt.Errorf("%w — you don't have access to this project", err)
}
Expand Down
Loading
Loading