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