2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-01-29 05:10:34 -06:00
|
|
|
"fmt"
|
2014-12-29 06:36:08 -06:00
|
|
|
"net/http"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/renderer"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func RenderToPng(c *middleware.Context) {
|
2015-01-08 02:00:00 -06:00
|
|
|
queryReader := util.NewUrlQueryReader(c.Req.URL)
|
2015-03-20 18:14:13 -05:00
|
|
|
queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery)
|
2014-12-29 06:36:08 -06:00
|
|
|
|
|
|
|
renderOpts := &renderer.RenderOpts{
|
2017-01-31 00:48:10 -06:00
|
|
|
Path: c.Params("*") + queryParams,
|
|
|
|
Width: queryReader.Get("width", "800"),
|
|
|
|
Height: queryReader.Get("height", "400"),
|
|
|
|
OrgId: c.OrgId,
|
2017-07-31 09:04:04 -05:00
|
|
|
Timeout: queryReader.Get("timeout", "60"),
|
2017-01-31 00:48:10 -06:00
|
|
|
Timezone: queryReader.Get("tz", ""),
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pngPath, err := renderer.RenderToPng(renderOpts)
|
2015-02-05 05:23:24 -06:00
|
|
|
|
2014-12-29 06:36:08 -06:00
|
|
|
if err != nil {
|
2017-07-31 09:04:04 -05:00
|
|
|
if err == renderer.ErrTimeout {
|
|
|
|
c.Handle(500, err.Error(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Handle(500, "Rendering failed.", err)
|
2015-02-05 05:23:24 -06:00
|
|
|
return
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Resp.Header().Set("Content-Type", "image/png")
|
|
|
|
http.ServeFile(c.Resp, c.Req.Request, pngPath)
|
|
|
|
}
|