Skip to content

Commit 525c641

Browse files
committed
[SER-690] Removed requestProcessor when removing widgets
1 parent 7b409a0 commit 525c641

File tree

2 files changed

+49
-84
lines changed

2 files changed

+49
-84
lines changed

bin/upgrade/DEV/scripts/remove_deleted_recors_from_widgets.js

+9-59
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
var plugins = require('../../../../plugins/pluginManager');
2-
const fetch = require('node-fetch');
3-
const https = require('https');
4-
5-
// PLEASE ENTER BELOW INFORMATIONS BEFORE RUNNING THIS SCRIPT
6-
const API_KEY = "YOUR_API_KEY";
7-
const SERVER_URL = "http://localhost";
8-
9-
10-
async function sendRequest(params) {
11-
try {
12-
const url = new URL(params.Url || SERVER_URL);
13-
const options = {
14-
method: params.requestType,
15-
headers: {
16-
'Content-Type': 'application/json',
17-
},
18-
agent: new https.Agent({
19-
rejectUnauthorized: false,
20-
}),
21-
};
22-
23-
const response = await fetch(url.href, options);
24-
if (response.status === 200 || response.status === 201) {
25-
return true;
26-
}
27-
else {
28-
return { err: 'There was an error while sending a request.', code: response.status + " " + response.statusText };
29-
}
30-
}
31-
catch (error) {
32-
return { err: 'There was an error while sending a request.', code: error.code || 'Unknown' };
33-
}
34-
}
2+
const dashboard = require('../../../../plugins/dashboards/api/parts/dashboards.js');
353

