forked from matthewmueller/joy
/
stats.go
76 lines (63 loc) · 1.74 KB
/
stats.go
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
// Package stats provides CLI analytics.
package stats
import (
"time"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
analytics "github.com/matthewmueller/firehose-analytics"
"github.com/matthewmueller/joy/internal/env"
"github.com/matthewmueller/store"
"github.com/pkg/errors"
"github.com/rapidloop/skv"
)
// Client sets up the AWS tracking client
var Client = func() *analytics.Analytics {
e := env.Get()
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(
e.AWSAccessKey,
e.AWSSecretKey,
"",
),
Region: aws.String(e.AWSRegion),
})
if err != nil {
log.Debugf("aws session not found")
sess = nil
}
return analytics.New(&analytics.Config{
Stream: e.FirehoseStream,
Session: sess,
Prefix: "joy:",
Dir: "joy",
})
}()
// Increment runs
func Increment(db *skv.KVStore) (runs int, err error) {
// defer log.Trace("increment").Stop(&err)
if err := db.Get("runs", &runs); err != nil && err != store.ErrNotFound {
return runs, errors.Wrapf(err, "error getting 'first time' field from the db")
}
// increment
runs++
if err := db.Put("runs", runs); err != nil {
return runs, errors.Wrapf(err, "error setting 'first time' field")
}
return runs, nil
}
// Track event `name` with optional `props`.
func Track(name string, fields map[string]interface{}) error {
return Client.Track(name, fields)
}
// TrackError tracks event `name` with errors
func TrackError(name string, now time.Time, err *error) error {
if *err == nil {
return nil
}
return Client.Track(name, map[string]interface{}{
"duration": time.Since(now).Round(time.Millisecond).String(),
"error": (*err).Error(),
})
}