Commit 830491fa by Torkel Ödegaard

Merge branch 'time-offset-to-image' of https://github.com/ryu1-sakai/grafana…

Merge branch 'time-offset-to-image' of https://github.com/ryu1-sakai/grafana into ryu1-sakai-time-offset-to-image
parents 3c304b9b 1bc55a2d
...@@ -14,11 +14,12 @@ func RenderToPng(c *middleware.Context) { ...@@ -14,11 +14,12 @@ func RenderToPng(c *middleware.Context) {
queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery) queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery)
renderOpts := &renderer.RenderOpts{ renderOpts := &renderer.RenderOpts{
Path: c.Params("*") + queryParams, Path: c.Params("*") + queryParams,
Width: queryReader.Get("width", "800"), Width: queryReader.Get("width", "800"),
Height: queryReader.Get("height", "400"), Height: queryReader.Get("height", "400"),
OrgId: c.OrgId, OrgId: c.OrgId,
Timeout: queryReader.Get("timeout", "30"), Timeout: queryReader.Get("timeout", "30"),
TimeOffset: queryReader.Get("timeOffset", ""),
} }
pngPath, err := renderer.RenderToPng(renderOpts) pngPath, err := renderer.RenderToPng(renderOpts)
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"time" "time"
...@@ -15,18 +16,44 @@ import ( ...@@ -15,18 +16,44 @@ import (
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
"strings"
) )
type RenderOpts struct { type RenderOpts struct {
Path string Path string
Width string Width string
Height string Height string
Timeout string Timeout string
OrgId int64 OrgId int64
TimeOffset string
} }
var rendererLog log.Logger = log.New("png-renderer") 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) { func RenderToPng(params *RenderOpts) (string, error) {
rendererLog.Info("Rendering", "path", params.Path) rendererLog.Info("Rendering", "path", params.Path)
...@@ -73,6 +100,12 @@ func RenderToPng(params *RenderOpts) (string, error) { ...@@ -73,6 +100,12 @@ func RenderToPng(params *RenderOpts) (string, error) {
return "", err return "", err
} }
tz := isoTimeOffsetToPosixTz(params.TimeOffset)
if tz != "" {
baseEnviron := os.Environ()
cmd.Env = appendEnviron(baseEnviron, "TZ", tz)
}
err = cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
return "", err return "", err
......
...@@ -69,6 +69,15 @@ ...@@ -69,6 +69,15 @@
</div> </div>
</script> </script>
<script type="text/ng-template" id="renderImageOptions.html">
<div class="gf-form-group">
<gf-form-switch class="gf-form"
label="Use browser time offset in image" label-class="width-18" switch-class="max-width-6"
checked="options.browserTimeOffset" on-change="buildUrl()">
</gf-form-switch>
</div>
</script>
<script type="text/ng-template" id="shareLink.html"> <script type="text/ng-template" id="shareLink.html">
<div class="share-modal-header"> <div class="share-modal-header">
<div class="share-modal-big-icon"> <div class="share-modal-big-icon">
...@@ -91,6 +100,7 @@ ...@@ -91,6 +100,7 @@
</div> </div>
</div> </div>
</div> </div>
<div ng-include src="'renderImageOptions.html'"></div>
<div class="gf-form" ng-show="modeSharePanel"> <div class="gf-form" ng-show="modeSharePanel">
<a href="{{imageUrl}}" target="_blank"><i class="fa fa-camera"></i> Direct link rendered image</a> <a href="{{imageUrl}}" target="_blank"><i class="fa fa-camera"></i> Direct link rendered image</a>
</div> </div>
......
...@@ -11,7 +11,7 @@ function (angular, _, $, require, config) { ...@@ -11,7 +11,7 @@ function (angular, _, $, require, config) {
module.controller('ShareModalCtrl', function($scope, $rootScope, $location, $timeout, timeSrv, templateSrv, linkSrv) { module.controller('ShareModalCtrl', function($scope, $rootScope, $location, $timeout, timeSrv, templateSrv, linkSrv) {
$scope.options = { forCurrent: true, includeTemplateVars: true, theme: 'current' }; $scope.options = { forCurrent: true, includeTemplateVars: true, browserTimeOffset: false, theme: 'current' };
$scope.editor = { index: $scope.tabIndex || 0}; $scope.editor = { index: $scope.tabIndex || 0};
$scope.init = function() { $scope.init = function() {
...@@ -83,6 +83,13 @@ function (angular, _, $, require, config) { ...@@ -83,6 +83,13 @@ function (angular, _, $, require, config) {
$scope.imageUrl = soloUrl.replace(config.appSubUrl + '/dashboard-solo/', config.appSubUrl + '/render/dashboard-solo/'); $scope.imageUrl = soloUrl.replace(config.appSubUrl + '/dashboard-solo/', config.appSubUrl + '/render/dashboard-solo/');
$scope.imageUrl += '&width=1000'; $scope.imageUrl += '&width=1000';
$scope.imageUrl += '&height=500'; $scope.imageUrl += '&height=500';
if ($scope.options.browserTimeOffset) {
var offsetMinutes = new Date().getTimezoneOffset(); // Negative if ahead of UTC
var sign = offsetMinutes < 0 ? '+' : '-';
var hours = ('0' + Math.abs(offsetMinutes) / 60).slice(-2);
var minutes = ('0' + Math.abs(offsetMinutes) % 60).slice(-2);
$scope.imageUrl += '&timeOffset=' + encodeURIComponent(sign + hours + minutes);
}
}; };
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment