From 255f86e48bbd7a55d13a7e0221cfde373e224023 Mon Sep 17 00:00:00 2001 From: Adrien Date: Thu, 23 May 2024 17:00:31 +0200 Subject: [PATCH] Better usage menu --- Chat.go | 104 +++++++++++++++----------- views/layouts/main.html | 2 +- views/partials/popover-models.html | 13 +--- views/partials/popover-usage.html | 114 +++++++++++++++++++++++++++-- 4 files changed, 172 insertions(+), 61 deletions(-) diff --git a/Chat.go b/Chat.go index 8ca1e9e..b9e869b 100644 --- a/Chat.go +++ b/Chat.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "sort" "strings" "time" @@ -379,64 +380,83 @@ func LoadUsageKPIHandler(c *fiber.Ctx) error { if !checkIfLogin() || !checkIfHaveKey() { return c.SendString("") } - var TotalUsage float32 - // Using the database. Get the sum of all usage.inputCost and outputCost - err := edgeClient.QuerySingle(edgeCtx, ` - WITH - U := ( - SELECT Usage - FILTER .user = global currentUser - ) - SELECT sum(U.input_cost) + sum(U.output_cost) - `, &TotalUsage) + + InputDateID := c.FormValue("month", time.Now().Format("01-2006")) + offset := c.FormValue("offset") + IsActive := false + + var InputDate time.Time + InputDate, err := time.Parse("01-2006", InputDateID) if err != nil { panic(err) } - now := time.Now() + if offset == "-1" { + InputDate = InputDate.AddDate(0, -1, 0) + IsActive = true + } else if offset == "1" { + InputDate = InputDate.AddDate(0, 1, 0) + IsActive = true + } - var TodayUsage float32 - // Using the database. Get the sum of all usage.inputCost and outputCost - err = edgeClient.QuerySingle(edgeCtx, ` - WITH - U := ( - SELECT Usage - FILTER .user = global currentUser AND .date >= $0 - ) - SELECT sum(U.input_cost) + sum(U.output_cost) - `, &TodayUsage, now.Add(time.Hour*-24)) + type UsageKPI struct { + Key struct { + ModelID string `edgedb:"model_id"` + } `edgedb:"key"` + TotalCost float32 `edgedb:"total_cost"` + TotalCount int64 `edgedb:"total_count"` + } + + var usages []UsageKPI + err = edgeClient.Query(edgeCtx, ` + WITH + U := ( + SELECT Usage + FILTER .user = global currentUser and .date >= $0 AND .date < $1 + ), + grouped := ( + GROUP U { + model_id, + input_cost, + output_cost, + } BY .model_id + ) + SELECT grouped { + key := .key { model_id }, + total_count := count(.elements), + total_cost := sum(.elements.input_cost) + sum(.elements.output_cost), + } FILTER .total_count > 0 ORDER BY .total_cost DESC + `, &usages, InputDate, InputDate.AddDate(0, 1, 0)) if err != nil { + fmt.Println(err) panic(err) } - var WeekUsage float32 - // Using the database. Get the sum of all usage.inputCost and outputCost - edgeClient.QuerySingle(edgeCtx, ` - WITH - U := ( - SELECT Usage - FILTER .user = global currentUser AND .date >= $0 - ) - SELECT sum(U.input_cost) + sum(U.output_cost) - `, &WeekUsage, now.Add(time.Hour*-24*7)) + BeautifullDate := InputDate.Format("Jan 2006") - var MonthUsage float32 - // Using the database. Get the sum of all usage.inputCost and outputCost - edgeClient.QuerySingle(edgeCtx, ` - WITH - U := ( - SELECT Usage - FILTER .user = global currentUser AND .date >= $0 - ) - SELECT sum(U.input_cost) + sum(U.output_cost) - `, &MonthUsage, now.Add(time.Hour*-24*30)) + var ( + TotalCount int64 + TotalCost float32 + ) + for _, usage := range usages { + TotalCost += usage.TotalCost + TotalCount += usage.TotalCount + } + + fmt.Println(TotalCost, TotalCount) out, err := pongo2.Must(pongo2.FromFile("views/partials/popover-usage.html")).Execute(pongo2.Context{ - "TotalUsage": TotalUsage, "TodayUsage": TodayUsage, "WeekUsage": WeekUsage, "MonthUsage": MonthUsage, + "usages": usages, + "TotalCost": TotalCost, + "TotalCount": TotalCount, + "Date": BeautifullDate, + "DateID": InputDate.Format("01-2006"), + "IsActive": IsActive, }) if err != nil { panic(err) } + return c.SendString(out) } diff --git a/views/layouts/main.html b/views/layouts/main.html index 85fdb87..5bf3de4 100644 --- a/views/layouts/main.html +++ b/views/layouts/main.html @@ -9,7 +9,7 @@ - + diff --git a/views/partials/popover-models.html b/views/partials/popover-models.html index 8f79a4d..37de8df 100644 --- a/views/partials/popover-models.html +++ b/views/partials/popover-models.html @@ -6,7 +6,7 @@