Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet Metric #768

Merged
merged 7 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the address isn't very useful considering it never changes and is not something that you can visualise in a meaningful way.

Copy link
Member Author

@peterjan peterjan Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was on the fence when I wrote it. I figured it couldn't hurt but removed it. It wasn't about ever needing to visualise multiple wallets or handle multiple addresses, it was more to ensure we never visualise anything we know not be related to "our wallet".


Confirmed types.Currency `json:"confirmed"`
Spendable types.Currency `json:"spendable"`
Unconfirmed types.Currency `json:"unconfirmed"`
}

WalletMetricsQueryOpts struct {
Address types.Address
}
)

type (
Expand Down
18 changes: 16 additions & 2 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 @@ -2025,9 +2027,9 @@ func (b *bus) metricsHandlerGET(jc jape.Context) {
switch key := jc.PathParam("key"); key {
case api.MetricContract:
var opts api.ContractMetricsQueryOpts
if jc.DecodeForm("fcid", &opts.ContractID) != nil {
if jc.DecodeForm("contractID", &opts.ContractID) != nil {
return
} else if jc.DecodeForm("host", &opts.HostKey) != nil {
} else if jc.DecodeForm("hostKey", &opts.HostKey) != nil {
return
} else if metrics, err := b.metrics(jc.Request.Context(), key, start, n, interval, opts); jc.Check("failed to get contract metrics", err) != nil {
return
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
2 changes: 1 addition & 1 deletion bus/client/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Client) ContractMetrics(ctx context.Context, start time.Time, n uint64,
values.Set("n", fmt.Sprint(n))
values.Set("interval", api.DurationMS(interval).String())
if opts.ContractID != (types.FileContractID{}) {
values.Set("fcid", opts.ContractID.String())
values.Set("contractID", opts.ContractID.String())
}
if opts.HostKey != (types.PublicKey{}) {
values.Set("hostKey", opts.HostKey.String())
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