-
Notifications
You must be signed in to change notification settings - Fork 52
/
recorder.go
79 lines (65 loc) 路 1.52 KB
/
recorder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package recorder
import (
"github.com/bricks-cloud/bricksllm/internal/event"
"github.com/bricks-cloud/bricksllm/internal/key"
)
type Recorder struct {
s Store
c Cache
us Store
uc Cache
ce CostEstimator
es EventsStore
}
type EventsStore interface {
InsertEvent(e *event.Event) error
}
type Store interface {
IncrementCounter(keyId string, incr int64) error
}
type Cache interface {
IncrementCounter(keyId string, rateLimitUnit key.TimeUnit, incr int64) error
}
type CostEstimator interface {
EstimatePromptCost(model string, tks int) (float64, error)
EstimateCompletionCost(model string, tks int) (float64, error)
}
func NewRecorder(s, us Store, c, uc Cache, ce CostEstimator, es EventsStore) *Recorder {
return &Recorder{
s: s,
c: c,
us: us,
uc: uc,
ce: ce,
es: es,
}
}
func (r *Recorder) RecordUserSpend(userId string, micros int64, costLimitUnit key.TimeUnit) error {
err := r.us.IncrementCounter(userId, micros)
if err != nil {
return err
}
if len(costLimitUnit) != 0 {
err = r.uc.IncrementCounter(userId, costLimitUnit, int64(micros))
if err != nil {
return err
}
}
return nil
}
func (r *Recorder) RecordKeySpend(keyId string, micros int64, costLimitUnit key.TimeUnit) error {
err := r.s.IncrementCounter(keyId, micros)
if err != nil {
return err
}
if len(costLimitUnit) != 0 {
err = r.c.IncrementCounter(keyId, costLimitUnit, int64(micros))
if err != nil {
return err
}
}
return nil
}
func (r *Recorder) RecordEvent(e *event.Event) error {
return r.es.InsertEvent(e)
}