-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: re-adding the storage stats route (#344)
- Loading branch information
Showing
6 changed files
with
702 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/jackalLabs/canine-chain/x/storage/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CmdGetStorageStats() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "get-storage-stats", | ||
Short: "lists stats about storage on the network", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx := client.GetClientContextFromCmd(cmd) | ||
|
||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
params := &types.QueryStorageStatsRequest{} | ||
|
||
res, err := queryClient.StorageStats(context.Background(), params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return clientCtx.PrintProto(res) | ||
}, | ||
} | ||
|
||
flags.AddPaginationFlagsToCmd(cmd, cmd.Use) | ||
flags.AddQueryFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/jackalLabs/canine-chain/x/storage/types" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func (k Keeper) StorageStats(c context.Context, req *types.QueryStorageStatsRequest) (*types.QueryStorageStatsResponse, error) { | ||
if req == nil { | ||
return nil, status.Error(codes.InvalidArgument, "invalid request") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
|
||
payment := k.GetAllStoragePaymentInfo(ctx) | ||
|
||
var spacePurchased int64 | ||
var spaceUsed int64 | ||
var activeUsers uint64 | ||
|
||
for _, info := range payment { | ||
if info.End.Before(ctx.BlockTime()) { | ||
continue | ||
} | ||
spacePurchased += info.SpaceAvailable | ||
spaceUsed += info.SpaceUsed | ||
activeUsers++ | ||
} | ||
|
||
decSpent := sdk.NewDec(spacePurchased) | ||
decUsed := sdk.NewDec(spaceUsed) | ||
|
||
ratio := decUsed.Quo(decSpent).MulInt64(100) | ||
|
||
return &types.QueryStorageStatsResponse{ | ||
Purchased: uint64(spacePurchased), | ||
Used: uint64(spaceUsed), | ||
UsedRatio: ratio, | ||
ActiveUsers: activeUsers, | ||
}, nil | ||
} |
Oops, something went wrong.