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"
|
2023-03-09 02:23:25 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2022-01-21 06:50:42 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
|
|
"github.com/posthog/posthog-go"
|
|
|
|
)
|
|
|
|
|
2022-09-30 06:40:10 +08:00
|
|
|
// flags to keep for telemetry
|
|
|
|
var isFreeTier bool
|
|
|
|
var isEE bool
|
|
|
|
|
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-09-30 06:40:10 +08:00
|
|
|
// setEEForTelemetry - store EE flag without having an import cycle when used for telemetry
|
|
|
|
// (as the ee package needs the logic package as currently written).
|
|
|
|
func SetEEForTelemetry(eeFlag bool) {
|
|
|
|
isEE = eeFlag
|
|
|
|
}
|
|
|
|
|
|
|
|
// setFreeTierForTelemetry - store free tier flag without having an import cycle when used for telemetry
|
|
|
|
// (as the ee package needs the logic package as currently written).
|
|
|
|
func SetFreeTierForTelemetry(freeTierFlag bool) {
|
|
|
|
isFreeTier = freeTierFlag
|
|
|
|
}
|
|
|
|
|
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).
|
2022-09-12 20:57:47 +08:00
|
|
|
Set("servers", d.Servers).
|
2022-01-21 06:50:42 +08:00
|
|
|
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).
|
2022-09-30 06:40:10 +08:00
|
|
|
Set("version", d.Version).
|
|
|
|
Set("is_ee", isEE).
|
|
|
|
Set("is_free_tier", isFreeTier),
|
2022-01-21 06:50:42 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2023-03-09 02:23:25 +08:00
|
|
|
data.Servers = getServerCount()
|
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
|
|
|
|
}
|
2023-03-09 02:23:25 +08:00
|
|
|
|
|
|
|
// getServerCount returns number of servers from database
|
|
|
|
func getServerCount() int {
|
|
|
|
data, err := database.FetchRecords(database.SERVER_UUID_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, "errror retrieving server data", err.Error())
|
|
|
|
}
|
|
|
|
return len(data)
|
|
|
|
}
|
2022-01-21 06:50:42 +08:00
|
|
|
|
|
|
|
// 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-12-21 04:29:09 +08:00
|
|
|
func getClientCount(nodes []models.Node) clientCount {
|
2022-01-22 23:12:33 +08:00
|
|
|
var count clientCount
|
2022-01-21 06:50:42 +08:00
|
|
|
for _, node := range nodes {
|
2022-12-21 04:29:09 +08:00
|
|
|
host, err := GetHost(node.HostID.String())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch host.OS {
|
2022-05-28 22:23:32 +08:00
|
|
|
case "darwin":
|
2022-01-21 06:50:42 +08:00
|
|
|
count.MacOS += 1
|
|
|
|
case "windows":
|
|
|
|
count.Windows += 1
|
|
|
|
case "linux":
|
|
|
|
count.Linux += 1
|
|
|
|
case "freebsd":
|
|
|
|
count.FreeBSD += 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
|
2022-09-12 20:36:17 +08:00
|
|
|
Servers int
|
2022-01-21 06:50:42 +08:00
|
|
|
Version string
|
2022-09-30 06:40:10 +08:00
|
|
|
IsEE bool
|
|
|
|
IsFreeTier bool
|
2022-01-21 06:50:42 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|