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

Implement proper paging of results #18

Merged
merged 5 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 additions & 2 deletions src/commands/node/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Flags } from "@oclif/core";
import { Result } from "ethers/lib/utils";
import { BlockchainCommand } from "../../base";
import { getContract, getProvider, normalizeHex, normalizeRecords } from "../../helpers";

Expand All @@ -11,14 +12,23 @@ export default class NodeList extends BlockchainCommand {
operator: Flags.string({ description: "Filter by operator ID.", helpValue: "ID" }),
skip: Flags.integer({ description: "The number of results to skip.", helpValue: "N", default: 0 }),
size: Flags.integer({ description: "The number of results to list.", helpValue: "N", default: 100 }),
page: Flags.integer({ description: "The contract call paging size.", helpValue: "N", default: 100 }),
};

public async run(): Promise<void> {
const { flags } = await this.parse(NodeList);
const provider = await getProvider(flags.network);
const nodes = await getContract(flags.network, "nodes", provider);
const operatorId = normalizeHex(flags.operator);
const records = await nodes.getNodes(operatorId, flags.topology, flags.skip, flags.size);
console.log(normalizeRecords(records));
const blockTag = await provider.getBlockNumber();
const results: Result[] = [];
for (let i = flags.skip; i != Number.MAX_VALUE && results.length < flags.size; ) {
const records: Result[] = await nodes.getNodes(operatorId, flags.topology, i, flags.page, { blockTag });
// Advance to the next page or signal early exit if exhausted all records
i = records.length !== flags.page ? Number.MAX_VALUE : i + records.length;
// Append as many results as requested, but no more than that
results.push(...records.slice(0, flags.size - results.length));
}
console.log(normalizeRecords(results));
}
}
21 changes: 16 additions & 5 deletions src/commands/project/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Flags } from "@oclif/core";
import { Result } from "ethers/lib/utils";
import { BlockchainCommand } from "../../base";
import { getContract, getProvider, normalizeHex, normalizeRecords } from "../../helpers";

Expand All @@ -10,17 +11,27 @@ export default class ProjectList extends BlockchainCommand {
owner: Flags.string({ description: "Filter by owner address.", helpValue: "ADDR" }),
skip: Flags.integer({ description: "The number of results to skip.", helpValue: "N", default: 0 }),
size: Flags.integer({ description: "The number of results to list.", helpValue: "N", default: 100 }),
page: Flags.integer({ description: "The contract call paging size.", helpValue: "N", default: 100 }),
};

public async run(): Promise<void> {
const { flags } = await this.parse(ProjectList);
const provider = await getProvider(flags.network);
const projects = await getContract(flags.network, "projects", provider);
let records = await projects.getProjects(flags.skip, flags.size);
if (flags.owner) {
const owner = normalizeHex(flags.owner);
records = records.filter((value: { owner: string }) => value.owner.toLowerCase() === owner.toLowerCase());
const owner = normalizeHex(flags.owner);
const blockTag = await provider.getBlockNumber();
const results: Result[] = [];
for (let i = flags.skip; i != Number.MAX_VALUE && results.length < flags.size; ) {
let records: Result[] = await projects.getProjects(i, flags.page, { blockTag });
// Advance to the next page or signal early exit if exhausted all records
i = records.length !== flags.page ? Number.MAX_VALUE : i + records.length;
// Apply filters
if (flags.owner) {
records = records.filter((value) => value.owner.toLowerCase() === owner.toLowerCase());
}
// Append as many results as requested, but no more than that
results.push(...records.slice(0, flags.size - results.length));
}
console.log(normalizeRecords(records));
console.log(normalizeRecords(results));
}
}