forked from fhirbase/fhirbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
105 lines (80 loc) · 1.96 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
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
package main
import (
"bytes"
"fmt"
"net/http"
"runtime"
"sync"
"github.com/denisbrodbeck/machineid"
jsoniter "github.com/json-iterator/go"
)
const EventEndpointURL = "https://license.aidbox.app/event"
var eventsWg sync.WaitGroup
// DisableStats flag disables stats submition
var DisableStats = false
func getMachineID() (string, error) {
return machineid.ProtectedID("fhirbase")
}
func submitEvent(et string, payload interface{}) {
if DisableStats {
return
}
eventsWg.Add(1)
defer eventsWg.Done()
// fmt.Printf("event: submitting %s event\n", et)
machineID, err := getMachineID()
// fmt.Printf("event: machine id %s %s\n", machineID, err)
if err != nil {
fmt.Printf("stats error: %+v\n", err)
return
}
body, err := jsoniter.MarshalToString(map[string]interface{}{
"product": "fhirbase",
"machineId": machineID,
"version": Version,
"type": et,
"payload": map[string]interface{}{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"event": payload,
},
})
// fmt.Printf("event: marshalled stuff %s\n", body)
if err != nil {
// fmt.Printf("stats error: %+v\n", err)
return
}
client := &http.Client{}
req, err := http.NewRequest("POST", EventEndpointURL, bytes.NewBuffer([]byte(body)))
if err != nil {
// fmt.Printf("stats error: %+v\n", err)
return
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
// fmt.Printf("stats error: %+v\n", err)
return
}
// fmt.Printf("event: submitted\n")
defer resp.Body.Close()
}
func submitInitEvent(fhirVersion string) {
go submitEvent("init", map[string]string{
"fhirVersion": fhirVersion,
})
}
func submitErrorEvent(err error) {
go submitEvent("error", map[string]string{
"error": fmt.Sprintf("%+v", err),
})
}
func submitLoadEvent(stats map[string]uint, drtion int) {
go submitEvent("load", map[string]interface{}{
"stats": stats,
"duration": drtion,
})
}
func waitForAllEventsSubmitted() {
eventsWg.Wait()
}