-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: account plan subscriptions and space usage API sugar (#1171)
This PR allows you to get a list of subscriptions for your account plan, and also get the current per space usage (I've not exposed the full report yet since we don't have any UI that would use it). e.g. Getting subscriptions list: ```js const account = Object.values(client.accounts())[0] const subs = await account.plan.subscriptions() for (const sub of subs.ok) { console.log(`ID: ${sub.subscription}`) console.log(`Consumers: ${sub.consumers.join(', ')}`) console.log(`Provider: ${sub.provider}`) } ``` Getting usage: ```js const space = client.spaces()[0] const usage = await space.usage.get() console.log(`${space.did()}: ${usage.ok} bytes`) ``` So, you no longer have to invoke capabilities directly like this: https://github.com/web3-storage/w3cli/blob/ca21f6736fb8c2180d3634f40931b91e4f0bb964/index.js#L553-L571
- Loading branch information
Alan Shaw
authored
Nov 22, 2023
1 parent
a31f667
commit 0bc589b
Showing
8 changed files
with
211 additions
and
72 deletions.
There are no files selected for viewing
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
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
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
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
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 |
---|---|---|
@@ -1,47 +1,99 @@ | ||
export * from '@web3-storage/access/space' | ||
import * as Usage from './capability/usage.js' | ||
import * as API from './types.js' | ||
|
||
export class Space { | ||
/** @type {import('./types.js').DID} */ | ||
#did | ||
/** | ||
* @typedef {object} Model | ||
* @property {API.SpaceDID} id | ||
* @property {{name?:string}} [meta] | ||
* @property {API.Agent} agent | ||
*/ | ||
|
||
/** @type {Record<string, any>} */ | ||
#meta | ||
export class Space { | ||
#model | ||
|
||
/** | ||
* @param {import('./types.js').DID} did | ||
* @param {Record<string, any>} meta | ||
* @param {Model} model | ||
*/ | ||
constructor(did, meta = {}) { | ||
this.#did = did | ||
this.#meta = meta | ||
constructor(model) { | ||
this.#model = model | ||
this.usage = new StorageUsage(model) | ||
} | ||
|
||
/** | ||
* The given space name. | ||
*/ | ||
get name() { | ||
/* c8 ignore next */ | ||
return String(this.#meta.name ?? '') | ||
return String(this.#model.meta?.name ?? '') | ||
} | ||
|
||
/** | ||
* The DID of the space. | ||
*/ | ||
did() { | ||
return this.#did | ||
return this.#model.id | ||
} | ||
|
||
/** | ||
* Whether the space has been registered with the service. | ||
* User defined space metadata. | ||
*/ | ||
registered() { | ||
return Boolean(this.#meta.isRegistered) | ||
meta() { | ||
return this.#model.meta | ||
} | ||
} | ||
|
||
export class StorageUsage { | ||
#model | ||
|
||
/** | ||
* User defined space metadata. | ||
* @param {Model} model | ||
*/ | ||
meta() { | ||
return this.#meta | ||
constructor(model) { | ||
this.#model = model | ||
} | ||
|
||
/** | ||
* Get the current usage in bytes. | ||
*/ | ||
async get() { | ||
const { agent } = this.#model | ||
const space = this.#model.id | ||
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, | ||
} | ||
const result = await Usage.report({ agent }, { space, period }) | ||
/* c8 ignore next */ | ||
if (result.error) return result | ||
|
||
const provider = /** @type {API.ProviderDID} */ (agent.connection.id.did()) | ||
const report = result.ok[provider] | ||
|
||
return { | ||
/* c8 ignore next */ | ||
ok: report?.size.final == null ? undefined : BigInt(report.size.final), | ||
} | ||
} | ||
} | ||
|
||
/** @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 */ | ||
const startOfLastMonth = (now) => { | ||
const d = startOfMonth(now) | ||
d.setUTCMonth(d.getUTCMonth() - 1) | ||
return d | ||
} |
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
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
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 |
---|---|---|
@@ -1,16 +1,56 @@ | ||
import * as Signer from '@ucanto/principal/ed25519' | ||
import assert from 'assert' | ||
import * as StoreCapabilities from '@web3-storage/capabilities/store' | ||
import * as Test from './test.js' | ||
import { Space } from '../src/space.js' | ||
import * as Account from '../src/account.js' | ||
import * as Result from '../src/result.js' | ||
import { randomCAR } from './helpers/random.js' | ||
|
||
describe('spaces', () => { | ||
it('should get meta', async () => { | ||
/** | ||
* @type {Test.Suite} | ||
*/ | ||
export const testSpace = { | ||
'should get meta': async (assert, { client }) => { | ||
const signer = await Signer.generate() | ||
const name = `space-${Date.now()}` | ||
const isRegistered = true | ||
const space = new Space(signer.did(), { name, isRegistered }) | ||
const space = new Space({ | ||
id: signer.did(), | ||
meta: { name }, | ||
agent: client.agent, | ||
}) | ||
assert.equal(space.did(), signer.did()) | ||
assert.equal(space.name, name) | ||
assert.equal(space.registered(), isRegistered) | ||
assert.equal(space.meta().name, name) | ||
}) | ||
}) | ||
assert.equal(space.meta()?.name, name) | ||
}, | ||
|
||
'should get usage': async (assert, { client, grantAccess, mail }) => { | ||
const space = await client.createSpace('test') | ||
|
||
const email = 'alice@web.mail' | ||
const login = Account.login(client, email) | ||
const message = await mail.take() | ||
assert.deepEqual(message.to, email) | ||
await grantAccess(message) | ||
const account = Result.try(await login) | ||
|
||
Result.try(await account.provision(space.did())) | ||
await space.save() | ||
|
||
const size = 1138 | ||
const archive = await randomCAR(size) | ||
await client.agent.invokeAndExecute(StoreCapabilities.add, { | ||
nb: { | ||
link: archive.cid, | ||
size, | ||
}, | ||
}) | ||
|
||
const found = client.spaces().find((s) => s.did() === space.did()) | ||
if (!found) return assert.fail('space not found') | ||
|
||
const usage = Result.unwrap(await found.usage.get()) | ||
assert.equal(usage, BigInt(size)) | ||
}, | ||
} | ||
|
||
Test.test({ Space: testSpace }) |