-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (129 loc) · 4.84 KB
/
index.js
File metadata and controls
140 lines (129 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var fs = require('fs'),
path = require('path'),
_ = require('lodash'),
fs = require('fs'),
path = require('path');
'use strict';
var provider = null,
config;
function loadProviders() {
return new Promise(function(resolve,reject){
fs.readdir(__dirname+'/lib/providers/',function(err,files){
if( err ) {
reject();
return;
}
var providers = [];
files.forEach(function(file){
var file = path.parse(file);
providers.push(file.name)
});
resolve(providers);
})
});
}
function setProvider(options,providers) {
return new Promise(function(resolve,reject){
var configPath = path.resolve(options.configPath) || __dirname + '/config.json';
this.config = options.config ? options.config : require(configPath); //use config if passed in otherwise fall back to configPath
getProvider(this.config,providers)
.then(resolve);
})
}
function getProvider(config,providers) {
return new Promise(function(resolve,reject){
if( !config || !config.provider || providers.indexOf(config.provider) == -1 ) {
reject("Invalid provider. Please specify one of `" + providers.join(', ') + "` in config.provider property of your config file.");
}
try{
var provider = require(__dirname+'/lib/providers/'+config.provider+'.js');
provider.init();
resolve(provider);
}
catch(e) {
reject("Invalid provider. Please specify one of `" + supportedProviders.join(', ') + "` in config.tracker property of your config file.");
}
});
}
function getUser(app,req) {
return new Promise(function(resolve,reject) {
if( !req.accessToken )
return reject();
app.models.User.findById(req.accessToken.userId,function(err,user){
if( err ) return reject();
if( !user ) return reject();
resolve(user);
});
});
}
function addCustomData(customData) {
var data = {};
if( !this.config.dataPoints ) {
return data;
}
_.each(this.config.dataPoints,function(value,key) {
var path = typeof value === 'object' && value.name ? value.name : value;
var defaultValue = typeof value === 'object' && value.default ? value.default : undefined;
try{
_.set(data,key,_.get(customData,path,defaultValue));
}catch(e){
log(e);
}
});
return data;
}
function init(app,getCustomDataCallback) {
var remotes = app.remotes();
remotes.after('**',function(ctx,next){
try{
var model = ctx.method.sharedClass.name;
var method = ctx.method.name;
var path = ctx.method.sharedClass.http.path;
var query = ctx.req._parsedUrl.query;
var userId = ctx.req.accessToken ? ctx.req.accessToken.userId : null;
var accessToken = ctx.req.accessToken ? ctx.req.accessToken.id : -1;
data = {model:model,method:method,path:path,query:query,userId:userId,accessToken:accessToken};
getUser(app,ctx.req)
.then(function(user){
data = _.assign({},{user:user.toJSON()},data);
getCustomDataCallback(ctx).then(
function(customData) {
data = _.assign({},addCustomData(customData),data);
this.provider.track(data);
},
function(customData) {
this.provider.track(data);
}
);
})
.catch(function(){
getCustomDataCallback(ctx).then (
function(customData) {
data = _.assign({},addCustomData(customData),data);
this.provider.track(data);
},
function(customData) {
this.provider.track(data);
}
);
});
}
catch(e) {
log(e);
}
next();
});
}
function log(e) {
console.log( 'LoopbackAnalytics:','Error', e );
}
module.exports = function analytics(app,options,getCustomDataCallback) {
options = options || {configPath: __dirname + '/../../server/config-analytics.json'};
getCustomDataCallback = getCustomDataCallback || function(){ return new Promise(function(resolve,reject){ resolve(); }); };
this.options = options;
app.on('started', function() {
loadProviders()
.then(function(providers){ return setProvider(options,providers); })
.then(function(provider){ this.provider = provider; init(app,getCustomDataCallback); });
});
}