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-01-17 05:07:16 -06:00
|
|
|
"time"
|
2018-05-24 08:26:27 -05:00
|
|
|
|
2020-04-21 09:16:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2018-05-24 08:26:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (rs *RenderingService) startPlugin(ctx context.Context) error {
|
2020-01-08 10:43:28 -06:00
|
|
|
return rs.pluginInfo.Start(ctx)
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
|
|
|
|
2020-02-19 12:47:39 -06:00
|
|
|
func (rs *RenderingService) renderViaPlugin(ctx context.Context, 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.
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
filePath, err := rs.getNewFilePath(RenderPNG)
|
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{
|
|
|
|
Url: rs.getURL(opts.Path),
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
rsp, err := rs.pluginInfo.Renderer.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.
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
|
|
|
|
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 04:53:33 -05:00
|
|
|
rsp, err := rs.pluginInfo.Renderer.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
|
|
|
}
|