Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
380f7072
Commit
380f7072
authored
Aug 09, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
workin on png rendering
parent
c669f8c6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
96 additions
and
9 deletions
+96
-9
.gitignore
+1
-1
_vendor/phantomjs/render.js
+1
-1
backend/components/phantom_renderer.go
+60
-2
backend/components/phantom_renderer_test.go
+17
-4
backend/httpApi/api.go
+16
-0
grafana
+1
-1
No files found.
.gitignore
View file @
380f7072
...
...
@@ -2,7 +2,7 @@ node_modules
coverage/
.aws-config.json
dist
data/png
public
gin-bin
# locally required config files
...
...
_vendor/phantomjs/render.js
View file @
380f7072
...
...
@@ -20,7 +20,7 @@ page.viewportSize = {
};
page
.
open
(
params
.
url
,
function
(
status
)
{
console
.
log
(
'Loading a web page
'
);
console
.
log
(
'Loading a web page
: '
+
params
.
url
);
setTimeout
(
function
()
{
console
.
log
(
'rendering panel to '
+
params
.
png
);
...
...
backend/components/phantom_renderer.go
View file @
380f7072
package
components
import
(
"crypto/md5"
"encoding/hex"
"io"
"os"
"os/exec"
"path/filepath"
"time"
log
"github.com/alecthomas/log4go"
)
type
PhantomRenderer
struct
{
ImagesDir
string
ImagesDir
string
PhantomDir
string
}
func
(
renderer
*
PhantomRenderer
)
Render
(
url
string
)
{
func
(
self
*
PhantomRenderer
)
RenderToPng
(
url
string
)
(
string
,
error
)
{
log
.
Info
(
"PhantomRenderer::renderToPng url %v"
,
url
)
binPath
,
_
:=
filepath
.
Abs
(
filepath
.
Join
(
self
.
PhantomDir
,
"phantomjs"
))
scriptPath
,
_
:=
filepath
.
Abs
(
filepath
.
Join
(
self
.
PhantomDir
,
"render.js"
))
pngPath
,
_
:=
filepath
.
Abs
(
filepath
.
Join
(
self
.
ImagesDir
,
getHash
(
url
)))
pngPath
=
pngPath
+
".png"
cmd
:=
exec
.
Command
(
binPath
,
scriptPath
,
"url="
+
url
,
"width=100"
,
"height=100"
,
"png="
+
pngPath
)
stdout
,
err
:=
cmd
.
StdoutPipe
()
if
err
!=
nil
{
return
""
,
err
}
stderr
,
err
:=
cmd
.
StderrPipe
()
if
err
!=
nil
{
return
""
,
err
}
err
=
cmd
.
Start
()
if
err
!=
nil
{
return
""
,
err
}
go
io
.
Copy
(
os
.
Stdout
,
stdout
)
go
io
.
Copy
(
os
.
Stdout
,
stderr
)
done
:=
make
(
chan
error
)
go
func
()
{
cmd
.
Wait
()
close
(
done
)
}()
select
{
case
<-
time
.
After
(
10
*
time
.
Second
)
:
if
err
:=
cmd
.
Process
.
Kill
();
err
!=
nil
{
log
.
Error
(
"failed to kill: %v"
,
err
)
}
case
<-
done
:
}
return
pngPath
,
nil
}
func
getHash
(
text
string
)
string
{
hasher
:=
md5
.
New
()
hasher
.
Write
([]
byte
(
text
))
return
hex
.
EncodeToString
(
hasher
.
Sum
(
nil
))
}
backend/components/phantom_renderer_test.go
View file @
380f7072
...
...
@@ -2,6 +2,7 @@ package components
import
(
"io/ioutil"
"os"
"testing"
.
"github.com/smartystreets/goconvey/convey"
...
...
@@ -9,14 +10,26 @@ import (
func
TestPhantomRender
(
t
*
testing
.
T
)
{
Convey
(
"Can render url"
,
func
()
{
Convey
(
"Can render url"
,
t
,
func
()
{
tempDir
,
_
:=
ioutil
.
TempDir
(
""
,
"img"
)
renderer
:=
&
PhantomRenderer
{
ImagesDir
:
tempDir
}
renderer
.
Render
(
"http://www.google.com"
)
//So(err, ShouldBeNil)
renderer
:=
&
PhantomRenderer
{
ImagesDir
:
tempDir
,
PhantomDir
:
"../../_vendor/phantomjs/"
}
png
,
err
:=
renderer
.
RenderToPng
(
"http://www.google.com"
)
So
(
err
,
ShouldBeNil
)
So
(
exists
(
png
),
ShouldEqual
,
true
)
//_, err = os.Stat(store.getFilePathForDashboard("hello"))
//So(err, ShouldBeNil)
})
}
func
exists
(
path
string
)
bool
{
_
,
err
:=
os
.
Stat
(
path
)
if
err
==
nil
{
return
true
}
if
os
.
IsNotExist
(
err
)
{
return
false
}
return
false
}
backend/httpApi/api.go
View file @
380f7072
...
...
@@ -5,6 +5,7 @@ import (
log
"github.com/alecthomas/log4go"
"github.com/gin-gonic/gin"
"github.com/torkelo/grafana-pro/backend/components"
"github.com/torkelo/grafana-pro/backend/models"
"github.com/torkelo/grafana-pro/backend/stores"
)
...
...
@@ -13,12 +14,14 @@ type HttpServer struct {
port
string
shutdown
chan
bool
store
stores
.
Store
renderer
*
components
.
PhantomRenderer
}
func
NewHttpServer
(
port
string
,
store
stores
.
Store
)
*
HttpServer
{
self
:=
&
HttpServer
{}
self
.
port
=
port
self
.
store
=
store
self
.
renderer
=
&
components
.
PhantomRenderer
{
ImagesDir
:
"data/png"
,
PhantomDir
:
"_vendor/phantomjs"
}
return
self
}
...
...
@@ -48,6 +51,8 @@ func (self *HttpServer) ListenAndServe() {
r
.
GET
(
"/api/search/"
,
self
.
search
)
r
.
POST
(
"/api/dashboard"
,
self
.
postDashboard
)
r
.
GET
(
"/api/render"
,
self
.
renderToPng
)
r
.
Static
(
"/public"
,
"./public"
)
r
.
Static
(
"/app"
,
"./public/app"
)
r
.
Static
(
"/img"
,
"./public/img"
)
...
...
@@ -79,6 +84,17 @@ func (self *HttpServer) getDashboard(c *gin.Context) {
c
.
JSON
(
200
,
dash
.
Data
)
}
func
(
self
*
HttpServer
)
renderToPng
(
c
*
gin
.
Context
)
{
qs
:=
c
.
Request
.
URL
.
Query
()
url
:=
qs
[
"url"
][
0
]
pngPath
,
err
:=
self
.
renderer
.
RenderToPng
(
url
)
if
err
!=
nil
{
c
.
HTML
(
500
,
"error.html"
,
nil
)
}
c
.
File
(
pngPath
)
}
func
(
self
*
HttpServer
)
search
(
c
*
gin
.
Context
)
{
query
:=
c
.
Params
.
ByName
(
"q"
)
...
...
grafana
@
1bc277fd
Subproject commit
f068b2c1d32d74af63e0b36aeed4ac0cc2bb8838
Subproject commit
1bc277fd87469b1deb0e1b1571d7010f0f396d5a
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment