-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45a0dee
commit 8efe15c
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
backend/packages/server/src/features/runtime/controllers/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
...require("./runtimes"), | ||
...require("./runtime"), | ||
}; |
16 changes: 16 additions & 0 deletions
16
backend/packages/server/src/features/runtime/controllers/runtime.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
79 changes: 79 additions & 0 deletions
79
backend/packages/server/src/features/runtime/controllers/runtimes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters