@@ -15,21 +15,41 @@ import (
15
15
"github.com/go-echarts/go-echarts/v2/opts"
16
16
)
17
17
18
+ type GraphHandler struct {
19
+ a * ApiHandler
20
+ cache []Cache
21
+ }
22
+
23
+ // if basecount, filter and DB Rows missmatch, Cache will be updated
24
+ type Cache struct {
25
+ ID string
26
+ BaseCount int //
27
+ Filter string
28
+ Data []db.RequestSummary
29
+ }
30
+
31
+ func NewGraphHandler (a * ApiHandler ) * GraphHandler {
32
+ var cache []Cache
33
+ return & GraphHandler {a , cache }
34
+ }
35
+
36
+ // Generate Token Graphs for API Key and Admin overview and allow to filter by timeframes
37
+
18
38
type Graph struct {
19
39
key string
20
40
kind string // can be "user" or "apiKey"
21
41
w http.ResponseWriter
22
42
r * http.Request
23
43
}
24
44
25
- func (a * ApiHandler ) GetTableGraph (w http.ResponseWriter , r * http.Request ) {
26
- ok := a .auth .ValidateSessionToken (w , r )
45
+ func (g * GraphHandler ) GetTableGraph (w http.ResponseWriter , r * http.Request ) {
46
+ ok := g . a .auth .ValidateSessionToken (w , r )
27
47
if ! ok {
28
48
http .Error (w , "Not Authorized" , http .StatusForbidden )
29
49
return
30
50
}
31
51
key := strings .TrimPrefix (r .URL .Path , "/api2/table/graph/get/" )
32
- a .RenderGraph (& Graph {
52
+ g .RenderGraph (& Graph {
33
53
key : key ,
34
54
kind : "apiKey" ,
35
55
w : w ,
@@ -38,16 +58,16 @@ func (a *ApiHandler) GetTableGraph(w http.ResponseWriter, r *http.Request) {
38
58
39
59
}
40
60
41
- func (a * ApiHandler ) GetAdminTableGraph (w http.ResponseWriter , r * http.Request ) {
61
+ func (g * GraphHandler ) GetAdminTableGraph (w http.ResponseWriter , r * http.Request ) {
42
62
43
- ok , err := a .auth .ValidateAdminSession (w , r )
63
+ ok , err := g . a .auth .ValidateAdminSession (w , r )
44
64
if err != nil || ! ok {
45
65
http .Error (w , "Not Authorized" , http .StatusForbidden )
46
66
return
47
67
}
48
68
49
69
key := strings .TrimPrefix (r .URL .Path , "/api2/admin/table/graph/get/" )
50
- a .RenderGraph (& Graph {
70
+ g .RenderGraph (& Graph {
51
71
key : key ,
52
72
kind : "user" ,
53
73
w : w ,
@@ -56,10 +76,10 @@ func (a *ApiHandler) GetAdminTableGraph(w http.ResponseWriter, r *http.Request)
56
76
57
77
}
58
78
59
- func (a * ApiHandler ) RenderGraph (g * Graph ) {
79
+ func (g * GraphHandler ) RenderGraph (gr * Graph ) {
60
80
61
81
selectedfilter := "24h" // default Value
62
- header := g .r .Header .Get ("HX-Current-URL" )
82
+ header := gr .r .Header .Get ("HX-Current-URL" )
63
83
currentURL , err := url .Parse (header )
64
84
if err != nil {
65
85
log .Println ("Error Parsing HX-Current-URL for Graph Table" )
@@ -90,13 +110,6 @@ func (a *ApiHandler) RenderGraph(g *Graph) {
90
110
filter = "This Year"
91
111
}
92
112
93
- data , err := a .db .LookupApiKeyUserStats (g .key , g .kind , filter )
94
- if err != nil {
95
- log .Println (err )
96
- http .Error (g .w , "Could not get Data from DB for User " + string (g .key ), 500 )
97
- return
98
- }
99
-
100
113
// create a new line instance
101
114
line := charts .NewLine ()
102
115
// set some global options like Title/Legend/ToolTip or anything else
@@ -108,9 +121,10 @@ func (a *ApiHandler) RenderGraph(g *Graph) {
108
121
charts .WithYAxisOpts (opts.YAxis {SplitNumber : 2 }),
109
122
// charts.WithVisualMapOpts(opts.VisualMap{Show: opts.Bool(false)})
110
123
)
124
+ data := g .GetTableGraphData (gr , filter )
111
125
112
126
// Put data into instance
113
- td , err := a . GetAdminTableGraphData ( g .w , data , filter )
127
+ td , err := g . SetTableGraphData ( gr .w , data , filter )
114
128
if err != nil {
115
129
return
116
130
}
@@ -141,7 +155,7 @@ func (a *ApiHandler) RenderGraph(g *Graph) {
141
155
Filter : filter ,
142
156
}
143
157
// var buf bytes.Buffer
144
- if err := t .Execute (g .w , snippetData ); err != nil {
158
+ if err := t .Execute (gr .w , snippetData ); err != nil {
145
159
log .Println ("Error Templating Chart" , err )
146
160
return
147
161
}
@@ -157,7 +171,46 @@ type TableData struct {
157
171
totalCount int
158
172
}
159
173
160
- func (a * ApiHandler ) GetAdminTableGraphData (w http.ResponseWriter , d []db.RequestSummary , filter string ) (* TableData , error ) {
174
+ func (g * GraphHandler ) GetTableGraphData (gr * Graph , filter string ) []db.RequestSummary {
175
+ for _ , row := range g .cache {
176
+ if row .ID == gr .key && row .Filter == filter {
177
+ count , err := g .a .db .LookupApiKeyUserStatsRows (gr .key , gr .kind )
178
+ if err == nil && count == row .BaseCount {
179
+ return row .Data
180
+ }
181
+ if err != nil {
182
+ log .Println (err )
183
+ }
184
+ row .Data = g .LookupTableGraphData (gr , filter )
185
+ row .BaseCount = count
186
+ }
187
+ }
188
+ rowCount , err := g .a .db .LookupApiKeyUserStatsRows (gr .key , gr .kind )
189
+ if err != nil {
190
+ log .Println ("could not count rows for caching: " , err )
191
+ }
192
+
193
+ row := Cache {
194
+ Data : g .LookupTableGraphData (gr , filter ),
195
+ BaseCount : rowCount ,
196
+ Filter : filter ,
197
+ ID : gr .key ,
198
+ }
199
+ g .cache = append (g .cache , row )
200
+ return row .Data
201
+ }
202
+
203
+ func (g * GraphHandler ) LookupTableGraphData (gr * Graph , filter string ) []db.RequestSummary {
204
+ data , err := g .a .db .LookupApiKeyUserStats (gr .key , gr .kind , filter )
205
+ if err != nil && data != nil {
206
+ log .Println (err )
207
+ http .Error (gr .w , "Could not get Data from DB for User " + string (gr .key ), 500 )
208
+ return nil
209
+ }
210
+ return data
211
+ }
212
+
213
+ func (g * GraphHandler ) SetTableGraphData (w http.ResponseWriter , d []db.RequestSummary , filter string ) (* TableData , error ) {
161
214
162
215
td := & TableData {
163
216
data : make ([]opts.LineData , 0 ),
@@ -188,7 +241,7 @@ func (a *ApiHandler) GetAdminTableGraphData(w http.ResponseWriter, d []db.Reques
188
241
totalTokens = item .TokenCountComplete + item .TokenCountPrompt
189
242
td .data = append (td .data , opts.LineData {Value : totalTokens })
190
243
td .totalCount = td .totalCount + totalTokens
191
- loc , err := time .LoadLocation (a .timeZone )
244
+ loc , err := time .LoadLocation (g . a .timeZone )
192
245
if err != nil {
193
246
log .Println ("Error Displaying Timezone, maybe the TIMEZONE env is wrongly set" )
194
247
td .timeAxis = append (td .timeAxis , item .RequestTime .Format (format ))
0 commit comments