Skip to content

Commit 17e44ee

Browse files
authored
Merge pull request #6467 from Countly/sdk_bom
[sdk] Backoff and Enforcement additions
2 parents a2efcbd + 08daef5 commit 17e44ee

File tree

6 files changed

+476
-64
lines changed

6 files changed

+476
-64
lines changed

plugins/sdk/api/api.js

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ plugins.register("/permissions/features", function(ob) {
5252
"ltl":true,
5353
"lt":true,
5454
"rcz":true
55+
"bom": true,
56+
"bom_at": 10,
57+
"bom_rqp": 0.5,
58+
"bom_ra": 24,
59+
"bom_d": 60
5560
}
5661
* }
5762
*/
@@ -60,21 +65,29 @@ plugins.register("/permissions/features", function(ob) {
6065
if (params.qstring.method !== "sc") {
6166
return false;
6267
}
63-
return new Promise(function(resolve) {
64-
getSDKConfig(params).then(function(config) {
65-
delete config._id;
66-
config.v = 1;
67-
config.t = Date.now();
68-
config.c = config.config || {};
68+
return getSDKConfig(params).then(function(config) {
69+
delete config._id;
70+
let cc = config.config || {};
71+
if (typeof cc.bom_rqp !== "undefined") {
72+
cc.bom_rqp = cc.bom_rqp / 100;
73+
}
74+
config.v = 2;
75+
config.t = Date.now();
76+
config.c = cc;
77+
78+
return getEnforcement(params).then(function(enforcement) {
79+
if (enforcement && enforcement.enforcement) {
80+
for (let key in config.c) {
81+
if (enforcement.enforcement[key] === false) {
82+
delete config.c[key];
83+
}
84+
}
85+
}
6986
delete config.config;
7087
common.returnOutput(params, config);
71-
})
72-
.catch(function(err) {
73-
common.returnMessage(params, 400, 'Error: ' + err);
74-
})
75-
.finally(function() {
76-
resolve();
77-
});
88+
});
89+
}).catch(function(err) {
90+
common.returnMessage(params, 400, 'Error: ' + err);
7891
});
7992
});
8093

@@ -100,6 +113,7 @@ plugins.register("/permissions/features", function(ob) {
100113
if (params.qstring.method === "sdk-config") {
101114
validateRead(params, FEATURE_NAME, function() {
102115
getSDKConfig(params).then(function(res) {
116+
// TODO: check if filtering is needed here too
103117
common.returnOutput(params, res.config || {});
104118
})
105119
.catch(function(err) {
@@ -153,7 +167,12 @@ plugins.register("/permissions/features", function(ob) {
153167
"ltlpt",
154168
"ltl",
155169
"lt",
156-
"rcz"
170+
"rcz",
171+
"bom",
172+
"bom_at",
173+
"bom_rqp",
174+
"bom_ra",
175+
"bom_d"
157176
];
158177
for (var key in configToSave) {
159178
if (validOptions.indexOf(key) === -1) {
@@ -178,6 +197,17 @@ plugins.register("/permissions/features", function(ob) {
178197
});
179198
});
180199
}
200+
if (params.qstring.method === "sdk-enforcement") {
201+
validateRead(params, FEATURE_NAME, function() {
202+
getEnforcement(params).then(function(res) {
203+
common.returnOutput(params, res.enforcement || {});
204+
})
205+
.catch(function(err) {
206+
common.returnMessage(params, 400, 'Error: ' + err);
207+
});
208+
});
209+
return true;
210+
}
181211

182212
return false;
183213
});
@@ -189,6 +219,32 @@ plugins.register("/permissions/features", function(ob) {
189219
switch (paths[3]) {
190220
case 'update-parameter': validateUpdate(params, FEATURE_NAME, updateParameter);
191221
break;
222+
case 'update-enforcement':
223+
validateUpdate(params, FEATURE_NAME, function() {
224+
var enforcement = params.qstring.enforcement;
225+
if (typeof enforcement === "string") {
226+
try {
227+
enforcement = JSON.parse(enforcement);
228+
}
229+
catch (SyntaxError) {
230+
return common.returnMessage(params, 400, 'Error parsing enforcement');
231+
}
232+
}
233+
common.outDb.collection('sdk_enforcement').updateOne(
234+
{ _id: params.app_id + "" },
235+
{ $set: { enforcement: enforcement } },
236+
{ upsert: true },
237+
function(err) {
238+
if (err) {
239+
common.returnMessage(params, 500, 'Error saving enforcement to database');
240+
}
241+
else {
242+
common.returnOutput(params, { result: 'Success' });
243+
}
244+
}
245+
);
246+
});
247+
break;
192248
default: common.returnMessage(params, 404, 'Invalid endpoint');
193249
break;
194250
}
@@ -497,4 +553,21 @@ plugins.register("/permissions/features", function(ob) {
497553
});
498554
});
499555
}
556+
557+
/**
558+
* Function to get enforcement info for the given app
559+
* @param {Object} params - params object
560+
* @returns {Promise} response
561+
*/
562+
function getEnforcement(params) {
563+
return new Promise(function(resolve, reject) {
564+
common.outDb.collection('sdk_enforcement').findOne({ _id: params.app_id + ""}, function(err, res) {
565+
if (err) {
566+
console.log(err);
567+
return reject();
568+
}
569+
return resolve(res || {});
570+
});
571+
});
572+
}
500573
}());

plugins/sdk/frontend/public/javascripts/countly.models.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,30 @@
984984
dataType: "json"
985985
});
986986
},
987+
getEnforcement: function() {
988+
return CV.$.ajax({
989+
type: "GET",
990+
url: countlyCommon.API_PARTS.data.r,
991+
data: {
992+
app_id: countlyCommon.ACTIVE_APP_ID,
993+
method: 'sdk-enforcement'
994+
},
995+
dataType: "json"
996+
}).then(function(res) {
997+
return res || {};
998+
});
999+
},
1000+
updateEnforcement: function(enforcement) {
1001+
return CV.$.ajax({
1002+
type: "POST",
1003+
url: countlyCommon.API_PARTS.data.w + "/sdk-config/update-enforcement",
1004+
data: {
1005+
app_id: countlyCommon.ACTIVE_APP_ID,
1006+
enforcement: JSON.stringify(enforcement)
1007+
},
1008+
dataType: "json"
1009+
});
1010+
},
9871011
calculate: function() {
9881012
var chartData = countlyCommon.extractTwoLevelData(countlySDK.getDb(), countlySDK.getMeta("sdks"), countlySDK.clearObject, [
9891013
{
@@ -1026,6 +1050,11 @@
10261050
context.dispatch("countlySDK/sdk/all", sdk, {root: true});
10271051
});
10281052
},
1053+
initializeEnforcement: function(context) {
1054+
return countlySDK.service.getEnforcement().then(function(enforcement) {
1055+
context.dispatch("countlySDK/sdk/enforcement", enforcement, {root: true});
1056+
});
1057+
},
10291058
fetchSDKStats: function(context) {
10301059
context.dispatch('onFetchInit', "sdks");
10311060
countlySDK.service.fetchSDKStats().then(function() {
@@ -1095,13 +1124,17 @@
10951124
state: function() {
10961125
return {
10971126
all: {},
1127+
enforcement: {},
10981128
isTableLoading: true
10991129
};
11001130
},
11011131
getters: {
11021132
all: function(state) {
11031133
return state.all;
11041134
},
1135+
enforcement: function(state) {
1136+
return state.enforcement;
1137+
},
11051138
isTableLoading: function(_state) {
11061139
return _state.isTableLoading;
11071140
}
@@ -1110,6 +1143,9 @@
11101143
setAll: function(state, val) {
11111144
state.all = val;
11121145
},
1146+
setEnforcement: function(state, val) {
1147+
state.enforcement = val;
1148+
},
11131149
setTableLoading: function(state, value) {
11141150
state.isTableLoading = value;
11151151
}
@@ -1118,10 +1154,16 @@
11181154
all: function(context, val) {
11191155
context.commit("setAll", val);
11201156
},
1157+
enforcement: function(context, val) {
1158+
context.commit("setEnforcement", val);
1159+
},
11211160
update: function(context, parameter) {
11221161
var updateParameter = Object.assign({}, parameter);
11231162
return countlySDK.service.updateParameter(updateParameter);
11241163
},
1164+
updateEnforcement: function(context, enforcement) {
1165+
return countlySDK.service.updateEnforcement(enforcement);
1166+
},
11251167
setTableLoading: function(context, value) {
11261168
context.commit("setTableLoading", value);
11271169
}

0 commit comments

Comments
 (0)