From 1bc55a2d38e24e6318d20c9bfae5e3549a866fad Mon Sep 17 00:00:00 2001 From: Ryuichi Sakai Date: Sun, 15 Jan 2017 18:15:20 +0900 Subject: [PATCH] Enable image rendering to use browser time offset --- pkg/api/render.go | 11 ++--- pkg/components/renderer/renderer.go | 43 ++++++++++++++++--- .../dashboard/partials/shareModal.html | 10 +++++ .../app/features/dashboard/shareModalCtrl.js | 9 +++- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/pkg/api/render.go b/pkg/api/render.go index 6018656badb..480d888d1c3 100644 --- a/pkg/api/render.go +++ b/pkg/api/render.go @@ -14,11 +14,12 @@ func RenderToPng(c *middleware.Context) { queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery) renderOpts := &renderer.RenderOpts{ - Path: c.Params("*") + queryParams, - Width: queryReader.Get("width", "800"), - Height: queryReader.Get("height", "400"), - OrgId: c.OrgId, - Timeout: queryReader.Get("timeout", "30"), + Path: c.Params("*") + queryParams, + Width: queryReader.Get("width", "800"), + Height: queryReader.Get("height", "400"), + OrgId: c.OrgId, + Timeout: queryReader.Get("timeout", "30"), + TimeOffset: queryReader.Get("timeOffset", ""), } pngPath, err := renderer.RenderToPng(renderOpts) diff --git a/pkg/components/renderer/renderer.go b/pkg/components/renderer/renderer.go index 6db6a6c3d35..d98b4bd40c7 100644 --- a/pkg/components/renderer/renderer.go +++ b/pkg/components/renderer/renderer.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "time" @@ -15,18 +16,44 @@ import ( "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" + "strings" ) type RenderOpts struct { - Path string - Width string - Height string - Timeout string - OrgId int64 + Path string + Width string + Height string + Timeout string + OrgId int64 + TimeOffset string } var rendererLog log.Logger = log.New("png-renderer") +func isoTimeOffsetToPosixTz(isoOffset string) string { + re := regexp.MustCompile(`^([+-])([0-1][0-9]|2[0-4])([0-5][0-9])$`) + results := re.FindStringSubmatch(isoOffset) + if results == nil { + return "" + } + sign := "+" + if results[1] == "+" { + sign = "-" // "+" is west and "-" is east in POSIX TZ + } + return fmt.Sprintf("SOMEWHERE%s%s:%s", sign, results[2], results[3]) +} + +func appendEnviron(baseEnviron []string, name string, value string) []string { + results := make([]string, 0) + prefix := fmt.Sprintf("%s=", name) + for _, v := range baseEnviron { + if !strings.HasPrefix(v, prefix) { + results = append(results, v) + } + } + return append(results, fmt.Sprintf("%s=%s", name, value)) +} + func RenderToPng(params *RenderOpts) (string, error) { rendererLog.Info("Rendering", "path", params.Path) @@ -73,6 +100,12 @@ func RenderToPng(params *RenderOpts) (string, error) { return "", err } + tz := isoTimeOffsetToPosixTz(params.TimeOffset) + if tz != "" { + baseEnviron := os.Environ() + cmd.Env = appendEnviron(baseEnviron, "TZ", tz) + } + err = cmd.Start() if err != nil { return "", err diff --git a/public/app/features/dashboard/partials/shareModal.html b/public/app/features/dashboard/partials/shareModal.html index bab75a7f523..3e8ca8acfa6 100644 --- a/public/app/features/dashboard/partials/shareModal.html +++ b/public/app/features/dashboard/partials/shareModal.html @@ -69,6 +69,15 @@ + +