diff --git a/logic/stats/PublicStats.go b/logic/stats/PublicStats.go new file mode 100644 index 0000000..1293266 --- /dev/null +++ b/logic/stats/PublicStats.go @@ -0,0 +1,44 @@ +package stats + +import ( + "time" + + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" +) + +func HandlePublicStats(handle *Handle) { + handle.Post("/stats/public/main", func(c *Context) *Error { + if handle.DataMap["PublicMainLastUpdate"] != nil && handle.DataMap["PublicMainLastUpdate"].(int64) > time.Now().UnixMilli() { + c.ShowMessage = false + return c.SendJSON(handle.DataMap["PublicMain"]) + } + number_of_models, err := GetDbVar[int](c, "count(*)", "models") + if err != nil { + return c.E500M("Could not get statistics", err) + } + number_of_classfications, err := GetDbVar[int](c, "count(*)", "tasks where task_type=$1", TASK_TYPE_CLASSIFICATION) + if err != nil { + return c.E500M("Could not get statistics", err) + } + number_of_images_processed, err := GetDbVar[int](c, "count(*)", "model_data_point") + if err != nil { + return c.E500M("Could not get statistics", err) + } + + handle.DataMap["PublicMainLastUpdate"] = time.Now().UnixNano() + 60*60*1000*1000 + handle.DataMap["PublicMain"] = struct { + NumberOfModels int `json:"number_of_models"` + NumberOfClassfications int `json:"number_of_classfications"` + NumberOfImagesProcessed int `json:"number_of_images_processed"` + }{ + *number_of_models, + *number_of_classfications, + *number_of_images_processed, + } + + c.ShowMessage = false + return c.SendJSON(handle.DataMap["PublicMain"]) + }) +} diff --git a/logic/stats/index.go b/logic/stats/index.go new file mode 100644 index 0000000..48f14a6 --- /dev/null +++ b/logic/stats/index.go @@ -0,0 +1,9 @@ +package stats + +import ( + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils" +) + +func HandleStats(handle *Handle) { + HandlePublicStats(handle) +} diff --git a/logic/utils/handler.go b/logic/utils/handler.go index 328ebda..ebf79c9 100644 --- a/logic/utils/handler.go +++ b/logic/utils/handler.go @@ -46,6 +46,7 @@ type Handle struct { gets []HandleFunc posts []HandleFunc deletes []HandleFunc + DataMap map[string]interface{} Config Config validate *validator.Validate } @@ -523,7 +524,7 @@ func NewHandler(db db.Db, config Config) *Handle { var posts []HandleFunc var deletes []HandleFunc validate := validator.New() - x := &Handle{db, gets, posts, deletes, config, validate} + x := &Handle{db, gets, posts, deletes, map[string]interface{}{}, config, validate} http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { diff --git a/main.go b/main.go index 35f5daf..2433118 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "git.andr3h3nriqu3s.com/andr3/fyp/logic/db" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/models" + . "git.andr3h3nriqu3s.com/andr3/fyp/logic/stats" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/runner" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/users" @@ -48,6 +49,7 @@ func main() { UsersEndpints(db, handle) HandleModels(handle) HandleTasks(handle) + HandleStats(handle) handle.Startup() } diff --git a/webpage/src/NavBar.svelte b/webpage/src/NavBar.svelte index 1bd3bae..f90d75a 100644 --- a/webpage/src/NavBar.svelte +++ b/webpage/src/NavBar.svelte @@ -6,22 +6,22 @@