Skip to content

Commit

Permalink
adding 200 year stats (#417)
Browse files Browse the repository at this point in the history
* adding 200 year stats

* keeping track of users on the 200 year plan
  • Loading branch information
TheMarstonConnell authored Feb 22, 2024
1 parent 8966fe3 commit 4d3ddc3
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 152 deletions.
9 changes: 5 additions & 4 deletions proto/canine_chain/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,14 @@ message QueryActiveProvidersResponse {
message QueryStorageStatsRequest {}

message QueryStorageStatsResponse {
uint64 purchased = 1;
uint64 used = 2;
int64 purchased = 1;
int64 used = 2;
bytes usedRatio = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
uint64 activeUsers = 4;
uint64 uniqueUsers = 5;
int64 activeUsers = 4;
int64 uniqueUsers = 5;
int64 permUsed = 6;
}
// this line is used by starport scaffolding # 3
18 changes: 18 additions & 0 deletions x/storage/keeper/active_deals.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,21 @@ func (k Keeper) GetAllActiveDeals(ctx sdk.Context) (list []types.ActiveDeals) {

return
}

// IterateActiveDeals runs `fn` for each active deal in the store
func (k Keeper) IterateActiveDeals(ctx sdk.Context, fn func(deal types.ActiveDeals) bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ActiveDealsKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.ActiveDeals
k.cdc.MustUnmarshal(iterator.Value(), &val)

if fn(val) {
return
}

}
}
40 changes: 32 additions & 8 deletions x/storage/keeper/grpc_query_storage_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v3/x/storage/types"
Expand All @@ -20,29 +21,52 @@ func (k Keeper) StorageStats(c context.Context, req *types.QueryStorageStatsRequ

var spacePurchased int64
var spaceUsed int64
var activeUsers uint64
var totalUsers uint64

totalUsers := make(map[string]bool)

for _, info := range payment {
totalUsers++ // counting in total users
totalUsers[info.Address] = true // counting in total users
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)

users := make(map[string]bool)

var permSize int64
k.IterateActiveDeals(ctx, func(deal types.ActiveDeals) bool {
users[deal.Creator] = true
totalUsers[deal.Creator] = true

if deal.Endblock == "0" {
return false
}

s := deal.Filesize
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
ctx.Logger().Debug("cannot parse active deal")
return false
}

permSize += i

return false
})

return &types.QueryStorageStatsResponse{
Purchased: uint64(spacePurchased),
Used: uint64(spaceUsed),
Purchased: spacePurchased,
Used: spaceUsed,
UsedRatio: ratio,
ActiveUsers: activeUsers,
UniqueUsers: totalUsers,
ActiveUsers: int64(len(users)),
UniqueUsers: int64(len(totalUsers)),
PermUsed: permSize,
}, nil
}
Loading

0 comments on commit 4d3ddc3

Please sign in to comment.