mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
96dfb385ca
* Chore: Replace response status with const var * Apply suggestions from code review Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> * Add net/http import --------- Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package searchV2
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
|
)
|
|
|
|
type SearchHTTPService interface {
|
|
RegisterHTTPRoutes(storageRoute routing.RouteRegister)
|
|
}
|
|
|
|
type searchHTTPService struct {
|
|
search SearchService
|
|
}
|
|
|
|
func ProvideSearchHTTPService(search SearchService) SearchHTTPService {
|
|
return &searchHTTPService{search: search}
|
|
}
|
|
|
|
func (s *searchHTTPService) RegisterHTTPRoutes(storageRoute routing.RouteRegister) {
|
|
storageRoute.Post("/", middleware.ReqSignedIn, routing.Wrap(s.doQuery))
|
|
}
|
|
|
|
func (s *searchHTTPService) doQuery(c *contextmodel.ReqContext) response.Response {
|
|
searchReadinessCheckResp := s.search.IsReady(c.Req.Context(), c.SignedInUser.GetOrgID())
|
|
if !searchReadinessCheckResp.IsReady {
|
|
dashboardSearchNotServedRequestsCounter.With(prometheus.Labels{
|
|
"reason": searchReadinessCheckResp.Reason,
|
|
}).Inc()
|
|
|
|
return response.JSON(http.StatusOK, &backend.DataResponse{
|
|
Frames: []*data.Frame{{
|
|
Name: "Loading",
|
|
}},
|
|
Error: nil,
|
|
})
|
|
}
|
|
|
|
body, err := io.ReadAll(c.Req.Body)
|
|
if err != nil {
|
|
return response.Error(http.StatusInternalServerError, "error reading bytes", err)
|
|
}
|
|
|
|
query := &DashboardQuery{}
|
|
err = json.Unmarshal(body, query)
|
|
if err != nil {
|
|
return response.Error(http.StatusBadRequest, "error parsing body", err)
|
|
}
|
|
|
|
resp := s.search.doDashboardQuery(c.Req.Context(), c.SignedInUser, c.SignedInUser.GetOrgID(), *query)
|
|
|
|
if resp.Error != nil {
|
|
return response.Error(http.StatusInternalServerError, "error handling search request", resp.Error)
|
|
}
|
|
|
|
if len(resp.Frames) == 0 {
|
|
msg := "invalid search response"
|
|
return response.Error(http.StatusInternalServerError, msg, errors.New(msg))
|
|
}
|
|
|
|
bytes, err := resp.MarshalJSON()
|
|
if err != nil {
|
|
return response.Error(http.StatusInternalServerError, "error marshalling response", err)
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, bytes)
|
|
}
|