@@ -3,6 +3,7 @@ package extproc
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "slices"
6
7
7
8
"github.com/kaz/pprotein/internal/collect"
8
9
"github.com/labstack/echo/v4"
@@ -34,6 +35,8 @@ func (h *handler) Register(g *echo.Group) error {
34
35
g .GET ("" , h .getIndex )
35
36
g .POST ("" , h .postIndex )
36
37
g .GET ("/:id" , h .getId )
38
+ g .GET ("/data/:id" , h .getData )
39
+ g .GET ("/data/latest" , h .getLatestData )
37
40
38
41
return nil
39
42
}
@@ -66,3 +69,46 @@ func (h *handler) getId(c echo.Context) error {
66
69
67
70
return c .Stream (http .StatusOK , "application/json" , r )
68
71
}
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
+ }
0 commit comments