2018-05-24 08:26:27 -05:00
|
|
|
package rendering
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-19 07:47:17 -06:00
|
|
|
"errors"
|
2018-05-24 08:26:27 -05:00
|
|
|
"fmt"
|
|
|
|
|
2020-04-21 09:16:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2018-05-24 08:26:27 -05:00
|
|
|
)
|
|
|
|
|
2024-02-08 06:09:34 -06:00
|
|
|
func (rs *RenderingService) renderViaPlugin(ctx context.Context, renderType RenderType, renderKey string, opts Opts) (*RenderResult, error) {
|
2020-04-21 09:16:41 -05:00
|
|
|
// gives plugin some additional time to timeout and return possible errors.
|
2022-01-26 16:02:19 -06:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
|
2020-04-21 09:16:41 -05:00
|
|
|
defer cancel()
|
|
|
|
|
2024-02-08 06:09:34 -06:00
|
|
|
filePath, err := rs.getNewFilePath(renderType)
|
2020-04-21 09:16:41 -05: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{
|
2024-05-14 05:24:18 -05:00
|
|
|
Url: rs.getGrafanaCallbackURL(opts.Path),
|
2020-04-21 09:16:41 -05:00
|
|
|
Width: int32(opts.Width),
|
|
|
|
Height: int32(opts.Height),
|
|
|
|
DeviceScaleFactor: float32(opts.DeviceScaleFactor),
|
2021-05-12 10:16:57 -05:00
|
|
|
FilePath: filePath,
|
2020-04-21 09:16:41 -05:00
|
|
|
Timeout: int32(opts.Timeout.Seconds()),
|
|
|
|
RenderKey: renderKey,
|
|
|
|
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
|
|
|
Domain: rs.domain,
|
|
|
|
Headers: headers,
|
2022-08-30 05:09:38 -05:00
|
|
|
AuthToken: rs.Cfg.RendererAuthToken,
|
2024-05-14 05:24:18 -05:00
|
|
|
Encoding: string(renderType),
|
2020-04-21 09:16:41 -05:00
|
|
|
}
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
2023-12-14 10:33:29 -06:00
|
|
|
rc, err := rs.plugin.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rsp, err := rc.Render(ctx, req)
|
2020-11-19 07:47:17 -06:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2020-04-27 07:29:46 -05:00
|
|
|
rs.log.Info("Rendering timed out")
|
|
|
|
return nil, ErrTimeout
|
|
|
|
}
|
2018-05-24 08:26:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.Error != "" {
|
2020-11-05 04:57:20 -06:00
|
|
|
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
|
|
|
|
2021-05-12 10:16:57 -05: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-26 16:02:19 -06:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
|
2021-05-12 10:16:57 -05: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{
|
2024-05-14 05:24:18 -05:00
|
|
|
Url: rs.getGrafanaCallbackURL(opts.Path),
|
2021-05-12 10:16:57 -05:00
|
|
|
FilePath: filePath,
|
|
|
|
RenderKey: renderKey,
|
|
|
|
Domain: rs.domain,
|
|
|
|
Timeout: int32(opts.Timeout.Seconds()),
|
|
|
|
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
|
|
|
Headers: headers,
|
2022-08-30 05:09:38 -05:00
|
|
|
AuthToken: rs.Cfg.RendererAuthToken,
|
2021-05-12 10:16:57 -05:00
|
|
|
}
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
2023-12-14 10:33:29 -06:00
|
|
|
rc, err := rs.plugin.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp, err := rc.RenderCSV(ctx, req)
|
2021-05-12 10:16:57 -05: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 08:26:27 -05:00
|
|
|
}
|