Skip to content

Commit cd2b65c

Browse files
committed
[frontend] remote configs
1 parent f889a63 commit cd2b65c

File tree

3 files changed

+98
-34
lines changed

3 files changed

+98
-34
lines changed

api/jobs/ping.js

+62-33
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,74 @@
22

33
const job = require('../parts/jobs/job.js'),
44
log = require('../utils/log.js')('job:ping'),
5-
config = require("../../frontend/express/config.js"),
5+
countlyConfig = require("../../frontend/express/config.js"),
6+
versionInfo = require('../../frontend/express/version.info'),
67
request = require('request');
78

89
class PingJob extends job.Job {
910
run (db, done) {
10-
if(config.web.track != "none"){
11-
db.collection("members").findOne({global_admin:true}, function(err, member){
12-
if(!err && member){
13-
var date = new Date();
14-
request({
15-
uri:"https://stats.count.ly/i",
16-
method:"GET",
17-
timeout:4E3,
18-
qs:{
19-
device_id:member.email,
20-
app_key:"386012020c7bf7fcb2f1edf215f1801d6146913f",
21-
timestamp: Math.floor(date.getTime()/1000),
22-
hour: date.getHours(),
23-
dow: date.getDay(),
24-
events:JSON.stringify([
25-
{
26-
key: "PING",
27-
count: 1
28-
}
29-
])
30-
}
31-
}, function(a/*, c, b*/) {
32-
log.d('Done running ping job: %j', a);
33-
done();
34-
});
11+
request("http://localhost/configs", function(err, res, body){log.d(err, body);});
12+
var countlyConfigOrig = JSON.parse(JSON.stringify(countlyConfig));
13+
var url = "https://count.ly/configurations/ce/tracking";
14+
if(versionInfo.type != "777a2bf527a18e0fffe22fb5b3e322e68d9c07a6"){
15+
url = "https://count.ly/configurations/ee/tracking";
16+
}
17+
request(url, function (err, response, body) {
18+
if(typeof body === "string"){
19+
try{
20+
body = JSON.parse(body);
3521
}
36-
else{
37-
done();
22+
catch(ex){body = null;}
23+
}
24+
if(body){
25+
if(countlyConfigOrig.web.use_intercom && typeof body.intercom !== "undefined"){
26+
countlyConfig.web.use_intercom = body.intercom;
3827
}
39-
});
40-
}
41-
else{
42-
done();
43-
}
28+
if(typeof countlyConfigOrig.web.track === "undefined" && typeof body.stats !== "undefined"){
29+
if(body.stats){
30+
countlyConfig.web.track = null;
31+
}
32+
else{
33+
countlyConfig.web.track = "none";
34+
}
35+
}
36+
}
37+
log.d(err, body, countlyConfigOrig, countlyConfig);
38+
if(countlyConfig.web.track != "none"){
39+
db.collection("members").findOne({global_admin:true}, function(err, member){
40+
if(!err && member){
41+
var date = new Date();
42+
request({
43+
uri:"https://stats.count.ly/i",
44+
method:"GET",
45+
timeout:4E3,
46+
qs:{
47+
device_id:member.email,
48+
app_key:"386012020c7bf7fcb2f1edf215f1801d6146913f",
49+
timestamp: Math.floor(date.getTime()/1000),
50+
hour: date.getHours(),
51+
dow: date.getDay(),
52+
events:JSON.stringify([
53+
{
54+
key: "PING",
55+
count: 1
56+
}
57+
])
58+
}
59+
}, function(a/*, c, b*/) {
60+
log.d('Done running ping job: %j', a);
61+
done();
62+
});
63+
}
64+
else{
65+
done();
66+
}
67+
});
68+
}
69+
else{
70+
done();
71+
}
72+
});
4473
}
4574
}
4675

frontend/express/app.js

+35
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,36 @@ process.on('unhandledRejection', (reason, p) => {
9898
log.e("Logging unhandled rejection");
9999
});
100100

101+
var countlyConfigOrig = JSON.parse(JSON.stringify(countlyConfig));
102+
function recheckConfigs(){
103+
var url = "https://count.ly/configurations/ce/tracking";
104+
if(COUNTLY_TYPE != "777a2bf527a18e0fffe22fb5b3e322e68d9c07a6"){
105+
url = "https://count.ly/configurations/ee/tracking";
106+
}
107+
request(url, function (error, response, body) {
108+
if(typeof body === "string"){
109+
try{
110+
body = JSON.parse(body);
111+
}
112+
catch(ex){body = null;}
113+
}
114+
if(body){
115+
if(countlyConfigOrig.web.use_intercom && typeof body.intercom !== "undefined"){
116+
countlyConfig.web.use_intercom = body.intercom;
117+
}
118+
if(typeof countlyConfigOrig.web.track === "undefined" && typeof body.stats !== "undefined"){
119+
if(body.stats){
120+
countlyConfig.web.track = null;
121+
}
122+
else{
123+
countlyConfig.web.track = "none";
124+
}
125+
}
126+
}
127+
});
128+
}
129+
recheckConfigs();
130+
101131
var countlyDb = plugins.dbConnection(countlyConfig);
102132

103133
function sha1Hash(str, addSalt) {
@@ -426,6 +456,11 @@ app.get(countlyConfig.path+'/ping', function(req, res, next) {
426456
});
427457
});
428458

459+
app.get(countlyConfig.path+'/configs', function(req, res, next) {
460+
recheckConfigs();
461+
res.send("Success");
462+
});
463+
429464
app.get(countlyConfig.path+'/session', function(req, res, next) {
430465
if (req.session.uid) {
431466
if(Date.now() > req.session.expires){

frontend/express/config.sample.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ var countlyConfig = {
6868
* @property {number} [port=6001] - api port number to use, default 6001
6969
* @property {string} [host=localhost] - host to which to bind connection
7070
* @property {boolean} use_intercom - true, to use intercom in dashboard for communication with Countly
71-
* @property {string} track - allow Countly to collect stats about amount of apps and datapoints as well as feature usage.
7271
* @property {boolean} secure_cookies - true, to use secure cookies, enable only if you have https enabled
72+
* @property {string} track - allow Countly to collect stats about amount of apps and datapoints as well as feature usage.
7373
* Possible values are:
7474
* "all" - track all,
7575
* "GA" - track only Global admins,

0 commit comments

Comments
 (0)