netmaker/logic/telemetry.go

156 lines
3.9 KiB
Go
Raw Normal View History

2022-01-26 00:00:56 +08:00
package logic
2022-01-21 06:50:42 +08:00
import (
"encoding/json"
"time"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
"github.com/posthog/posthog-go"
)
2022-01-26 00:00:56 +08:00
// posthog_pub_key - Key for sending data to PostHog
const posthog_pub_key = "phc_1vEXhPOA1P7HP5jP2dVU9xDTUqXHAelmtravyZ1vvES"
2022-01-23 22:39:48 +08:00
2022-01-26 00:00:56 +08:00
// posthog_endpoint - Endpoint of PostHog server
const posthog_endpoint = "https://app.posthog.com"
2022-01-23 22:39:48 +08:00
2022-01-26 00:00:56 +08:00
// sendTelemetry - gathers telemetry data and sends to posthog
2022-01-26 01:36:25 +08:00
func sendTelemetry() error {
2022-01-21 06:50:42 +08:00
if servercfg.Telemetry() == "off" {
2022-01-26 01:36:25 +08:00
return nil
2022-01-21 06:50:42 +08:00
}
2022-01-26 00:00:56 +08:00
var telRecord, err = fetchTelemetryRecord()
2022-01-21 06:50:42 +08:00
if err != nil {
2022-01-26 01:36:25 +08:00
return err
2022-01-21 06:50:42 +08:00
}
// get telemetry data
d, err := fetchTelemetryData()
if err != nil {
2022-01-26 01:36:25 +08:00
return err
2022-01-21 06:50:42 +08:00
}
2022-01-26 00:00:56 +08:00
client, err := posthog.NewWithConfig(posthog_pub_key, posthog.Config{Endpoint: posthog_endpoint})
2022-01-21 06:50:42 +08:00
if err != nil {
2022-01-26 01:36:25 +08:00
return err
2022-01-21 06:50:42 +08:00
}
defer client.Close()
// send to posthog
2022-01-26 01:36:25 +08:00
return client.Enqueue(posthog.Capture{
2022-01-26 00:00:56 +08:00
DistinctId: telRecord.UUID,
2022-01-21 06:50:42 +08:00
Event: "daily checkin",
Properties: posthog.NewProperties().
Set("nodes", d.Nodes).
Set("non-server nodes", d.Count.NonServer).
Set("extclients", d.ExtClients).
Set("users", d.Users).
Set("networks", d.Networks).
Set("linux", d.Count.Linux).
Set("darwin", d.Count.MacOS).
Set("windows", d.Count.Windows).
Set("freebsd", d.Count.FreeBSD).
Set("docker", d.Count.Docker).
Set("k8s", d.Count.K8S).
Set("version", d.Version),
})
}
// fetchTelemetry - fetches telemetry data: count of various object types in DB
2022-01-22 23:12:33 +08:00
func fetchTelemetryData() (telemetryData, error) {
var data telemetryData
data.ExtClients = getDBLength(database.EXT_CLIENT_TABLE_NAME)
data.Users = getDBLength(database.USERS_TABLE_NAME)
data.Networks = getDBLength(database.NETWORKS_TABLE_NAME)
2022-01-21 06:50:42 +08:00
data.Version = servercfg.GetVersion()
2022-01-26 00:00:56 +08:00
nodes, err := GetAllNodes()
2022-01-21 06:50:42 +08:00
if err == nil {
data.Nodes = len(nodes)
data.Count = getClientCount(nodes)
}
return data, err
}
// setTelemetryTimestamp - Give the entry in the DB a new timestamp
2022-01-29 07:27:21 +08:00
func setTelemetryTimestamp(telRecord *models.Telemetry) error {
2022-01-21 06:50:42 +08:00
lastsend := time.Now().Unix()
var serverTelData = models.Telemetry{
2022-01-29 22:37:53 +08:00
UUID: telRecord.UUID,
LastSend: lastsend,
TrafficKeyPriv: telRecord.TrafficKeyPriv,
TrafficKeyPub: telRecord.TrafficKeyPub,
2022-01-21 06:50:42 +08:00
}
2022-01-26 01:36:25 +08:00
jsonObj, err := json.Marshal(&serverTelData)
2022-01-21 06:50:42 +08:00
if err != nil {
return err
}
err = database.Insert(database.SERVER_UUID_RECORD_KEY, string(jsonObj), database.SERVER_UUID_TABLE_NAME)
return err
}
// getClientCount - returns counts of nodes with various OS types and conditions
2022-01-22 23:12:33 +08:00
func getClientCount(nodes []models.Node) clientCount {
var count clientCount
2022-01-21 06:50:42 +08:00
for _, node := range nodes {
switch node.OS {
case "macos":
count.MacOS += 1
case "windows":
count.Windows += 1
case "linux":
count.Linux += 1
case "freebsd":
count.FreeBSD += 1
}
if !(node.IsServer == "yes") {
count.NonServer += 1
}
}
return count
}
// fetchTelemetryRecord - get the existing UUID and Timestamp from the DB
func fetchTelemetryRecord() (models.Telemetry, error) {
var rawData string
var telObj models.Telemetry
var err error
rawData, err = database.FetchRecord(database.SERVER_UUID_TABLE_NAME, database.SERVER_UUID_RECORD_KEY)
if err != nil {
return telObj, err
}
err = json.Unmarshal([]byte(rawData), &telObj)
return telObj, err
}
2022-01-22 23:12:33 +08:00
// getDBLength - get length of DB to get count of objects
func getDBLength(dbname string) int {
data, err := database.FetchRecords(dbname)
if err != nil {
return 0
}
return len(data)
}
2022-01-23 22:39:48 +08:00
// telemetryData - What data to send to posthog
2022-01-22 23:12:33 +08:00
type telemetryData struct {
2022-01-21 06:50:42 +08:00
Nodes int
ExtClients int
Users int
2022-01-22 23:12:33 +08:00
Count clientCount
2022-01-21 06:50:42 +08:00
Networks int
Version string
}
2022-01-23 22:39:48 +08:00
// clientCount - What types of netclients we're tallying
2022-01-22 23:12:33 +08:00
type clientCount struct {
2022-01-21 06:50:42 +08:00
MacOS int
Windows int
Linux int
FreeBSD int
K8S int
Docker int
NonServer int
}