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 all 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
11 changes: 11 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,16 @@ type (
ContractID types.FileContractID
HostKey types.PublicKey
}

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

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

WalletMetricsQueryOpts struct{}
Copy link
Member Author

Choose a reason for hiding this comment

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

kept this for consistency and extensibility

)

type (
Expand Down
16 changes: 14 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,14 @@ func (b *bus) metricsHandlerGET(jc jape.Context) {
jc.Encode(metrics)
return
}
case api.MetricWallet:
var opts api.WalletMetricsQueryOpts
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 +2083,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
15 changes: 14 additions & 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 Expand Up @@ -71,6 +71,19 @@ func (c *Client) ContractSetMetrics(ctx context.Context, start time.Time, n uint
return resp, nil
}

func (c *Client) WalletMetrics(ctx context.Context, start time.Time, n uint64, interval time.Duration, opts api.WalletMetricsQueryOpts) ([]api.WalletMetric, error) {
values := url.Values{}
values.Set("start", api.TimeRFC3339(start).String())
values.Set("n", fmt.Sprint(n))
values.Set("interval", api.DurationMS(interval).String())

var resp []api.WalletMetric
if err := c.metric(ctx, api.MetricWallet, values, &resp); err != nil {
return nil, err
}
return resp, nil
}

func (c *Client) RecordContractSetChurnMetric(ctx context.Context, metrics ...api.ContractSetChurnMetric) error {
return c.c.WithContext(ctx).PUT(fmt.Sprintf("/metric/%s", api.MetricContractSetChurn), api.ContractSetChurnMetricRequestPUT{
Metrics: metrics,
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
Loading