-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracker.js
161 lines (144 loc) · 4.73 KB
/
Tracker.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var mongoose = require('mongoose');
var onFinished = require('on-finished');
var URL = require('url');
var Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
var logSchema = new Schema({
log : String,
time: { type: Date, index: true }
});
var requestSchema = new Schema({
status : { type: Number, index: true },
url : { type: String, index: true },
method : String,
content : String,
time : { type: Number, index: true },
startTime: { type: Date, index: true },
endTime : { type: Date, index: true },
tracker : { type: Schema.Types.ObjectId, ref: 'Tracker', index: true },
body : Object
}, {
timestamps: true
});
requestSchema.path('createdAt').expires('7d');
var trackerSchema = new Schema({
url : { type: String, index: true },
host : { type: String, index: true },
pathname : { type: String, index: true },
search : { type: String, index: true },
appId : { type: Number, index: true },
hash : String,
query : Object,
logs : [ logSchema ],
time : { type: Number, index: true },
startTime: { type: Date, index: true, default: Date.now },
endTime : { type: Date, index: true },
status : { type: Number, index: true },
method : String,
body : Object,
requests : [ { type: Schema.Types.ObjectId, ref: 'Request', index: true } ]
}, {
timestamps: true
});
trackerSchema.path('createdAt').expires('7d');
trackerSchema.methods.track = function (log) {
this.logs.push({
log : log,
time: Date.now()
});
};
trackerSchema.methods.request = function (url, method, body, promise) {
var st = Date.now();
method = method || 'GET';
this.track('api request ' + method + ' ' + url);
var _this = this
promise.then(function (data) {
var text = data.text,
response = data.response;
var request = new Request({
status : response.status,
url : url,
method : method,
content : text,
startTime: st,
endTime : Date.now(),
time : Date.now() - st,
tracker : _this._id,
body : body
});
request.save();
_this.requests.push(request._id);
_this.track('api end success ' + method + ' ' + url);
}, function (err) {
var request = new Request({
status : err.status,
url : url,
method : method,
content : err,
startTime: st,
endTime : Date.now(),
time : Date.now() - st,
tracker : _this._id,
body : err.body
});
request.save();
_this.requests.push(request._id);
_this.track(`api end error ${method} ${url}`);
})
this.requestPromises.push(promise);
return promise;
};
trackerSchema.methods.end = function (status) {
this.endTime = Date.now();
this.time = this.endTime - this.startTime;
this.status = status;
this.track('request end');
this.save();
var _this = this
Promise.all(_this.requestPromises).then(function () {
_this.track('done');
_this.save();
}).catch(function (err) {
_this.track('There has some requests error!');
_this.save();
});
};
trackerSchema.methods.start = function (appId, url, method, body) {
this.appId = appId;
this.url = url;
this.method = method;
this.body = body;
this.requestPromises = [];
this.track('request start');
var urlObj = URL.parse(this.url, true);
Object.assign(this, urlObj);
};
trackerSchema.statics.start = function (uri) {
return mongoose.connect(uri, console.log);
};
trackerSchema.statics.express = function (options) {
this.start(options.uri);
var _this = this;
return function (req, res, next) {
if (mongoose.connection.readyState !== 1) return next();
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
var appId = (res.locals.info && res.locals.info.appId) || null;
if (options.appIds && options.appIds instanceof Array && options.appIds.indexOf(appId) !== -1) {
req.tracker = new _this();
req.tracker.start(appId, fullUrl, req.method, req.body);
/* eslint-disable */
res.locals.tracker = req.tracker._id;
/* eslint-ensable */
onFinished(res, function (err, response) {
req.tracker.end(response.statusCode);
});
next();
} else {
next();
}
}
};
var Request = mongoose.model('Request', requestSchema);
var Tracker = mongoose.model('Tracker', trackerSchema);
Tracker.Request = Request;
module.exports = Tracker;