Skip to content

Commit

Permalink
use types in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo committed Sep 30, 2024
1 parent 78f2fee commit 1a70adf
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions packages/brain/src/modules/apiClients/postgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ enum Columns {
error = "error"
}

interface ValidatorPerformancePostgres {
[Columns.validatorIndex]: number;
[Columns.epoch]: number;
[Columns.executionClient]: ExecutionClient;
[Columns.consensusClient]: ConsensusClient;
[Columns.slot]: number;
[Columns.liveness]: boolean;
[Columns.blockProposalStatus]: BlockProposalStatus;
[Columns.syncCommitteeRewards]: number;
[Columns.attestationsTotalRewards]: string;
[Columns.error]: string;
}

export class PostgresClient {
private readonly tableName = "validators_performance";
private readonly BLOCK_PROPOSAL_STATUS = "BLOCK_PROPOSAL_STATUS";
Expand Down Expand Up @@ -114,7 +127,6 @@ SELECT pg_total_relation_size('${this.tableName}');
END $$;
`);

// TODO: implement with an object the columns && nullish check of each column
const query = `
-- Create the table if not exists
CREATE TABLE IF NOT EXISTS ${this.tableName} (
Expand Down Expand Up @@ -199,10 +211,8 @@ SELECT * FROM ${this.tableName}
WHERE ${Columns.validatorIndex} = ANY($1)
`;

const result = await this.sql.unsafe(query, [validatorIndexes]);
// TODO: add type to result
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return result.map((row: any) => ({
const result = (await this.sql.unsafe(query, [validatorIndexes])) as ValidatorPerformancePostgres[];
return result.map((row: ValidatorPerformancePostgres) => ({
validatorIndex: row.validator_index,
epoch: row.epoch,
executionClient: row.execution_client,
Expand Down Expand Up @@ -242,10 +252,16 @@ AND ${Columns.epoch} <= $3
`;

const result = await this.sql.unsafe(query, [validatorIndexes, startEpoch, endEpoch]);
// TODO: add type to result
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return result.reduce((map: Map<string, ValidatorPerformance[]>, row: any) => {

return result.reduce((map: Map<string, ValidatorPerformance[]>, row) => {
const key = row.validator_index;

// print type ofs
logger.debug(`typeof row.validator_index: ${typeof row.validator_index}`);
logger.debug(`typeof row.epoch: ${typeof row.epoch}`);
logger.debug(`typeof row.execution_client: ${typeof row.execution_client}`);
logger.debug(`typeof row.consensus_client: ${typeof row.consensus_client}`);

const performanceData = {
validatorIndex: row.validator_index,
epoch: row.epoch,
Expand All @@ -259,11 +275,8 @@ AND ${Columns.epoch} <= $3
error: JSON.parse(row.error)
};

if (map.has(key)) {
map.get(key)?.push(performanceData);
} else {
map.set(key, [performanceData]);
}
if (map.has(key)) map.get(key)?.push(performanceData);
else map.set(key, [performanceData]);

return map;
}, new Map<string, ValidatorPerformance[]>());
Expand Down

0 comments on commit 1a70adf

Please sign in to comment.