2022-12-22 21:09:37 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2023-01-13 21:01:34 +01:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
|
|
|
"github.com/openziti/zrok/rest_model_zrok"
|
|
|
|
"github.com/openziti/zrok/rest_server_zrok/operations/metadata"
|
2022-12-22 21:09:37 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
type shareDetailHandler struct{}
|
2022-12-22 21:09:37 +01:00
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
func newShareDetailHandler() *shareDetailHandler {
|
|
|
|
return &shareDetailHandler{}
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
func (h *shareDetailHandler) Handle(params metadata.GetShareDetailParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
2022-12-22 21:09:37 +01:00
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error starting transaction: %v", err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return metadata.NewGetShareDetailInternalServerError()
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
2023-01-04 19:43:37 +01:00
|
|
|
shr, err := str.FindShareWithToken(params.ShrToken, tx)
|
2022-12-22 21:09:37 +01:00
|
|
|
if err != nil {
|
2023-01-04 19:43:37 +01:00
|
|
|
logrus.Errorf("error finding share '%v': %v", params.ShrToken, err)
|
|
|
|
return metadata.NewGetShareDetailNotFound()
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
|
|
|
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error finding environments for account '%v': %v", principal.Email, err)
|
2023-01-04 19:43:37 +01:00
|
|
|
return metadata.NewGetShareDetailInternalServerError()
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, env := range envs {
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.EnvironmentId == env.Id {
|
2022-12-22 21:09:37 +01:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Errorf("environment not matched for share '%v' for account '%v'", params.ShrToken, principal.Email)
|
2023-01-04 19:43:37 +01:00
|
|
|
return metadata.NewGetShareDetailNotFound()
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
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([]*store.Share{shr})
|
2022-12-22 21:09:37 +01:00
|
|
|
if err != nil {
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Errorf("error querying spark data for share: %v", err)
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
2023-03-13 19:19:38 +01:00
|
|
|
} else {
|
|
|
|
logrus.Debug("skipping spark data; no influx configuration")
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
|
|
|
feEndpoint := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.FrontendEndpoint != nil {
|
|
|
|
feEndpoint = *shr.FrontendEndpoint
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
|
|
|
feSelection := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.FrontendSelection != nil {
|
|
|
|
feSelection = *shr.FrontendSelection
|
2022-12-22 21:09:37 +01:00
|
|
|
}
|
|
|
|
beProxyEndpoint := ""
|
2023-01-04 19:13:50 +01:00
|
|
|
if shr.BackendProxyEndpoint != nil {
|
|
|
|
beProxyEndpoint = *shr.BackendProxyEndpoint
|
2022-12-22 21:09:37 +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
|
|
|
return metadata.NewGetShareDetailOK().WithPayload(&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 21:09:37 +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 21:09:37 +01:00
|
|
|
})
|
|
|
|
}
|