From ed0d7ef8dbf833f74fe5d6709f95a8be839dee25 Mon Sep 17 00:00:00 2001 From: gadotroee <55343099+gadotroee@users.noreply.github.com> Date: Wed, 5 May 2021 12:33:53 +0300 Subject: [PATCH] General stats api fix (#29) * refactor and validation --- api/pkg/configs/fiber_config.go | 21 -------- api/pkg/controllers/entries_controller.go | 61 ++++++++++++++++------- api/pkg/inserter/main.go | 10 ++-- api/pkg/routes/public_routes.go | 3 +- 4 files changed, 50 insertions(+), 45 deletions(-) delete mode 100644 api/pkg/configs/fiber_config.go diff --git a/api/pkg/configs/fiber_config.go b/api/pkg/configs/fiber_config.go deleted file mode 100644 index 5fccb8ecf..000000000 --- a/api/pkg/configs/fiber_config.go +++ /dev/null @@ -1,21 +0,0 @@ -package configs - -import ( - "os" - "strconv" - "time" - - "github.com/gofiber/fiber/v2" -) - -// FiberConfig func for configuration Fiber app. -// See: https://docs.gofiber.io/api/fiber#config -func FiberConfig() fiber.Config { - // Define server settings. - readTimeoutSecondsCount, _ := strconv.Atoi(os.Getenv("SERVER_READ_TIMEOUT")) - - // Return Fiber configuration. - return fiber.Config{ - ReadTimeout: time.Second * time.Duration(readTimeoutSecondsCount), - } -} diff --git a/api/pkg/controllers/entries_controller.go b/api/pkg/controllers/entries_controller.go index caa055e24..a8400dcb6 100644 --- a/api/pkg/controllers/entries_controller.go +++ b/api/pkg/controllers/entries_controller.go @@ -16,40 +16,53 @@ const ( HardLimit = 200 ) +func getSortAndOrder(operator string) (string, string) { + var sort, order string + if strings.ToLower(operator) == "gt" { + sort = ">" + order = "asc" + } else if strings.ToLower(operator) == "lt" { + sort = "<" + order = "desc" + } else { + fmt.Println("Unsupported sort option") + return "", "" + } + return sort, order + +} func GetEntries(c *fiber.Ctx) error { limit, e := strconv.Atoi(c.Query("limit", "200")) utils.CheckErr(e) if limit > HardLimit { + fmt.Printf("Limit is greater than hard limit - using hard limit, requestedLimit: %v, hard: %v", limit, HardLimit) limit = HardLimit } - sortOption := c.Query("operator", "lt") - var sortingOperator string - var ordering string - if strings.ToLower(sortOption) == "gt" { - sortingOperator = ">" - ordering = "asc" - } else if strings.ToLower(sortOption) == "lt" { - sortingOperator = "<" - ordering = "desc" - } else { - fmt.Println("Unsupported") - return nil + operator, order := getSortAndOrder(c.Query("operator", "lt")) + if operator == "" || order == "" { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "msg": "invalid operator", + }) } - timestamp, e := strconv.Atoi(c.Query("timestamp", "-1")) + if timestamp < 0 { + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "msg": "invalid timestamp", + }) + } utils.CheckErr(e) var entries []models.MizuEntry - database.GetEntriesTable(). - Order(fmt.Sprintf("timestamp %s", ordering)). - Where(fmt.Sprintf("timestamp %s %v",sortingOperator, timestamp)). + Order(fmt.Sprintf("timestamp %s", order)). + Where(fmt.Sprintf("timestamp %s %v", operator, timestamp)). Omit("entry"). // remove the "big" entry field Limit(limit). Find(&entries) - if len(entries) > 0 && ordering == "desc"{ + if len(entries) > 0 && order == "desc" { + // the entries always order from oldest to newest so we should revers utils.ReverseSlice(entries) } @@ -92,4 +105,16 @@ func DeleteAllEntries(c *fiber.Ctx) error { return c.Status(fiber.StatusOK).JSON(fiber.Map{ "msg": "Success", }) -} \ No newline at end of file + +} + +func GetGeneralStats(c *fiber.Ctx) error { + sqlQuery := "SELECT count(*) as count, min(timestamp) as min, max(timestamp) as max from mizu_entries" + var result struct { + Count int + Min int + Max int + } + database.GetEntriesTable().Raw(sqlQuery).Scan(&result) + return c.Status(fiber.StatusOK).JSON(&result) +} diff --git a/api/pkg/inserter/main.go b/api/pkg/inserter/main.go index bf6ae6ae1..3727e179d 100644 --- a/api/pkg/inserter/main.go +++ b/api/pkg/inserter/main.go @@ -21,7 +21,7 @@ func StartReadingFiles(harChannel chan *har.Entry, workingDir *string) { if workingDir != nil && *workingDir != "" { startReadingFiles(*workingDir) } else { - startReadingSChan(harChannel) + startReadingChannel(harChannel) } } @@ -50,20 +50,20 @@ func startReadingFiles(workingDir string) { for _, entry := range inputHar.Log.Entries { time.Sleep(time.Millisecond * 250) - SaveHarToDb(*entry, "") + saveHarToDb(*entry, "") } rmErr := os.Remove(inputFilePath) utils.CheckErr(rmErr) } } -func startReadingSChan(harChannel chan *har.Entry) { +func startReadingChannel(harChannel chan *har.Entry) { for entry := range harChannel { - SaveHarToDb(*entry, "") + saveHarToDb(*entry, "") } } -func SaveHarToDb(entry har.Entry, source string) { +func saveHarToDb(entry har.Entry, source string) { entryBytes, _ := json.Marshal(entry) serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL) entryId := primitive.NewObjectID().Hex() diff --git a/api/pkg/routes/public_routes.go b/api/pkg/routes/public_routes.go index 1f465baf2..eac2d86ef 100644 --- a/api/pkg/routes/public_routes.go +++ b/api/pkg/routes/public_routes.go @@ -12,5 +12,6 @@ func EntriesRoutes(fiberApp *fiber.App) { routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries) routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry - routeGroup.Get("/resetDB", controllers.DeleteAllEntries) // get single (full) entry + routeGroup.Get("/resetDB", controllers.DeleteAllEntries) // get single (full) entry + routeGroup.Get("/generalStats", controllers.GetGeneralStats) // get general stats about entries in DB }