Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
21 changes: 16 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
module github.com/rs/jplot

go 1.14
go 1.17

require (
github.com/dustin/go-humanize v1.0.0
github.com/elgs/gojq v0.0.0-20160421194050-81fa9a608a13
github.com/mattn/go-isatty v0.0.14
github.com/mattn/go-sixel v0.0.1
github.com/monochromegane/terminal v0.0.0-20161222050454-9bc47e2707d9
github.com/wcharczuk/go-chart/v2 v2.1.0
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c
)

require (
github.com/blend/go-sdk v1.20211204.3 // indirect
github.com/elgs/gosplitargs v0.0.0-20161028071935-a491c5eeb3c8 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/monochromegane/terminal v0.0.0-20161222050454-9bc47e2707d9
github.com/wcharczuk/go-chart v2.0.1+incompatible
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
github.com/soniakeys/quant v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/wcharczuk/go-chart v2.0.1+incompatible // indirect
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 // indirect
)
643 changes: 638 additions & 5 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion graph/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"

"github.com/rs/jplot/data"
chart "github.com/wcharczuk/go-chart"
chart "github.com/wcharczuk/go-chart/v2"
)

type Dash struct {
Expand Down
4 changes: 2 additions & 2 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

humanize "github.com/dustin/go-humanize"
"github.com/rs/jplot/data"
chart "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
chart "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)

func init() {
Expand Down
4 changes: 2 additions & 2 deletions graph/legend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package graph
import (
"math"

chart "github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
chart "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)

// custom version of chart.Legend
Expand Down
44 changes: 31 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main // import "github.com/rs/jplot"
import (
"flag"
"fmt"
"github.com/rs/jplot/streamer"
"os"
"os/signal"
"strings"
Expand All @@ -28,6 +29,10 @@ func main() {
fmt.Fprintln(out, " option:")
fmt.Fprintln(out, " - counter: Computes the difference with the last value. The value must increase monotonically.")
fmt.Fprintln(out, " - marker: When the value is none-zero, a vertical line is drawn.")
fmt.Fprintln(out, " - port: launches a web-server that will be drawing images. This disables drawing in terminal")
fmt.Fprintln(out, " - hiw (http image width): defines web server's image width")
fmt.Fprintln(out, " - hih (http image height): defines web server's image width")
fmt.Fprintln(out, " - hirr (http image refresh rate): how often image will be refreshed (milliseconds)")
fmt.Fprintln(out, " path:")
fmt.Fprintln(out, " JSON field path (eg: field.sub-field).")
}
Expand All @@ -36,15 +41,12 @@ func main() {
" Note that counter fields are computed based on this interval.")
steps := flag.Int("steps", 100, "Number of values to plot.")
rows := flag.Int("rows", 0, "Limits the height of the graph output.")
port := flag.Int("port", 0, "web server port")
hiw := flag.Int("hiw", 500, "web server image height")
hih := flag.Int("hih", 500, "web server image width")
hirr := flag.Int("hirr", 1000, "web server refresh rate (milliseconds)")
flag.Parse()

if !term.HasGraphicsSupport() {
fatal("iTerm2 or DRCS Sixel graphics required")
}
if os.Getenv("TERM") == "screen" {
fatal("screen and tmux not supported")
}

if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
Expand All @@ -66,6 +68,25 @@ func main() {
Specs: specs,
Data: dp,
}
go func() {
if er := dp.Run(specs); er != nil {
fatal("Data source error: ", er)
}
}()

if *port != 0 {
err := streamer.Start(*port, &dash, *hiw, *hih, *hirr)
if err != nil {
fatal("cannot start server: %s", err.Error())
}
}

if !term.HasGraphicsSupport() {
fatal("iTerm2 or DRCS Sixel graphics required")
}
if os.Getenv("TERM") == "screen" {
fatal("screen and tmux not supported")
}

wg := &sync.WaitGroup{}
wg.Add(1)
Expand Down Expand Up @@ -106,9 +127,6 @@ func main() {
}
}()

if err := dp.Run(specs); err != nil {
fatal("Data source error: ", err)
}
}

func fatal(a ...interface{}) {
Expand Down Expand Up @@ -149,9 +167,9 @@ func render(dash graph.Dash, rows int) {
rows = size.Row
}
// Use iTerm2 image display feature.
term := term.NewImageWriter(width, height)
defer term.Close()
if err := dash.Render(term, width, height); err != nil {
tm := term.NewImageWriter(width, height)
defer tm.Close()
if err := dash.Render(tm, width, height); err != nil {
fatal(fmt.Sprintf("cannot render graph: %v", err.Error()))
}
}
31 changes: 31 additions & 0 deletions streamer/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package streamer

import (
"fmt"
"github.com/rs/jplot/graph"
"log"
"net"
"net/http"
)

func Start(port int, dash *graph.Dash, width, height, refreshRate int) error {
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
if err != nil {
return err
}

stream := &Streamer{
dash: dash,
width: width,
height: height,
refreshRate: refreshRate,
}

log.Println("starting server on ", port)
er := http.Serve(listener, stream)
if er != nil {
return er
}

return nil
}
62 changes: 62 additions & 0 deletions streamer/streamer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package streamer

import (
"errors"
"fmt"
"github.com/rs/jplot/graph"
"net/http"
"syscall"
)

type Streamer struct {
dash *graph.Dash

width, height, refreshRate int
}

func (s *Streamer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.URL.String() == "/" {
err := s.serveStatic(resp)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
fmt.Println("error on serving static: ", err)
}
return
}

resp.Header().Add("Content-Type", "image/png")
err := s.dash.Render(resp, s.width, s.height)
if err == nil {
return
}

if errors.Is(err, syscall.EPIPE) {
return
}

fmt.Println("error on render: ", err)
}

func (s *Streamer) serveStatic(resp http.ResponseWriter) error {
resp.Header().Add("Content-Type", "text/html")
_, err := resp.Write([]byte(fmt.Sprintf(`
<html>
<head>
</head>
<body>
<img id="data" src="data.png">
</body>

<script>

var i = 0
setInterval( () => {
i++
document.getElementById("data").src = "data.png" + "?q=" + i
}, %d)
</script>
</html>
`, s.refreshRate)))

return err
}
9 changes: 9 additions & 0 deletions vendor/github.com/mattn/go-isatty/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions vendor/github.com/mattn/go-isatty/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/mattn/go-isatty/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/mattn/go-isatty/go.test.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/mattn/go-isatty/isatty_bsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/mattn/go-isatty/isatty_others.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/mattn/go-isatty/isatty_plan9.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading