diff --git a/backend/pkg/commons/db/migrations/postgres/20241003062452_validator_status_counts.sql b/backend/pkg/commons/db/migrations/postgres/20241003062452_validator_status_counts.sql new file mode 100644 index 000000000..dbb6d2ebc --- /dev/null +++ b/backend/pkg/commons/db/migrations/postgres/20241003062452_validator_status_counts.sql @@ -0,0 +1,11 @@ +-- +goose Up +-- +goose StatementBegin +SELECT 'up SQL query'; +CREATE TABLE IF NOT EXISTS validators_status_counts (status varchar(20) primary key, validator_count int not null); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +SELECT 'down SQL query'; +DROP TABLE IF EXISTS validators_status_counts; +-- +goose StatementEnd diff --git a/backend/pkg/exporter/db/db.go b/backend/pkg/exporter/db/db.go index 0d092c1d1..033d652ea 100644 --- a/backend/pkg/exporter/db/db.go +++ b/backend/pkg/exporter/db/db.go @@ -531,6 +531,8 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie log.Info("updating validator status and metadata") valiudatorUpdateTs := time.Now() + validatorStatusCounts := make(map[string]int) + updates := 0 for _, v := range validators { // exchange farFutureEpoch with the corresponding max sql value @@ -572,6 +574,7 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie if err != nil { log.Error(err, "error saving new validator", 0, map[string]interface{}{"index": v.Index}) } + validatorStatusCounts[v.Status]++ } else { // status = // CASE @@ -617,6 +620,7 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie v.Status = string(constypes.DbActiveOnline) } + validatorStatusCounts[v.Status]++ if c.Status != v.Status { log.Debugf("Status changed for validator %v from %v to %v", v.Index, c.Status, v.Status) log.Debugf("v.ActivationEpoch %v, latestEpoch %v, lastAttestationSlots[v.Index] %v, thresholdSlot %v, lastGlobalAttestedEpoch: %v, lastValidatorAttestedEpoch: %v", v.ActivationEpoch, latestEpoch, lastAttestationSlot, thresholdSlot, lastGlobalAttestedEpoch, lastValidatorAttestedEpoch) @@ -752,9 +756,22 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie return fmt.Errorf("error updating activation epoch balance for validator %v: %w", newValidator.Validatorindex, err) } } - log.Infof("updating validator activation epoch balance completed, took %v", time.Since(s)) + log.Infof("updating validator status counts") + s = time.Now() + _, err = tx.Exec("TRUNCATE TABLE validators_status_counts;") + if err != nil { + return fmt.Errorf("error truncating validators_status_counts table: %w", err) + } + for status, count := range validatorStatusCounts { + _, err = tx.Exec("INSERT INTO validators_status_counts (status, validator_count) VALUES ($1, $2);", status, count) + if err != nil { + return fmt.Errorf("error updating validator status counts: %w", err) + } + } + log.Infof("updating validator status counts completed, took %v", time.Since(s)) + s = time.Now() _, err = tx.Exec("ANALYZE (SKIP_LOCKED) validators;") if err != nil {