Skip to content

Commit

Permalink
nft-ttr clean up how logging works. retrievals log gateway url
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed Jun 20, 2022
1 parent bde02c3 commit 529a8ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
8 changes: 5 additions & 3 deletions packages/cron/src/bin/nft-ttr.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ export async function* cli(
iterable = measureNftTimeToRetrievability({
secrets,
...createMeasureOptionsFromSade(opts, secrets),
...options,
...(options.fetchImage ? { fetchImage: options.fetchImage } : {}),
...(options.store ? { store: options.store } : {}),
console: options.console,
})
})
argParser.parse(argv)
Expand All @@ -233,8 +235,8 @@ function parseBasicAuth(basicAuthEnvVarString) {
* @param {string[]} argv
*/
async function main(argv) {
// eslint-disable-next-line no-empty,@typescript-eslint/no-unused-vars,no-unused-vars
for await (const _ of cli(argv)) {
for await (const _ of cli(argv, { console, env: process.env })) {
console.debug(_)
}
}

Expand Down
28 changes: 16 additions & 12 deletions packages/cron/src/jobs/measureNftTimeToRetrievability.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const EXAMPLE_NFT_IMG_URL = new URL(
* @property {string} image
* @property {"retrieve"} type
* @property {URL} url
* @property {URL} gateway
* @property {number} contentLength
* @property {Date} startTime
* @property {Milliseconds} duration
Expand Down Expand Up @@ -100,15 +101,18 @@ function readMeasureTtrOptions(options) {
*/

/**
* @typedef StoreLog
* @property {"store"} type
* @typedef StoreStartLog
* @property {"store/start"} type
* @property {string} image
* @property {Date} startTime
* @property {Milliseconds} duration
*/

/**
* @typedef {StoreLog|Activity<"start"|"finish">|RetrieveLog} MeasureTtrLog
* @typedef StoreLog
* @property {"store"} type
* @property {string} image
* @property {Date} startTime
* @property {Milliseconds} duration
*/

/**
Expand All @@ -117,9 +121,7 @@ function readMeasureTtrOptions(options) {
* * upload to nft.storage
* * retrieve image through ipfs gateway
* @param {MeasureTtrOptions} options
* @returns {AsyncIterable<
* StoreLog|Activity<"start"|"finish">|RetrieveLog
* >}
* @returns {AsyncIterable<StoreStartLog|StoreLog|Activity<"start"|"finish">|RetrieveLog>}
*/
export async function* measureNftTimeToRetrievability(options) {
// separate secrets and config to avoid logging secrets
Expand All @@ -128,7 +130,6 @@ export async function* measureNftTimeToRetrievability(options) {
const start = {
type: 'start',
}
config.console.debug(start)
yield start
for await (const image of config.images) {
const imageId = Number(new Date()).toString()
Expand All @@ -144,12 +145,13 @@ export async function* measureNftTimeToRetrievability(options) {
const storeStartedAt = now()
/** @type {StoreFunction} */
const store = config.store || ((nft) => client.store(nft))
const storeBeforeLog = {
type: 'store/before',
/** @type {StoreStartLog} */
const storeStartLog = {
type: 'store/start',
image: imageId,
startTime: new Date(),
}
config.console.debug(storeBeforeLog)
yield storeStartLog
const metadata = await store(nft)
const storeEndAt = now()
/** @type {StoreLog} */
Expand All @@ -160,14 +162,14 @@ export async function* measureNftTimeToRetrievability(options) {
duration: Milliseconds.subtract(storeEndAt, storeStartedAt),
}
yield storeLog
config.console.debug(storeLog)
for (const gateway of config.gateways) {
/** @type {RetrieveLog} */
let retrieval
try {
retrieval = await retrieve(options, {
id: imageId,
url: createGatewayRetrievalUrl(gateway, metadata.ipnft),
gateway,
})
} catch (error) {
console.error('error retrieving', error)
Expand Down Expand Up @@ -220,6 +222,7 @@ export const httpImageFetcher = (fetch) => async (url) => {
* @param {object} image
* @param {string} image.id
* @param {URL} image.url
* @param {URL} image.gateway
*/
async function retrieve(options, image) {
const retrieveFetchDate = new Date()
Expand All @@ -234,6 +237,7 @@ async function retrieve(options, image) {
type: 'retrieve',
url: image.url,
image: image.id,
gateway: image.gateway,
/** length in bytes */
contentLength: retrievedImage.size,
startTime: retrieveFetchDate,
Expand Down

0 comments on commit 529a8ca

Please sign in to comment.