Skip to content

Commit

Permalink
Update SQL query (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo authored Sep 27, 2024
1 parent c958e28 commit 78f2fee
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions packages/brain/src/modules/apiClients/postgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SELECT pg_total_relation_size('${this.tableName}');
await this.sql.unsafe(`
DO $$
BEGIN
CREATE TYPE ${this.CONSENSUS_CLIENT} AS ENUM('${ConsensusClient.Teku}', '${ConsensusClient.Prysm}', '${ConsensusClient.Lighthouse}', '${ConsensusClient.Nimbus}', '${ConsensusClient.Unknown}');
CREATE TYPE ${this.CONSENSUS_CLIENT} AS ENUM('${ConsensusClient.Teku}', '${ConsensusClient.Lodestar}', '${ConsensusClient.Prysm}', '${ConsensusClient.Lighthouse}', '${ConsensusClient.Nimbus}', '${ConsensusClient.Unknown}');
EXCEPTION
WHEN duplicate_object THEN NULL;
END $$;
Expand All @@ -118,7 +118,7 @@ SELECT pg_total_relation_size('${this.tableName}');
const query = `
-- Create the table if not exists
CREATE TABLE IF NOT EXISTS ${this.tableName} (
${Columns.validatorIndex} BIGINT NOT NULL,
${Columns.validatorIndex} BIGINT NOT NULL,
${Columns.epoch} BIGINT NOT NULL,
${Columns.executionClient} ${this.EXECUTION_CLIENT} NOT NULL,
${Columns.consensusClient} ${this.CONSENSUS_CLIENT} NOT NULL,
Expand Down Expand Up @@ -148,26 +148,17 @@ CREATE TABLE IF NOT EXISTS ${this.tableName} (
}

/**
* Inserts the given performance data into the database.
* Inserts the given performance data into the database. If the data already exists for the given validator index and epoch,
*
* If there is no error, the query will update the record if there is a duplicate key exception.
* If there is an error, it will perform a normal insert without updating the record.
*
* IMPORTANT: there is an assumption where if the error object exist its being assumed that data is nullish!
* this error nullish must be properly updated otherwise the the query will not update the record.
* IMPORTANT: it must be noted that the query will update if the data already exists for the given validator index and epoch.
* If the data exists without an error and the new data has an error, the error will be updated and the other fields will remain the same.
*
* @param data - The performance data to insert.
*/
public async insertPerformanceData(data: ValidatorPerformance): Promise<void> {
// Define the base insert query
let query = `
const query = `
INSERT INTO ${this.tableName} (${Columns.validatorIndex}, ${Columns.epoch}, ${Columns.executionClient}, ${Columns.consensusClient}, ${Columns.slot}, ${Columns.liveness}, ${Columns.blockProposalStatus}, ${Columns.syncCommitteeRewards}, ${Columns.attestationsTotalRewards}, ${Columns.error})
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
`;

// Check if error exists; if not, add the ON CONFLICT clause to update existing records on conflict
if (!data.error) {
query += `
ON CONFLICT (${Columns.validatorIndex}, ${Columns.epoch})
DO UPDATE SET
${Columns.executionClient} = EXCLUDED.${Columns.executionClient},
Expand All @@ -179,7 +170,6 @@ DO UPDATE SET
${Columns.attestationsTotalRewards} = EXCLUDED.${Columns.attestationsTotalRewards},
${Columns.error} = EXCLUDED.${Columns.error};
`;
}

// Execute the query with the appropriate parameters
await this.sql.unsafe(query, [
Expand Down

0 comments on commit 78f2fee

Please sign in to comment.