Skip to content

Commit

Permalink
Merge branch 'kaz:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ikura-hamu authored Dec 12, 2023
2 parents b2d0f1e + 8e867dc commit 3e082a7
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 16 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)
}
16 changes: 12 additions & 4 deletions 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 All @@ -15,9 +15,17 @@ export default defineComponent({
tsv: "",
};
},
async beforeCreate() {
const resp = await fetch(`/api/httplog/${this.$route.params.id}`);
this.tsv = await resp.text();
async created() {
await this.updateTsv(this.$route.params.id);
},
async beforeRouteUpdate(route) {
await this.updateTsv(route.params.id);
},
methods: {
async updateTsv(id: string | string[]) {
const resp = await fetch(`/api/httplog/${id}`);
this.tsv = await resp.text();
},
},
});
</script>
24 changes: 16 additions & 8 deletions view/src/components/MemoEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ export default defineComponent({
memo: { Text: null },
};
},
async beforeCreate() {
try {
const resp = await fetch(`/api/memo/${this.$route.params.id}`);
this.memo = await resp.json();
} catch (e) {
this.summary = `Error: ${e instanceof Error ? e.message : e}`;
}
async created() {
await this.updateMemo(this.$route.params.id);
},
async beforeRouteUpdate(route) {
await this.updateMemo(route.params.id);
},
methods: {
async updateMemo(id: string | string[]) {
try {
const resp = await fetch(`/api/memo/${id}`);
this.memo = await resp.json();
} catch (e) {
this.summary = `Error: ${e instanceof Error ? e.message : e}`;
}
},
},
});
</script>
</script>
16 changes: 12 additions & 4 deletions 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 All @@ -15,9 +15,17 @@ export default defineComponent({
tsv: "",
};
},
async beforeCreate() {
const resp = await fetch(`/api/slowlog/${this.$route.params.id}`);
this.tsv = await resp.text();
async created() {
await this.updateTsv(this.$route.params.id);
},
async beforeRouteUpdate(route) {
await this.updateTsv(route.params.id);
},
methods: {
async updateTsv(id: string | string[]) {
const resp = await fetch(`/api/slowlog/${id}`);
this.tsv = await resp.text();
},
},
});
</script>
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 3e082a7

Please sign in to comment.