Skip to content

Commit

Permalink
Paginate offences and nextKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
MOZGIII committed Nov 6, 2024
1 parent 2d7f416 commit 8374253
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/registerMetrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiPromise } from "@polkadot/api";
import { Gauge } from "prom-client";
import { countPaginated } from "./utils";

export default (api: ApiPromise) => {
if (typeof api?.query?.session?.validators === "function") {
Expand Down Expand Up @@ -53,8 +54,14 @@ export default (api: ApiPromise) => {
name: "humanode_state_session_next_keys_count",
help: "count of the session keys to use in the next session",
async collect() {
const nextKeys = await api.query.session.nextKeys.entries();
this.set(nextKeys.length);
const count = await countPaginated((startKey) =>
api.query.session.nextKeys.keysPaged({
args: [],
pageSize: 1000,
startKey,
})
);
this.set(count);
},
});
}
Expand All @@ -64,8 +71,14 @@ export default (api: ApiPromise) => {
name: "humanode_state_offences_reports_count",
help: "count of the offence reports",
async collect() {
const reports = await api.query.offences.reports.entries();
this.set(reports.length);
const count = await countPaginated((startKey) =>
api.query.offences.reports.keysPaged({
args: [],
pageSize: 1000,
startKey,
})
);
this.set(count);
},
});
}
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { StorageKey } from "@polkadot/types";
import type { AnyTuple } from "@polkadot/types-codec/types";

type PagintatedFn = <A extends AnyTuple>(
startKey: undefined | string
) => Promise<StorageKey<A>[]>;

export const countPaginated = async (query: PagintatedFn): Promise<number> => {
let total = 0;
let startKey: undefined | any = undefined;

while (true) {
const keys = await query(startKey);
const len = keys.length;
if (len === 0) {
break;
}
total += len;
startKey = keys[len - 1].toString();
}

return total;
};

0 comments on commit 8374253

Please sign in to comment.