-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
145 lines (124 loc) · 3.27 KB
/
main.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
'use strict';
// StatHat API node.js module
var _ =require('lodash');
var async = require('async');
var request = require('requestretry');
var DEFAULT = {
// How many requests should `node-stathat` we send at the same time
concurrency: 4,
// Should node-stathat use https requests
useHTTPS: false,
// `request-retry` options
request: {
maxAttempts: 5, // try 5 times
retryDelay: 5000 // wait for 5s before trying again
}
};
function StatHat(options){
this.opts = _.defaults(options || {}, DEFAULT);
this.opts.concurrency = parseInt(this.opts.concurrency, 10);
this.postQueue = async.queue(this.processTask.bind(this), this.opts.concurrency);
}
StatHat.prototype = {
postRequest: function(path, params, f) {
var options = _.defaults({
url: 'http' + (this.opts.useHTTPS ? 's':'') + '://api.stathat.com' + path,
method: 'POST',
json:true,
form: params
}, this.opts.requestOptions);
request(options, function(err, response, body){
if(err){
return f(600, err.message, body);
}
f(response.statusCode, body);
});
},
queueOrPostRequest: function(path, params, f) {
if (_.isFunction(f)) {
this.postRequest(path, params, f);
} else {
this.postQueue.push({
path: path,
params: params
});
}
},
processTask: function(task, f) {
this.postRequest(task.path, task.params, function(status, data) {
if (status !== 200) {
// @todo, emit error
console.log('stathat post error', task, status, data);
}
f();
});
},
length: function() {
return this.postQueue.length();
},
trackValue: function(user_key, stat_key, value, f) {
this.queueOrPostRequest('/v', {
key: stat_key,
ukey: user_key,
value: value
}, f);
},
trackValueWithTime: function(user_key, stat_key, value, timestamp, f) {
this.queueOrPostRequest('/v', {
key: stat_key,
ukey: user_key,
value: value,
t: timestamp
}, f);
},
trackCount: function(user_key, stat_key, count, f) {
this.queueOrPostRequest('/c', {
key: stat_key,
ukey: user_key,
count: count
}, f);
},
trackCountWithTime: function(user_key, stat_key, count, timestamp, f) {
this.queueOrPostRequest('/c', {
key: stat_key,
ukey: user_key,
count: count,
t: timestamp
}, f);
},
trackEZValue: function(ezkey, stat_name, value, f) {
this.queueOrPostRequest('/ez', {
ezkey: ezkey,
stat: stat_name,
value: value
}, f);
},
trackEZValueWithTime: function(ezkey, stat_name, value, timestamp, f) {
this.queueOrPostRequest('/ez', {
ezkey: ezkey,
stat: stat_name,
value: value,
t: timestamp
}, f);
},
trackEZCount: function(ezkey, stat_name, count, f) {
this.queueOrPostRequest('/ez', {
ezkey: ezkey,
stat: stat_name,
count: count
}, f);
},
trackEZCountWithTime: function(ezkey, stat_name, count, timestamp, f) {
this.queueOrPostRequest('/ez', {
ezkey: ezkey,
stat: stat_name,
count: count,
t: timestamp
}, f);
}
};
module.exports = new StatHat();
module.exports.setup = function(options){
return new StatHat(options);
};
module.exports.StatHat = StatHat;