Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get file status rest api for indexer gateway #71

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions indexer/gateway/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -172,12 +173,22 @@ 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
continue
}

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.
Expand Down
2 changes: 2 additions & 0 deletions indexer/gateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func newRouter() *gin.Engine {
// handlers
router.GET("/file", downloadFile)
router.GET("/file/:cid/*filePath", downloadFileInFolder)
router.GET("/file/info/:cid", getFileStatus)
router.GET("/node/status", getNodeStatus)
router.POST("/file/segment", uploadSegment)

return router
Expand Down
73 changes: 73 additions & 0 deletions indexer/gateway/status.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading