Skip to content

Commit

Permalink
Merge pull request #36 from tohutohu/master
Browse files Browse the repository at this point in the history
feat: 各ページにログをダウンロードできるリンクを追加
  • Loading branch information
kaz authored Nov 24, 2023
2 parents ff6ea1c + 5880a86 commit 7a39730
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
46 changes: 46 additions & 0 deletions internal/extproc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package extproc
import (
"fmt"
"net/http"
"slices"

"github.com/kaz/pprotein/internal/collect"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -66,3 +69,46 @@ 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
}
if entry.Status != collect.StatusOk {
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)
}
46 changes: 46 additions & 0 deletions internal/pprof/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pprof
import (
"fmt"
"net/http"
"slices"
"sync"

"github.com/kaz/pprotein/internal/collect"
Expand Down Expand Up @@ -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
}
Expand All @@ -54,3 +57,46 @@ 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
}
if entry.Status != collect.StatusOk {
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)
}
2 changes: 1 addition & 1 deletion view/src/components/HttpLogEntry.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<TsvTable :tsv="tsv" />
<TsvTable :tsv="tsv" :link="`/api/httplog/data/${$route.params.id}`"/>
</template>

<script lang="ts">
Expand Down
2 changes: 1 addition & 1 deletion view/src/components/SlowLogEntry.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<TsvTable :tsv="tsv" />
<TsvTable :tsv="tsv" :link="`/api/slowlog/data/${$route.params.id}`"/>
</template>

<script lang="ts">
Expand Down
4 changes: 4 additions & 0 deletions view/src/components/TsvTable.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<section>
<a :href="link" download>Download</a>
<table border="1">
<thead>
<tr>
Expand Down Expand Up @@ -39,6 +40,9 @@ export default defineComponent({
type: String,
required: true,
},
link: {
type: String
}
},
data() {
return {
Expand Down

0 comments on commit 7a39730

Please sign in to comment.