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

Use types in postgres #364

Merged
merged 3 commits into from
Sep 30, 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
43 changes: 27 additions & 16 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 @@ -233,19 +243,23 @@ WHERE ${Columns.validatorIndex} = ANY($1)
validatorIndexes: string[];
startEpoch: number;
endEpoch: number;
}): Promise<Map<string, ValidatorPerformance[]>> {
}): Promise<Map<number, ValidatorPerformance[]>> {
const query = `
SELECT * FROM ${this.tableName}
WHERE ${Columns.validatorIndex} = ANY($1)
AND ${Columns.epoch} >= $2
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) => {
const result = (await this.sql.unsafe(query, [
validatorIndexes,
startEpoch,
endEpoch
])) as ValidatorPerformancePostgres[];

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

const performanceData = {
validatorIndex: row.validator_index,
epoch: row.epoch,
Expand All @@ -259,14 +273,11 @@ 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[]>());
}, new Map<number, ValidatorPerformance[]>());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/brain/src/modules/validatorsDataIngest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export async function fetchAndProcessValidatorsData({
granularity?: Granularity;
}): Promise<
Map<
string, // validatorIndex
number, // validatorIndex
ValidatorsDataProcessed // processed data of the validator
>
> {
logger.info("Processing validators data");
const mapValidatorPerformance = new Map<string, ValidatorsDataProcessed>();
const mapValidatorPerformance = new Map<number, ValidatorsDataProcessed>();

// Get start timestamp and end timestamp
const endDate = new Date();
Expand Down