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"
|
2022-12-22 22:06:23 +01:00
|
|
|
)
|
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
func sparkDataForShares(shrs []*store.Share) (map[string][]int64, error) {
|
2022-12-22 22:06:23 +01:00
|
|
|
out := make(map[string][]int64)
|
|
|
|
|
2023-01-04 19:13:50 +01:00
|
|
|
if len(shrs) > 0 {
|
2022-12-22 22:06:23 +01:00
|
|
|
qapi := idb.QueryAPI(cfg.Influx.Org)
|
|
|
|
|
2023-01-04 19:13:50 +01:00
|
|
|
result, err := qapi.Query(context.Background(), sparkFluxQuery(shrs))
|
2022-12-22 22:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
|
|
|
combinedRate := int64(0)
|
|
|
|
readRate := result.Record().ValueByKey("bytesRead")
|
|
|
|
if readRate != nil {
|
|
|
|
combinedRate += readRate.(int64)
|
|
|
|
}
|
|
|
|
writeRate := result.Record().ValueByKey("bytesWritten")
|
|
|
|
if writeRate != nil {
|
|
|
|
combinedRate += writeRate.(int64)
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
shrToken := result.Record().ValueByKey("share").(string)
|
|
|
|
shrMetrics := out[shrToken]
|
|
|
|
shrMetrics = append(shrMetrics, combinedRate)
|
|
|
|
out[shrToken] = shrMetrics
|
2022-12-22 22:06:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:13:50 +01:00
|
|
|
func sparkFluxQuery(shrs []*store.Share) string {
|
2023-01-04 20:21:23 +01:00
|
|
|
shrFilter := "|> filter(fn: (r) =>"
|
2023-01-04 19:13:50 +01:00
|
|
|
for i, shr := range shrs {
|
2022-12-22 22:06:23 +01:00
|
|
|
if i > 0 {
|
2023-01-04 20:21:23 +01:00
|
|
|
shrFilter += " or"
|
2022-12-22 22:06:23 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
shrFilter += fmt.Sprintf(" r[\"share\"] == \"%v\"", shr.Token)
|
2022-12-22 22:06:23 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
shrFilter += ")"
|
2022-12-22 22:06:23 +01:00
|
|
|
query := "read = from(bucket: \"zrok\")" +
|
|
|
|
"|> range(start: -5m)" +
|
|
|
|
"|> filter(fn: (r) => r[\"_measurement\"] == \"xfer\")" +
|
|
|
|
"|> filter(fn: (r) => r[\"_field\"] == \"bytesRead\" or r[\"_field\"] == \"bytesWritten\")" +
|
2023-03-07 22:29:39 +01:00
|
|
|
"|> filter(fn: (r) => r[\"namespace\"] == \"backend\")" +
|
2023-01-04 20:21:23 +01:00
|
|
|
shrFilter +
|
2022-12-22 22:06:23 +01:00
|
|
|
"|> aggregateWindow(every: 5s, fn: sum, createEmpty: true)\n" +
|
|
|
|
"|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")" +
|
|
|
|
"|> yield(name: \"last\")"
|
|
|
|
return query
|
|
|
|
}
|