All files / utils importCreditEstimate.js

100% Statements 10/10
83.33% Branches 10/12
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                        17x 17x 28x 10x       28x 1x   1x 1x       17x     3x  
/**
 * Mirrors entities/create.js's / items/create.js's own size computation — an
 * estimate, not required to be byte-exact (some entries may later be skipped by
 * import-worker.js due to name conflicts or per-item validation failures, in
 * which case the real debit ends up lower than this preflight number, never
 * higher).
 *
 * @param {Array<object>} entries the import's entries
 * @param {number} dynamoDbStorageCreditsPerKb
 * @returns {number}
 */
function estimateImportCreditCost(entries, dynamoDbStorageCreditsPerKb) {
  let totalBytes = 0
  for (const entry of entries) {
    if (entry.mode === 'create') {
      totalBytes += Buffer.byteLength(
        JSON.stringify({ fields: entry.entity?.fields || [], forms: entry.entity?.forms || [] })
      )
    }
    if (entry.includeItems && Array.isArray(entry.items)) {
      for (const rawItem of entry.items) {
        // eslint-disable-next-line no-unused-vars
        const { originalId, ...data } = rawItem || {}
        totalBytes += Buffer.byteLength(JSON.stringify(data))
      }
    }
  }
  return Math.ceil((totalBytes / 1024) * dynamoDbStorageCreditsPerKb)
}
 
module.exports = { estimateImportCreditCost }