-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script to create space allocations snapshot (#451)
## Description This PR addresses the issue storacha/project-tracking#183. This PR introduces the `billing/scripts/space-allocations-snapshot` script, which utilizes the `allocation` and `store` tables to calculate customer storage size and provide an estimate of their overall usage. Please note that this method is not entirely accurate, as it does not account for deleted storage. For detailed instructions on using the script, refer to the `billing/scripts/space-allocations-snapshot/README.md` file.
- Loading branch information
1 parent
00c597a
commit 6d411c6
Showing
19 changed files
with
1,299 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import * as Link from 'multiformats/link' | ||
import { DecodeFailure, EncodeFailure, Schema } from './lib.js' | ||
|
||
/** | ||
* @typedef {import('../lib/api').Allocation} Allocation | ||
* @typedef {import('../lib/api').AllocationSpaceInsertedAtIndex} AllocationSpaceInsertedAtIndex | ||
* @typedef {import('../types').InferStoreRecord<Allocation>} AllocationStoreRecord | ||
* @typedef {import('../lib/api').AllocationKey} AllocationKey | ||
* @typedef {import('../lib/api').AllocationListKey} AllocationListKey | ||
* @typedef {import('../types').InferStoreRecord<AllocationKey>} AllocationKeyStoreRecord | ||
* @typedef {{ space: string, insertedAt?: string }} AllocationListStoreRecord | ||
* @typedef {import('../types').StoreRecord} StoreRecord | ||
*/ | ||
|
||
const schema = Schema.struct({ | ||
space: Schema.did(), | ||
multihash: Schema.text(), | ||
cause: Schema.link({ version: 1 }), | ||
insertedAt: Schema.date(), | ||
size: Schema.bigint().greaterThanEqualTo(0n), | ||
}) | ||
|
||
/** @type {import('../lib/api').Validator<Allocation>} */ | ||
export const validate = (input) => schema.read(input) | ||
|
||
/** @type {import('../lib/api').Encoder<AllocationKey, AllocationKeyStoreRecord>} */ | ||
export const encodeKey = (input) => ({ ok: { multihash: input.multihash } }) | ||
|
||
/** @type {import('../lib/api').Encoder<Allocation, AllocationStoreRecord>} */ | ||
export const encode = (input) => { | ||
try { | ||
return { | ||
ok: { | ||
space: input.space.toString(), | ||
multihash: input.multihash, | ||
cause: input.cause.toString(), | ||
insertedAt: input.insertedAt.toISOString(), | ||
size: input.size.toString(), | ||
}, | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new EncodeFailure(`encoding allocation record: ${err.message}`, { | ||
cause: err, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
/** @type {import('../lib/api').Decoder<StoreRecord, Allocation>} */ | ||
export const decode = (input) => { | ||
try { | ||
return { | ||
ok: { | ||
space: Schema.did().from(input.space), | ||
multihash: /** @type {string} */ (input.multihash), | ||
cause: Link.parse(/** @type {string} */ (input.cause)), | ||
insertedAt: new Date(input.insertedAt), | ||
size: BigInt(input.size), | ||
}, | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new DecodeFailure(`decoding allocation record: ${err.message}`, { | ||
cause: err, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
export const lister = { | ||
/** @type {import('../lib/api').Encoder<AllocationListKey, AllocationListStoreRecord>} */ | ||
encodeKey: (input) => { | ||
/** @type AllocationListStoreRecord */ | ||
const conditions = { space: input.space.toString() } | ||
if (input.insertedAt) { | ||
conditions.insertedAt = input.insertedAt.toISOString() | ||
} | ||
return { | ||
ok: { | ||
...conditions, | ||
}, | ||
} | ||
}, | ||
/** @type {import('../lib/api').Decoder<StoreRecord, AllocationSpaceInsertedAtIndex>} */ | ||
decode: (input) => { | ||
try { | ||
return { | ||
ok: { | ||
space: Schema.did().from(input.space), | ||
insertedAt: new Date(input.insertedAt), | ||
size: BigInt(input.size), | ||
}, | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new DecodeFailure(`decoding allocation record: ${err.message}`, { | ||
cause: err, | ||
}), | ||
} | ||
} | ||
}, | ||
} |
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,106 @@ | ||
import * as Link from 'multiformats/link' | ||
import { DecodeFailure, EncodeFailure, Schema } from './lib.js' | ||
|
||
/** | ||
* @typedef {import('../lib/api.js').StoreTable} StoreTable | ||
* @typedef {import('../lib/api').StoreTableSpaceInsertedAtIndex} StoreTableSpaceInsertedAtIndex | ||
* @typedef {import('../types.js').InferStoreRecord<StoreTable>} StoreTableStoreRecord | ||
* @typedef {import('../lib/api.js').StoreTableKey} StoreTableKey | ||
* @typedef {import('../lib/api.js').StoreTableListKey} StoreTableListKey | ||
* @typedef {import('../types.js').InferStoreRecord<StoreTableKey>} StoreTableKeyStoreRecord | ||
* @typedef {{ space: string, insertedAt?: string}} StoreTableListStoreRecord | ||
* @typedef {import('../types.js').StoreRecord} StoreRecord | ||
*/ | ||
|
||
const schema = Schema.struct({ | ||
space: Schema.did(), | ||
link: Schema.link({ version: 1 }), | ||
invocation: Schema.link({ version: 1 }), | ||
insertedAt: Schema.date(), | ||
size: Schema.bigint().greaterThanEqualTo(0n), | ||
issuer: Schema.did().optional(), | ||
}) | ||
|
||
/** @type {import('../lib/api.js').Validator<StoreTable>} */ | ||
export const validate = (input) => schema.read(input) | ||
|
||
/** @type {import('../lib/api.js').Encoder<StoreTableKey, StoreTableKeyStoreRecord>} */ | ||
export const encodeKey = (input) => ({ ok: { link: input.link } }) | ||
|
||
/** @type {import('../lib/api.js').Encoder<StoreTable, StoreTableStoreRecord>} */ | ||
export const encode = (input) => { | ||
try { | ||
return { | ||
ok: { | ||
space: input.space.toString(), | ||
link: input.link.toString(), | ||
invocation: input.invocation.toString(), | ||
insertedAt: input.insertedAt.toISOString(), | ||
size: input.size.toString(), | ||
issuer: input.issuer?.toString(), | ||
}, | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new EncodeFailure(`encoding store record: ${err.message}`, { | ||
cause: err, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
/** @type {import('../lib/api.js').Decoder<StoreRecord, StoreTable>} */ | ||
export const decode = (input) => { | ||
try { | ||
return { | ||
ok: { | ||
space: Schema.did().from(input.space), | ||
link: Link.parse(/** @type {string} */ (input.link)), | ||
invocation: Link.parse(/** @type {string} */ (input.invocation)), | ||
insertedAt: new Date(input.insertedAt), | ||
size: BigInt(input.size), | ||
issuer: Schema.did().from(input.issuer), | ||
}, | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new DecodeFailure(`decoding store record: ${err.message}`, { | ||
cause: err, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
export const lister = { | ||
/** @type {import('../lib/api.js').Encoder<StoreTableListKey, StoreTableListStoreRecord>} */ | ||
encodeKey: (input) => { | ||
/** @type StoreTableListStoreRecord */ | ||
const conditions = { space: input.space.toString() } | ||
if (input.insertedAt) { | ||
conditions.insertedAt = input.insertedAt.toISOString() | ||
} | ||
return { | ||
ok: { | ||
...conditions, | ||
}, | ||
} | ||
}, | ||
/** @type {import('../lib/api.js').Decoder<StoreRecord, StoreTableSpaceInsertedAtIndex>} */ | ||
decode: (input) => { | ||
try { | ||
return { | ||
ok: { | ||
space: Schema.did().from(input.space), | ||
insertedAt: new Date(input.insertedAt), | ||
size: BigInt(input.size), | ||
}, | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new DecodeFailure(`decoding allocation record: ${err.message}`, { | ||
cause: err, | ||
}), | ||
} | ||
} | ||
}, | ||
} |
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
Oops, something went wrong.