Skip to content

Commit

Permalink
CN Clean up saveFileForMultihashToFS logs (#4192)
Browse files Browse the repository at this point in the history
  • Loading branch information
SidSethi committed Oct 27, 2022
1 parent 1c608bb commit 8651064
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
13 changes: 5 additions & 8 deletions creator-node/src/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ async function fetchFileFromNetworkAndWriteToDisk({
}) {
// First try to fetch from other cnode gateways if user has non-empty replica set.
decisionTree.recordStage({
name: 'About to fetch content via gateways',
data: { gatewayContentRoutes }
name: 'About to fetch content via gateways'
})

// Note - Requests are intentionally not parallel to minimize additional load on gateways
Expand Down Expand Up @@ -370,12 +369,11 @@ async function saveFileForMultihashToFS(
name: 'About to start running saveFileForMultihashToFS()',
data: {
multihash,
targetGateways,
gatewayContentRoutes,
expectedStoragePath,
parsedStoragePath
},
log: true
log: true,
logLevel: 'debug'
})

// Create dir at expected storage path in which to store retrieved data
Expand Down Expand Up @@ -418,8 +416,7 @@ async function saveFileForMultihashToFS(
}/${fileNameForImage}`
)
decisionTree.recordStage({
name: 'Updated gatewayUrlsMapped',
data: { gatewayContentRoutes }
name: 'Updated gatewayUrlsMapped'
})
}

Expand Down Expand Up @@ -457,7 +454,7 @@ async function saveFileForMultihashToFS(

return e
} finally {
decisionTree.printTree()
decisionTree.printTree({ logLevel: 'debug' })
}

// If no error, return nothing
Expand Down
55 changes: 43 additions & 12 deletions creator-node/src/utils/decisionTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type RecordStageParams = {
name: string
data: object | null
log: boolean
logLevel?: 'error' | 'warn' | 'info' | 'debug'
}

/**
Expand All @@ -36,7 +37,12 @@ module.exports = class DecisionTree {
this.name = name
}

recordStage = ({ name, data = null, log = false }: RecordStageParams) => {
recordStage = ({
name,
data = null,
log = false,
logLevel = 'info'
}: RecordStageParams) => {
const timestamp = Date.now()

let stage: Stage
Expand All @@ -63,29 +69,54 @@ module.exports = class DecisionTree {
this.tree.push(stage)

if (log) {
this.printLastStage()
this._printLastStage({ logLevel })
}
}

printTree() {
this._logInfo(`DecisionTree Full - ${JSON.stringify(this.tree, null, 2)}`)
printTree({ logLevel = 'info' } = {}) {
this._log({
msg: `DecisionTree Full - ${JSON.stringify(this.tree, null, 2)}`,
logLevel
})
}

printLastStage() {
private _printLastStage({ logLevel = 'info' } = {}) {
if (this.tree.length > 0) {
this._logInfo(
`DecisionTree Last Stage - ${JSON.stringify(
this._log({
msg: `DecisionTree Last Stage - ${JSON.stringify(
this.tree[this.tree.length - 1],
null,
2
)}`
)
)}`,
logLevel
})
} else {
this._logInfo('DecisionTree Last Stage - empty')
this._log({
msg: 'DecisionTree Last Stage - empty',
logLevel
})
}
}

private _logInfo(msg: string) {
this.logger.info(`${this.name} - ${msg}`)
private _log({ msg = '', logLevel = 'info' } = {}) {
let logFn
switch (logLevel) {
case 'error':
logFn = this.logger.error
break
case 'warn':
logFn = this.logger.warn
break
case 'info':
logFn = this.logger.info
break
case 'debug':
logFn = this.logger.debug
break
default:
logFn = this.logger.debug
}

logFn(`${this.name} - ${msg}`)
}
}

0 comments on commit 8651064

Please sign in to comment.