From 2d3895d3c0e39f024de9927dd707b451a3393155 Mon Sep 17 00:00:00 2001 From: tohutohu Date: Thu, 23 Nov 2023 10:06:50 +0000 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=90=84=E3=83=9A=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E3=81=AB=E3=83=AD=E3=82=B0=E3=82=92=E3=83=80=E3=82=A6?= =?UTF-8?q?=E3=83=B3=E3=83=AD=E3=83=BC=E3=83=89=E3=81=A7=E3=81=8D=E3=82=8B?= =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=AF=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/extproc/handler.go | 42 ++++++++++++++++++++++++++++ internal/pprof/handler.go | 42 ++++++++++++++++++++++++++++ view/src/components/HttpLogEntry.vue | 2 +- view/src/components/SlowLogEntry.vue | 2 +- view/src/components/TsvTable.vue | 4 +++ 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/internal/extproc/handler.go b/internal/extproc/handler.go index 9d96389..7c01ff0 100644 --- a/internal/extproc/handler.go +++ b/internal/extproc/handler.go @@ -3,6 +3,7 @@ package extproc import ( "fmt" "net/http" + "slices" "github.com/kaz/pprotein/internal/collect" "github.com/labstack/echo/v4" @@ -34,6 +35,8 @@ func (h *handler) Register(g *echo.Group) error { g.GET("", h.getIndex) g.POST("", h.postIndex) g.GET("/:id", h.getId) + g.GET("/data/:id", h.getData) + g.GET("/data/latest", h.getLatestData) return nil } @@ -66,3 +69,42 @@ func (h *handler) getId(c echo.Context) error { return c.Stream(http.StatusOK, "application/json", r) } + +func (h *handler) getData(c echo.Context) error { + id := c.Param("id") + entries := h.collector.List() + for _, entry := range entries { + if entry.Snapshot.ID == id { + bodyPath, err := entry.Snapshot.BodyPath() + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err)) + } + + return c.File(bodyPath) + } + } + return echo.NewHTTPError(http.StatusNotFound) +} + +func (h *handler) getLatestData(c echo.Context) error { + label := c.QueryParam("label") + + entries := h.collector.List() + slices.SortFunc(entries, func(a, b *collect.Entry) int { + return b.Snapshot.SnapshotMeta.Datetime.Compare(a.Snapshot.SnapshotMeta.Datetime) + }) + + if len(entries) > 0 { + for _, entry := range entries { + if label != "" && label != entry.Snapshot.SnapshotTarget.Label { + continue + } + bodyPath, err := entry.Snapshot.BodyPath() + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err)) + } + return c.File(bodyPath) + } + } + return echo.NewHTTPError(http.StatusNotFound) +} diff --git a/internal/pprof/handler.go b/internal/pprof/handler.go index ec58aec..4257764 100644 --- a/internal/pprof/handler.go +++ b/internal/pprof/handler.go @@ -3,6 +3,7 @@ package pprof import ( "fmt" "net/http" + "slices" "sync" "github.com/kaz/pprotein/internal/collect" @@ -32,6 +33,8 @@ func (h *handler) Register(g *echo.Group) error { g.GET("", h.getIndex) g.POST("", h.postIndex) + g.GET("/data/:id", h.getData) + g.GET("/data/latest", h.getLatestData) return nil } @@ -54,3 +57,42 @@ func (h *handler) postIndex(c echo.Context) error { return c.NoContent(http.StatusOK) } + +func (h *handler) getData(c echo.Context) error { + id := c.Param("id") + entries := h.collector.List() + for _, entry := range entries { + if entry.Snapshot.ID == id { + bodyPath, err := entry.Snapshot.BodyPath() + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err)) + } + + return c.File(bodyPath) + } + } + return echo.NewHTTPError(http.StatusNotFound) +} + +func (h *handler) getLatestData(c echo.Context) error { + label := c.QueryParam("label") + + entries := h.collector.List() + slices.SortFunc(entries, func(a, b *collect.Entry) int { + return b.Snapshot.SnapshotMeta.Datetime.Compare(a.Snapshot.SnapshotMeta.Datetime) + }) + + if len(entries) > 0 { + for _, entry := range entries { + if label != "" && label != entry.Snapshot.SnapshotTarget.Label { + continue + } + bodyPath, err := entry.Snapshot.BodyPath() + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err)) + } + return c.File(bodyPath) + } + } + return echo.NewHTTPError(http.StatusNotFound) +} diff --git a/view/src/components/HttpLogEntry.vue b/view/src/components/HttpLogEntry.vue index 2e0041e..ed49c89 100644 --- a/view/src/components/HttpLogEntry.vue +++ b/view/src/components/HttpLogEntry.vue @@ -1,5 +1,5 @@