Skip to content

Commit

Permalink
Runtimes api, #611 (#637)
Browse files Browse the repository at this point in the history
* Add runtimes api, #611

* Add single runtime api, #611
  • Loading branch information
wliyongfeng authored Mar 13, 2023
1 parent 45a0dee commit 8efe15c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
...require("./runtimes"),
...require("./runtime"),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const {
runtime: { getRuntimeCollection },
} = require("@statescan/mongo");

async function getRuntime(ctx) {
const { version, height } = ctx.params;
const col = await getRuntimeCollection();
ctx.body = await col.findOne({
height: parseInt(height),
"runtimeVersion.specVersion": parseInt(version),
});
}

module.exports = {
getRuntime,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { extractPage } = require("../../../utils");
const {
runtime: { getRuntimeCollection },
} = require("@statescan/mongo");

function lookupCount(lookup = [], id) {
const { type } = lookup.types[id] || {};
const variants = type.def.variant.variants;
return variants.length;
}

function normalizeRuntime(item) {
const {
height,
runtimeVersion: { specName, specVersion },
metadata: { lookup = [], pallets = [] },
} = item || {};

return {
startHeight: height,
specName,
specVersion,
count: {
pallets: pallets.length,
calls: pallets.reduce((result, pallet) => {
if (!pallet.calls) {
return result;
}

return result + lookupCount(lookup, pallet.calls.type);
}, 0),
events: pallets.reduce((result, pallet) => {
if (!pallet.events) {
return result;
}

return result + lookupCount(lookup, pallet.events.type);
}, 0),
storage: pallets.reduce((result, pallet) => {
if (!pallet.storage) {
return result;
}

return result + (pallet.storage.items || []).length;
}, 0),
constants: pallets.reduce((result, pallet) => {
return result + (pallet.constants || []).length;
}, 0),
},
};
}

async function getRuntimes(ctx) {
const { page, pageSize } = extractPage(ctx);
if (pageSize === 0 || page < 0) {
ctx.status = 400;
return;
}

const col = await getRuntimeCollection();
const items = await col
.find({})
.sort({ height: -1 })
.skip(page * pageSize)
.limit(pageSize)
.toArray();
const total = await col.estimatedDocumentCount();

ctx.body = {
items: items.map(normalizeRuntime),
page,
pageSize,
total,
};
}

module.exports = {
getRuntimes,
};
8 changes: 8 additions & 0 deletions backend/packages/server/src/features/runtime/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { getRuntimes, getRuntime } = require("./controllers");
const Router = require("koa-router");

const router = new Router();
router.get("/runtimes", getRuntimes);
router.get("/runtimes/:version(\\d+)_:height(\\d+)", getRuntime);

module.exports = router;
1 change: 1 addition & 0 deletions backend/packages/server/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const routes = [
require("./features/unfinalized/routes"),
require("./features/search/routes"),
require("./features/spec/routes"),
require("./features/runtime/routes"),
require("./features/status/routes"),
];

Expand Down

0 comments on commit 8efe15c

Please sign in to comment.