inital commit & import from main project
This commit is contained in:
71
api.go
Normal file
71
api.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/go-chi/render"
|
||||
"net/http"
|
||||
"somegit.dev/ALHP/ALHP.GO/ent"
|
||||
"somegit.dev/ALHP/ALHP.GO/ent/dbpackage"
|
||||
)
|
||||
|
||||
type StatsResponse struct {
|
||||
Failed int `json:"failed"`
|
||||
Skipped int `json:"skipped"`
|
||||
Latest int `json:"latest"`
|
||||
Queued int `json:"queued"`
|
||||
LTO *struct {
|
||||
Enabled int `json:"enabled"`
|
||||
Disabled int `json:"disabled"`
|
||||
Unknown int `json:"unknown"`
|
||||
} `json:"lto"`
|
||||
}
|
||||
|
||||
func GetStats(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
var v []struct {
|
||||
Status dbpackage.Status `json:"status"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
db.DBPackage.Query().GroupBy(dbpackage.FieldStatus).Aggregate(ent.Count()).ScanX(ctx, &v)
|
||||
|
||||
resp := new(StatsResponse)
|
||||
resp.LTO = new(struct {
|
||||
Enabled int `json:"enabled"`
|
||||
Disabled int `json:"disabled"`
|
||||
Unknown int `json:"unknown"`
|
||||
})
|
||||
for _, c := range v {
|
||||
switch c.Status { //nolint:exhaustive
|
||||
case dbpackage.StatusFailed:
|
||||
resp.Failed = c.Count
|
||||
case dbpackage.StatusSkipped:
|
||||
resp.Skipped = c.Count
|
||||
case dbpackage.StatusLatest:
|
||||
resp.Latest = c.Count
|
||||
case dbpackage.StatusQueued:
|
||||
resp.Queued = c.Count
|
||||
}
|
||||
}
|
||||
|
||||
var v2 []struct {
|
||||
Status dbpackage.Lto `json:"lto"` //nolint:tagliatelle
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
db.DBPackage.Query().Where(dbpackage.StatusNEQ(dbpackage.StatusSkipped)).
|
||||
GroupBy(dbpackage.FieldLto).Aggregate(ent.Count()).ScanX(ctx, &v2)
|
||||
|
||||
for _, c := range v2 {
|
||||
switch c.Status {
|
||||
case dbpackage.LtoUnknown:
|
||||
resp.LTO.Unknown = c.Count
|
||||
case dbpackage.LtoDisabled, dbpackage.LtoAutoDisabled:
|
||||
resp.LTO.Disabled += c.Count
|
||||
case dbpackage.LtoEnabled:
|
||||
resp.LTO.Enabled = c.Count
|
||||
}
|
||||
}
|
||||
|
||||
render.Status(r, http.StatusOK)
|
||||
render.JSON(w, r, resp)
|
||||
}
|
||||
Reference in New Issue
Block a user