Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: w3 usage report catches/warns about errors invoking usage/report #169

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 45 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@
}
}

export async function whoami() {

Check warning on line 571 in index.js

View workflow job for this annotation

GitHub Actions / Test

Missing JSDoc comment
const client = await getClient()
console.log(client.did())
}
Expand All @@ -586,34 +586,52 @@
from: startOfLastMonth(now),
to: now,
}

const failures = []
let total = 0
for await (const { account, provider, space, size } of getSpaceUsageReports(
for await (const result of getSpaceUsageReports(
client,
period
)) {
if (opts?.json) {
console.log(
dagJSON.stringify({
account,
provider,
space,
size,
reportedAt: now.toISOString(),
})
)
if ('error' in result) {
failures.push(result)
} else {
console.log(` Account: ${account}`)
console.log(`Provider: ${provider}`)
console.log(` Space: ${space}`)
console.log(
` Size: ${opts?.human ? filesize(size.final) : size.final}\n`
)
if (opts?.json) {
const { account, provider, space, size } = result
console.log(
dagJSON.stringify({
account,
provider,
space,
size,
reportedAt: now.toISOString(),
})
)
} else {
const { account, provider, space, size } = result
console.log(` Account: ${account}`)
console.log(`Provider: ${provider}`)
console.log(` Space: ${space}`)
console.log(
` Size: ${opts?.human ? filesize(size.final) : size.final}\n`
)
}
total += result.size.final
}
total += size.final
}
if (!opts?.json) {
console.log(` Total: ${opts?.human ? filesize(total) : total}`)
if (failures.length) {
console.warn(``)
console.warn(` WARNING: there were ${failures.length} errors getting usage reports for some spaces.`)
console.warn(` This may happen if your agent does not have usage/report authorization for a space.`)
console.warn(` These spaces were not included in the usage report total:`)
for (const fail of failures) {
console.warn(` * space: ${fail.space}`)
// @ts-expect-error error is unknown
console.warn(` error: ${fail.error?.message}`)
console.warn(` account: ${fail.account}`)
}
}
}
}

Expand All @@ -628,7 +646,14 @@
)
for (const { consumers } of subscriptions.results) {
for (const space of consumers) {
const result = await client.capability.usage.report(space, period)
/** @type {import('@web3-storage/upload-client/types').UsageReportSuccess} */
let result
try {
result = await client.capability.usage.report(space, period)
} catch (error) {
yield { error, space, period, consumers, account: account.did() }
continue
}
for (const [, report] of Object.entries(result)) {
yield { account: account.did(), ...report }
}
Expand Down