2018-05-24 15:26:27 +02:00
|
|
|
package rendering
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-11-19 14:47:17 +01:00
|
|
|
"errors"
|
2018-05-24 15:26:27 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
2020-04-21 16:16:41 +02:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2024-02-08 13:09:34 +01:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2018-05-24 15:26:27 +02:00
|
|
|
)
|
|
|
|
|
|
2024-02-08 13:09:34 +01:00
|
|
|
func (rs *RenderingService) renderViaPlugin(ctx context.Context, renderType RenderType, renderKey string, opts Opts) (*RenderResult, error) {
|
|
|
|
|
if renderType == RenderPDF {
|
|
|
|
|
if !rs.features.IsEnabled(ctx, featuremgmt.FlagNewPDFRendering) {
|
|
|
|
|
return nil, fmt.Errorf("feature 'newPDFRendering' disabled")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts.Encoding = "pdf"
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 16:16:41 +02:00
|
|
|
// gives plugin some additional time to timeout and return possible errors.
|
2022-01-27 02:02:19 +04:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
|
2020-04-21 16:16:41 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
2024-02-08 13:09:34 +01:00
|
|
|
filePath, err := rs.getNewFilePath(renderType)
|
2020-04-21 16:16:41 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headers := map[string]*pluginextensionv2.StringList{}
|
|
|
|
|
|
|
|
|
|
for k, values := range opts.Headers {
|
|
|
|
|
headers[k] = &pluginextensionv2.StringList{
|
|
|
|
|
Values: values,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := &pluginextensionv2.RenderRequest{
|
|
|
|
|
Url: rs.getURL(opts.Path),
|
|
|
|
|
Width: int32(opts.Width),
|
|
|
|
|
Height: int32(opts.Height),
|
|
|
|
|
DeviceScaleFactor: float32(opts.DeviceScaleFactor),
|
2021-05-12 17:16:57 +02:00
|
|
|
FilePath: filePath,
|
2020-04-21 16:16:41 +02:00
|
|
|
Timeout: int32(opts.Timeout.Seconds()),
|
|
|
|
|
RenderKey: renderKey,
|
|
|
|
|
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
|
|
|
|
Domain: rs.domain,
|
|
|
|
|
Headers: headers,
|
2022-08-30 12:09:38 +02:00
|
|
|
AuthToken: rs.Cfg.RendererAuthToken,
|
2024-02-08 13:09:34 +01:00
|
|
|
Encoding: opts.Encoding,
|
2020-04-21 16:16:41 +02:00
|
|
|
}
|
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
|
2023-12-14 17:33:29 +01:00
|
|
|
rc, err := rs.plugin.Client()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
rsp, err := rc.Render(ctx, req)
|
2020-11-19 14:47:17 +01:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2020-04-27 15:29:46 +03:00
|
|
|
rs.log.Info("Rendering timed out")
|
|
|
|
|
return nil, ErrTimeout
|
|
|
|
|
}
|
2018-05-24 15:26:27 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if rsp.Error != "" {
|
2020-11-05 11:57:20 +01:00
|
|
|
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
|
2018-05-24 15:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 17:16:57 +02:00
|
|
|
return &RenderResult{FilePath: filePath}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rs *RenderingService) renderCSVViaPlugin(ctx context.Context, renderKey string, opts CSVOpts) (*RenderCSVResult, error) {
|
|
|
|
|
// gives plugin some additional time to timeout and return possible errors.
|
2022-01-27 02:02:19 +04:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
|
2021-05-12 17:16:57 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
filePath, err := rs.getNewFilePath(RenderCSV)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headers := map[string]*pluginextensionv2.StringList{}
|
|
|
|
|
for k, values := range opts.Headers {
|
|
|
|
|
headers[k] = &pluginextensionv2.StringList{
|
|
|
|
|
Values: values,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := &pluginextensionv2.RenderCSVRequest{
|
|
|
|
|
Url: rs.getURL(opts.Path),
|
|
|
|
|
FilePath: filePath,
|
|
|
|
|
RenderKey: renderKey,
|
|
|
|
|
Domain: rs.domain,
|
|
|
|
|
Timeout: int32(opts.Timeout.Seconds()),
|
|
|
|
|
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
|
|
|
|
Headers: headers,
|
2022-08-30 12:09:38 +02:00
|
|
|
AuthToken: rs.Cfg.RendererAuthToken,
|
2021-05-12 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
|
2023-12-14 17:33:29 +01:00
|
|
|
rc, err := rs.plugin.Client()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rsp, err := rc.RenderCSV(ctx, req)
|
2021-05-12 17:16:57 +02:00
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
|
|
|
|
rs.log.Info("Rendering timed out")
|
|
|
|
|
return nil, ErrTimeout
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rsp.Error != "" {
|
|
|
|
|
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &RenderCSVResult{FilePath: filePath, FileName: rsp.FileName}, nil
|
2018-05-24 15:26:27 +02:00
|
|
|
}
|