Skip to content

Commit 4bc8c66

Browse files
authored
Merge pull request #24 from bat-bs/reporting
feat: add last month, this month, last year, this year filter and totalCount for Table
2 parents 32b7613 + 597576e commit 4bc8c66

File tree

5 files changed

+123
-30
lines changed

5 files changed

+123
-30
lines changed

api/graph.go

+30-10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ func (a *ApiHandler) RenderGraph(g *Graph) {
8080
filter = "7 days"
8181
case "30d":
8282
filter = "30 days"
83+
case "last-month":
84+
filter = "Last Month"
85+
case "this-month":
86+
filter = "This Month"
87+
case "last-year":
88+
filter = "Last Year"
89+
case "this-year":
90+
filter = "This Year"
8391
}
8492

8593
data, err := a.db.LookupApiKeyUserStats(g.key, g.kind, filter)
@@ -112,21 +120,25 @@ func (a *ApiHandler) RenderGraph(g *Graph) {
112120
// Where the magic happens
113121
chartSnippet := line.RenderSnippet()
114122

115-
tmpl := "{{.Element}} {{.Script}}"
123+
tmpl := "{{.Element}} <div class=\"content-center -ml-4 w-96 text-center text-xs grid\" ><i>{{.Filter}}: {{.TotalCount}}</i> </div> {{.Script}}"
116124
t := template.New("snippet")
117125
t, err = t.Parse(tmpl)
118126
if err != nil {
119127
log.Println("error templating", err)
120128

121129
}
122130
snippetData := struct {
123-
Element template.HTML
124-
Script template.HTML
125-
Option template.HTML
131+
Element template.HTML
132+
Script template.HTML
133+
Option template.HTML
134+
TotalCount int
135+
Filter string
126136
}{
127-
Element: template.HTML(chartSnippet.Element),
128-
Script: template.HTML(chartSnippet.Script),
129-
Option: template.HTML(chartSnippet.Option),
137+
Element: template.HTML(chartSnippet.Element),
138+
Script: template.HTML(chartSnippet.Script),
139+
Option: template.HTML(chartSnippet.Option),
140+
TotalCount: td.totalCount,
141+
Filter: filter,
130142
}
131143
// var buf bytes.Buffer
132144
if err := t.Execute(g.w, snippetData); err != nil {
@@ -140,8 +152,9 @@ func (a *ApiHandler) RenderGraph(g *Graph) {
140152
}
141153

142154
type TableData struct {
143-
data []opts.LineData
144-
timeAxis []string
155+
data []opts.LineData
156+
timeAxis []string
157+
totalCount int
145158
}
146159

147160
func (a *ApiHandler) GetAdminTableGraphData(w http.ResponseWriter, d []db.RequestSummary, filter string) (*TableData, error) {
@@ -152,22 +165,29 @@ func (a *ApiHandler) GetAdminTableGraphData(w http.ResponseWriter, d []db.Reques
152165
}
153166
var totalTokens int
154167
format := "15:04"
168+
155169
switch filter {
156170
case "24 Hours":
157171
format = "15:04"
172+
158173
case "7 days":
159174
format = "Mon"
160-
case "30 days":
175+
case "Last Month", "This Month", "30 days":
161176
format = "02"
177+
case "Last Year", "This Year":
178+
format = "Jan"
162179
}
180+
163181
if len(d) < 1 {
164182
http.Error(w, "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;No Data", 200)
165183
err := errors.New("data for Key is Empty")
166184
return nil, err
167185
}
186+
168187
for _, item := range d {
169188
totalTokens = item.TokenCountComplete + item.TokenCountPrompt
170189
td.data = append(td.data, opts.LineData{Value: totalTokens})
190+
td.totalCount = td.totalCount + totalTokens
171191
loc, err := time.LoadLocation(a.timeZone)
172192
if err != nil {
173193
log.Println("Error Displaying Timezone, maybe the TIMEZONE env is wrongly set")

cmd/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func main() {
2929
mux := http.NewServeMux()
3030
a := auth.Init(mux, db)
3131
//
32-
proxy.Init(mux, db) // Start AI Proxy
33-
go web.Init(mux, a) // Start Web UI
34-
go api.ApiInit(mux, a, db) // Start Backend API
32+
proxy.Init(mux, db) // Start AI Proxy
33+
web.Init(mux, a) // Start Web UI
34+
api.ApiInit(mux, a, db) // Start Backend API
3535

3636
log.Printf("Serving on http://localhost:%d", 8082)
3737
log.Fatal(http.ListenAndServe(":8082", mux))

db/database.go

+37-9
Original file line numberDiff line numberDiff line change
@@ -205,33 +205,61 @@ type RequestSummary struct {
205205
}
206206

207207
func (d *Database) LookupApiKeyUserStats(uid string, kind string, filter string) ([]RequestSummary, error) {
208-
dateTrunc := "day"
209-
if filter == "24 Hours" {
208+
var dateTrunc string
209+
210+
// build sql condition based on filter
211+
var condition string
212+
switch filter {
213+
case "24 Hours":
214+
condition = "r.request_time >= NOW() - INTERVAL '1 day'"
210215
dateTrunc = "hour"
216+
case "30 days", "7 days":
217+
condition = "r.request_time >= NOW() - INTERVAL '1 month'"
218+
dateTrunc = "day"
219+
case "This Month":
220+
dateTrunc = "day"
221+
condition = `
222+
r.request_time >= date_trunc('month', current_timestamp)
223+
AND r.request_time < date_trunc('month', current_timestamp) + interval '1 month'`
224+
case "Last Month":
225+
dateTrunc = "day"
226+
condition = `
227+
r.request_time >= date_trunc('month', current_timestamp) - interval '1 month'
228+
AND r.request_time < date_trunc('month', current_timestamp)`
229+
case "This Year":
230+
dateTrunc = "month"
231+
condition = `
232+
r.request_time >= date_trunc('year', current_timestamp)
233+
AND r.request_time < date_trunc('year', current_timestamp) + interval '1 year'`
234+
case "Last Year":
235+
dateTrunc = "month"
236+
condition = `
237+
r.request_time >= date_trunc('year', current_timestamp) - interval '1 year'
238+
AND r.request_time < date_trunc('year', current_timestamp)`
211239
}
212240

213241
// handle "user" view for admintable and "apiKey" view for usertable
214242
if kind == "user" {
215243
kind = "u.id"
216-
} else if kind == "apiKey" {
244+
} else {
217245
kind = "a.UUID"
218246
}
247+
219248
query := fmt.Sprintf(`
220249
SELECT
221250
%[1]s,
222251
SUM(r.token_count_prompt),
223252
SUM(r.token_count_complete),
224-
date_trunc('%[2]s', r.request_time) AS request_hour
253+
date_trunc('%[2]s', r.request_time) AS rq_time
225254
FROM requests r
226255
INNER JOIN apikeys a ON a.UUID = r.api_key_id
227256
INNER JOIN users u on a.Owner = u.id
228257
WHERE
229258
%[1]s = $1
230-
AND r.request_time >= NOW() - INTERVAL '%[3]s'
231-
GROUP BY %[1]s, request_hour
232-
ORDER BY request_hour;`,
233-
kind, dateTrunc, filter)
234-
259+
AND %[3]s
260+
GROUP BY %[1]s, rq_time
261+
ORDER BY rq_time;`,
262+
kind, dateTrunc, condition)
235263
rows, err := d.db.Query(query, uid)
236264
if err != nil {
237265
return nil, err

templates/adminTable.html.templ

+26-4
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,44 @@
2323
}
2424
</script>
2525

26-
<div class="flex max-w-screen justify-end mb-3">
26+
<div class="flex max-w-screen justify-end mb-2">
27+
<div class="flex bg-slate-800 justify-end rounded">
2728
<a href="?filter=24h">
28-
<button id="btn-24h" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-2 px-4 rounded-l">
29+
<button id="btn-24h" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 rounded-l">
2930
24 Hours
3031
</button>
3132
</a>
3233
<a href="?filter=7d">
33-
<button id="btn-7d" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-2 px-4 ">
34+
<button id="btn-7d" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 ">
3435
7 Days
3536
</button>
3637
</a>
3738
<a href="?filter=30d">
38-
<button id="btn-30d" class="justify-end bg-blue-300 hover:bg-blue-500 text-gray-800 font-bold py-2 px-4 rounded-r">
39+
<button id="btn-30d" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 mr-2 ">
3940
30 Days
4041
</button>
4142
</a>
43+
<a href="?filter=this-month">
44+
<button id="btn-this-month" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 ">
45+
This Month
46+
</button>
47+
</a>
48+
<a href="?filter=last-month">
49+
<button id="btn-last-month" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 mr-2 ">
50+
Last Month
51+
</button>
52+
</a>
53+
<a href="?filter=this-year">
54+
<button id="btn-this-year" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 ">
55+
This Year
56+
</button>
57+
</a>
58+
<a href="?filter=last-year">
59+
<button id="btn-last-year" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 rounded-r">
60+
Last Year
61+
</button>
62+
</a>
63+
</div>
4264
</div>
4365

4466
<table class="min-w-full divide-y dark:text-gray-200 divide-gray-200 shadow overflow-hidden rounded-lg">

templates/table.html.templ

+27-4
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,46 @@
2323
}
2424
</script>
2525

26-
<div class="flex max-w-screen justify-end mb-3">
26+
<div class="flex max-w-screen justify-end mb-2">
27+
<div class="flex bg-slate-800 justify-end rounded">
2728
<a href="?filter=24h">
28-
<button id="btn-24h" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-2 px-4 rounded-l">
29+
<button id="btn-24h" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 rounded-l">
2930
24 Hours
3031
</button>
3132
</a>
3233
<a href="?filter=7d">
33-
<button id="btn-7d" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-2 px-4 ">
34+
<button id="btn-7d" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 ">
3435
7 Days
3536
</button>
3637
</a>
3738
<a href="?filter=30d">
38-
<button id="btn-30d" class="justify-end bg-blue-300 hover:bg-blue-500 text-gray-800 font-bold py-2 px-4 rounded-r">
39+
<button id="btn-30d" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 mr-2 ">
3940
30 Days
4041
</button>
4142
</a>
43+
<a href="?filter=this-month">
44+
<button id="btn-this-month" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 ">
45+
This Month
46+
</button>
47+
</a>
48+
<a href="?filter=last-month">
49+
<button id="btn-last-month" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 mr-2 ">
50+
Last Month
51+
</button>
52+
</a>
53+
<a href="?filter=this-year">
54+
<button id="btn-this-year" class="bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 ">
55+
This Year
56+
</button>
57+
</a>
58+
<a href="?filter=last-year">
59+
<button id="btn-last-year" class="justify-end bg-blue-300 hover:bg-blue-400 text-gray-800 font-bold py-1 px-3 rounded-r">
60+
Last Year
61+
</button>
62+
</a>
4263
</div>
64+
</div>
65+
4366

4467
<table class="min-w-full divide-y dark:text-gray-200 divide-gray-200 shadow overflow-hidden rounded-lg">
4568
<thead class="bg-gray-50 dark:bg-slate-800 dark:text-white text-gray-500">

0 commit comments

Comments
 (0)