-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
106 lines (84 loc) · 4.45 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
// Copyright 2011-2014 Numerotron, Inc. / Patrick Crosby
//
// StatHat API node.js module
//
(function() {
var http = require('http');
var https = require('https');
var async = require('async');
var StatHat = {
useHTTPS: false,
postQueue: undefined, // Added below
trackValue: function(user_key, stat_key, value, callback) {
this._queueOrPostRequest('/v', {key: stat_key, ukey: user_key, value: value}, callback);
},
trackValueWithTime: function(user_key, stat_key, value, timestamp, callback) {
this._queueOrPostRequest('/v', {key: stat_key, ukey: user_key, value: value, t: timestamp}, callback);
},
trackCount: function(user_key, stat_key, count, callback) {
this._queueOrPostRequest('/c', {key: stat_key, ukey: user_key, count: count}, callback);
},
trackCountWithTime: function(user_key, stat_key, count, timestamp, callback) {
this._queueOrPostRequest('/c', {key: stat_key, ukey: user_key, count: count, t: timestamp}, callback);
},
trackEZValue: function(ezkey, stat_name, value, callback) {
this._queueOrPostRequest('/ez', {ezkey: ezkey, stat: stat_name, value: value}, callback);
},
trackEZValueWithTime: function(ezkey, stat_name, value, timestamp, callback) {
this._queueOrPostRequest('/ez', {ezkey: ezkey, stat: stat_name, value: value, t: timestamp}, callback);
},
trackEZCount: function(ezkey, stat_name, count, callback) {
this._queueOrPostRequest('/ez', {ezkey: ezkey, stat: stat_name, count: count}, callback);
},
trackEZCountWithTime: function(ezkey, stat_name, count, timestamp, callback) {
this._queueOrPostRequest('/ez', {ezkey: ezkey, stat: stat_name, count: count, t: timestamp}, callback);
},
_queueOrPostRequest: function(path, params, callback) {
if (typeof(callback) === 'function') {
this._postRequest(path, params, callback);
} else {
this.postQueue.push({path: path, params: params});
}
},
_processTask: function(task, callback) {
this._postRequest(task.path, task.params, function(status, data) {
if (status !== 200) {
console.log('stathat post error', task, status, data);
}
callback();
});
},
_postRequest: function(path, params, callback) {
var qs = Object.keys(params)
.map( function(k) { return k + '=' + params[k] } )
.join('&');
var options = {
hostname: 'api.stathat.com',
path: path,
method: 'POST',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : qs.length
}
};
var hmod = this.useHTTPS ? https : http;
var request = hmod.request(options, function(res) {
res.on('data', function(chunk) {
callback(res.statusCode, chunk);
});
});
request.on('error', function(e) {
if (!e) e = {};
callback(600,e.message);
});
request.write(qs);
request.end();
},
};
StatHat.postQueue = async.queue(StatHat._processTask.bind(StatHat),
parseInt(process.env.STATHAT_OUTBOUND_CONCURRENCY || '10')),
StatHat.length = function() {
return this.postQueue.length();
},
module.exports = StatHat;
}())