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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
)
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",
})
}
}
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 != "" {
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()

View File

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