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

Fix aggregate spending metrics not respecting time constraints #807

Merged
merged 2 commits into from
Dec 8, 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
12 changes: 6 additions & 6 deletions internal/testing/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2245,16 +2245,16 @@ func TestBusRecordedMetrics(t *testing.T) {
t.Fatalf("expected 1 metric, got %v", len(cMetrics))
} else if m := cMetrics[0]; m.Timestamp.Std().Before(startTime) {
t.Fatalf("expected time to be after start time %v, got %v", startTime, m.Timestamp.Std())
} else if m.ContractID == (types.FileContractID{}) {
t.Fatal("expected non-zero FCID")
} else if m.HostKey == (types.PublicKey{}) {
t.Fatal("expected non-zero Host")
} else if m.ContractID != (types.FileContractID{}) {
t.Fatal("expected zero FCID")
} else if m.HostKey != (types.PublicKey{}) {
t.Fatal("expected zero Host")
} else if m.RemainingCollateral == (types.Currency{}) {
t.Fatal("expected non-zero RemainingCollateral")
} else if m.RemainingFunds == (types.Currency{}) {
t.Fatal("expected non-zero RemainingFunds")
} else if m.RevisionNumber == 0 {
t.Fatal("expected non-zero RevisionNumber")
} else if m.RevisionNumber != 0 {
t.Fatal("expected zero RevisionNumber")
} else if !m.UploadSpending.IsZero() {
t.Fatal("expected zero UploadSpending")
} else if !m.DownloadSpending.IsZero() {
Expand Down
16 changes: 9 additions & 7 deletions stores/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,6 @@ func (m dbContractMetric) Aggregate(o dbContractMetric) (out dbContractMetric) {
listSpendingLo, carry := bits.Add64(uint64(m.ListSpendingLo), uint64(o.ListSpendingLo), 0)
listSpendingHi, _ := bits.Add64(uint64(m.ListSpendingHi), uint64(o.ListSpendingHi), carry)

out.FCID = fileContractID{}
out.Host = publicKey{}
out.RevisionNumber = 0

out.RemainingCollateralLo = unsigned64(remainingCollateralLo)
out.RemainingCollateralHi = unsigned64(remainingCollateralHi)
out.RemainingFundsLo = unsigned64(remainingFundsLo)
Expand Down Expand Up @@ -393,7 +389,7 @@ func (s *SQLStore) contractMetrics(ctx context.Context, start time.Time, n uint6
if opts.ContractID == (types.FileContractID{}) && opts.HostKey == (types.PublicKey{}) {
// if neither contract nor host filters were set, we return the
// aggregate spending for each period
metrics, err = s.findAggregatedContractPeriods(start, interval)
metrics, err = s.findAggregatedContractPeriods(start, n, interval)
} else {
// otherwise we return the first metric for each period like we usually
// do
Expand Down Expand Up @@ -487,7 +483,8 @@ func roundPeriodExpr(db *gorm.DB, start time.Time, interval time.Duration) claus
}
}

func (s *SQLStore) findAggregatedContractPeriods(start time.Time, interval time.Duration) ([]dbContractMetric, error) {
func (s *SQLStore) findAggregatedContractPeriods(start time.Time, n uint64, interval time.Duration) ([]dbContractMetric, error) {
end := start.Add(time.Duration(n) * interval)
var metricsWithPeriod []struct {
Metric dbContractMetric `gorm:"embedded"`
Period int64
Expand All @@ -498,16 +495,21 @@ func (s *SQLStore) findAggregatedContractPeriods(start time.Time, interval time.
INNER JOIN (
SELECT fcid, MIN(timestamp) as timestamp, ? AS Period
FROM contracts
WHERE timestamp >= ? AND timestamp < ?
GROUP BY Period, fcid) i
ON contracts.fcid = i.fcid AND contracts.timestamp = i.timestamp
`, roundPeriodExpr(s.dbMetrics, start, interval)).
`, roundPeriodExpr(s.dbMetrics, start, interval),
unixTimeMS(start), unixTimeMS(end)).
Scan(&metricsWithPeriod).
Error
if err != nil {
return nil, fmt.Errorf("failed to fetch aggregate metrics: %w", err)
}
var metrics []dbContractMetric
for _, m := range metricsWithPeriod {
m.Metric.FCID = fileContractID{}
m.Metric.Host = publicKey{}
m.Metric.RevisionNumber = 0
if m.Period != currentPeriod {
metrics = append(metrics, m.Metric)
currentPeriod = m.Period
Expand Down