Skip to content

Commit

Permalink
Merge pull request #44 from kqzh/feat_get_space_used
Browse files Browse the repository at this point in the history
  • Loading branch information
kqzh authored Aug 22, 2023
2 parents d533419 + f6b1388 commit 698d36d
Show file tree
Hide file tree
Showing 5 changed files with 732 additions and 37 deletions.
51 changes: 51 additions & 0 deletions internal/clients/spaceusage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package clients

import (
"os"
"path/filepath"
"strconv"

pb "github.com/vesoft-inc/nebula-agent/v3/pkg/proto"
)

type SpaceUsage struct {
DataPath string
}

func NewSpaceUsage(dataPath string) *SpaceUsage {
return &SpaceUsage{
DataPath: dataPath,
}
}

func (s *SpaceUsage) GetSpaceUsages() (*pb.GetSpaceUsagesResponse, error) {
spaceUsages := make([]*pb.GetSpaceUsagesResponse_SpaceUsageItem, 0)
dirs, err := filepath.Glob(filepath.Join(s.DataPath, "*/"))
if err != nil {
return nil, err
}
for _, dir := range dirs {
id, err := strconv.Atoi(filepath.Base(dir))
if err != nil {
continue
}

var size int64
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return err
})
if err != nil {
return nil, err
}

spaceUsages = append(spaceUsages, &pb.GetSpaceUsagesResponse_SpaceUsageItem{
Id: int64(id),
Usage: size,
})
}

return &pb.GetSpaceUsagesResponse{SpaceUsages: spaceUsages}, nil
}
4 changes: 4 additions & 0 deletions internal/server/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ func (a *AgentServer) HealthCheck(ctx context.Context, req *pb.HealthCheckReques
Status: "healthy",
}, nil
}

func (a *AgentServer) GetSpaceUsages(ctx context.Context, req *pb.GetSpaceUsagesRequest) (*pb.GetSpaceUsagesResponse, error) {
return clients.NewSpaceUsage(req.DataPath).GetSpaceUsages()
}
11 changes: 11 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Client interface {
ExistDir(req *pb.ExistDirRequest) (*pb.ExistDirResponse, error)
StopAgent(req *pb.StopAgentRequest) (*pb.StopAgentResponse, error)
HealthCheck(req *pb.HealthCheckRequest) (*pb.HealthCheckResponse, error)
GetSpaceUsages(req *pb.GetSpaceUsagesRequest) (*pb.GetSpaceUsagesResponse, error)
}

func genSessionId() string {
Expand Down Expand Up @@ -199,3 +200,13 @@ func (c *client) HealthCheck(req *pb.HealthCheckRequest) (resp *pb.HealthCheckRe

return c.agent.HealthCheck(c.ctx, req)
}

func (c *client) GetSpaceUsages(req *pb.GetSpaceUsagesRequest) (resp *pb.GetSpaceUsagesResponse, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("agent, get space usages failed: %w", err)
}
}()

return c.agent.GetSpaceUsages(c.ctx, req)
}
Loading

0 comments on commit 698d36d

Please sign in to comment.