Skip to content

Commit 551cdcc

Browse files
committed
[version] 22.06
1 parent c900e0b commit 551cdcc

16 files changed

+1965
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var pluginManager = require('../../../../plugins/pluginManager.js'),
2+
asyncjs = require('async');
3+
4+
console.log("Clearing long_task data");
5+
pluginManager.dbConnection().then((countlyDb) => {
6+
countlyDb.collection('long_tasks').find({taskgroup: true}).toArray(function(err, reports) {
7+
function upgrade(report, done) {
8+
console.log("Upgrading long_task for " + report.report_name);
9+
countlyDb.collection('long_tasks').deleteMany({"subtask": report._id + ""}, function(){
10+
done();
11+
});
12+
}
13+
asyncjs.eachSeries(reports, upgrade, function() {
14+
console.log("Clearing main task data");
15+
countlyDb.collection('long_tasks').updateMany({taskgroup: true}, {$set:{subtasks:{}}}, function(){
16+
console.log("Long_task upgrade finished");
17+
countlyDb.close();
18+
})
19+
});
20+
});
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var pluginManager = require('../../../../plugins/pluginManager.js');
2+
3+
console.log("Resetting Data Manager Settings");
4+
pluginManager.dbConnection().then((countlyDb) => {
5+
countlyDb.collection('plugins').update(
6+
{},
7+
{
8+
$unset: {
9+
'data-manager.allowUnexpectedEvents': 1,
10+
'data-manager.showUnplannedEventsUI': 1,
11+
'data-manager.autoHideUnplannedEvents': 1,
12+
'data-manager.triggerUnplannedError': 1,
13+
'data-manager.allowUnplannedEvents': 1,
14+
}
15+
},
16+
{upsert: false},
17+
function(err){
18+
if(err){
19+
console.err(err)
20+
}
21+
countlyDb.close();
22+
});
23+
});
+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
var pluginManager = require('../../../../plugins/pluginManager.js'),
2+
async = require('async'),
3+
hash = require('object-hash');
4+
5+
/**
6+
* The function creates a hash of the object disregarding
7+
* the order of constituents (arrays, objects)
8+
* @param {Object} obj Bookmark signature parts
9+
* @returns {String} sha1 hash
10+
*/
11+
function getBookmarkSignature(obj) {
12+
var signObj = {
13+
app_id: obj.app_id,
14+
namespace: obj.namespace,
15+
event_key: obj.event_key,
16+
creator: obj.creator
17+
};
18+
19+
["query_obj", "by_val"].forEach((fieldKey) => {
20+
if (fieldKey in obj) {
21+
if (typeof obj[fieldKey] === 'string') {
22+
signObj[fieldKey] = JSON.parse(obj[fieldKey]);
23+
}
24+
else {
25+
signObj[fieldKey] = obj[fieldKey];
26+
}
27+
}
28+
});
29+
30+
signObj.namespace = signObj.namespace || "";
31+
signObj.query_obj = signObj.query_obj || {};
32+
signObj.by_val = signObj.by_val || [];
33+
34+
return hash(signObj, {
35+
unorderedArrays: true,
36+
unorderedObjects: true
37+
});
38+
}
39+
40+
function processBookmark(obj) {
41+
42+
if (obj.by_val) {
43+
if (typeof obj.by_val === 'string') {
44+
if (obj.by_val.trim().startsWith("[")) {
45+
obj.by_val = JSON.parse(obj.by_val);
46+
}
47+
else {
48+
obj.by_val = [obj.by_val];
49+
}
50+
}
51+
else {
52+
obj.by_val = obj.by_val;
53+
}
54+
55+
obj.by_val = JSON.stringify(obj.by_val);
56+
}
57+
else {
58+
obj.by_val = "[]";
59+
}
60+
if (!obj.by_val_text) {
61+
obj.by_val_text = "";
62+
}
63+
64+
if (!obj.query_obj) {
65+
obj.query_obj = "{}";
66+
}
67+
68+
if (!obj.query_text) {
69+
obj.query_text = "";
70+
}
71+
72+
obj.sign = getBookmarkSignature({
73+
app_id: obj.app_id,
74+
event_key: obj.event_key,
75+
query_obj: obj.query_obj,
76+
by_val: obj.by_val,
77+
creator: obj.creator
78+
});
79+
80+
obj.prev_id = obj._id;
81+
82+
delete obj._id;
83+
}
84+
85+
function log(type) {
86+
return function(message, payload) {
87+
payload = payload || "";
88+
console.log(`[fix_bookmarks.js] ${type}: ${message}`, payload);
89+
}
90+
}
91+
92+
var error = log("ERROR");
93+
var warn = log("WARN");
94+
var success = log("SUCCESS");
95+
96+
pluginManager.dbConnection("countly_drill").then((db) => {
97+
98+
var source = "drill_bookmarks",
99+
sink = "drill_bookmarks",
100+
hardDelete = true;
101+
102+
function deleteOld(id, callback) {
103+
if (hardDelete){
104+
db.collection(source).remove({"_id": id}, function(deleteError, res) {
105+
if (deleteError) {
106+
error(`DB error while deleting old drill bookmark {_id: ${id}}`, deleteError);
107+
}
108+
callback(deleteError, res);
109+
});
110+
}
111+
else {
112+
db.collection(source).update({"_id": id}, {$set: {namespace: 'deleted'}}, function(deleteError, res) {
113+
if (deleteError) {
114+
error(`DB error while soft deleting old drill bookmark {_id: ${id}}`, deleteError);
115+
}
116+
callback(deleteError, res);
117+
});
118+
}
119+
}
120+
121+
function insertNew(obj, callback) {
122+
db.collection(sink).insertOne(obj, function(insertError, insertionRes) {
123+
if (insertError) {
124+
error(`DB error while inserting processed drill bookmark: ${JSON.stringify(obj, null, 4)}`, insertError);
125+
}
126+
callback(insertError, insertionRes)
127+
});
128+
}
129+
130+
function upgrade(bookmark, done) {
131+
if (!bookmark.action) {
132+
done();
133+
}
134+
else if (bookmark.action === "upgrade") {
135+
delete bookmark.action;
136+
insertNew(bookmark, function(insertError, obj) {
137+
if (!insertError && obj.insertedId) {
138+
deleteOld(bookmark.prev_id, function(deleteError, res) {
139+
if (!deleteError) {
140+
success(`Bookmark ${bookmark.prev_id} moved to ${obj.insertedId}.`);
141+
}
142+
done();
143+
});
144+
}
145+
else {
146+
done();
147+
}
148+
});
149+
}
150+
else if (bookmark.action === "delete") {
151+
deleteOld(bookmark.prev_id, function(deleteError, res) {
152+
if (!deleteError) {
153+
warn(`Bookmark ${bookmark.prev_id} is deleted (duplicate sign).`);
154+
}
155+
done();
156+
});
157+
}
158+
}
159+
160+
db.collection(sink).createIndex({ "sign": 1 }, { unique: true }, function() {});
161+
162+
db.collection(source).find().toArray(function(err, result) {
163+
164+
var signsLookup = {};
165+
166+
result.forEach(function(bookmark) {
167+
if (bookmark.sign || bookmark.namespace) {
168+
return warn(`Bookmark ${bookmark._id} is already processed, skipping...`);
169+
}
170+
processBookmark(bookmark);
171+
if (!signsLookup[bookmark.sign]) {
172+
bookmark.action = "upgrade";
173+
signsLookup[bookmark.sign] = true;
174+
}
175+
else {
176+
bookmark.action = "delete";
177+
return warn(`A bookmark with same sign ${bookmark.sign} has already been inserted, latter will be deleted.`);
178+
}
179+
})
180+
181+
async.forEach(result, upgrade, function() {
182+
success("Finished processing drill bookmarks.");
183+
db.close();
184+
});
185+
});
186+
187+
});
188+
189+
/*
190+
191+
Old format:
192+
193+
{
194+
"app_id": "5ccc6c81f8315136327757a7",
195+
"event_key": "[CLY]_session",
196+
"name": "Test bookmarks",
197+
"desc": "Test bookmarks",
198+
"global": true,
199+
"creator": "5fd8b6fe31131d7f680ca777",
200+
"event_app_id": "c535971cf35af2a9f09c9b3ce1ec201e",
201+
"query_obj": "{\"up.av\":{\"$in\":[\"6:4:1\"]}}",
202+
"query_text": "App Version = 6.4.1"
203+
"by_val": "[]",
204+
"by_val_text": "",
205+
//
206+
"_id": "01054c68313dbc181aa34327a3ccd023", // Custom string id
207+
}
208+
209+
New format:
210+
211+
{
212+
"app_id": "5c62927f23864d069e405717",
213+
"event_key": "[CLY]_session",
214+
"name": "t1",
215+
"desc": "",
216+
"global": true,
217+
"creator": "60ad16ef6aff41f13fe49a0d",
218+
"event_app_id": "eb7a0f37aac21566de13ef8f3945d700",
219+
"query_obj": "{\"up.av\":{\"rgxntc\":[\"asd\"]}}",
220+
"query_text": "App Version doesnt contain asd",
221+
"by_val": "[]",
222+
"by_val_text": "",
223+
//
224+
"namespace": "activity-map", (doesn't exist if drill)
225+
"sign": "7fc7db14f548337cfc34088d48f30ed8d3c50c05",
226+
//
227+
"_id": "ObjectId(6138534e135e969971377436)", // ObjectId
228+
}
229+
230+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var pluginManager = require('../../../../plugins/pluginManager.js');
2+
3+
console.log("Fixing app_id in cohorts collection.");
4+
pluginManager.dbConnection().then((countlyDb) => {
5+
6+
var cursor = countlyDb.collection('cohorts').find({});
7+
var requests = [];
8+
cursor.forEach(function(cohort) {
9+
if (typeof cohort.app_id !== 'string') {
10+
requests.push({
11+
'updateOne': {
12+
'filter': { '_id': cohort._id },
13+
'update': { '$set': {"app_id": cohort.app_id + ""} }
14+
}
15+
});
16+
}
17+
if (requests.length === 500) {
18+
//Execute per 500 operations and re-init
19+
console.log("Flushing changes:"+requests.length);
20+
countlyDb.collection('cohorts').bulkWrite(requests, function(err) {
21+
if (err) {
22+
console.error(err);
23+
}
24+
});
25+
requests = [];
26+
}
27+
}, function() {
28+
if (requests.length > 0) {
29+
console.log("Flushing changes:"+requests.length);
30+
countlyDb.collection('cohorts').bulkWrite(requests, function(err) {
31+
if (err) {
32+
console.error(err);
33+
}
34+
console.log("Done");
35+
countlyDb.close();
36+
});
37+
}
38+
else {
39+
console.log("Done");
40+
countlyDb.close();
41+
}
42+
});
43+
});

0 commit comments

Comments
 (0)