Skip to content

Commit 7a39730

Browse files
authored
Merge pull request #36 from tohutohu/master
feat: 各ページにログをダウンロードできるリンクを追加
2 parents ff6ea1c + 5880a86 commit 7a39730

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

internal/extproc/handler.go

+46
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package extproc
33
import (
44
"fmt"
55
"net/http"
6+
"slices"
67

78
"github.com/kaz/pprotein/internal/collect"
89
"github.com/labstack/echo/v4"
@@ -34,6 +35,8 @@ func (h *handler) Register(g *echo.Group) error {
3435
g.GET("", h.getIndex)
3536
g.POST("", h.postIndex)
3637
g.GET("/:id", h.getId)
38+
g.GET("/data/:id", h.getData)
39+
g.GET("/data/latest", h.getLatestData)
3740

3841
return nil
3942
}
@@ -66,3 +69,46 @@ func (h *handler) getId(c echo.Context) error {
6669

6770
return c.Stream(http.StatusOK, "application/json", r)
6871
}
72+
73+
func (h *handler) getData(c echo.Context) error {
74+
id := c.Param("id")
75+
entries := h.collector.List()
76+
for _, entry := range entries {
77+
if entry.Snapshot.ID == id {
78+
bodyPath, err := entry.Snapshot.BodyPath()
79+
if err != nil {
80+
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err))
81+
}
82+
83+
return c.File(bodyPath)
84+
}
85+
}
86+
return echo.NewHTTPError(http.StatusNotFound)
87+
}
88+
89+
func (h *handler) getLatestData(c echo.Context) error {
90+
label := c.QueryParam("label")
91+
92+
entries := h.collector.List()
93+
slices.SortFunc(entries, func(a, b *collect.Entry) int {
94+
return b.Snapshot.SnapshotMeta.Datetime.Compare(a.Snapshot.SnapshotMeta.Datetime)
95+
})
96+
97+
if len(entries) > 0 {
98+
for _, entry := range entries {
99+
if label != "" && label != entry.Snapshot.SnapshotTarget.Label {
100+
continue
101+
}
102+
if entry.Status != collect.StatusOk {
103+
continue
104+
}
105+
106+
bodyPath, err := entry.Snapshot.BodyPath()
107+
if err != nil {
108+
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err))
109+
}
110+
return c.File(bodyPath)
111+
}
112+
}
113+
return echo.NewHTTPError(http.StatusNotFound)
114+
}

internal/pprof/handler.go

+46
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pprof
33
import (
44
"fmt"
55
"net/http"
6+
"slices"
67
"sync"
78

89
"github.com/kaz/pprotein/internal/collect"
@@ -32,6 +33,8 @@ func (h *handler) Register(g *echo.Group) error {
3233

3334
g.GET("", h.getIndex)
3435
g.POST("", h.postIndex)
36+
g.GET("/data/:id", h.getData)
37+
g.GET("/data/latest", h.getLatestData)
3538

3639
return nil
3740
}
@@ -54,3 +57,46 @@ func (h *handler) postIndex(c echo.Context) error {
5457

5558
return c.NoContent(http.StatusOK)
5659
}
60+
61+
func (h *handler) getData(c echo.Context) error {
62+
id := c.Param("id")
63+
entries := h.collector.List()
64+
for _, entry := range entries {
65+
if entry.Snapshot.ID == id {
66+
bodyPath, err := entry.Snapshot.BodyPath()
67+
if err != nil {
68+
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err))
69+
}
70+
71+
return c.File(bodyPath)
72+
}
73+
}
74+
return echo.NewHTTPError(http.StatusNotFound)
75+
}
76+
77+
func (h *handler) getLatestData(c echo.Context) error {
78+
label := c.QueryParam("label")
79+
80+
entries := h.collector.List()
81+
slices.SortFunc(entries, func(a, b *collect.Entry) int {
82+
return b.Snapshot.SnapshotMeta.Datetime.Compare(a.Snapshot.SnapshotMeta.Datetime)
83+
})
84+
85+
if len(entries) > 0 {
86+
for _, entry := range entries {
87+
if label != "" && label != entry.Snapshot.SnapshotTarget.Label {
88+
continue
89+
}
90+
if entry.Status != collect.StatusOk {
91+
continue
92+
}
93+
94+
bodyPath, err := entry.Snapshot.BodyPath()
95+
if err != nil {
96+
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to get body path: %v", err))
97+
}
98+
return c.File(bodyPath)
99+
}
100+
}
101+
return echo.NewHTTPError(http.StatusNotFound)
102+
}

view/src/components/HttpLogEntry.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<TsvTable :tsv="tsv" />
2+
<TsvTable :tsv="tsv" :link="`/api/httplog/data/${$route.params.id}`"/>
33
</template>
44

55
<script lang="ts">

view/src/components/SlowLogEntry.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<TsvTable :tsv="tsv" />
2+
<TsvTable :tsv="tsv" :link="`/api/slowlog/data/${$route.params.id}`"/>
33
</template>
44

55
<script lang="ts">

view/src/components/TsvTable.vue

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<template>
22
<section>
3+
<a :href="link" download>Download</a>
34
<table border="1">
45
<thead>
56
<tr>
@@ -39,6 +40,9 @@ export default defineComponent({
3940
type: String,
4041
required: true,
4142
},
43+
link: {
44+
type: String
45+
}
4246
},
4347
data() {
4448
return {

0 commit comments

Comments
 (0)