2022-12-22 19:51:29 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/metadata"
|
2022-12-22 19:51:29 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type environmentDetailHandler struct{}
|
|
|
|
|
|
|
|
func newEnvironmentDetailHandler() *environmentDetailHandler {
|
|
|
|
return &environmentDetailHandler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *environmentDetailHandler) Handle(params metadata.GetEnvironmentDetailParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("error starting transaction for user '%v': %v", principal.Email, err)
|
2022-12-22 19:51:29 +01:00
|
|
|
return metadata.NewGetEnvironmentDetailInternalServerError()
|
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
2022-12-22 20:56:19 +01:00
|
|
|
senv, err := str.FindEnvironmentForAccount(params.EnvZID, int(principal.ID), tx)
|
2022-12-22 19:51:29 +01:00
|
|
|
if err != nil {
|
2022-12-22 20:56:19 +01:00
|
|
|
logrus.Errorf("environment '%v' not found for account '%v': %v", params.EnvZID, principal.Email, err)
|
2022-12-22 19:51:29 +01:00
|
|
|
return metadata.NewGetEnvironmentDetailNotFound()
|
|
|
|
}
|
2023-05-17 17:23:16 +02:00
|
|
|
es := &rest_model_zrok.EnvironmentAndResources{
|
2022-12-22 19:51:29 +01:00
|
|
|
Environment: &rest_model_zrok.Environment{
|
|
|
|
Address: senv.Address,
|
|
|
|
CreatedAt: senv.CreatedAt.UnixMilli(),
|
|
|
|
Description: senv.Description,
|
|
|
|
Host: senv.Host,
|
|
|
|
UpdatedAt: senv.UpdatedAt.UnixMilli(),
|
|
|
|
ZID: senv.ZId,
|
|
|
|
},
|
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
shrs, err := str.FindSharesForEnvironment(senv.Id, tx)
|
2022-12-22 19:51:29 +01:00
|
|
|
if err != nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("error finding shares for environment '%v' for user '%v': %v", senv.ZId, principal.Email, err)
|
2022-12-22 19:51:29 +01:00
|
|
|
return metadata.NewGetEnvironmentDetailInternalServerError()
|
|
|
|
}
|
2023-05-11 21:21:10 +02:00
|
|
|
sparkRx := make(map[string][]int64)
|
|
|
|
sparkTx := make(map[string][]int64)
|
2023-03-13 19:19:38 +01:00
|
|
|
if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
|
2023-05-11 21:21:10 +02:00
|
|
|
sparkRx, sparkTx, err = sparkDataForShares(shrs)
|
2022-12-22 19:51:29 +01:00
|
|
|
if err != nil {
|
2023-01-30 17:38:55 +01:00
|
|
|
logrus.Errorf("error querying spark data for shares for user '%v': %v", principal.Email, err)
|
2022-12-22 19:51:29 +01:00
|
|
|
}
|
2023-03-13 19:19:38 +01:00
|
|
|
} else {
|
|
|
|
logrus.Debug("skipping spark data for shares; no influx configuration")
|
2022-12-22 19:51:29 +01:00
|
|
|
}
|
2023-01-04 19:13:50 +01:00
|
|
|
for _, shr := range shrs {
|
2022-12-22 19:51:29 +01:00
|
|
|
feEndpoint := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.FrontendEndpoint != nil {
|
|
|
|
feEndpoint = *shr.FrontendEndpoint
|
2022-12-22 19:51:29 +01:00
|
|
|
}
|
|
|
|
feSelection := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.FrontendSelection != nil {
|
|
|
|
feSelection = *shr.FrontendSelection
|
2022-12-22 19:51:29 +01:00
|
|
|
}
|
|
|
|
beProxyEndpoint := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.BackendProxyEndpoint != nil {
|
|
|
|
beProxyEndpoint = *shr.BackendProxyEndpoint
|
2022-12-22 19:51:29 +01:00
|
|
|
}
|
2023-05-11 21:21:10 +02:00
|
|
|
var sparkData []*rest_model_zrok.SparkDataSample
|
|
|
|
for i := 0; i < len(sparkRx[shr.Token]) && i < len(sparkTx[shr.Token]); i++ {
|
|
|
|
sparkData = append(sparkData, &rest_model_zrok.SparkDataSample{Rx: float64(sparkRx[shr.Token][i]), Tx: float64(sparkTx[shr.Token][i])})
|
|
|
|
}
|
2023-01-04 19:43:37 +01:00
|
|
|
es.Shares = append(es.Shares, &rest_model_zrok.Share{
|
2023-01-04 19:13:50 +01:00
|
|
|
Token: shr.Token,
|
|
|
|
ZID: shr.ZId,
|
|
|
|
ShareMode: shr.ShareMode,
|
|
|
|
BackendMode: shr.BackendMode,
|
2022-12-22 19:51:29 +01:00
|
|
|
FrontendSelection: feSelection,
|
|
|
|
FrontendEndpoint: feEndpoint,
|
|
|
|
BackendProxyEndpoint: beProxyEndpoint,
|
2023-01-04 19:13:50 +01:00
|
|
|
Reserved: shr.Reserved,
|
2023-05-11 21:41:21 +02:00
|
|
|
Activity: sparkData,
|
2023-01-04 19:13:50 +01:00
|
|
|
CreatedAt: shr.CreatedAt.UnixMilli(),
|
|
|
|
UpdatedAt: shr.UpdatedAt.UnixMilli(),
|
2022-12-22 19:51:29 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return metadata.NewGetEnvironmentDetailOK().WithPayload(es)
|
|
|
|
}
|