|
| 1 | +/* eslint-disable no-console */ |
| 2 | +/* eslint-disable consistent-return */ |
| 3 | +/* eslint-disable no-restricted-syntax */ |
| 4 | +/* eslint-disable no-param-reassign */ |
| 5 | +/* |
| 6 | + * Compare metadata between ES and DB. |
| 7 | + */ |
| 8 | +const lodash = require('lodash'); |
| 9 | + |
| 10 | +const scriptUtil = require('./util'); |
| 11 | +const scriptConstants = require('./constants'); |
| 12 | + |
| 13 | +const hashKeyMapping = { |
| 14 | + ProjectTemplate: 'id', |
| 15 | + ProductTemplate: 'id', |
| 16 | + ProjectType: 'key', |
| 17 | + ProductCategory: 'key', |
| 18 | + MilestoneTemplate: 'id', |
| 19 | + OrgConfig: 'id', |
| 20 | + Form: 'id', |
| 21 | + PlanConfig: 'id', |
| 22 | + PriceConfig: 'id', |
| 23 | + BuildingBlock: 'id', |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Process a single delta. |
| 28 | + * |
| 29 | + * @param {String} modelName the model name the delta belongs to |
| 30 | + * @param {Object} delta the diff delta. |
| 31 | + * @param {Object} dbData the data from DB |
| 32 | + * @param {Object} esData the data from ES |
| 33 | + * @param {Object} finalData the data patched |
| 34 | + * @returns {undefined} |
| 35 | + */ |
| 36 | +function processDelta(modelName, delta, dbData, esData, finalData) { |
| 37 | + const hashKey = hashKeyMapping[modelName]; |
| 38 | + if (delta.dataType === 'array' && delta.path.length === 1) { |
| 39 | + if (delta.type === 'delete') { |
| 40 | + console.log(`one dbOnly found for ${modelName} with ${hashKey} ${delta.originalValue[hashKey]}`); |
| 41 | + return { |
| 42 | + type: 'dbOnly', |
| 43 | + modelName, |
| 44 | + hashKey, |
| 45 | + hashValue: delta.originalValue[hashKey], |
| 46 | + dbCopy: delta.originalValue, |
| 47 | + }; |
| 48 | + } |
| 49 | + if (delta.type === 'add') { |
| 50 | + console.log(`one esOnly found for ${modelName} with ${hashKey} ${delta.value[hashKey]}`); |
| 51 | + return { |
| 52 | + type: 'esOnly', |
| 53 | + modelName, |
| 54 | + hashKey, |
| 55 | + hashValue: delta.value[hashKey], |
| 56 | + esCopy: delta.value, |
| 57 | + }; |
| 58 | + } |
| 59 | + } |
| 60 | + if (['add', 'delete', 'modify'].includes(delta.type)) { |
| 61 | + const path = scriptUtil.generateJSONPath(lodash.slice(delta.path, 1)); |
| 62 | + const hashValue = lodash.get(finalData, lodash.slice(delta.path, 0, 1))[hashKey]; |
| 63 | + const hashObject = lodash.set({}, hashKey, hashValue); |
| 64 | + const dbCopy = lodash.find(dbData, hashObject); |
| 65 | + const esCopy = lodash.find(esData, hashObject); |
| 66 | + console.log(`one mismatch found for ${modelName} with ${hashKey} ${hashValue}`); |
| 67 | + return { |
| 68 | + type: 'mismatch', |
| 69 | + kind: delta.type, |
| 70 | + modelName, |
| 71 | + hashKey, |
| 72 | + hashValue, |
| 73 | + path, |
| 74 | + dbCopy, |
| 75 | + esCopy, |
| 76 | + }; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +/** |
| 82 | + * Compare Metadata data from ES and DB. |
| 83 | + * |
| 84 | + * @param {Object} dbData the data from DB |
| 85 | + * @param {Object} esData the data from ES |
| 86 | + * @returns {Object} the data to feed handlebars template |
| 87 | + */ |
| 88 | +function compareMetadata(dbData, esData) { |
| 89 | + const data = { |
| 90 | + nestedModels: {}, |
| 91 | + }; |
| 92 | + |
| 93 | + const countInconsistencies = () => { |
| 94 | + lodash.set(data, 'meta.totalObjects', 0); |
| 95 | + lodash.map(data.nestedModels, (model) => { |
| 96 | + const counts = Object.keys(model.mismatches).length + model.dbOnly.length + model.esOnly.length; |
| 97 | + lodash.set(model, 'meta.counts', counts); |
| 98 | + data.meta.totalObjects += counts; |
| 99 | + }); |
| 100 | + }; |
| 101 | + |
| 102 | + const storeDelta = (modelName, delta) => { |
| 103 | + if (lodash.isUndefined(data.nestedModels[modelName])) { |
| 104 | + data.nestedModels[modelName] = { |
| 105 | + mismatches: {}, |
| 106 | + dbOnly: [], |
| 107 | + esOnly: [], |
| 108 | + }; |
| 109 | + } |
| 110 | + if (delta.type === 'mismatch') { |
| 111 | + if (lodash.isUndefined(data.nestedModels[modelName].mismatches[delta.hashValue])) { |
| 112 | + data.nestedModels[modelName].mismatches[delta.hashValue] = []; |
| 113 | + } |
| 114 | + data.nestedModels[modelName].mismatches[delta.hashValue].push(delta); |
| 115 | + return; |
| 116 | + } |
| 117 | + if (delta.type === 'dbOnly') { |
| 118 | + data.nestedModels[modelName].dbOnly.push(delta); |
| 119 | + return; |
| 120 | + } |
| 121 | + if (delta.type === 'esOnly') { |
| 122 | + data.nestedModels[modelName].esOnly.push(delta); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + for (const refPath of Object.keys(scriptConstants.associations.metadata)) { |
| 127 | + const modelName = scriptConstants.associations.metadata[refPath]; |
| 128 | + const { deltas, finalData } = scriptUtil.diffData( |
| 129 | + dbData[refPath], |
| 130 | + esData[refPath], |
| 131 | + { |
| 132 | + hashKey: hashKeyMapping[modelName], |
| 133 | + modelPathExprssions: lodash.set({}, modelName, '[*]'), |
| 134 | + }, |
| 135 | + ); |
| 136 | + for (const delta of deltas) { |
| 137 | + if (scriptUtil.isIgnoredPath(`metadata.${refPath}`, delta.path)) { |
| 138 | + continue; // eslint-disable-line no-continue |
| 139 | + } |
| 140 | + const deltaWithCopy = processDelta(modelName, delta, dbData[refPath], esData[refPath], finalData); |
| 141 | + if (deltaWithCopy) { |
| 142 | + storeDelta(modelName, deltaWithCopy); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + countInconsistencies(); |
| 147 | + return data; |
| 148 | +} |
| 149 | + |
| 150 | +module.exports = { |
| 151 | + compareMetadata, |
| 152 | +}; |
0 commit comments