-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstathat.go
More file actions
284 lines (236 loc) · 5.69 KB
/
stathat.go
File metadata and controls
284 lines (236 loc) · 5.69 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2011 Numerotron Inc.
// Use of this source code is governed by an MIT-style license
// that can be found in the LICENSE file.
//
// Developed at www.stathat.com by Patrick Crosby
// Contact us on twitter with any questions: twitter.com/stat_hat
// The stathat package makes it easy to post any values to your StatHat
// account.
package stathat
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"time"
)
type statKind int
const (
_ = iota
COUNTER statKind = iota
VALUE
)
func (sk statKind) classicPath() string {
switch sk {
case COUNTER:
return "/c"
case VALUE:
return "/v"
}
return ""
}
type apiKind int
const (
_ = iota
CLASSIC apiKind = iota
EZ
)
const hostname = "api.stathat.com"
func (ak apiKind) path(sk statKind) string {
switch ak {
case EZ:
return "/ez"
case CLASSIC:
return sk.classicPath()
}
return ""
}
type statReport struct {
StatKey string
UserKey string
Value float64
statType statKind
apiType apiKind
}
var statReportChannel chan *statReport
var testingEnv = false
type testPost struct {
url string
values url.Values
}
var testPostChannel chan *testPost
var Verbose = false
var done chan bool
func setTesting() {
testingEnv = true
testPostChannel = make(chan *testPost)
}
func init() {
statReportChannel = make(chan *statReport, 100000)
go processStats()
done = make(chan bool)
}
// Using the classic API, posts a count to a stat.
func PostCount(statKey, userKey string, count int) error {
statReportChannel <- newClassicStatCount(statKey, userKey, count)
return nil
}
// Using the classic API, posts a count of 1 to a stat.
func PostCountOne(statKey, userKey string) error {
return PostCount(statKey, userKey, 1)
}
// Using the classic API, posts a value to a stat.
func PostValue(statKey, userKey string, value float64) error {
statReportChannel <- newClassicStatValue(statKey, userKey, value)
return nil
}
// Using the EZ API, posts a count of 1 to a stat.
func PostEZCountOne(statName, ezkey string) error {
return PostEZCount(statName, ezkey, 1)
}
// Using the EZ API, posts a count to a stat.
func PostEZCount(statName, ezkey string, count int) error {
statReportChannel <- newEZStatCount(statName, ezkey, count)
return nil
}
// Using the EZ API, posts a value to a stat.
func PostEZValue(statName, ezkey string, value float64) error {
statReportChannel <- newEZStatValue(statName, ezkey, value)
return nil
}
func newEZStatCount(statName, ezkey string, count int) *statReport {
return &statReport{StatKey: statName,
UserKey: ezkey,
Value: float64(count),
statType: COUNTER,
apiType: EZ}
}
func newEZStatValue(statName, ezkey string, value float64) *statReport {
return &statReport{StatKey: statName,
UserKey: ezkey,
Value: value,
statType: VALUE,
apiType: EZ}
}
func newClassicStatCount(statKey, userKey string, count int) *statReport {
return &statReport{StatKey: statKey,
UserKey: userKey,
Value: float64(count),
statType: COUNTER,
apiType: CLASSIC}
}
func newClassicStatValue(statKey, userKey string, value float64) *statReport {
return &statReport{StatKey: statKey,
UserKey: userKey,
Value: value,
statType: VALUE,
apiType: CLASSIC}
}
func (sr *statReport) values() url.Values {
switch sr.apiType {
case EZ:
return sr.ezValues()
case CLASSIC:
return sr.classicValues()
}
return nil
}
func (sr *statReport) ezValues() url.Values {
switch sr.statType {
case COUNTER:
return sr.ezCounterValues()
case VALUE:
return sr.ezValueValues()
}
return nil
}
func (sr *statReport) classicValues() url.Values {
switch sr.statType {
case COUNTER:
return sr.classicCounterValues()
case VALUE:
return sr.classicValueValues()
}
return nil
}
func (sr *statReport) ezCommonValues() url.Values {
result := make(url.Values)
result.Set("stat", sr.StatKey)
result.Set("ezkey", sr.UserKey)
return result
}
func (sr *statReport) classicCommonValues() url.Values {
result := make(url.Values)
result.Set("key", sr.StatKey)
result.Set("ukey", sr.UserKey)
return result
}
func (sr *statReport) ezCounterValues() url.Values {
result := sr.ezCommonValues()
result.Set("count", sr.valueString())
return result
}
func (sr *statReport) ezValueValues() url.Values {
result := sr.ezCommonValues()
result.Set("value", sr.valueString())
return result
}
func (sr *statReport) classicCounterValues() url.Values {
result := sr.classicCommonValues()
result.Set("count", sr.valueString())
return result
}
func (sr *statReport) classicValueValues() url.Values {
result := sr.classicCommonValues()
result.Set("value", sr.valueString())
return result
}
func (sr *statReport) valueString() string {
return strconv.FormatFloat(sr.Value, 'g', -1, 64)
}
func (sr *statReport) path() string {
return sr.apiType.path(sr.statType)
}
func (sr *statReport) url() string {
return fmt.Sprintf("http://%s%s", hostname, sr.path())
}
func processStats() {
for {
sr, ok := <-statReportChannel
if !ok {
done <- true
break
}
if Verbose {
log.Printf("posting stat to stathat: %s, %v", sr.url(), sr.values())
}
if testingEnv {
testPostChannel <- &testPost{sr.url(), sr.values()}
continue
}
r, err := http.PostForm(sr.url(), sr.values())
if err != nil {
log.Printf("error posting stat to stathat: %s", err)
continue
}
if Verbose {
body, _ := ioutil.ReadAll(r.Body)
log.Printf("stathat post result: %s", body)
}
r.Body.Close()
}
}
// Wait for all stats to be sent, or until timeout. Useful for simple command-
// line apps to defer a call to this in main()
func WaitUntilFinished(timeout time.Duration) bool {
close(statReportChannel)
select {
case <-done:
return true
case <-time.After(timeout):
return false
}
return false
}