Skip to content

Commit

Permalink
Feat: re-adding the storage stats route (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Jun 21, 2023
2 parents 80212f1 + b81ac08 commit 8c06429
Show file tree
Hide file tree
Showing 6 changed files with 702 additions and 102 deletions.
17 changes: 17 additions & 0 deletions proto/canine_chain/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ service Query {
option (google.api.http).get =
"/jackal-dao/canine-chain/storage/price_check/{duration}/{bytes}";
}

// Queries the storage space used and purchased
rpc StorageStats(QueryStorageStatsRequest) returns (QueryStorageStatsResponse) {
option (google.api.http).get =
"/jackal-dao/canine-chain/storage/storage_stats";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down Expand Up @@ -280,4 +286,15 @@ message QueryPriceCheckResponse {
int64 price = 1;
}

message QueryStorageStatsRequest {}

message QueryStorageStatsResponse {
uint64 purchased = 1;
uint64 used = 2;
bytes usedRatio = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
uint64 activeUsers = 4;
}
// this line is used by starport scaffolding # 3
1 change: 1 addition & 0 deletions x/storage/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func GetQueryCmd(_ string) *cobra.Command {

cmd.AddCommand(CmdCheckPrice())
// this line is used by starport scaffolding # 1
cmd.AddCommand(CmdGetStorageStats())

return cmd
}
36 changes: 36 additions & 0 deletions x/storage/client/cli/query_storage_stats.go
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
}
45 changes: 45 additions & 0 deletions x/storage/keeper/grpc_query_storage_stats.go
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
}
Loading

0 comments on commit 8c06429

Please sign in to comment.