github.com/kuoss/venti@v0.2.20/pkg/handler/remote/remote.go (about) 1 package remote 2 3 import ( 4 "errors" 5 "fmt" 6 "net/url" 7 8 "github.com/gin-gonic/gin" 9 "github.com/kuoss/venti/pkg/handler/api" 10 "github.com/kuoss/venti/pkg/model" 11 dsService "github.com/kuoss/venti/pkg/service/datasource" 12 "github.com/kuoss/venti/pkg/service/remote" 13 ) 14 15 type RemoteHandler struct { 16 datasourceService *dsService.DatasourceService 17 remoteService *remote.RemoteService 18 } 19 20 func New(datasourceService *dsService.DatasourceService, remoteService *remote.RemoteService) *RemoteHandler { 21 return &RemoteHandler{ 22 datasourceService, 23 remoteService, 24 } 25 } 26 27 // GET /api/remote/healthy 28 func (h *RemoteHandler) Healthy(c *gin.Context) { 29 h.remoteAction(c, remote.ActionHealthy, "") 30 } 31 32 // GET /api/remote/metadata 33 func (h *RemoteHandler) Metadata(c *gin.Context) { 34 h.remoteAction(c, remote.ActionMetadata, "") 35 } 36 37 // GET /api/remote/query 38 func (h *RemoteHandler) Query(c *gin.Context) { 39 values := url.Values{} 40 values.Set("time", c.Query("time")) 41 values.Set("timeout", c.Query("timeout")) 42 values.Set("query", c.Query("query")) 43 values.Set("logFormat", c.Query("logFormat")) 44 h.remoteAction(c, remote.ActionQuery, values.Encode()) 45 } 46 47 // GET /api/remote/query_range 48 func (h *RemoteHandler) QueryRange(c *gin.Context) { 49 values := url.Values{} 50 values.Set("start", c.Query("start")) 51 values.Set("end", c.Query("end")) 52 values.Set("step", c.Query("step")) 53 values.Set("timeout", c.Query("timeout")) 54 values.Set("query", c.Query("query")) 55 values.Set("logFormat", c.Query("logFormat")) 56 h.remoteAction(c, remote.ActionQueryRange, values.Encode()) 57 } 58 59 func (h *RemoteHandler) remoteAction(c *gin.Context, action remote.Action, rawQuery string) { 60 datasource, err := h.getDatasourceWithParams(c.Query("dsName"), c.Query("dsType")) 61 if err != nil { 62 api.ResponseError(c, api.ErrorInternal, fmt.Errorf("getDatasourceWithParams err: %w", err)) 63 return 64 } 65 code, body, err := h.remoteService.GET(c.Request.Context(), &datasource, action, rawQuery) 66 if err != nil { 67 api.ResponseError(c, api.ErrorInternal, fmt.Errorf("GET err: %w", err)) 68 return 69 } 70 c.String(code, body) 71 } 72 73 // Select and return the datasource corresponding to the dsID or dsType parameter 74 func (h *RemoteHandler) getDatasourceWithParams(dsName string, dsType string) (model.Datasource, error) { 75 if dsName == "" && dsType == "" { 76 return model.Datasource{}, errors.New("either dsName or dsType must be specified") 77 } 78 79 // If there is a dsID, return the datasource of the corresponding index 80 if dsName != "" { 81 datasource, err := h.datasourceService.GetDatasourceByName(dsName) 82 if err != nil { 83 return model.Datasource{}, fmt.Errorf("GetDatasourceByName err: %w", err) 84 } 85 return datasource, nil 86 } 87 // The following handles cases where there is no dsName... 88 // Invalid if dsType is neither lethe nor prometheus 89 if dsType != string(model.DatasourceTypeLethe) && dsType != string(model.DatasourceTypePrometheus) { 90 return model.Datasource{}, errors.New("invalid dsType") 91 } 92 // Returns the main datasource for the requested dsType 93 datasource, err := h.datasourceService.GetMainDatasourceByType(model.DatasourceType(dsType)) 94 if err != nil { 95 return model.Datasource{}, fmt.Errorf("GetMainDatasourceByType err: %w", err) 96 } 97 return datasource, nil 98 }