General stats api fix (#29)

* refactor and validation
This commit is contained in:
gadotroee
2021-05-05 12:33:53 +03:00
committed by GitHub
parent 4d6528771a
commit ed0d7ef8db
4 changed files with 50 additions and 45 deletions

View File

@@ -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),
}
}

View File

@@ -16,40 +16,53 @@ const (
HardLimit = 200 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 { func GetEntries(c *fiber.Ctx) error {
limit, e := strconv.Atoi(c.Query("limit", "200")) limit, e := strconv.Atoi(c.Query("limit", "200"))
utils.CheckErr(e) utils.CheckErr(e)
if limit > HardLimit { if limit > HardLimit {
fmt.Printf("Limit is greater than hard limit - using hard limit, requestedLimit: %v, hard: %v", limit, HardLimit)
limit = HardLimit limit = HardLimit
} }
sortOption := c.Query("operator", "lt") operator, order := getSortAndOrder(c.Query("operator", "lt"))
var sortingOperator string if operator == "" || order == "" {
var ordering string return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
if strings.ToLower(sortOption) == "gt" { "msg": "invalid operator",
sortingOperator = ">" })
ordering = "asc"
} else if strings.ToLower(sortOption) == "lt" {
sortingOperator = "<"
ordering = "desc"
} else {
fmt.Println("Unsupported")
return nil
} }
timestamp, e := strconv.Atoi(c.Query("timestamp", "-1")) 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) utils.CheckErr(e)
var entries []models.MizuEntry var entries []models.MizuEntry
database.GetEntriesTable(). database.GetEntriesTable().
Order(fmt.Sprintf("timestamp %s", ordering)). Order(fmt.Sprintf("timestamp %s", order)).
Where(fmt.Sprintf("timestamp %s %v",sortingOperator, timestamp)). Where(fmt.Sprintf("timestamp %s %v", operator, timestamp)).
Omit("entry"). // remove the "big" entry field Omit("entry"). // remove the "big" entry field
Limit(limit). Limit(limit).
Find(&entries) 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) utils.ReverseSlice(entries)
} }
@@ -92,4 +105,16 @@ func DeleteAllEntries(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"msg": "Success", "msg": "Success",
}) })
}
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)
} }

View File

@@ -21,7 +21,7 @@ func StartReadingFiles(harChannel chan *har.Entry, workingDir *string) {
if workingDir != nil && *workingDir != "" { if workingDir != nil && *workingDir != "" {
startReadingFiles(*workingDir) startReadingFiles(*workingDir)
} else { } else {
startReadingSChan(harChannel) startReadingChannel(harChannel)
} }
} }
@@ -50,20 +50,20 @@ func startReadingFiles(workingDir string) {
for _, entry := range inputHar.Log.Entries { for _, entry := range inputHar.Log.Entries {
time.Sleep(time.Millisecond * 250) time.Sleep(time.Millisecond * 250)
SaveHarToDb(*entry, "") saveHarToDb(*entry, "")
} }
rmErr := os.Remove(inputFilePath) rmErr := os.Remove(inputFilePath)
utils.CheckErr(rmErr) utils.CheckErr(rmErr)
} }
} }
func startReadingSChan(harChannel chan *har.Entry) { func startReadingChannel(harChannel chan *har.Entry) {
for entry := range harChannel { 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) entryBytes, _ := json.Marshal(entry)
serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL) serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL)
entryId := primitive.NewObjectID().Hex() entryId := primitive.NewObjectID().Hex()

View File

@@ -13,4 +13,5 @@ func EntriesRoutes(fiberApp *fiber.App) {
routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry 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
} }