Skip to content

Commit

Permalink
Merge branch 'master' into next
Browse files Browse the repository at this point in the history
# Conflicts:
#	plugins/views/tests.js
  • Loading branch information
ar2rsawseen committed Nov 2, 2023
2 parents 719e47f + 694b2bc commit d4620d9
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 21 deletions.
10 changes: 6 additions & 4 deletions api/utils/rights.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,12 @@ exports.validateRead = function(params, feature, callback, callbackParam) {
}
else {
isFeatureAllowedInReadPermissionObject = false;
for (var i = 0; i < feature.length; i++) {
if (isPermissionObjectExistForRead && (member.permission.r[params.qstring.app_id].all || (member.permission.r[params.qstring.app_id].allowed && member.permission.r[params.qstring.app_id].allowed[feature[i]]))) {
isFeatureAllowedInReadPermissionObject = true;
break;
if (feature) {
for (var i = 0; i < feature.length; i++) {
if (isPermissionObjectExistForRead && (member.permission.r[params.qstring.app_id].all || (member.permission.r[params.qstring.app_id].allowed && member.permission.r[params.qstring.app_id].allowed[feature[i]]))) {
isFeatureAllowedInReadPermissionObject = true;
break;
}
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"moment-timezone": "0.5.43",
"mongodb": "4.9.1",
"nginx-conf": "2.1.0",
"nodemailer": "6.9.6",
"nodemailer": "6.9.7",
"object-hash": "3.0.0",
"offline-geocoder": "git+https://github.com/Countly/offline-geocoder.git",
"properties-parser": "0.6.0",
Expand Down
74 changes: 72 additions & 2 deletions plugins/views/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,77 @@ const escapedViewSegments = { "name": true, "segment": true, "height": true, "wi
});
});
}
else if (ob.params.qstring.method === "omit_segments") {
validateDelete(ob.params, FEATURE_NAME, function(params) {
if (!params.qstring.app_id) {
common.returnMessage(params, 400, 'Missing request parameter: app_id');
resolve();
return;
}
if (params.qstring.omit_list) {
var omit = [];
try {
omit = JSON.parse(ob.params.qstring.omit_list);
}
catch (SyntaxError) {
log.e('Parsing data failed: ', ob.params.qstring.omit_list);
common.returnMessage(params, 400, 'Cannot parse parameter: omit_list');
resolve();
return;
}
if (!Array.isArray(omit)) {
common.returnMessage(params, 400, 'Invalid request parameter: omit_list');
resolve();
return;
}
var unset = {};
for (var zz = 0; zz < omit.length; zz++) {
unset["segments." + omit[zz]] = "";
}
common.db.collection('views').updateOne({"_id": common.db.ObjectID(appId)}, {$set: {omit: omit}, "$unset": unset}, function(err5) {
if (err5) {
log.e(err5);
common.returnMessage(params, 400, "Updating database failed");
resolve();
}
else {
plugins.dispatch("/systemlogs", {params: params, action: "view_segments_ommit", data: { update: omit}});

var promises = [];
var errCn = 0;
for (var z = 0; z < omit.length; z++) {
var colName = "app_viewdata" + crypto.createHash('sha1').update(omit[z] + appId).digest('hex');
promises.push(new Promise(function(resolve2) {
common.db.collection(colName).drop(function(err) {
if (err && err.code !== 26) { //if error is not collection not found.(Because it is possible for it to not exist)
log.e(JSON.stringify(err));
errCn++;
}
resolve2();
});
}));
}
Promise.all(promises).then(function() {
log.d("Segments omittion compleated for:" + JSON.stringify(omit));
if (errCn > 0) {
plugins.dispatch("/systemlogs", {params: params, action: "view_segments_ommit_complete", data: { update: omit, error: "Failed to delete some(" + errCn + ") collections. Please call omiting again."}});
}
else {
plugins.dispatch("/systemlogs", {params: params, action: "view_segments_ommit_complete", data: { update: omit}});
}
});
common.returnMessage(params, 200, 'Success');
resolve();
}
});
}
else {
common.returnMessage(params, 400, 'Nothing is passed for omiting');
}
});
}
else {
common.returnMessage(ob.params, 400, 'Invalid method. Must be one of:delete_view,rename_views ');
common.returnMessage(ob.params, 400, 'Invalid method. Must be one of:delete_view,rename_views,omit_segments ');
resolve();
}
});
Expand Down Expand Up @@ -1962,6 +2031,7 @@ const escapedViewSegments = { "name": true, "segment": true, "height": true, "wi
function recordMetrics(params, currEvent, user, view, viewInfo) {
viewInfo = viewInfo || {};
viewInfo.segments = viewInfo.segments || {};
viewInfo.omit = viewInfo.omit || [];

//making sure metrics are strings
// tmpSet["meta_v2." + tmpMetric.set + "." + escapedMetricVal] = true;
Expand Down Expand Up @@ -1991,7 +2061,7 @@ const escapedViewSegments = { "name": true, "segment": true, "height": true, "wi
segKey = "platform";
}
let tmpSegVal = currEvent.segmentation[segKey] + "";
if (!(escapedViewSegments[segKey] === true)) {
if (!(escapedViewSegments[segKey] === true) && !(Array.isArray(viewInfo.omit) && viewInfo.omit.indexOf(segKey) !== -1)) {
if (!viewInfo.segments[segKey] && (segKey === 'platform' || Object.keys(viewInfo.segments).length < plugins.getConfig("views").segment_limit)) {
viewInfo.segments[segKey] = {};
}
Expand Down
134 changes: 134 additions & 0 deletions plugins/views/scripts/omitViewSegments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//Put script in ./countly/plugins/views/omitViewSegments.js
/*
The script deletes data for specific segments in aggregated data. It also can set an omitting list in the database to ensure that segments are also omitted on incoming data.
*/

//to run:
// cd `countly dir`
// node ./plugins/views/scripts/omitViewSegments.js


var crypto = require('crypto');
var pluginManager = require('./../../../plugins/pluginManager.js');
var Promise = require("bluebird");

var app_list = []; //leave empty to process all apps or add specific app ids to the array.
var DRY_RUN = false; //set to true to see what will be deleted without actually deleting anything
var save_list_in_database = true; //If omitting is successful, should list be saved in views setting. If saved in views setting, it will ensure segment not reappearing on incoming data. It will have effect only if upgraded to version at least 23.06.16

var omit = ["Cats", "Dogs"];//list with segments to omit

Promise.all([pluginManager.dbConnection("countly")]).spread(function(countlyDb) {
if (!Array.isArray(omit) || omit.length === 0) {
console.log("No segments to omit");
countlyDb.close();
return;
}
else {
console.log("Validating app list: ");
getAppList({db: countlyDb}, function(err, apps) {
console.log(apps.length + " apps found");
if (apps && apps.length > 0) {
Promise.each(apps, function(app) {
return new Promise(function(resolve) {
console.log("processing app:" + app.name);
var promises = [];
var errCn = 0;
for (var z = 0; z < omit.length; z++) {
var colName = "app_viewdata" + crypto.createHash('sha1').update(omit[z] + (app._id + "")).digest('hex');
promises.push(new Promise(function(resolve2) {
if (DRY_RUN) {
console.log("DRY RUN: collection we would try to delete: " + colName);
resolve2();
}
else {
console.log(colName);
countlyDb.collection(colName).drop(function(err) {
if (err && err.code !== 26) {
console.log(JSON.stringify(err));
errCn++;
}
resolve2();
});
}
}));
}
Promise.all(promises).then(function() {
console.log("Segments omittion compleated for:" + JSON.stringify(omit));
if (errCn > 0) {
console.log("Some errors occured while deleting collections");
resolve();
}
else {
if (save_list_in_database && !DRY_RUN) {
console.log("adding segments to omit list in view document. Only if upgraded to lates version this list will be taken in account on incoming data.");

var unset = {};
for (var z = 0; z < omit.length; z++) {
unset["segments." + omit[z]] = "";
}

countlyDb.collection("views").update({_id: app._id}, {$set: {"omit": omit}, "$unset": unset}, function(err) {
if (err) {
console.log(err);
}
resolve();
});
}
else {
resolve();
}

}

});
});
}).then(function() {
console.log("completed");
countlyDb.close();
return;

}).catch(function(rejection) {
console.log("Error");
console.log("Error:", rejection);
countlyDb.close();
return;
});
}
else {
console.log("exiting as there are no apps to process.");
countlyDb.close();
return;
}
});
}

});

function getAppList(options, callback) {
var query = {};
if (app_list && app_list.length > 0) {
var listed = [];
for (var z = 0; z < app_list.length; z++) {
listed.push(options.db.ObjectId(app_list[z]));
}
query = {_id: {$in: listed}};
}

try {
options.db.collection("apps").find(query).toArray(function(err, apps) {
if (err) {
console.log("Couldn't get app list in dataviews.getAppList");
callback(err, []);
}
else {
callback(err, apps);
}
});
}
catch (err) {
console.log("Error getting apps: ", err);
callback(err, []);
}

}
Loading

0 comments on commit d4620d9

Please sign in to comment.