364
async function recheckFunnelWidgets(countlyDb) {
375
console.log("Detecting deleted data for funnels...");
@@ -63,13 +31,7 @@ async function recheckFunnelWidgets(countlyDb) {
6331
};
6432

6533
try {
66-
var response = await sendRequest({
67-
requestType: 'GET',
68-
Url: SERVER_URL + "/o/dashboards/recheck_widgets?apiKey=" + API_KEY + "&matchOperator=" + JSON.stringify(matchOperator),
69-
});
70-
if (!response.err) {
71-
console.log("Deleted funnels removed from widgets successfully.");
72-
}
34+
return await dashboard.removeDeletedRecordsFromWidgets({member: {username: 'unknown'}}, matchOperator, countlyDb);
7335
}
7436
catch (error) {
7537
console.log('Error while sending a request: ', error);
@@ -113,13 +75,7 @@ async function recheckFormulasWidgets(countlyDb) {
11375
};
11476

11577
try {
116-
var response = await sendRequest({
117-
requestType: 'GET',
118-
Url: SERVER_URL + "/o/dashboards/recheck_widgets?apiKey=" + API_KEY + "&matchOperator=" + JSON.stringify(matchOperator),
119-
});
120-
if (!response.err) {
121-
console.log("Deleted formulas removed from widgets successfully.");
122-
}
78+
return await dashboard.removeDeletedRecordsFromWidgets({member: {username: 'unknown'}}, matchOperator, countlyDb);
12379
}
12480
catch (error) {
12581
console.log('Error while sending a request: ', error);
@@ -130,28 +86,21 @@ async function recheckFormulasWidgets(countlyDb) {
13086
}
13187
}
13288

133-
async function recheckDrillWidgets() {
89+
async function recheckDrillWidgets(countlyDb) {
13490
console.log("Detecting deleted data for drill...");
13591
const matchOperator = {
13692
"widget_type": "drill",
13793
"drill_query": { $size: 0 }
13894
};
13995

14096
try {
141-
var response = await sendRequest({
142-
requestType: 'GET',
143-
Url: SERVER_URL + "/o/dashboards/recheck_widgets?apiKey=" + API_KEY + "&matchOperator=" + JSON.stringify(matchOperator),
144-
});
145-
if (!response.err) {
146-
console.log("Deleted drills removed from widgets successfully.");
147-
}
97+
return await dashboard.removeDeletedRecordsFromWidgets({member: {username: 'unknown'}}, matchOperator, countlyDb);
14898
}
14999
catch (error) {
150100
console.log('Error while sending a request: ', error);
151101
}
152102
}
153103

154-
155104
plugins.dbConnection().then(async(countlyDb) => {
156105
try {
157106
await recheckFunnelWidgets(countlyDb);
@@ -168,11 +117,12 @@ plugins.dbConnection().then(async(countlyDb) => {
168117
}
169118

170119
try {
171-
await recheckDrillWidgets();
120+
await recheckDrillWidgets(countlyDb);
172121
}
173122
catch (error) {
174123
console.log('Error in recheckDrillWidgets:', error);
175124
}
176-
countlyDb.close();
125+
finally {
126+
countlyDb.close();
127+
}
177128
});
178-

plugins/dashboards/api/parts/dashboards.js

+40-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var countlyModel = require("../../../../api/lib/countly.model.js"),
99
countlyCommon = require('../../../../api/lib/countly.common'),
1010
fetch = require("../../../../api/parts/data/fetch.js"),
1111
log = common.log('dashboards:api'),
12-
requestProcessor = require('../../../../api/utils/requestProcessor.js'),
1312
plugins = require("../../../pluginManager.js");
1413

1514
/** @lends module:api/parts/data/dashboard */
@@ -563,25 +562,28 @@ dashboard.fetchNoteData = async function(params, apps, widget) {
563562
return widget;
564563
};
565564

566-
567565
/**
568566
* Remove deleted records from widgets
569-
*
570-
* @param {string} apiKey user's api key
567+
* @param {object} params params object
571568
* @param {object} matchOperator match operator for aggregation
569+
* @param {object} db database object - if coming from script
572570
* @returns {boolean} true if success
573571
*/
574-
dashboard.removeDeletedRecordsFromWidgets = async function(apiKey, matchOperator) {
572+
dashboard.removeDeletedRecordsFromWidgets = async function(params, matchOperator, db) {
575573
try {
576-
if (!apiKey || !matchOperator) {
577-
log.e('missing parameters for removeDeletedRecordsFromWidgets', apiKey, matchOperator);
574+
if (!params || !matchOperator) {
575+
log.e('missing parameters for removeDeletedRecordsFromWidgets', params, matchOperator);
578576
return false;
579577
}
580578

581579
if (typeof matchOperator === 'string') {
582580
matchOperator = JSON.parse(matchOperator);
583581
}
584582

583+
if (typeof db !== 'undefined') {
584+
common.db = db;
585+
}
586+
585587
var pipeline = [
586588
{
587589
$match: matchOperator
@@ -614,22 +616,17 @@ dashboard.removeDeletedRecordsFromWidgets = async function(apiKey, matchOperator
614616
log.e('dashbordId or widgetId could not found in remove widget');
615617
continue;
616618
}
617-
var params = {
618-
'req': {
619-
url: "/i/dashboards/remove-widget?dashboard_id=" + dashboardId + "&widget_id=" + widgetId + "&api_key=" + apiKey
620-
},
621-
//adding custom processing for API responses
622-
'APICallback': function(err, responseData, headers, returnCode) {
623-
if (err) {
624-
log.e('Error while removing widget from dashboard', err, responseData, headers, returnCode);
625-
return false;
619+
620+
await new Promise((resolve, reject) => {
621+
dashboard.deleteWidget(params, dashboardId, widgetId, function(success) {
622+
if (success) {
623+
resolve();
626624
}
627625
else {
628-
return true;
626+
reject();
629627
}
630-
}
631-
};
632-
requestProcessor.processRequest(params);
628+
});
629+
});
633630
}
634631
return true;
635632
}
@@ -639,16 +636,34 @@ dashboard.removeDeletedRecordsFromWidgets = async function(apiKey, matchOperator
639636
}
640637
};
641638

639+
dashboard.deleteWidget = function(params, dashboardId, widgetId, callback) {
640+
common.db.collection("dashboards").update({_id: common.db.ObjectID(dashboardId)}, { $pull: {widgets: common.db.ObjectID(widgetId)}}, function(dashboardErr) {
641+
if (!dashboardErr) {
642+
common.db.collection("widgets").findAndModify({_id: common.db.ObjectID(widgetId)}, {}, {}, {remove: true}, function(widgetErr, widgetResult) {
643+
if (widgetErr || !widgetResult || !widgetResult.value) {
644+
common.returnMessage(params, 500, "Failed to remove widget");
645+
callback(false);
646+
}
647+
else {
648+
var logData = widgetResult.value;
649+
logData.dashboard = dashboard.name;
642650

643-
dashboard.callWidgetRecheck = function(apiKey, matchOperator) {
644-
return plugins.dispatch("/dashboard/clean-deleted-widgets", {
645-
api_key: apiKey,
646-
match: matchOperator
651+
plugins.dispatch("/systemlogs", {params: params, action: "widget_deleted", data: logData});
652+
plugins.dispatch("/dashboard/widget/deleted", {params: params, widget: widgetResult.value});
653+
common.returnMessage(params, 200, 'Success');
654+
callback(true);
655+
}
656+
});
657+
}
658+
else {
659+
common.returnMessage(params, 500, "Failed to remove widget");
660+
callback(false);
661+
}
647662
});
648663
};
649664

650665
plugins.register("/dashboard/clean-deleted-widgets", async function(ob) {
651-
var response = await dashboard.removeDeletedRecordsFromWidgets(ob.api_key, ob.match);
666+
var response = await dashboard.removeDeletedRecordsFromWidgets(ob.params, ob.match);
652667
return response;
653668
}, true);
654669

0 commit comments

Comments
 (0)