Skip to content

Commit

Permalink
return object instead of Map in postgres client
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo committed Oct 18, 2024
1 parent 4f2346d commit 33b2330
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 57 deletions.
12 changes: 6 additions & 6 deletions packages/brain/src/modules/apiClients/postgres/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import postgres from "postgres";
import logger from "../../logger/index.js";
import { EpochsValidatorsMap, DataPerEpoch, Columns, ValidatorsDataPerEpochMap, PostgresDataRow } from "./types.js";
import { DataPerEpoch, Columns, ValidatorsDataPerEpochMap, PostgresDataRow, EpochsValidatorsData } from "./types.js";

export class PostgresClient {
private readonly tableName = "epochs_data";
Expand Down Expand Up @@ -99,7 +99,7 @@ ${Columns.error} = EXCLUDED.${Columns.error}
validatorIndexes: string[];
startEpoch: number;
endEpoch: number;
}): Promise<EpochsValidatorsMap> {
}): Promise<EpochsValidatorsData> {
const query = `
SELECT * FROM ${this.tableName}
WHERE ${Columns.validatorindex} = ANY($1)
Expand All @@ -109,7 +109,7 @@ AND ${Columns.epoch} <= $3

const result: PostgresDataRow[] = await this.sql.unsafe(query, [validatorIndexes, startEpoch, endEpoch]);

const epochsValidatorsMap: EpochsValidatorsMap = new Map();
const epochsValidatorData: EpochsValidatorsData = Object.create(null);

for (const row of result) {
const { validatorindex, epoch, clients, attestation, block, synccommittee, slot, error } = row;
Expand All @@ -124,12 +124,12 @@ AND ${Columns.epoch} <= $3
[Columns.error]: error
};

if (!epochsValidatorsMap.has(epochNumber)) epochsValidatorsMap.set(epochNumber, new Map());
if (!epochsValidatorData[epochNumber]) epochsValidatorData[epochNumber] = Object.create(null);

epochsValidatorsMap.get(epochNumber)!.set(validatorIndexNumber, data);
epochsValidatorData[epochNumber][validatorIndexNumber] = data;
}

return epochsValidatorsMap;
return epochsValidatorData;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions packages/brain/src/modules/apiClients/postgres/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export enum Columns {
error = "error"
}

// Indexed by epoch number
export type EpochsValidatorsMap = Map<number, ValidatorsDataPerEpochMap>;
export interface EpochsValidatorsData {
[epoch: number]: {
[validatorIndex: number]: DataPerEpoch;
};
}

// Indexed by validator index
export type ValidatorsDataPerEpochMap = Map<number, DataPerEpoch>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import { RequestParsed } from "./types.js";
import { BrainDataBase } from "../../../../db/index.js";
import { PostgresClient } from "../../../../apiClients/index.js";
import { Tag, tags } from "@stakingbrain/common";
import { DataPerEpoch, ValidatorsDataPerEpochMap } from "../../../../apiClients/types.js";
import { DataPerEpoch } from "../../../../apiClients/types.js";
import { StakingBrainDb } from "../../../../db/types.js";

interface EpochsTagsValidatorsData {
[epoch: number]: {
[tag: string]: {
[validatorIndex: number]: DataPerEpoch;
};
};
}

export const createIndexerEpochsRouter = ({
postgresClient,
brainDb
Expand All @@ -31,10 +39,14 @@ export const createIndexerEpochsRouter = ({
return;
}

const tagsEpochsMap = await getTagsEpochsMap({ postgresClient, validatorsTagIndexesMap, start, end });
const epochsTagsValidatorsData = await getEpochsTagsValidatorsData({
postgresClient,
validatorsTagIndexesMap,
start,
end
});

// serialize the map to an object
res.send(convertMapIntoObject(tagsEpochsMap));
res.send(epochsTagsValidatorsData);
} catch (e) {
logger.error(e);
res.status(500).send({ message: "Internal server error" });
Expand Down Expand Up @@ -79,7 +91,7 @@ function getValidatorsTagsIndexesMap({
return validatorsTagIndexesMap;
}

async function getTagsEpochsMap({
async function getEpochsTagsValidatorsData({
postgresClient,
validatorsTagIndexesMap,
start,
Expand All @@ -89,53 +101,26 @@ async function getTagsEpochsMap({
validatorsTagIndexesMap: Map<Tag, number[]>;
start: number;
end: number;
}): Promise<Map<number, Map<Tag, ValidatorsDataPerEpochMap>>> {
const tagsEpochsMap: Map<number, Map<Tag, ValidatorsDataPerEpochMap>> = new Map();
}): Promise<EpochsTagsValidatorsData> {
const epochsTagsValidatorsData: EpochsTagsValidatorsData = Object.create(null);

// Get epochs data for each tag
for (const [tag, indexes] of validatorsTagIndexesMap) {
const epochsValidatorsMap = await postgresClient.getEpochsDataMapForEpochRange({
const epochsValidatorsData = await postgresClient.getEpochsDataMapForEpochRange({
validatorIndexes: indexes.map((index) => index.toString()),
startEpoch: start,
endEpoch: end
});

for (const [epoch, validatorsDataMap] of epochsValidatorsMap) {
if (!tagsEpochsMap.has(epoch)) tagsEpochsMap.set(epoch, new Map());
// Add the data to the object
for (const [epoch, data] of Object.entries(epochsValidatorsData)) {
const epochNumber = parseInt(epoch);

tagsEpochsMap.get(epoch)!.set(tag, validatorsDataMap);
}
}

return tagsEpochsMap;
}
if (!epochsTagsValidatorsData[epochNumber]) epochsTagsValidatorsData[epochNumber] = Object.create(null);

function convertMapIntoObject(map: Map<number, Map<Tag, ValidatorsDataPerEpochMap>>): {
[epoch: number]: {
[tag: string]: {
[validatorIndex: number]: DataPerEpoch;
};
};
} {
const object: {
[epoch: number]: {
[tag: string]: {
[validatorIndex: number]: DataPerEpoch;
};
};
} = {};

for (const [epoch, tagsValidatorsMap] of map) {
object[epoch] = {};

for (const [tag, validatorsDataMap] of tagsValidatorsMap) {
object[epoch][tag] = {};

for (const [validatorIndex, data] of validatorsDataMap) {
object[epoch][tag][validatorIndex] = data;
}
epochsTagsValidatorsData[epochNumber][tag] = data;
}
}

return object;
return epochsTagsValidatorsData;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PostgresClient } from "../../../../modules/apiClients/index.js";
import type { NumberOfDaysToQuery } from "../../../../modules/validatorsDataIngest/types.js";
import type { EpochsValidatorsMap } from "../../../../modules/apiClients/postgres/types.js";
import type { EpochsValidatorsData } from "../../../../modules/apiClients/postgres/types.js";
import { fetchAndProcessValidatorsData } from "../../../validatorsDataIngest/index.js";

export async function fetchValidatorsPerformanceData({
Expand All @@ -15,7 +15,7 @@ export async function fetchValidatorsPerformanceData({
numberOfDaysToQuery?: NumberOfDaysToQuery;
minGenesisTime: number;
secondsPerSlot: number;
}): Promise<EpochsValidatorsMap> {
}): Promise<EpochsValidatorsData> {
return await fetchAndProcessValidatorsData({
validatorIndexes,
postgresClient,
Expand Down
4 changes: 2 additions & 2 deletions packages/brain/src/modules/apiServers/ui/calls/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
BeaconchainPoolVoluntaryExitsPostRequest,
Web3signerPostResponse,
Web3signerHealthcheckResponse,
EpochsValidatorsMap
EpochsValidatorsData
} from "../../../apiClients/types.js";
import { NumberOfDaysToQuery } from "../../../validatorsDataIngest/types.js";

Expand All @@ -29,7 +29,7 @@ export interface RpcMethods {
fetchValidatorsPerformanceData: (
validatorIndexes: string[],
numberOfDaysToQuery?: NumberOfDaysToQuery
) => Promise<EpochsValidatorsMap>;
) => Promise<EpochsValidatorsData>;
}

export type ActionRequestOrigin = "ui" | "api";
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
@@ -1,5 +1,5 @@
import { PostgresClient } from "../apiClients/index.js";
import type { EpochsValidatorsMap } from "../apiClients/postgres/types.js";
import type { EpochsValidatorsData } from "../apiClients/postgres/types.js";
import logger from "../logger/index.js";
import { getStartAndEndEpochs } from "./getStartAndEndEpochs.js";
import { NumberOfDaysToQuery } from "./types.js";
Expand All @@ -26,7 +26,7 @@ export async function fetchAndProcessValidatorsData({
minGenesisTime: number; // import from backend index
secondsPerSlot: number; // immport from backend index
numberOfDaysToQuery?: NumberOfDaysToQuery;
}): Promise<EpochsValidatorsMap> {
}): Promise<EpochsValidatorsData> {
logger.info("Processing epochs data");

// Get start timestamp and end timestamp
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/socket/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
Web3signerDeleteResponse,
Web3signerHealthcheckResponse,
Web3signerPostResponse,
EpochsValidatorsMap,
EpochsValidatorsData,
NumberOfDaysToQuery
} from "@stakingbrain/brain";
import { StakerConfig } from "@stakingbrain/common";
Expand All @@ -33,7 +33,7 @@ export interface RpcMethods {
}: {
validatorIndexes: string[];
numberOfDaysToQuery?: NumberOfDaysToQuery;
}) => Promise<EpochsValidatorsMap>;
}) => Promise<EpochsValidatorsData>;
// Network
getStakerConfig: () => Promise<StakerConfig>;
}
Expand Down

0 comments on commit 33b2330

Please sign in to comment.