2018-05-24 21:26:27 +08:00
|
|
|
package rendering
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-19 21:47:17 +08:00
|
|
|
"errors"
|
2018-05-24 21:26:27 +08:00
|
|
|
"fmt"
|
|
|
|
|
2020-04-21 22:16:41 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2018-05-24 21:26:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (rs *RenderingService) startPlugin(ctx context.Context) error {
|
2020-01-09 00:43:28 +08:00
|
|
|
return rs.pluginInfo.Start(ctx)
|
2018-05-24 21:26:27 +08:00
|
|
|
}
|
|
|
|
|
2020-02-20 02:47:39 +08:00
|
|
|
func (rs *RenderingService) renderViaPlugin(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
|
2020-04-21 22:16:41 +08:00
|
|
|
// gives plugin some additional time to timeout and return possible errors.
|
2022-01-27 06:02:19 +08:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
|
2020-04-21 22:16:41 +08:00
|
|
|
defer cancel()
|
|
|
|
|
2021-05-12 23:16:57 +08:00
|
|
|
filePath, err := rs.getNewFilePath(RenderPNG)
|
2020-04-21 22:16:41 +08: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 23:16:57 +08:00
|
|
|
FilePath: filePath,
|
2020-04-21 22:16:41 +08:00
|
|
|
Timeout: int32(opts.Timeout.Seconds()),
|
|
|
|
RenderKey: renderKey,
|
|
|
|
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
|
|
|
Domain: rs.domain,
|
|
|
|
Headers: headers,
|
|
|
|
}
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
2021-11-01 17:53:33 +08:00
|
|
|
rsp, err := rs.pluginInfo.Renderer.Render(ctx, req)
|
2020-11-19 21:47:17 +08:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2020-04-27 20:29:46 +08:00
|
|
|
rs.log.Info("Rendering timed out")
|
|
|
|
return nil, ErrTimeout
|
|
|
|
}
|
2018-05-24 21:26:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.Error != "" {
|
2020-11-05 18:57:20 +08:00
|
|
|
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
|
2018-05-24 21:26:27 +08:00
|
|
|
}
|
|
|
|
|
2021-05-12 23:16:57 +08: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 06:02:19 +08:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
|
2021-05-12 23:16:57 +08: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,
|
|
|
|
}
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
2021-11-01 17:53:33 +08:00
|
|
|
rsp, err := rs.pluginInfo.Renderer.RenderCSV(ctx, req)
|
2021-05-12 23:16:57 +08: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 21:26:27 +08:00
|
|
|
}
|