Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
skip buffer by using io.WriterTo directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jorinvo committed Feb 13, 2017
1 parent 8f03af4 commit 2bcfd08
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"runtime"
"time"
Expand Down Expand Up @@ -104,7 +103,7 @@ func main() {
out, err = os.Create(*file)
fatal(err, "failed to create file")
}
_, err = io.Copy(out, plot)
_, err = plot.WriteTo(out)
fatal(err, "failed to copy to file")

// Upload to Slack
Expand Down
10 changes: 2 additions & 8 deletions promplot/plot.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package promplot

import (
"bytes"
"fmt"
"io"
"regexp"
Expand All @@ -20,7 +19,7 @@ var labelText = regexp.MustCompile("\\{(.*)\\}")

// Plot creates a plot from metric data and saves it to a temporary file.
// It's the callers responsibility to remove the returned file when no longer needed.
func Plot(metrics model.Matrix, title, format string) (io.Reader, error) {
func Plot(metrics model.Matrix, title, format string) (io.WriterTo, error) {
p, err := plot.New()
if err != nil {
return nil, fmt.Errorf("failed to create new plot: %v", err)
Expand Down Expand Up @@ -90,10 +89,5 @@ func Plot(metrics model.Matrix, title, format string) (io.Reader, error) {
}
p.Draw(draw.Crop(draw.New(c), margin, -margin, margin, -margin))

b := new(bytes.Buffer)
if _, err = c.WriteTo(b); err != nil {
return nil, fmt.Errorf("failed to save plot: %v", err)
}

return b, nil
return c, nil
}
6 changes: 3 additions & 3 deletions promplot/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Slack posts a file to a Slack channel.
func Slack(token, channel, title string, plot io.Reader) error {
func Slack(token, channel, title string, plot io.WriterTo) error {
api := slack.New(token)

_, _, err := api.PostMessage(channel, title, slack.PostMessageParameters{
Expand All @@ -35,9 +35,9 @@ func Slack(token, channel, title string, plot io.Reader) error {
panic(fmt.Errorf("failed to delete tmp file: %v", err))
}
}()
_, err = io.Copy(f, plot)
_, err = plot.WriteTo(f)
if err != nil {
return fmt.Errorf("failed to copy plot to file: %v", err)
return fmt.Errorf("failed to write plot to file: %v", err)
}

_, err = api.UploadFile(slack.FileUploadParameters{
Expand Down

0 comments on commit 2bcfd08

Please sign in to comment.