Skip to content
Permalink
Browse files

add usage statistics sending

  • Loading branch information...
mlapshin committed Oct 3, 2018
1 parent dea75b6 commit 735028fccc9177f3f91b537ce9cf829d61b18cc9
Showing with 84 additions and 8 deletions.
  1. +11 −0 README.md
  2. +2 −0 dbinit.go
  3. +6 −4 load.go
  4. +9 −0 main.go
  5. +56 −4 stats.go
@@ -22,6 +22,17 @@ Please proceed to the [Getting
Started](https://fhirbase.gitbook.io/project/getting-started) tutorial
for PostgreSQL and Fhirbase installation instructions.
## Usage Statistics
Please note that Fhirbase collects anonymous usage statistics. It does
not send any FHIR data, only events happened (database initialized,
resources imported and error happened). You can check the [source code
for stats
sender](https://github.com/fhirbase/fhirbase/blob/master/stats.go)
yourself.
You can turn off usage statistics sending with `--nostats` global
flag.
## Development
@@ -113,6 +113,8 @@ func InitCommand(c *cli.Context) error {

bars.Wait()

submitInitEvent(fhirVersion)

fmt.Printf("Database initialized with FHIR schema version '%s'\n", fhirVersion)

return nil
10 load.go
@@ -694,7 +694,7 @@ func prewalkDirs(fileNames []string) ([]string, error) {
return result, nil
}

func loadFIles(files []string, ldr loader, memUsage bool) error {
func loadFiles(files []string, ldr loader, memUsage bool) error {
db := GetConnection(nil)
defer db.Close()

@@ -738,7 +738,9 @@ func loadFIles(files []string, ldr loader, memUsage bool) error {

bars.Wait()

loadDuration := time.Since(startTime) / time.Second
loadDuration := int(time.Since(startTime).Seconds())

submitLoadEvent(insertedCounts, loadDuration)

fmt.Printf("Done, inserted %d resources in %d seconds:\n", totalCount, loadDuration)

@@ -814,7 +816,7 @@ func LoadCommand(c *cli.Context) error {
f.Close()
}

return loadFIles(files, ldr, memUsage)
return loadFiles(files, ldr, memUsage)
}

files, err := prewalkDirs(c.Args())
@@ -823,5 +825,5 @@ func LoadCommand(c *cli.Context) error {
return errors.Wrap(err, "cannot prewalk directories")
}

return loadFIles(files, ldr, memUsage)
return loadFiles(files, ldr, memUsage)
}
@@ -40,6 +40,11 @@ func main() {
app.Usage = "command-line utility to operate on FHIR data with PostgreSQL database."

app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "nostats",
Usage: "Disable sending of usage statistics",
Destination: &DisableStats,
},
cli.StringFlag{
Name: "host, n",
Value: "localhost",
@@ -284,9 +289,13 @@ version.`,
err := app.Run(os.Args)

if err != nil {
submitErrorEvent(err)
fmt.Printf("%+v\n", err)

waitForAllEventsSubmitted()
os.Exit(1)
}

waitForAllEventsSubmitted()
os.Exit(0)
}
@@ -2,52 +2,104 @@ 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()

if err == nil {
// 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": "0.0.1",
"version": Version,
"type": et,
"payload": payload,
"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) {
submitEvent("init", map[string]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()
}

0 comments on commit 735028f

Please sign in to comment.
You can’t perform that action at this time.