Add export entries endpoint for better up9 connect funcionality (#72)

* no message
* no message
* no message
This commit is contained in:
gadotroee 2021-06-17 16:48:05 +03:00 committed by GitHub
parent bc3efc6d4c
commit 485bc7fd2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 25 deletions

View File

@ -9,6 +9,7 @@ import (
"mizuserver/pkg/models" "mizuserver/pkg/models"
"mizuserver/pkg/utils" "mizuserver/pkg/utils"
"mizuserver/pkg/validation" "mizuserver/pkg/validation"
"time"
) )
const ( const (
@ -75,11 +76,23 @@ func GetHARs(c *fiber.Ctx) error {
return c.Status(fiber.StatusBadRequest).JSON(err) return c.Status(fiber.StatusBadRequest).JSON(err)
} }
var timestampFrom, timestampTo int64
if entriesFilter.From < 0 {
timestampFrom = 0
} else {
timestampFrom = entriesFilter.From
}
if entriesFilter.To <= 0 {
timestampTo = time.Now().UnixNano() / int64(time.Millisecond)
} else {
timestampTo = entriesFilter.To
}
var entries []models.MizuEntry var entries []models.MizuEntry
database.GetEntriesTable(). database.GetEntriesTable().
Where(fmt.Sprintf("timestamp BETWEEN %v AND %v", timestampFrom, timestampTo)).
Order(fmt.Sprintf("timestamp %s", order)). Order(fmt.Sprintf("timestamp %s", order)).
// Where(fmt.Sprintf("timestamp %s %v", operatorSymbol, entriesFilter.Timestamp)).
Limit(1000).
Find(&entries) Find(&entries)
if len(entries) > 0 { if len(entries) > 0 {
@ -125,6 +138,50 @@ func GetHARs(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).SendStream(buffer) return c.Status(fiber.StatusOK).SendStream(buffer)
} }
func GetFullEntries(c *fiber.Ctx) error {
entriesFilter := &models.HarFetchRequestBody{}
order := OrderDesc
if err := c.QueryParser(entriesFilter); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(err)
}
err := validation.Validate(entriesFilter)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(err)
}
var timestampFrom, timestampTo int64
if entriesFilter.From < 0 {
timestampFrom = 0
} else {
timestampFrom = entriesFilter.From
}
if entriesFilter.To <= 0 {
timestampTo = time.Now().UnixNano() / int64(time.Millisecond)
} else {
timestampTo = entriesFilter.To
}
var entries []models.MizuEntry
database.GetEntriesTable().
Where(fmt.Sprintf("timestamp BETWEEN %v AND %v", timestampFrom, timestampTo)).
Order(fmt.Sprintf("timestamp %s", order)).
Find(&entries)
if len(entries) > 0 {
// the entries always order from oldest to newest so we should revers
utils.ReverseSlice(entries)
}
entriesArray := make([]har.Entry, 0)
for _, entryData := range entries {
var harEntry har.Entry
_ = json.Unmarshal([]byte(entryData.Entry), &harEntry)
entriesArray = append(entriesArray, harEntry)
}
return c.Status(fiber.StatusOK).JSON(entriesArray)
}
func GetEntry(c *fiber.Ctx) error { func GetEntry(c *fiber.Ctx) error {
var entryData models.EntryData var entryData models.EntryData
database.GetEntriesTable(). database.GetEntriesTable().

View File

@ -48,7 +48,8 @@ type EntriesFilter struct {
} }
type HarFetchRequestBody struct { type HarFetchRequestBody struct {
Limit int `query:"limit"` From int64 `query:"from"`
To int64 `query:"to"`
} }
type WebSocketEntryMessage struct { type WebSocketEntryMessage struct {
@ -56,7 +57,6 @@ type WebSocketEntryMessage struct {
Data *BaseEntryDetails `json:"data,omitempty"` Data *BaseEntryDetails `json:"data,omitempty"`
} }
type WebSocketTappedEntryMessage struct { type WebSocketTappedEntryMessage struct {
*shared.WebSocketMessageMetadata *shared.WebSocketMessageMetadata
Data *tap.OutputChannelItem Data *tap.OutputChannelItem
@ -82,7 +82,6 @@ func CreateWebsocketTappedEntryMessage(base *tap.OutputChannelItem) ([]byte, err
return json.Marshal(message) return json.Marshal(message)
} }
// ExtendedHAR is the top level object of a HAR log. // ExtendedHAR is the top level object of a HAR log.
type ExtendedHAR struct { type ExtendedHAR struct {
Log *ExtendedLog `json:"log"` Log *ExtendedLog `json:"log"`

View File

@ -11,8 +11,11 @@ func EntriesRoutes(fiberApp *fiber.App) {
routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries) routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries)
routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry
routeGroup.Get("/exportEntries", controllers.GetFullEntries)
routeGroup.Get("/har", controllers.GetHARs) routeGroup.Get("/har", controllers.GetHARs)
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 routeGroup.Get("/generalStats", controllers.GetGeneralStats) // get general stats about entries in DB

View File

@ -5,8 +5,10 @@ import (
) )
type MizuFetchOptions struct { type MizuFetchOptions struct {
Limit uint16 FromTimestamp int64
ToTimestamp int64
Directory string Directory string
MizuPort uint
} }
var mizuFetchOptions = MizuFetchOptions{} var mizuFetchOptions = MizuFetchOptions{}
@ -23,6 +25,8 @@ var fetchCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(fetchCmd) rootCmd.AddCommand(fetchCmd)
fetchCmd.Flags().Uint16VarP(&mizuFetchOptions.Limit, "limit", "l", 1000, "Provide a custom limit for entries to fetch")
fetchCmd.Flags().StringVarP(&mizuFetchOptions.Directory, "directory", "d", ".", "Provide a custom directory for fetched entries") fetchCmd.Flags().StringVarP(&mizuFetchOptions.Directory, "directory", "d", ".", "Provide a custom directory for fetched entries")
fetchCmd.Flags().Int64Var(&mizuFetchOptions.FromTimestamp, "from", 0, "Custom start timestamp for fetched entries")
fetchCmd.Flags().Int64Var(&mizuFetchOptions.ToTimestamp, "to", 0, "Custom end timestamp fetched entries")
fetchCmd.Flags().UintVarP(&mizuFetchOptions.MizuPort, "port", "p", 8899, "Custom port for mizu")
} }

View File

@ -14,7 +14,7 @@ import (
) )
func RunMizuFetch(fetch *MizuFetchOptions) { func RunMizuFetch(fetch *MizuFetchOptions) {
resp, err := http.Get(fmt.Sprintf("http://localhost:8899/api/har?limit=%v", fetch.Limit)) resp, err := http.Get(fmt.Sprintf("http://localhost:%v/api/har?from=%v&to=%v", fetch.MizuPort, fetch.FromTimestamp, fetch.ToTimestamp))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -53,7 +53,7 @@ func Unzip(reader *zip.Reader, dest string) error {
path := filepath.Join(dest, f.Name) path := filepath.Join(dest, f.Name)
// Check for ZipSlip (Directory traversal) // Check for ZipSlip (Directory traversal)
if !strings.HasPrefix(path, filepath.Clean(dest) + string(os.PathSeparator)) { if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path: %s", path) return fmt.Errorf("illegal file path: %s", path)
} }
@ -90,5 +90,3 @@ func Unzip(reader *zip.Reader, dest string) error {
return nil return nil
} }