2022-12-22 22:06:23 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
2023-05-12 19:24:29 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"strconv"
|
2022-12-22 22:06:23 +01:00
|
|
|
)
|
|
|
|
|
2023-05-12 17:57:34 +02:00
|
|
|
func sparkDataForEnvironments(envs []*store.Environment) (rx, tx map[int][]int64, err error) {
|
|
|
|
rx = make(map[int][]int64)
|
|
|
|
tx = make(map[int][]int64)
|
|
|
|
if len(envs) > 0 {
|
|
|
|
qapi := idb.QueryAPI(cfg.Metrics.Influx.Org)
|
|
|
|
|
|
|
|
envFilter := "|> filter(fn: (r) =>"
|
|
|
|
for i, env := range envs {
|
|
|
|
if i > 0 {
|
|
|
|
envFilter += " or"
|
|
|
|
}
|
|
|
|
envFilter += fmt.Sprintf(" r[\"envId\"] == \"%d\"", env.Id)
|
|
|
|
}
|
|
|
|
envFilter += ")"
|
|
|
|
query := fmt.Sprintf("from(bucket: \"%v\")\n", cfg.Metrics.Influx.Bucket) +
|
|
|
|
"|> range(start: -5m)\n" +
|
|
|
|
"|> filter(fn: (r) => r[\"_measurement\"] == \"xfer\")\n" +
|
|
|
|
"|> filter(fn: (r) => r[\"_field\"] == \"rx\" or r[\"_field\"] == \"tx\")\n" +
|
|
|
|
"|> filter(fn: (r) => r[\"namespace\"] == \"backend\")\n" +
|
|
|
|
envFilter +
|
2023-05-12 19:24:29 +02:00
|
|
|
"|> drop(columns: [\"share\", \"acctId\"])\n" +
|
2023-05-12 17:57:34 +02:00
|
|
|
"|> aggregateWindow(every: 10s, fn: sum, createEmpty: true)\n"
|
|
|
|
|
|
|
|
result, err := qapi.Query(context.Background(), query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
2023-05-12 19:24:29 +02:00
|
|
|
envIdS := result.Record().ValueByKey("envId").(string)
|
|
|
|
envId, err := strconv.ParseInt(envIdS, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error parsing '%v': %v", envIdS, err)
|
|
|
|
continue
|
|
|
|
}
|
2023-05-12 17:57:34 +02:00
|
|
|
switch result.Record().Field() {
|
|
|
|
case "rx":
|
|
|
|
rxV := int64(0)
|
|
|
|
if v, ok := result.Record().Value().(int64); ok {
|
|
|
|
rxV = v
|
|
|
|
}
|
|
|
|
rxData := append(rx[int(envId)], rxV)
|
|
|
|
rx[int(envId)] = rxData
|
|
|
|
|
|
|
|
case "tx":
|
|
|
|
txV := int64(0)
|
|
|
|
if v, ok := result.Record().Value().(int64); ok {
|
|
|
|
txV = v
|
|
|
|
}
|
|
|
|
txData := append(tx[int(envId)], txV)
|
|
|
|
tx[int(envId)] = txData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rx, tx, nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 21:21:10 +02:00
|
|
|
func sparkDataForShares(shrs []*store.Share) (rx, tx map[string][]int64, err error) {
|
|
|
|
rx = make(map[string][]int64)
|
|
|
|
tx = make(map[string][]int64)
|
2023-01-04 19:13:50 +01:00
|
|
|
if len(shrs) > 0 {
|
2023-03-13 19:19:38 +01:00
|
|
|
qapi := idb.QueryAPI(cfg.Metrics.Influx.Org)
|
2022-12-22 22:06:23 +01:00
|
|
|
|
2023-05-12 17:57:34 +02:00
|
|
|
shrFilter := "|> filter(fn: (r) =>"
|
|
|
|
for i, shr := range shrs {
|
|
|
|
if i > 0 {
|
|
|
|
shrFilter += " or"
|
|
|
|
}
|
|
|
|
shrFilter += fmt.Sprintf(" r[\"share\"] == \"%v\"", shr.Token)
|
|
|
|
}
|
|
|
|
shrFilter += ")"
|
|
|
|
query := fmt.Sprintf("from(bucket: \"%v\")\n", cfg.Metrics.Influx.Bucket) +
|
|
|
|
"|> range(start: -5m)\n" +
|
|
|
|
"|> filter(fn: (r) => r[\"_measurement\"] == \"xfer\")\n" +
|
|
|
|
"|> filter(fn: (r) => r[\"_field\"] == \"rx\" or r[\"_field\"] == \"tx\")\n" +
|
|
|
|
"|> filter(fn: (r) => r[\"namespace\"] == \"backend\")\n" +
|
|
|
|
shrFilter +
|
|
|
|
"|> aggregateWindow(every: 10s, fn: sum, createEmpty: true)\n"
|
|
|
|
|
2023-05-11 21:21:10 +02:00
|
|
|
result, err := qapi.Query(context.Background(), query)
|
2022-12-22 22:06:23 +01:00
|
|
|
if err != nil {
|
2023-05-11 21:21:10 +02:00
|
|
|
return nil, nil, err
|
2022-12-22 22:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
2023-01-04 19:13:50 +01:00
|
|
|
shrToken := result.Record().ValueByKey("share").(string)
|
2023-05-11 21:21:10 +02:00
|
|
|
switch result.Record().Field() {
|
|
|
|
case "rx":
|
|
|
|
rxV := int64(0)
|
|
|
|
if v, ok := result.Record().Value().(int64); ok {
|
|
|
|
rxV = v
|
|
|
|
}
|
|
|
|
rxData := append(rx[shrToken], rxV)
|
|
|
|
rx[shrToken] = rxData
|
|
|
|
|
|
|
|
case "tx":
|
|
|
|
txV := int64(0)
|
|
|
|
if v, ok := result.Record().Value().(int64); ok {
|
|
|
|
txV = v
|
|
|
|
}
|
|
|
|
txData := append(tx[shrToken], txV)
|
|
|
|
tx[shrToken] = txData
|
|
|
|
}
|
2022-12-22 22:06:23 +01:00
|
|
|
}
|
|
|
|
}
|
2023-05-11 21:21:10 +02:00
|
|
|
return rx, tx, nil
|
2022-12-22 22:06:23 +01:00
|
|
|
}
|