|
| 1 | +var plugins = require('../../../../plugins/pluginManager'); |
| 2 | +const dashboard = require('../../../../plugins/dashboards/api/parts/dashboards.js'); |
| 3 | + |
| 4 | +async function recheckFunnelWidgets(countlyDb) { |
| 5 | + console.log("Detecting deleted data for funnels..."); |
| 6 | + |
| 7 | + const widgets = await countlyDb.collection('widgets').find({ widget_type: 'funnels', funnel_type: { $exists: true, $ne: [] } }, { funnel_type: 1 }).toArray(); |
| 8 | + if (!widgets || !widgets.length) { |
| 9 | + console.log("No widgets found."); |
| 10 | + return; |
| 11 | + } |
| 12 | + const funnelIdsInWidgets = widgets.map(widget => widget.funnel_type[0].split('***')[1]); |
| 13 | + const existingFunnels = await countlyDb.collection('funnels').find({ _id: { $in: funnelIdsInWidgets } }, { _id: 1, app_id: 1 }).toArray(); |
| 14 | + if (!existingFunnels || !existingFunnels.length) { |
| 15 | + console.log("No funnels found."); |
| 16 | + return; |
| 17 | + } |
| 18 | + const formattedExistingFunnels = existingFunnels.map(funnel => funnel.app_id + "***" + funnel._id.toString()); |
| 19 | + const missingFunnelIds = widgets.filter(function(result) { |
| 20 | + return !formattedExistingFunnels.includes(result.funnel_type[0]); |
| 21 | + }).map(function(result) { |
| 22 | + return result.funnel_type[0]; |
| 23 | + }); |
| 24 | + |
| 25 | + if (missingFunnelIds.length) { |
| 26 | + const matchOperator = { |
| 27 | + widget_type: "funnels", |
| 28 | + "funnel_type": { |
| 29 | + $in: missingFunnelIds |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + try { |
| 34 | + return await dashboard.removeDeletedRecordsFromWidgets({member: {username: 'unknown'}}, matchOperator, countlyDb); |
| 35 | + } |
| 36 | + catch (error) { |
| 37 | + console.log('Error while sending a request: ', error); |
| 38 | + } |
| 39 | + } |
| 40 | + else { |
| 41 | + console.log("No deleted funnels found in widgets."); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +async function recheckFormulasWidgets(countlyDb) { |
| 46 | + console.log("Detecting deleted data for formulas..."); |
| 47 | + |
| 48 | + const widgets = await countlyDb.collection('widgets').find({ widget_type: 'formulas', cmetric_refs: { $exists: true, $ne: [] } }, { "cmetric_refs._id": 1 }).toArray(); |
| 49 | + if (!widgets || !widgets.length) { |
| 50 | + console.log("No widgets found."); |
| 51 | + return; |
| 52 | + } |
| 53 | + const ids = widgets.map(item => countlyDb.ObjectID(item.cmetric_refs[0]._id)); |
| 54 | + const existingFormulas = await countlyDb.collection('calculated_metrics').find({ _id: { $in: ids } }, { _id: 1 }).toArray(); |
| 55 | + if (!existingFormulas || !existingFormulas.length) { |
| 56 | + console.log("No formulas found."); |
| 57 | + return; |
| 58 | + } |
| 59 | + const missingFormulasIds = widgets.filter(widget => { |
| 60 | + return !existingFormulas.some(formula => String(formula._id) === widget.cmetric_refs[0]._id); |
| 61 | + }).map(function(result) { |
| 62 | + return result.cmetric_refs[0]._id; |
| 63 | + }); |
| 64 | + |
| 65 | + if (missingFormulasIds.length) { |
| 66 | + const matchOperator = { |
| 67 | + widget_type: "formulas", |
| 68 | + "cmetric_refs": { |
| 69 | + $elemMatch: { |
| 70 | + _id: { |
| 71 | + $in: missingFormulasIds |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + try { |
| 78 | + return await dashboard.removeDeletedRecordsFromWidgets({member: {username: 'unknown'}}, matchOperator, countlyDb); |
| 79 | + } |
| 80 | + catch (error) { |
| 81 | + console.log('Error while sending a request: ', error); |
| 82 | + } |
| 83 | + } |
| 84 | + else { |
| 85 | + console.log("No deleted formulas found in widgets."); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +async function recheckDrillWidgets(countlyDb) { |
| 90 | + console.log("Detecting deleted data for drill..."); |
| 91 | + const matchOperator = { |
| 92 | + "widget_type": "drill", |
| 93 | + "drill_query": { $size: 0 } |
| 94 | + }; |
| 95 | + |
| 96 | + try { |
| 97 | + return await dashboard.removeDeletedRecordsFromWidgets({member: {username: 'unknown'}}, matchOperator, countlyDb); |
| 98 | + } |
| 99 | + catch (error) { |
| 100 | + console.log('Error while sending a request: ', error); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +plugins.dbConnection().then(async(countlyDb) => { |
| 105 | + try { |
| 106 | + await recheckFunnelWidgets(countlyDb); |
| 107 | + } |
| 108 | + catch (error) { |
| 109 | + console.log('Error in recheckFunnelWidgets:', error); |
| 110 | + } |
| 111 | + |
| 112 | + try { |
| 113 | + await recheckFormulasWidgets(countlyDb); |
| 114 | + } |
| 115 | + catch (error) { |
| 116 | + console.log('Error in recheckFormulasWidgets:', error); |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + await recheckDrillWidgets(countlyDb); |
| 121 | + } |
| 122 | + catch (error) { |
| 123 | + console.log('Error in recheckDrillWidgets:', error); |
| 124 | + } |
| 125 | + finally { |
| 126 | + countlyDb.close(); |
| 127 | + } |
| 128 | +}); |
0 commit comments