Skip to content

Commit

Permalink
stores: add wallet metric
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Nov 28, 2023
1 parent dc05019 commit e9e7f5d
Show file tree
Hide file tree
Showing 14 changed files with 606 additions and 361 deletions.
15 changes: 15 additions & 0 deletions api/metrcis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
MetricContractSet = "contractset"
MetricContractSetChurn = "churn"
MetricContract = "contract"
MetricWallet = "wallet"
)

type (
Expand Down Expand Up @@ -75,6 +76,20 @@ type (
ContractID types.FileContractID
HostKey types.PublicKey
}

WalletMetric struct {
Timestamp time.Time `json:"timestamp"`

Address types.Address `json:"address"`

ConfirmedBalance types.Currency `json:"confirmedBalance"`
SpendableBalance types.Currency `json:"spendableBalance"`
UnconfirmedBalance types.Currency `json:"unconfirmedBalance"`
}

WalletMetricsQueryOpts struct {
Address types.Address
}
)

type (
Expand Down
14 changes: 14 additions & 0 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ type (

ContractSetChurnMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.ContractSetChurnMetricsQueryOpts) ([]api.ContractSetChurnMetric, error)
RecordContractSetChurnMetric(ctx context.Context, metrics ...api.ContractSetChurnMetric) error

WalletMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.WalletMetricsQueryOpts) ([]api.WalletMetric, error)
}
)

Expand Down Expand Up @@ -2059,6 +2061,16 @@ func (b *bus) metricsHandlerGET(jc jape.Context) {
jc.Encode(metrics)
return
}
case api.MetricWallet:
var opts api.WalletMetricsQueryOpts
if jc.DecodeForm("address", &opts.Address) != nil {
return
} else if metrics, err := b.metrics(jc.Request.Context(), key, start, n, interval, opts); jc.Check("failed to get wallet metrics", err) != nil {
return
} else {
jc.Encode(metrics)
return
}
default:
jc.Error(fmt.Errorf("unknown metric '%s'", key), http.StatusBadRequest)
return
Expand All @@ -2073,6 +2085,8 @@ func (b *bus) metrics(ctx context.Context, key string, start time.Time, n uint64
return b.mtrcs.ContractSetMetrics(ctx, start, n, interval, opts.(api.ContractSetMetricsQueryOpts))
case api.MetricContractSetChurn:
return b.mtrcs.ContractSetChurnMetrics(ctx, start, n, interval, opts.(api.ContractSetChurnMetricsQueryOpts))
case api.MetricWallet:
return b.mtrcs.WalletMetrics(ctx, start, n, interval, opts.(api.WalletMetricsQueryOpts))
}
return nil, nil
}
Expand Down
3 changes: 3 additions & 0 deletions internal/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func NewBus(cfg BusConfig, dir string, seed types.PrivateKey, l *zap.Logger) (ht

w := wallet.NewSingleAddressWallet(seed, sqlStore, cfg.UsedUTXOExpiry, zap.NewNop().Sugar())
tp.TransactionPoolSubscribe(w)
if err := cs.ConsensusSetSubscribe(w, modules.ConsensusChangeRecent, nil); err != nil {
return nil, nil, err
}

if m := cfg.Miner; m != nil {
if err := cs.ConsensusSetSubscribe(m, ccid, nil); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion stores/hostdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ func (s *SQLStore) addCustomTestHost(hk types.PublicKey, na string) error {
announcement: hostdb.Announcement{NetAddress: na},
}}...)
s.lastSave = time.Now().Add(s.persistInterval * -2)
return s.applyUpdates(false)
return s.applyUpdates(false, false)
}

// hosts returns all hosts in the db. Only used in testing since preloading all
Expand Down
Loading

0 comments on commit e9e7f5d

Please sign in to comment.