2022-08-03 20:58:11 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-10-19 21:21:15 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-08-03 20:58:11 +02:00
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2022-10-19 21:21:15 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
2022-08-03 20:58:11 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/metadata"
|
2022-08-03 21:05:28 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-03 20:58:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func overviewHandler(_ metadata.OverviewParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2022-08-03 21:05:28 +02:00
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return metadata.NewOverviewInternalServerError()
|
2022-08-03 21:05:28 +02:00
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
|
|
|
|
if err != nil {
|
2022-09-09 16:20:05 +02:00
|
|
|
logrus.Errorf("error finding environments for '%v': %v", principal.Email, err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return metadata.NewOverviewInternalServerError()
|
2022-08-03 21:05:28 +02:00
|
|
|
}
|
|
|
|
var out rest_model_zrok.EnvironmentServicesList
|
|
|
|
for _, env := range envs {
|
|
|
|
svcs, err := str.FindServicesForEnvironment(env.Id, tx)
|
|
|
|
if err != nil {
|
2022-10-19 18:24:43 +02:00
|
|
|
logrus.Errorf("error finding services for environment '%v': %v", env.ZId, err)
|
2022-09-26 22:35:06 +02:00
|
|
|
return metadata.NewOverviewInternalServerError()
|
2022-08-03 21:05:28 +02:00
|
|
|
}
|
2022-10-19 21:21:15 +02:00
|
|
|
es := &rest_model_zrok.EnvironmentServices{
|
|
|
|
Environment: &rest_model_zrok.Environment{
|
|
|
|
Address: env.Address,
|
2022-11-01 21:52:02 +01:00
|
|
|
CreatedAt: env.CreatedAt.UnixMilli(),
|
2022-10-19 21:21:15 +02:00
|
|
|
Description: env.Description,
|
|
|
|
Host: env.Host,
|
2022-11-01 21:52:02 +01:00
|
|
|
UpdatedAt: env.UpdatedAt.UnixMilli(),
|
2022-10-19 21:21:15 +02:00
|
|
|
ZID: env.ZId,
|
|
|
|
},
|
|
|
|
}
|
2022-12-07 18:24:00 +01:00
|
|
|
|
|
|
|
var sparkData map[string][]int64
|
|
|
|
if cfg.Influx != nil {
|
|
|
|
sparkData, err = sparkDataForServices(svcs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error querying spark data for services: %v", err)
|
|
|
|
return metadata.NewOverviewInternalServerError()
|
|
|
|
}
|
2022-10-19 21:21:15 +02:00
|
|
|
}
|
2022-12-07 18:24:00 +01:00
|
|
|
|
2022-10-19 21:21:15 +02:00
|
|
|
for _, svc := range svcs {
|
2022-12-01 18:54:14 +01:00
|
|
|
feEndpoint := ""
|
2022-11-22 17:14:58 +01:00
|
|
|
if svc.FrontendEndpoint != nil {
|
2022-12-01 18:54:14 +01:00
|
|
|
feEndpoint = *svc.FrontendEndpoint
|
2022-11-22 17:14:58 +01:00
|
|
|
}
|
2022-12-01 18:54:14 +01:00
|
|
|
feSelection := ""
|
|
|
|
if svc.FrontendSelection != nil {
|
|
|
|
feSelection = *svc.FrontendSelection
|
|
|
|
}
|
|
|
|
beProxyEndpoint := ""
|
2022-11-22 17:14:58 +01:00
|
|
|
if svc.BackendProxyEndpoint != nil {
|
2022-12-01 18:54:14 +01:00
|
|
|
beProxyEndpoint = *svc.BackendProxyEndpoint
|
2022-11-22 17:14:58 +01:00
|
|
|
}
|
2022-10-19 21:21:15 +02:00
|
|
|
es.Services = append(es.Services, &rest_model_zrok.Service{
|
2022-11-30 18:46:19 +01:00
|
|
|
Token: svc.Token,
|
2022-12-01 18:54:14 +01:00
|
|
|
ZID: svc.ZId,
|
|
|
|
ShareMode: svc.ShareMode,
|
|
|
|
BackendMode: svc.BackendMode,
|
|
|
|
FrontendSelection: feSelection,
|
|
|
|
FrontendEndpoint: feEndpoint,
|
|
|
|
BackendProxyEndpoint: beProxyEndpoint,
|
|
|
|
Reserved: svc.Reserved,
|
2022-11-30 18:46:19 +01:00
|
|
|
Metrics: sparkData[svc.Token],
|
2022-12-01 18:54:14 +01:00
|
|
|
CreatedAt: svc.CreatedAt.UnixMilli(),
|
|
|
|
UpdatedAt: svc.UpdatedAt.UnixMilli(),
|
2022-10-19 21:21:15 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
out = append(out, es)
|
|
|
|
}
|
|
|
|
return metadata.NewOverviewOK().WithPayload(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sparkDataForServices(svcs []*store.Service) (map[string][]int64, error) {
|
|
|
|
out := make(map[string][]int64)
|
|
|
|
|
|
|
|
if len(svcs) > 0 {
|
|
|
|
qapi := idb.QueryAPI(cfg.Influx.Org)
|
|
|
|
|
|
|
|
result, err := qapi.Query(context.Background(), sparkFluxQuery(svcs))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
|
|
|
combinedRate := int64(0)
|
2022-10-19 23:37:29 +02:00
|
|
|
readRate := result.Record().ValueByKey("bytesRead")
|
2022-10-19 21:21:15 +02:00
|
|
|
if readRate != nil {
|
2022-10-19 23:37:29 +02:00
|
|
|
combinedRate += readRate.(int64)
|
2022-10-19 19:20:47 +02:00
|
|
|
}
|
2022-10-19 23:37:29 +02:00
|
|
|
writeRate := result.Record().ValueByKey("bytesWritten")
|
2022-10-19 21:21:15 +02:00
|
|
|
if writeRate != nil {
|
2022-10-19 23:37:29 +02:00
|
|
|
combinedRate += writeRate.(int64)
|
2022-10-19 19:20:47 +02:00
|
|
|
}
|
2022-11-30 18:10:00 +01:00
|
|
|
svcToken := result.Record().ValueByKey("service").(string)
|
|
|
|
svcMetrics := out[svcToken]
|
2022-10-19 21:21:15 +02:00
|
|
|
svcMetrics = append(svcMetrics, combinedRate)
|
2022-11-30 18:10:00 +01:00
|
|
|
out[svcToken] = svcMetrics
|
2022-08-03 21:05:28 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-19 21:21:15 +02:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sparkFluxQuery(svcs []*store.Service) string {
|
|
|
|
svcFilter := "|> filter(fn: (r) =>"
|
|
|
|
for i, svc := range svcs {
|
|
|
|
if i > 0 {
|
|
|
|
svcFilter += " or"
|
|
|
|
}
|
2022-11-30 18:10:00 +01:00
|
|
|
svcFilter += fmt.Sprintf(" r[\"service\"] == \"%v\"", svc.Token)
|
2022-10-19 21:21:15 +02:00
|
|
|
}
|
|
|
|
svcFilter += ")"
|
|
|
|
query := "read = from(bucket: \"zrok\")" +
|
|
|
|
"|> range(start: -5m)" +
|
|
|
|
"|> filter(fn: (r) => r[\"_measurement\"] == \"xfer\")" +
|
2022-10-19 23:37:29 +02:00
|
|
|
"|> filter(fn: (r) => r[\"_field\"] == \"bytesRead\" or r[\"_field\"] == \"bytesWritten\")" +
|
2022-10-19 21:21:15 +02:00
|
|
|
"|> filter(fn: (r) => r[\"namespace\"] == \"frontend\")" +
|
|
|
|
svcFilter +
|
2022-10-19 23:37:29 +02:00
|
|
|
"|> aggregateWindow(every: 5s, fn: sum, createEmpty: true)\n" +
|
|
|
|
"|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")" +
|
|
|
|
"|> yield(name: \"last\")"
|
2022-10-19 21:21:15 +02:00
|
|
|
return query
|
2022-08-03 20:58:11 +02:00
|
|
|
}
|