|
@@ -6,6 +6,74 @@ const { discover } = require('@joystream/service-discovery')
|
|
|
const axios = require('axios')
|
|
|
const stripEndingSlash = require('@joystream/storage-utils/stripEndingSlash')
|
|
|
|
|
|
+function mapInfoToStatus(providers, currentHeight) {
|
|
|
+ return providers.map(({ providerId, info }) => {
|
|
|
+ if (info) {
|
|
|
+ return {
|
|
|
+ providerId,
|
|
|
+ identity: info.identity.toString(),
|
|
|
+ expiresIn: info.expires_at.sub(currentHeight).toNumber(),
|
|
|
+ expired: currentHeight.gte(info.expires_at),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ providerId,
|
|
|
+ identity: null,
|
|
|
+ status: 'down',
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function makeAssetUrl(contentId, source) {
|
|
|
+ source = stripEndingSlash(source)
|
|
|
+ return `${source}/asset/v0/${encodeAddress(contentId)}`
|
|
|
+}
|
|
|
+
|
|
|
+async function assetRelationshipState(api, contentId, providers) {
|
|
|
+ const dataObject = await api.query.dataDirectory.dataObjectByContentId(contentId)
|
|
|
+
|
|
|
+ const relationshipIds = await api.query.dataObjectStorageRegistry.relationshipsByContentId(contentId)
|
|
|
+
|
|
|
+ // how many relationships associated with active providers and in ready state
|
|
|
+ const activeRelationships = await Promise.all(
|
|
|
+ relationshipIds.map(async (id) => {
|
|
|
+ let relationship = await api.query.dataObjectStorageRegistry.relationships(id)
|
|
|
+ relationship = relationship.unwrap()
|
|
|
+ // only interested in ready relationships
|
|
|
+ if (!relationship.ready) {
|
|
|
+ return undefined
|
|
|
+ }
|
|
|
+ // Does the relationship belong to an active provider ?
|
|
|
+ return providers.find((provider) => relationship.storage_provider.eq(provider))
|
|
|
+ })
|
|
|
+ )
|
|
|
+
|
|
|
+ return [activeRelationships.filter((active) => active).length, dataObject.unwrap().liaison_judgement]
|
|
|
+}
|
|
|
+
|
|
|
+// HTTP HEAD with axios all known content ids on each provider
|
|
|
+async function countContentAvailability(contentIds, source) {
|
|
|
+ const content = {}
|
|
|
+ let found = 0
|
|
|
+ let missing = 0
|
|
|
+ for (let i = 0; i < contentIds.length; i++) {
|
|
|
+ const assetUrl = makeAssetUrl(contentIds[i], source)
|
|
|
+ try {
|
|
|
+ const info = await axios.head(assetUrl)
|
|
|
+ content[encodeAddress(contentIds[i])] = {
|
|
|
+ type: info.headers['content-type'],
|
|
|
+ bytes: info.headers['content-length'],
|
|
|
+ }
|
|
|
+ // TODO: cross check against dataobject size
|
|
|
+ found++
|
|
|
+ } catch (err) {
|
|
|
+ missing++
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return { found, missing, content }
|
|
|
+}
|
|
|
+
|
|
|
async function main() {
|
|
|
const runtime = await RuntimeApi.create()
|
|
|
const { api } = runtime
|
|
@@ -127,72 +195,4 @@ async function main() {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-function mapInfoToStatus(providers, currentHeight) {
|
|
|
- return providers.map(({ providerId, info }) => {
|
|
|
- if (info) {
|
|
|
- return {
|
|
|
- providerId,
|
|
|
- identity: info.identity.toString(),
|
|
|
- expiresIn: info.expires_at.sub(currentHeight).toNumber(),
|
|
|
- expired: currentHeight.gte(info.expires_at),
|
|
|
- }
|
|
|
- }
|
|
|
- return {
|
|
|
- providerId,
|
|
|
- identity: null,
|
|
|
- status: 'down',
|
|
|
- }
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
-// HTTP HEAD with axios all known content ids on each provider
|
|
|
-async function countContentAvailability(contentIds, source) {
|
|
|
- const content = {}
|
|
|
- let found = 0
|
|
|
- let missing = 0
|
|
|
- for (let i = 0; i < contentIds.length; i++) {
|
|
|
- const assetUrl = makeAssetUrl(contentIds[i], source)
|
|
|
- try {
|
|
|
- const info = await axios.head(assetUrl)
|
|
|
- content[encodeAddress(contentIds[i])] = {
|
|
|
- type: info.headers['content-type'],
|
|
|
- bytes: info.headers['content-length'],
|
|
|
- }
|
|
|
- // TODO: cross check against dataobject size
|
|
|
- found++
|
|
|
- } catch (err) {
|
|
|
- missing++
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return { found, missing, content }
|
|
|
-}
|
|
|
-
|
|
|
-function makeAssetUrl(contentId, source) {
|
|
|
- source = stripEndingSlash(source)
|
|
|
- return `${source}/asset/v0/${encodeAddress(contentId)}`
|
|
|
-}
|
|
|
-
|
|
|
-async function assetRelationshipState(api, contentId, providers) {
|
|
|
- const dataObject = await api.query.dataDirectory.dataObjectByContentId(contentId)
|
|
|
-
|
|
|
- const relationshipIds = await api.query.dataObjectStorageRegistry.relationshipsByContentId(contentId)
|
|
|
-
|
|
|
- // how many relationships associated with active providers and in ready state
|
|
|
- const activeRelationships = await Promise.all(
|
|
|
- relationshipIds.map(async (id) => {
|
|
|
- let relationship = await api.query.dataObjectStorageRegistry.relationships(id)
|
|
|
- relationship = relationship.unwrap()
|
|
|
- // only interested in ready relationships
|
|
|
- if (!relationship.ready) {
|
|
|
- return undefined
|
|
|
- }
|
|
|
- // Does the relationship belong to an active provider ?
|
|
|
- return providers.find((provider) => relationship.storage_provider.eq(provider))
|
|
|
- })
|
|
|
- )
|
|
|
-
|
|
|
- return [activeRelationships.filter((active) => active).length, dataObject.unwrap().liaison_judgement]
|
|
|
-}
|
|
|
-
|
|
|
main()
|