Skip to content

Commit

Permalink
Added GetDatastoreSize
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Feb 22, 2024
1 parent afee350 commit 9a171d8
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 2 deletions.
5 changes: 4 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ func (bl *FxBlockchain) serve(w http.ResponseWriter, r *http.Request) {
actionGetFolderSize: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
bl.handleGetFolderSize(r.Context(), from, w, r)
},
actionGetDatastoreSize: func(from peer.ID, w http.ResponseWriter, r *http.Request) {
bl.handleGetDatastoreSize(r.Context(), from, w, r)
},
}

// Look up the function in the map and call it
Expand Down Expand Up @@ -677,7 +680,7 @@ func (bl *FxBlockchain) authorized(pid peer.ID, action string) bool {
return true
}
switch action {
case actionBloxFreeSpace, actionAccountFund, actionAssetsBalance, actionGetFolderSize, actionFetchContainerLogs, actionEraseBlData, actionWifiRemoveall, actionReboot, actionPartition, actionDeleteWifi, actionDisconnectWifi, actionDeleteFulaConfig, actionGetAccount, actionSeeded, actionAccountExists, actionPoolCreate, actionPoolJoin, actionPoolCancelJoin, actionPoolRequests, actionPoolList, actionPoolVote, actionPoolLeave, actionManifestUpload, actionManifestStore, actionManifestAvailable, actionManifestRemove, actionManifestRemoveStorer, actionManifestRemoveStored:
case actionBloxFreeSpace, actionAccountFund, actionAssetsBalance, actionGetDatastoreSize, actionGetFolderSize, actionFetchContainerLogs, actionEraseBlData, actionWifiRemoveall, actionReboot, actionPartition, actionDeleteWifi, actionDisconnectWifi, actionDeleteFulaConfig, actionGetAccount, actionSeeded, actionAccountExists, actionPoolCreate, actionPoolJoin, actionPoolCancelJoin, actionPoolRequests, actionPoolList, actionPoolVote, actionPoolLeave, actionManifestUpload, actionManifestStore, actionManifestAvailable, actionManifestRemove, actionManifestRemoveStorer, actionManifestRemoveStored:
bl.authorizedPeersLock.RLock()
_, ok := bl.authorizedPeers[pid]
bl.authorizedPeersLock.RUnlock()
Expand Down
66 changes: 66 additions & 0 deletions blockchain/blox.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,43 @@ func (bl *FxBlockchain) GetFolderSize(ctx context.Context, to peer.ID, r wifi.Ge
}
}

func (bl *FxBlockchain) GetDatastoreSize(ctx context.Context, to peer.ID, r wifi.GetDatastoreSizeRequest) ([]byte, error) {

if bl.allowTransientConnection {
ctx = network.WithUseTransient(ctx, "fx.blockchain")
}

var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(r); err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+to.String()+".invalid/"+actionGetDatastoreSize, &buf)
if err != nil {
return nil, err
}
resp, err := bl.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
switch {
case err != nil:
return nil, err
case resp.StatusCode != http.StatusAccepted:
// Attempt to parse the body as JSON.
if jsonErr := json.Unmarshal(b, &apiError); jsonErr != nil {
// If we can't parse the JSON, return the original body in the error.
return nil, fmt.Errorf("unexpected response: %d %s", resp.StatusCode, string(b))
}
// Return the parsed error message and description.
return nil, fmt.Errorf("unexpected response: %d %s - %s", resp.StatusCode, apiError.Message, apiError.Description)
default:
return b, nil
}
}

func (bl *FxBlockchain) DeleteWifi(ctx context.Context, to peer.ID, r wifi.DeleteWifiRequest) ([]byte, error) {

if bl.allowTransientConnection {
Expand Down Expand Up @@ -428,3 +465,32 @@ func (bl *FxBlockchain) handleGetFolderSize(ctx context.Context, from peer.ID, w
}

}

func (bl *FxBlockchain) handleGetDatastoreSize(ctx context.Context, from peer.ID, w http.ResponseWriter, r *http.Request) {
log := log.With("action", actionGetDatastoreSize, "from", from)

// Parse the JSON body of the request into the DeleteWifiRequest struct
var req wifi.GetDatastoreSizeRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
log.Error("failed to decode request: %v", err)
http.Error(w, "failed to decode request", http.StatusBadRequest)
return
}
log.Debugw("handleGetDatastoreSize received", "req", req)

out := wifi.GetDatastoreSizeResponse{}
res, err := wifi.GetDatastoreSize(ctx, req)
if err != nil {
out = wifi.GetDatastoreSizeResponse{}
} else {
out = res
}
log.Debugw("handleGetDatastoreSize response", "out", out)
w.WriteHeader(http.StatusAccepted)
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Error("failed to write response: %v", err)
http.Error(w, "failed to write response", http.StatusInternalServerError)
return
}

}
4 changes: 4 additions & 0 deletions blockchain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
actionGetAccount = "get-account"
actionFetchContainerLogs = "fetch-container-logs"
actionGetFolderSize = "get-folder-size"
actionGetDatastoreSize = "get-datastore-size"
)

type LinkWithLimit struct {
Expand Down Expand Up @@ -358,6 +359,7 @@ type Blockchain interface {
GetAccount(context.Context, peer.ID) ([]byte, error)
FetchContainerLogs(context.Context, peer.ID, wifi.FetchContainerLogsRequest) ([]byte, error)
GetFolderSize(context.Context, peer.ID, wifi.GetFolderSizeRequest) ([]byte, error)
GetDatastoreSize(context.Context, peer.ID, wifi.GetDatastoreSizeRequest) ([]byte, error)
}

var requestTypes = map[string]reflect.Type{
Expand Down Expand Up @@ -396,6 +398,7 @@ var requestTypes = map[string]reflect.Type{
actionGetAccount: reflect.TypeOf(GetAccountRequest{}),
actionFetchContainerLogs: reflect.TypeOf(wifi.FetchContainerLogsRequest{}),
actionGetFolderSize: reflect.TypeOf(wifi.GetFolderSizeRequest{}),
actionGetDatastoreSize: reflect.TypeOf(wifi.GetDatastoreSizeRequest{}),
}

var responseTypes = map[string]reflect.Type{
Expand Down Expand Up @@ -434,4 +437,5 @@ var responseTypes = map[string]reflect.Type{
actionGetAccount: reflect.TypeOf(GetAccountResponse{}),
actionFetchContainerLogs: reflect.TypeOf(wifi.FetchContainerLogsResponse{}),
actionGetFolderSize: reflect.TypeOf(wifi.GetFolderSizeResponse{}),
actionGetDatastoreSize: reflect.TypeOf(wifi.GetDatastoreSizeResponse{}),
}
2 changes: 1 addition & 1 deletion cmd/blox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Child struct {

var (
logger = logging.Logger("fula/cmd/blox")
baseURL = "http://localhost:5002"
baseURL = "http://localhost:5001"
app struct {
cli.App
initOnly bool
Expand Down
5 changes: 5 additions & 0 deletions mobile/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,8 @@ func (c *Client) GetFolderSize(folderPath string) ([]byte, error) {
ctx := context.TODO()
return c.bl.GetFolderSize(ctx, c.bloxPid, wifi.GetFolderSizeRequest{FolderPath: folderPath})
}

func (c *Client) GetDatastoreSize() ([]byte, error) {
ctx := context.TODO()
return c.bl.GetDatastoreSize(ctx, c.bloxPid, wifi.GetDatastoreSizeRequest{})
}
67 changes: 67 additions & 0 deletions wap/pkg/wifi/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os/exec"
Expand All @@ -14,7 +15,9 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/ipfs/kubo/client/rpc"
"github.com/libp2p/go-libp2p/core/crypto"
ma "github.com/multiformats/go-multiaddr"
"golang.org/x/crypto/ed25519"
)

Expand All @@ -39,6 +42,25 @@ type GetFolderSizeResponse struct {
SizeInBytes string `json:"size"`
}

type GetDatastoreSizeRequest struct {
}

type GetDatastoreSizeResponse struct {
RepoSize string `json:"size"`
StorageMax string `json:"storage_max"`
NumObjects string `json:"count"`
RepoPath string `json:"folder_path"`
Version string `json:"version"`
}

type getDatastoreSizeResponseLocal struct {
RepoSize int64
StorageMax int64
NumObjects int
RepoPath string
Version string
}

type FetchContainerLogsResponse struct {
Status bool `json:"status"`
Msg string `json:"msg"`
Expand Down Expand Up @@ -138,6 +160,51 @@ func GetFolderSize(ctx context.Context, req GetFolderSizeRequest) (GetFolderSize
}, nil
}

func GetDatastoreSize(ctx context.Context, req GetDatastoreSizeRequest) (GetDatastoreSizeResponse, error) {
nodeMultiAddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5001")
if err != nil {
return GetDatastoreSizeResponse{}, fmt.Errorf("invalid multiaddress: %w", err)
}
node, err := rpc.NewApi(nodeMultiAddr)
if err != nil {
return GetDatastoreSizeResponse{}, fmt.Errorf("error creating API client: %v", err)
}

stat, err := node.Request("repo/stat").Send(ctx)
if err != nil {
return GetDatastoreSizeResponse{}, fmt.Errorf("error executing api command: %v", err)
}

if stat.Output != nil {
defer stat.Output.Close() // Ensure the output is closed after reading

data, err := io.ReadAll(stat.Output)
if err != nil {
return GetDatastoreSizeResponse{}, fmt.Errorf("error reading response output: %v", err)
}
log.Debugw("GetDatastoreSize response received", "data", string(data))
var response getDatastoreSizeResponseLocal
err = json.Unmarshal(data, &response)
if err != nil {
return GetDatastoreSizeResponse{}, fmt.Errorf("error unmarshaling response data: %v", err)
}
responseString := GetDatastoreSizeResponse{
RepoSize: fmt.Sprint(response.RepoSize),
StorageMax: fmt.Sprint(response.StorageMax),
NumObjects: fmt.Sprint(response.NumObjects),
RepoPath: response.RepoPath,
Version: response.Version,
}

// Optionally, print the response data for debugging
fmt.Println("Response data:", response)

return responseString, nil
} else {
return GetDatastoreSizeResponse{}, fmt.Errorf("no output received")
}
}

func GetBloxFreeSpace() (BloxFreeSpaceResponse, error) {
cmd := `df -B1 2>/dev/null | grep -nE '/storage/(usb|sd[a-z]|nvme)' | awk '{sum2+=$2; sum3+=$3; sum4+=$4; sum5+=$5} END { print NR "," sum2 "," sum3 "," sum4 "," sum5}'`
out, err := exec.Command("sh", "-c", cmd).Output()
Expand Down

0 comments on commit 9a171d8

Please sign in to comment.