956ab05148
* init * it works! but what a mess * nil ptr bug * split up client.go * split up search_request.go * split up data_query.go * split up response_parser * fix merge * update handling request * raw dsl agg parser * change rawQuery to rawDSLQuery * agg parser works but needs work * clean up agg parser * fix bugs with raw dsl parsers * feature toggle * fix tests * editor type selector * editor type added * add fix builder vs code by not using same query field * clean up * fix lint * pretty * editor type selection should be behind ft * adam's feedback * prettier
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package es
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
|
)
|
|
|
|
// requestEncoder handles encoding of search requests to Elasticsearch format
|
|
type requestEncoder struct {
|
|
logger log.Logger
|
|
}
|
|
|
|
// newRequestEncoder creates a new request encoder
|
|
func newRequestEncoder(logger log.Logger) *requestEncoder {
|
|
return &requestEncoder{
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// encodeBatchRequests encodes multiple requests into NDJSON format
|
|
func (e *requestEncoder) encodeBatchRequests(requests []*multiRequest) ([]byte, error) {
|
|
start := time.Now()
|
|
defer func() {
|
|
e.logger.Debug("Completed encoding of batch requests to json", "duration", time.Since(start))
|
|
}()
|
|
|
|
payload := bytes.Buffer{}
|
|
for _, r := range requests {
|
|
reqHeader, err := json.Marshal(r.header)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
payload.WriteString(string(reqHeader) + "\n")
|
|
|
|
body := ""
|
|
switch r.body.(type) {
|
|
case *SearchRequest:
|
|
reqBody, err := json.Marshal(r.body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
body = string(reqBody)
|
|
case string:
|
|
body = r.body.(string)
|
|
default:
|
|
return nil, fmt.Errorf("unknown request type: %T", r.body)
|
|
}
|
|
|
|
body = strings.ReplaceAll(body, "$__interval_ms", strconv.FormatInt(r.interval.Milliseconds(), 10))
|
|
body = strings.ReplaceAll(body, "$__interval", r.interval.String())
|
|
|
|
payload.WriteString(body + "\n")
|
|
}
|
|
|
|
return payload.Bytes(), nil
|
|
}
|