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 batch save #32

Merged
merged 1 commit into from
Apr 19, 2024
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
1 change: 1 addition & 0 deletions cmd/core/data-api-backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var backfillDataAPICmd = &cobra.Command{
}
}

log.Infof("Relayscan %s", vars.Version)
log.Infof("Using %d relays", len(relays))
for index, relay := range relays {
log.Infof("relay #%d: %s", index+1, relay.Hostname())
Expand Down
18 changes: 16 additions & 2 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,25 @@ func (s *DatabaseService) SaveDataAPIPayloadDeliveredBatch(entries []*DataAPIPay
if len(entries) == 0 {
return nil
}

query := `INSERT INTO ` + TableDataAPIPayloadDelivered + `
(relay, epoch, slot, parent_hash, block_hash, builder_pubkey, proposer_pubkey, proposer_fee_recipient, gas_limit, gas_used, value_claimed_wei, value_claimed_eth, num_tx, block_number, extra_data) VALUES
(:relay, :epoch, :slot, :parent_hash, :block_hash, :builder_pubkey, :proposer_pubkey, :proposer_fee_recipient, :gas_limit, :gas_used, :value_claimed_wei, :value_claimed_eth, :num_tx, :block_number, :extra_data)
ON CONFLICT DO NOTHING`
_, err := s.DB.NamedExec(query, entries)
return err

// Postgres can do max 65535 parameters at a time (otherwise error: "pq: got ... parameters but PostgreSQL only supports 65535 parameters")
for i := 0; i < len(entries); i += 3000 {
end := i + 3000
if end > len(entries) {
end = len(entries)
}

_, err := s.DB.NamedExec(query, entries[i:end])
if err != nil {
return err
}
}
return nil
}

func (s *DatabaseService) GetDataAPILatestPayloadDelivered(relay string) (*DataAPIPayloadDeliveredEntry, error) {
Expand Down Expand Up @@ -116,6 +129,7 @@ func (s *DatabaseService) GetDataAPILatestBid(relay string) (*DataAPIBuilderBidE
}

func (s *DatabaseService) GetTopRelays(since, until time.Time) (res []*TopRelayEntry, err error) {
// slot_start =slotToTime
query := `SELECT relay, count(relay) as payloads FROM ` + TableDataAPIPayloadDelivered + ` WHERE inserted_at > $1 AND inserted_at < $2 GROUP BY relay ORDER BY payloads DESC;`
err = s.DB.Select(&res, query, since.UTC(), until.UTC())
return res, err
Expand Down
Loading