Skip to content

Commit

Permalink
Paginate offences
Browse files Browse the repository at this point in the history
  • Loading branch information
MOZGIII committed Nov 6, 2024
1 parent 2d7f416 commit d052d2f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/registerMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,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 d052d2f

Please sign in to comment.