Skip to content

Commit

Permalink
stores: fix TestRemoveHosts
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Aug 28, 2024
1 parent 4320a76 commit 4062a07
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions stores/sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ func RemoveOfflineHosts(ctx context.Context, tx sql.Tx, minRecentFailures uint64
FROM contracts
INNER JOIN hosts h ON h.id = contracts.host_id
WHERE recent_downtime >= ? AND recent_scan_failures >= ?
`, maxDownTime, minRecentFailures)
`, DurationMS(maxDownTime), minRecentFailures)
if err != nil {
return 0, fmt.Errorf("failed to fetch contracts: %w", err)
}
Expand All @@ -1851,7 +1851,7 @@ func RemoveOfflineHosts(ctx context.Context, tx sql.Tx, minRecentFailures uint64

// delete hosts
res, err := tx.Exec(ctx, "DELETE FROM hosts WHERE recent_downtime >= ? AND recent_scan_failures >= ?",
maxDownTime, minRecentFailures)
DurationMS(maxDownTime), minRecentFailures)
if err != nil {
return 0, fmt.Errorf("failed to delete hosts: %w", err)
}
Expand Down Expand Up @@ -2135,7 +2135,7 @@ func SearchHosts(ctx context.Context, tx sql.Tx, autopilot, filterMode, usabilit
err := rows.Scan(&hostID, &h.KnownSince, &h.LastAnnouncement, (*PublicKey)(&h.PublicKey),
&h.NetAddress, (*PriceTable)(&h.PriceTable.HostPriceTable), &pte,
(*HostSettings)(&h.Settings), &h.Interactions.TotalScans, (*UnixTimeMS)(&h.Interactions.LastScan), &h.Interactions.LastScanSuccess,
&h.Interactions.SecondToLastScanSuccess, &h.Interactions.Uptime, &h.Interactions.Downtime,
&h.Interactions.SecondToLastScanSuccess, (*DurationMS)(&h.Interactions.Uptime), (*DurationMS)(&h.Interactions.Downtime),
&h.Interactions.SuccessfulInteractions, &h.Interactions.FailedInteractions, &h.Interactions.LostSectors,
&h.Scanned, &resolvedAddresses, &h.Blocked,
)
Expand Down
2 changes: 2 additions & 0 deletions stores/sql/sqlite/migrations/main/migration_00017_unix_ms.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
UPDATE hosts SET hosts.last_scan = hosts.last_scan / 1000;
UPDATE hosts SET hosts.uptime = hosts.uptime / 1000;
UPDATE hosts SET hosts.downtime = hosts.downtime / 1000;
UPDATE wallet_events SET wallet_events.timestamp = wallet_events.timestamp / 1000;
28 changes: 28 additions & 0 deletions stores/sql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type (
EncryptionKey object.EncryptionKey
Uint64Str uint64
UnixTimeMS time.Time
DurationMS time.Duration
Unsigned64 uint64
)

Expand All @@ -65,6 +66,7 @@ var (
_ scannerValuer = (*PublicKey)(nil)
_ scannerValuer = (*EncryptionKey)(nil)
_ scannerValuer = (*UnixTimeMS)(nil)
_ scannerValuer = (*DurationMS)(nil)
_ scannerValuer = (*Unsigned64)(nil)
)

Expand Down Expand Up @@ -340,6 +342,32 @@ func (u UnixTimeMS) Value() (driver.Value, error) {
return time.Time(u).UnixMilli(), nil
}

// Scan scan value into DurationMS, implements sql.Scanner interface.
func (d *DurationMS) Scan(value interface{}) error {
var msec int64
var err error
switch value := value.(type) {
case int64:
msec = value
case []uint8:
msec, err = strconv.ParseInt(string(value), 10, 64)
if err != nil {
return fmt.Errorf("failed to unmarshal DurationMS value: %v %T", value, value)
}
default:
return fmt.Errorf("failed to unmarshal DurationMS value: %v %T", value, value)
}

*d = DurationMS(msec) * DurationMS(time.Millisecond)
return nil
}

// Value returns a int64 value representing a duration in milliseconds,
// implements driver.Valuer interface.
func (d DurationMS) Value() (driver.Value, error) {
return time.Duration(d).Milliseconds(), nil
}

// Scan scan value into Uint64, implements sql.Scanner interface.
func (u *Uint64Str) Scan(value interface{}) error {
var s string
Expand Down

0 comments on commit 4062a07

Please sign in to comment.