Skip to content

Commit

Permalink
refactor(condo): DOMA-10063 use logTooManyReturnedIfRequired (#5148)
Browse files Browse the repository at this point in the history
  • Loading branch information
pahaz authored Sep 2, 2024
1 parent efe5b94 commit 6482b6d
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions apps/condo/domains/common/utils/serverSchema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const { getLogger } = require('@open-condo/keystone/logging')
const { getSchemaCtx } = require('@open-condo/keystone/schema')

const GLOBAL_QUERY_LIMIT = 1000
const TOO_MANY_RETURNED_LOG_LIMITS = Object.freeze([1100, 4900, 9000, 14900, 49000, 149000])
const TOO_MANY_RETURNED_LOG_LIMITS = Object.freeze([1100, 9000, 14900, 49000, 149000])
const TOO_MANY_RETURNED_RESULT_LOG_LIMIT = 4900
const logger = getLogger('common/utils/serverSchema.js')
const TIMEOUT_DURATION = Number(conf.TIMEOUT_CHUNKS_DURATION) || 60 * 1000

Expand All @@ -33,13 +34,17 @@ function logTooManyReturnedIfRequired (tooManyReturnedLimitCounters, allObjects,

if (allObjects && Array.isArray(allObjects) && allObjects.length > realLimit) {
const executionContext = getExecutionContext()
const reqId = executionContext?.reqId
const taskId = executionContext?.taskId
logger.warn({
msg: 'tooManyReturned',
tooManyLimit: realLimit,
count: allObjects.length,
functionName,
schemaName,
data,
reqId: executionContext?.reqId,
reqId,
taskId,
})
tooManyReturnedLimitCounters.shift() // remove counter and mark as already notified
}
Expand Down Expand Up @@ -108,14 +113,12 @@ class GqlWithKnexLoadList {
}
} while (--maxiterationsCount > 0 && newchunk.length)

logger.info({
msg: 'Return count',
logTooManyReturnedIfRequired([TOO_MANY_RETURNED_RESULT_LOG_LIMIT], all, {
functionName: 'GqlWithKnexLoadList.load',
schemaName: this.listKey,
data: {
singleRelations: this.singleRelations, multipleRelations: this.multipleRelations, where: this.where, fields: this.fields,
},
count: allLength,
})

return all
Expand Down Expand Up @@ -208,8 +211,7 @@ const loadListByChunks = async ({
let newChunk = []
let all = []
let newChunkLength
let haveWarnedAboutTooManyObjs = false
let allLength = 0
let tooManyReturnedLimitCounters = [...TOO_MANY_RETURNED_LOG_LIMITS]

const startTime = Date.now()

Expand All @@ -226,15 +228,14 @@ const loadListByChunks = async ({
limit,
loadListByChunksArgs: { where },
},
count: allLength,
count: all.length,
})

throw new Error('Operation timed out')
}

newChunk = await list.getAll(context, where, { sortBy, first: chunkSize, skip: skip })
newChunkLength = newChunk.length
allLength += newChunk.length

if (newChunkLength > 0) {
if (isFunction(chunkProcessor)) {
Expand All @@ -247,30 +248,22 @@ const loadListByChunks = async ({
all = all.concat(newChunk)
}

if ((!haveWarnedAboutTooManyObjs) && (all && Array.isArray(all) && all.length > 1000)) {
logger.warn({
msg: 'tooManyReturned',
functionName: 'loadListByChunks',
schemaName: get(list, 'gql.SINGULAR_FORM', ''),
data: {
limit: 1000,
loadListByChunksArgs: { where },
},
})
haveWarnedAboutTooManyObjs = true
}
logTooManyReturnedIfRequired(tooManyReturnedLimitCounters, all, {
functionName: 'loadListByChunks',
schemaName: get(list, 'gql.SINGULAR_FORM', ''),
data: {
where, sortBy, chunkSize,
},
})

} while (--maxIterationsCount > 0 && newChunkLength)

logger.info({
msg: 'Return count',
logTooManyReturnedIfRequired([TOO_MANY_RETURNED_RESULT_LOG_LIMIT], all, {
functionName: 'loadListByChunks',
schemaName: get(list, 'gql.SINGULAR_FORM', ''),
data: {
chunkSize,
limit,
loadListByChunksArgs: { where },
where, sortBy, chunkSize,
},
count: allLength,
})

return all
Expand Down

0 comments on commit 6482b6d

Please sign in to comment.