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: space usage reports #120

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
remove,
list,
whoami,
usageReport
} from './index.js'
import {
storeAdd,
Expand Down Expand Up @@ -203,6 +204,13 @@ cli
.option('--json', 'Format as newline delimited JSON')
.action(listProofs)

cli
.command('usage report')
.describe('Display report of current space usage in bytes.')
.option('--human', 'Format human readable values.', false)
.option('--json', 'Format as newline delimited JSON', false)
.action(usageReport)

cli
.command('can access claim')
.describe('Claim delegated capabilities for the authorized account.')
Expand Down
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
filesizeMB,
readProof,
uploadListResponseToString,
startOfLastMonth,
} from './lib.js'
import * as ucanto from '@ucanto/core'
import chalk from 'chalk'
Expand Down Expand Up @@ -475,3 +476,52 @@ export async function whoami() {
const client = await getClient()
console.log(client.did())
}

/**
* @param {object} [opts]
* @param {boolean} [opts.human]
* @param {boolean} [opts.json]
*/
export async function usageReport(opts) {
const client = await getClient()
const now = new Date()
const period = {
// we may not have done a snapshot for this month _yet_, so get report from last month -> now
from: startOfLastMonth(now),
to: now
Comment on lines +488 to +491
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we log the period we are tracking?

Copy link
Member Author

@alanshaw alanshaw Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's a report as of now.

If we were graphing the data then yes but we are not doing that here in the CLI :)

}

let total = 0
for await (const { account, provider, space, size } of getSpaceUsageReports(client, period)) {
if (opts?.json) {
console.log(dagJSON.stringify({ account, provider, space, size, reportedAt: now.toISOString() }))
} else {
console.log(` Account: ${account}`)
console.log(`Provider: ${provider}`)
console.log(` Space: ${space}`)
console.log(` Size: ${opts?.human ? filesize(size.final) : size.final}\n`)
}
total += size.final
}
if (!opts?.json) {
console.log(` Total: ${opts?.human ? filesize(total) : total}`)
}
}

/**
* @param {import('@web3-storage/w3up-client').Client} client
* @param {{ from: Date, to: Date }} period
*/
async function * getSpaceUsageReports (client, period) {
for (const account of Object.values(client.accounts())) {
const subscriptions = await client.capability.subscription.list(account.did())
for (const { consumers } of subscriptions.results) {
for (const space of consumers) {
const result = await client.capability.usage.report(space, period)
for (const [, report] of Object.entries(result)) {
yield { account: account.did(), ...report }
}
}
}
}
}
18 changes: 18 additions & 0 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,21 @@ export function parseCarLink(cidStr) {
return undefined
}
}

/** @param {string|number|Date} now */
const startOfMonth = (now) => {
const d = new Date(now)
d.setUTCDate(1)
d.setUTCHours(0)
d.setUTCMinutes(0)
d.setUTCSeconds(0)
d.setUTCMilliseconds(0)
return d
}

/** @param {string|number|Date} now */
export const startOfLastMonth = (now) => {
const d = startOfMonth(now)
d.setUTCMonth(d.getUTCMonth() - 1)
return d
}
Loading