2018-05-24 21:26:27 +08:00
package rendering
import (
"context"
"errors"
"time"
"github.com/grafana/grafana/pkg/models"
2022-08-10 17:56:48 +08:00
"github.com/grafana/grafana/pkg/services/org"
2022-10-27 23:27:03 +08:00
"github.com/grafana/grafana/pkg/util/errutil"
2018-05-24 21:26:27 +08:00
)
2020-11-05 18:57:20 +08:00
var ErrTimeout = errors . New ( "timeout error - you can set timeout in seconds with &timeout url parameter" )
2021-05-12 23:16:57 +08:00
var ErrConcurrentLimitReached = errors . New ( "rendering concurrent limit reached" )
var ErrRenderUnavailable = errors . New ( "rendering plugin not available" )
2022-10-27 23:27:03 +08:00
var ErrServerTimeout = errutil . NewBase ( errutil . StatusUnknown , "rendering.serverTimeout" , errutil . WithPublicMessage ( "error trying to connect to image-renderer service" ) )
2021-05-12 23:16:57 +08:00
type RenderType string
const (
RenderCSV RenderType = "csv"
RenderPNG RenderType = "png"
2024-02-08 20:09:34 +08:00
RenderPDF RenderType = "pdf"
2021-05-12 23:16:57 +08:00
)
2018-05-24 21:26:27 +08:00
2022-01-27 06:02:19 +08:00
type TimeoutOpts struct {
Timeout time . Duration // Timeout param passed to image-renderer service
RequestTimeoutMultiplier time . Duration // RequestTimeoutMultiplier used for plugin/HTTP request context timeout
}
type AuthOpts struct {
OrgID int64
UserID int64
2022-08-10 17:56:48 +08:00
OrgRole org . RoleType
2022-01-27 06:02:19 +08:00
}
func getRequestTimeout ( opt TimeoutOpts ) time . Duration {
if opt . RequestTimeoutMultiplier == 0 {
return opt . Timeout * 2 // default
}
return opt . Timeout * opt . RequestTimeoutMultiplier
}
2018-05-24 21:26:27 +08:00
type Opts struct {
2022-01-27 06:02:19 +08:00
TimeoutOpts
AuthOpts
2022-05-10 02:11:24 +08:00
ErrorOpts
2020-04-21 22:16:41 +08:00
Width int
Height int
Path string
Encoding string
Timezone string
ConcurrentLimit int
DeviceScaleFactor float64
Headers map [ string ] [ ] string
2022-02-09 17:23:32 +08:00
Theme models . Theme
2018-05-24 21:26:27 +08:00
}
2022-05-10 02:11:24 +08:00
type ErrorOpts struct {
// ErrorConcurrentLimitReached returns an ErrConcurrentLimitReached
// error instead of a rendering limit exceeded image.
ErrorConcurrentLimitReached bool
// ErrorRenderUnavailable returns an ErrRunderUnavailable error
// instead of a rendering unavailable image.
ErrorRenderUnavailable bool
}
2022-07-07 19:32:18 +08:00
type SanitizeSVGRequest struct {
Filename string
Content [ ] byte
}
type SanitizeSVGResponse struct {
Sanitized [ ] byte
}
2021-05-12 23:16:57 +08:00
type CSVOpts struct {
2022-01-27 06:02:19 +08:00
TimeoutOpts
AuthOpts
2021-05-12 23:16:57 +08:00
Path string
Encoding string
Timezone string
ConcurrentLimit int
Headers map [ string ] [ ] string
}
2018-05-24 21:26:27 +08:00
type RenderResult struct {
2019-11-20 16:26:59 +08:00
FilePath string
2018-05-24 21:26:27 +08:00
}
2021-05-12 23:16:57 +08:00
type RenderCSVResult struct {
FilePath string
FileName string
}
2024-02-08 20:09:34 +08:00
type renderFunc func ( ctx context . Context , renderType RenderType , renderKey string , options Opts ) ( * RenderResult , error )
2021-05-12 23:16:57 +08:00
type renderCSVFunc func ( ctx context . Context , renderKey string , options CSVOpts ) ( * RenderCSVResult , error )
2022-07-07 19:32:18 +08:00
type sanitizeFunc func ( ctx context . Context , req * SanitizeSVGRequest ) ( * SanitizeSVGResponse , error )
2018-05-24 21:26:27 +08:00
2022-01-27 06:02:19 +08:00
type renderKeyProvider interface {
get ( ctx context . Context , opts AuthOpts ) ( string , error )
afterRequest ( ctx context . Context , opts AuthOpts , renderKey string )
}
type SessionOpts struct {
Expiry time . Duration
RefreshExpiryOnEachRequest bool
}
type Session interface {
renderKeyProvider
Dispose ( ctx context . Context )
}
2022-01-29 01:24:15 +08:00
type CapabilitySupportRequestResult struct {
IsSupported bool
SemverConstraint string
}
2022-05-22 22:33:49 +08:00
//go:generate mockgen -destination=mock.go -package=rendering github.com/grafana/grafana/pkg/services/rendering Service
2018-05-24 21:26:27 +08:00
type Service interface {
2022-09-02 20:20:10 +08:00
IsAvailable ( ctx context . Context ) bool
2021-06-04 19:33:49 +08:00
Version ( ) string
2024-02-08 20:09:34 +08:00
Render ( ctx context . Context , renderType RenderType , opts Opts , session Session ) ( * RenderResult , error )
2022-01-27 06:02:19 +08:00
RenderCSV ( ctx context . Context , opts CSVOpts , session Session ) ( * RenderCSVResult , error )
2022-02-09 17:23:32 +08:00
RenderErrorImage ( theme models . Theme , error error ) ( * RenderResult , error )
2021-12-22 18:02:42 +08:00
GetRenderUser ( ctx context . Context , key string ) ( * RenderUser , bool )
2022-09-02 20:20:10 +08:00
HasCapability ( ctx context . Context , capability CapabilityName ) ( CapabilitySupportRequestResult , error )
2022-01-27 06:02:19 +08:00
CreateRenderingSession ( ctx context . Context , authOpts AuthOpts , sessionOpts SessionOpts ) ( Session , error )
2022-07-07 19:32:18 +08:00
SanitizeSVG ( ctx context . Context , req * SanitizeSVGRequest ) ( * SanitizeSVGResponse , error )
2018-05-24 21:26:27 +08:00
}