-
Notifications
You must be signed in to change notification settings - Fork 64
/
reporting.go
87 lines (71 loc) · 2.23 KB
/
reporting.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
80
81
82
83
84
85
86
87
package manager
import (
internal_errors "github.com/bricks-cloud/bricksllm/internal/errors"
"github.com/bricks-cloud/bricksllm/internal/event"
"github.com/bricks-cloud/bricksllm/internal/key"
)
type costStorage interface {
GetCounter(keyId string) (int64, error)
}
type keyStorage interface {
GetKey(keyId string) (*key.ResponseKey, error)
}
type eventStorage interface {
GetEvents(userId, customId string, keyIds []string, start, end int64) ([]*event.Event, error)
GetEventDataPoints(start, end, increment int64, tags, keyIds, customIds, userIds []string, filters []string) ([]*event.DataPoint, error)
GetLatencyPercentiles(start, end int64, tags, keyIds []string) ([]float64, error)
}
type ReportingManager struct {
es eventStorage
cs costStorage
ks keyStorage
}
func NewReportingManager(cs costStorage, ks keyStorage, es eventStorage) *ReportingManager {
return &ReportingManager{
cs: cs,
ks: ks,
es: es,
}
}
func (rm *ReportingManager) GetEventReporting(e *event.ReportingRequest) (*event.ReportingResponse, error) {
dataPoints, err := rm.es.GetEventDataPoints(e.Start, e.End, e.Increment, e.Tags, e.KeyIds, e.CustomIds, e.UserIds, e.Filters)
if err != nil {
return nil, err
}
percentiles, err := rm.es.GetLatencyPercentiles(e.Start, e.End, e.Tags, e.KeyIds)
if err != nil {
return nil, err
}
if len(percentiles) == 0 {
return nil, internal_errors.NewNotFoundError("latency percentiles are not found")
}
return &event.ReportingResponse{
DataPoints: dataPoints,
LatencyInMsMedian: percentiles[0],
LatencyInMs99th: percentiles[1],
}, nil
}
func (rm *ReportingManager) GetKeyReporting(keyId string) (*key.KeyReporting, error) {
k, err := rm.ks.GetKey(keyId)
if err != nil {
return nil, err
}
if k == nil {
return nil, internal_errors.NewNotFoundError("api key is not found")
}
micros, err := rm.cs.GetCounter(keyId)
if err != nil {
return nil, err
}
return &key.KeyReporting{
Id: keyId,
CostInMicroDollars: micros,
}, err
}
func (rm *ReportingManager) GetEvents(userId, customId string, keyIds []string, start, end int64) ([]*event.Event, error) {
events, err := rm.es.GetEvents(userId, customId, keyIds, start, end)
if err != nil {
return nil, err
}
return events, nil
}