Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Redm4x committed Apr 2, 2024
1 parent 9bd9fd0 commit 38ac4a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 5 additions & 7 deletions api/src/services/db/providerStatusService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export async function getNetworkCapacity() {
}

export const getProviderList = async () => {
// Fetch provider list with their attributes & auditors
const providers = await Provider.findAll({
const providersWithAttributesAndAuditors = await Provider.findAll({
where: {
deletedHeight: null
},
Expand All @@ -58,8 +57,7 @@ export const getProviderList = async () => {
]
});

// Fetch latest snapshot nodes for each provider
const providerNodes = await Provider.findAll({
const providerWithNodes = await Provider.findAll({
attributes: ["owner"],
where: {
deletedHeight: null
Expand All @@ -82,14 +80,14 @@ export const getProviderList = async () => {
]
});

const filteredProviders = providers.filter((value, index, self) => self.map((x) => x.hostUri).lastIndexOf(value.hostUri) === index);
const distinctProviders = providersWithAttributesAndAuditors.filter((value, index, self) => self.map((x) => x.hostUri).lastIndexOf(value.hostUri) === index);
const providerAttributeSchemaQuery = getProviderAttributesSchema();
const auditorsQuery = getAuditors();

const [auditors, providerAttributeSchema] = await Promise.all([auditorsQuery, providerAttributeSchemaQuery]);

return filteredProviders.map((x) => {
const nodes = providerNodes.find((p) => p.owner === x.owner)?.lastSnapshot?.nodes;
return distinctProviders.map((x) => {
const nodes = providerWithNodes.find((p) => p.owner === x.owner)?.lastSnapshot?.nodes;
return mapProviderToList(x, providerAttributeSchema, auditors, nodes);
});
};
Expand Down
12 changes: 8 additions & 4 deletions api/src/utils/map/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ export const mapProviderToList = (
): ProviderList => {
const isValidVersion = provider.cosmosSdkVersion ? semver.gte(provider.cosmosSdkVersion, "v0.45.9") : false;
const name = provider.isOnline ? new URL(provider.hostUri).hostname : null;
const gpuModels = (nodes || [])
.flatMap((x) => x.gpus)
.map((x) => ({ vendor: x.vendor, model: x.name, ram: x.memorySize, interface: x.interface }))
.filter((x, i, arr) => arr.findIndex((o) => x.vendor === o.vendor && x.model === o.model && x.ram === o.ram && x.interface === o.interface) === i);
const gpuModels = getDistinctGpuModelsFromNodes(nodes || []);

return {
owner: provider.owner,
Expand Down Expand Up @@ -93,6 +90,13 @@ export const mapProviderToList = (
} as ProviderList;
};

function getDistinctGpuModelsFromNodes(nodes: ProviderSnapshotNode[]) {
return nodes
.flatMap((x) => x.gpus)
.map((x) => ({ vendor: x.vendor, model: x.name, ram: x.memorySize, interface: x.interface }))
.filter((x, i, arr) => arr.findIndex((o) => x.vendor === o.vendor && x.model === o.model && x.ram === o.ram && x.interface === o.interface) === i);
}

export const getProviderAttributeValue = (
key: keyof ProviderAttributesSchema,
provider: Provider,
Expand Down

0 comments on commit 38ac4a2

Please sign in to comment.