Skip to content

Commit 3c9faab

Browse files
authored
add duration seconds reporting (#1528)
* add duration seconds reporting * Create tidy-goats-marry.md * tidy * typo
1 parent 0f86fd3 commit 3c9faab

15 files changed

Lines changed: 142 additions & 34 deletions

.changeset/tidy-goats-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@livekit/protocol": patch
3+
---
4+
5+
add duration seconds reporting

observability/agentsobs/gen_reporter.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

observability/agentsobs/gen_reporter_noop.go

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

observability/agentsv2obs/gen_reporter.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

observability/agentsv2obs/gen_reporter_noop.go

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

observability/corecallobs/gen_reporter.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

observability/corecallobs/gen_reporter_noop.go

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

observability/roomobs/gen_reporter.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

observability/roomobs/gen_reporter_noop.go

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

observability/sessiontimer.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
11
package observability
22

3-
import "time"
3+
import (
4+
"time"
5+
6+
"github.com/livekit/protocol/utils/options"
7+
)
48

59
type SessionTimer struct {
610
lastMilli int64
11+
lastSec int64
712
lastMin int64
13+
minSecs int64
14+
minMins int64
15+
}
16+
17+
type SessionTimerOption func(*SessionTimer)
18+
19+
// WithMinSeconds ensures the first Advance that produces a non-zero secs
20+
// return reports at least n seconds. Subsequent advances behave normally.
21+
func WithMinSeconds(n int64) SessionTimerOption {
22+
return func(h *SessionTimer) { h.minSecs = n }
823
}
924

10-
func NewSessionTimer(startTime time.Time) *SessionTimer {
25+
// WithMinMinutes ensures the first Advance that produces a non-zero mins
26+
// return reports at least n minutes. Subsequent advances behave normally.
27+
func WithMinMinutes(n int64) SessionTimerOption {
28+
return func(h *SessionTimer) { h.minMins = n }
29+
}
30+
31+
func NewSessionTimer(startTime time.Time, opts ...SessionTimerOption) *SessionTimer {
1132
ts := startTime.UnixMilli()
12-
return &SessionTimer{ts, ts}
33+
return options.Apply(&SessionTimer{lastMilli: ts, lastSec: ts, lastMin: ts}, opts)
1334
}
1435

15-
func (h *SessionTimer) Advance(now time.Time) (millis, mins int64) {
36+
func (h *SessionTimer) Advance(now time.Time) (millis, secs, mins int64) {
1637
ts := now.UnixMilli()
1738
if ts > h.lastMilli {
1839
millis = ts - h.lastMilli
1940
h.lastMilli = ts
2041
}
42+
if ts > h.lastSec {
43+
secs = max((ts-h.lastSec+999)/1000, h.minSecs)
44+
h.minSecs = 0
45+
h.lastSec += secs * 1000
46+
}
2147
if ts > h.lastMin {
22-
n := (ts - h.lastMin + 59999) / 60000
23-
mins += n
24-
h.lastMin += n * 60000
48+
mins = max((ts-h.lastMin+59999)/60000, h.minMins)
49+
h.minMins = 0
50+
h.lastMin += mins * 60000
2551
}
2652
return
2753
}

0 commit comments

Comments
 (0)