2022-09-21 04:25:07 -05:00
|
|
|
package screenshot
|
|
|
|
|
|
|
|
import (
|
2022-11-08 16:05:15 -06:00
|
|
|
"hash/fnv"
|
|
|
|
"strconv"
|
2022-09-21 04:25:07 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultHeight = 500
|
|
|
|
DefaultWidth = 1000
|
2022-11-23 03:28:24 -06:00
|
|
|
DefaultTheme = models.ThemeDark
|
|
|
|
DefaultTimeout = 15 * time.Second
|
2022-09-21 04:25:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ScreenshotOptions are the options for taking a screenshot.
|
|
|
|
type ScreenshotOptions struct {
|
2023-08-18 03:26:51 -05:00
|
|
|
// OrgID, DashboardUID and PanelID are required.
|
2023-02-09 14:23:01 -06:00
|
|
|
OrgID int64
|
2022-09-21 04:25:07 -05:00
|
|
|
DashboardUID string
|
|
|
|
PanelID int64
|
2023-08-18 03:26:51 -05:00
|
|
|
|
|
|
|
// These are optional. From and To must both be set to take effect.
|
|
|
|
// Width, Height, Theme and Timeout inherit their defaults from
|
|
|
|
// DefaultWidth, DefaultHeight, DefaultTheme and DefaultTimeout.
|
|
|
|
From string
|
|
|
|
To string
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Theme models.Theme
|
|
|
|
Timeout time.Duration
|
2022-09-21 04:25:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDefaults sets default values for missing or invalid options.
|
|
|
|
func (s ScreenshotOptions) SetDefaults() ScreenshotOptions {
|
|
|
|
if s.Width <= 0 {
|
|
|
|
s.Width = DefaultWidth
|
|
|
|
}
|
|
|
|
if s.Height <= 0 {
|
|
|
|
s.Height = DefaultHeight
|
|
|
|
}
|
|
|
|
switch s.Theme {
|
|
|
|
case models.ThemeDark, models.ThemeLight:
|
|
|
|
default:
|
|
|
|
s.Theme = DefaultTheme
|
|
|
|
}
|
|
|
|
if s.Timeout <= 0 {
|
|
|
|
s.Timeout = DefaultTimeout
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2022-11-08 16:05:15 -06:00
|
|
|
|
|
|
|
func (s ScreenshotOptions) Hash() []byte {
|
|
|
|
h := fnv.New64()
|
2023-02-09 14:23:01 -06:00
|
|
|
_, _ = h.Write([]byte(strconv.FormatInt(s.OrgID, 10)))
|
2022-11-08 16:05:15 -06:00
|
|
|
_, _ = h.Write([]byte(s.DashboardUID))
|
|
|
|
_, _ = h.Write([]byte(strconv.FormatInt(s.PanelID, 10)))
|
2022-11-23 03:28:24 -06:00
|
|
|
_, _ = h.Write([]byte(s.From))
|
|
|
|
_, _ = h.Write([]byte(s.To))
|
2022-11-08 16:05:15 -06:00
|
|
|
_, _ = h.Write([]byte(strconv.FormatInt(int64(s.Width), 10)))
|
|
|
|
_, _ = h.Write([]byte(strconv.FormatInt(int64(s.Height), 10)))
|
|
|
|
_, _ = h.Write([]byte(s.Theme))
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|