From 2daf5cf8f5cfad95a46388fd85f22d5cad7f3913 Mon Sep 17 00:00:00 2001 From: Conflux Date: Thu, 21 Nov 2024 18:48:20 +0800 Subject: [PATCH 1/4] feat: add get file status rest api for indexer gateway --- indexer/gateway/download.go | 16 +++++++++++++--- indexer/gateway/server.go | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/indexer/gateway/download.go b/indexer/gateway/download.go index d0c6eda..644e9ce 100644 --- a/indexer/gateway/download.go +++ b/indexer/gateway/download.go @@ -161,6 +161,7 @@ func getFileInfo(ctx context.Context, root common.Hash, txSeq *uint64) (info *no return nil, errors.New("no clients available") } + var finalInfo *node.FileInfo for _, client := range clients { if txSeq != nil { info, err = client.GetFileInfoByTxSeq(ctx, *txSeq) @@ -172,12 +173,21 @@ func getFileInfo(ctx context.Context, root common.Hash, txSeq *uint64) (info *no return nil, err } - if info != nil { - return info, nil + if info == nil { + return nil, nil } + + if finalInfo == nil { + finalInfo = info + } + + finalInfo.Finalized = finalInfo.Finalized && info.Finalized + finalInfo.IsCached = finalInfo.IsCached && info.IsCached + finalInfo.Pruned = finalInfo.Pruned || info.Pruned + finalInfo.UploadedSegNum = min(finalInfo.UploadedSegNum, info.UploadedSegNum) } - return nil, nil + return finalInfo, nil } // downloadAndServeFile downloads the file and serves it as an attachment. diff --git a/indexer/gateway/server.go b/indexer/gateway/server.go index 6308d56..b9ad3e2 100644 --- a/indexer/gateway/server.go +++ b/indexer/gateway/server.go @@ -52,6 +52,7 @@ func newRouter() *gin.Engine { // handlers router.GET("/file", downloadFile) router.GET("/file/:cid/*filePath", downloadFileInFolder) + router.GET("/status/:cid", getFileStatus) router.POST("/file/segment", uploadSegment) return router From 454910f7101efdafac6285bbb013b702a7164b87 Mon Sep 17 00:00:00 2001 From: Conflux Date: Thu, 21 Nov 2024 19:09:04 +0800 Subject: [PATCH 2/4] fix --- indexer/gateway/download.go | 1 + indexer/gateway/server.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/indexer/gateway/download.go b/indexer/gateway/download.go index 644e9ce..cc7233d 100644 --- a/indexer/gateway/download.go +++ b/indexer/gateway/download.go @@ -179,6 +179,7 @@ func getFileInfo(ctx context.Context, root common.Hash, txSeq *uint64) (info *no if finalInfo == nil { finalInfo = info + continue } finalInfo.Finalized = finalInfo.Finalized && info.Finalized diff --git a/indexer/gateway/server.go b/indexer/gateway/server.go index b9ad3e2..b209170 100644 --- a/indexer/gateway/server.go +++ b/indexer/gateway/server.go @@ -52,7 +52,8 @@ func newRouter() *gin.Engine { // handlers router.GET("/file", downloadFile) router.GET("/file/:cid/*filePath", downloadFileInFolder) - router.GET("/status/:cid", getFileStatus) + router.GET("/file/info/:cid", getFileStatus) + router.GET("/node/status", getNodeStatus) router.POST("/file/segment", uploadSegment) return router From ab2a4eafd9dc0440521b59cef2c246b249b2964c Mon Sep 17 00:00:00 2001 From: Conflux Date: Thu, 21 Nov 2024 19:10:37 +0800 Subject: [PATCH 3/4] add missing --- indexer/gateway/status.go | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 indexer/gateway/status.go diff --git a/indexer/gateway/status.go b/indexer/gateway/status.go new file mode 100644 index 0000000..28df26a --- /dev/null +++ b/indexer/gateway/status.go @@ -0,0 +1,73 @@ +package gateway + +import ( + "context" + "fmt" + "net/http" + "strconv" + + "github.com/0glabs/0g-storage-client/node" + "github.com/ethereum/go-ethereum/common" + "github.com/gin-gonic/gin" +) + +func getFileStatus(c *gin.Context) { + cid := c.Param("cid") + + var root common.Hash + var txSeq *uint64 + + if v, err := strconv.ParseUint(cid, 10, 64); err == nil { // TxnSeq is used as cid + txSeq = &v + } else { + root = common.HexToHash(cid) + } + + if txSeq == nil && (root == common.Hash{}) { + c.JSON(http.StatusBadRequest, "Either 'root' or 'txSeq' must be provided") + return + } + + fileInfo, err := getFileInfo(c, root, txSeq) + if err != nil { + c.JSON(http.StatusInternalServerError, fmt.Sprintf("Failed to retrieve file info: %v", err)) + return + } + + if fileInfo == nil { + c.JSON(http.StatusNotFound, "File not found") + return + } + + c.JSON(http.StatusOK, fileInfo) +} + +func getNodeStatus(c *gin.Context) { + if len(clients) == 0 { + c.JSON(http.StatusInternalServerError, "no clients available") + return + } + + var finalStatus *node.Status + for _, client := range clients { + status, err := client.GetStatus(context.Background()) + if err != nil { + c.JSON(http.StatusInternalServerError, fmt.Sprintf("Failed to retrieve node status: %v", err)) + } + + if finalStatus == nil { + finalStatus = &status + continue + } + + if finalStatus.LogSyncHeight > status.LogSyncHeight { + finalStatus.LogSyncHeight = status.LogSyncHeight + finalStatus.LogSyncBlock = status.LogSyncBlock + } + + finalStatus.ConnectedPeers = max(finalStatus.ConnectedPeers, status.ConnectedPeers) + finalStatus.NextTxSeq = min(finalStatus.NextTxSeq, status.NextTxSeq) + } + + c.JSON(http.StatusOK, finalStatus) +} From 996facceccb571b64920fcd889cf7293e2053baf Mon Sep 17 00:00:00 2001 From: Conflux Date: Fri, 22 Nov 2024 10:00:49 +0800 Subject: [PATCH 4/4] minor mod --- indexer/gateway/status.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/indexer/gateway/status.go b/indexer/gateway/status.go index 28df26a..47c9ed4 100644 --- a/indexer/gateway/status.go +++ b/indexer/gateway/status.go @@ -1,7 +1,6 @@ package gateway import ( - "context" "fmt" "net/http" "strconv" @@ -50,7 +49,7 @@ func getNodeStatus(c *gin.Context) { var finalStatus *node.Status for _, client := range clients { - status, err := client.GetStatus(context.Background()) + status, err := client.GetStatus(c) if err != nil { c.JSON(http.StatusInternalServerError, fmt.Sprintf("Failed to retrieve node status: %v", err)) }