From cdce9dba268ae5df6d28b806e1e314f048cc3eee Mon Sep 17 00:00:00 2001 From: Jacob Colvin Date: Sun, 27 Jun 2021 13:48:26 -0400 Subject: [PATCH] Replace --start-frame, --end-frame with --frames --- README.md | 6 +-- cmd/grafana-image-renderer-cli/main.go | 47 +++++++++++++++++---- pkg/sequencer/sequencer.go | 58 +++++++++++++++++--------- 3 files changed, 78 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 60aa0ce..e78dc84 100644 --- a/README.md +++ b/README.md @@ -59,12 +59,12 @@ Usage of sequence: Grafana API URL -dashboard string ID of the dashboard - -end-frame int - The last frame to render (default 2) -end-padding duration Duration to add to the end of the frame -frame-interval duration Time progression between frames, positive = forward, negative = backward (default 5m0s) + -frames string + The frames to render, pass a range and/or a set (e.g. 1-10,12,15) (default "1-2") -height int The height of the image (default 1080) -max-concurrency int @@ -73,8 +73,6 @@ Usage of sequence: Directory to write rendered frames to (default "frames") -panel int ID of the panel, 0 = Entire dashboard - -start-frame int - The first frame to render (default 1) -start-padding duration Duration to add to the start of the frame -start-time int diff --git a/cmd/grafana-image-renderer-cli/main.go b/cmd/grafana-image-renderer-cli/main.go index f3653ce..73d3dab 100644 --- a/cmd/grafana-image-renderer-cli/main.go +++ b/cmd/grafana-image-renderer-cli/main.go @@ -8,6 +8,8 @@ import ( "net/http" "os" "path/filepath" + "strconv" + "strings" "time" "github.com/MacroPower/grafana-image-renderer-sdk-go/pkg/client" @@ -51,8 +53,7 @@ func main() { startTimeMs int64 sequenceCommand = flag.NewFlagSet("sequence", flag.ExitOnError) - startFrame = sequenceCommand.Int("start-frame", 1, "The first frame to render") - endFrame = sequenceCommand.Int("end-frame", 2, "The last frame to render") + frames = sequenceCommand.String("frames", "1-2", "The frames to render, pass a range and/or a set (e.g. 1-10,12,15)") frameInterval = sequenceCommand.Duration("frame-interval", 5*time.Minute, "Time progression between frames, positive = forward, negative = backward") startPadding = sequenceCommand.Duration("start-padding", 0, "Duration to add to the start of the frame") endPadding = sequenceCommand.Duration("end-padding", 0, "Duration to add to the end of the frame") @@ -114,12 +115,8 @@ func main() { fmt.Println("max-concurrency must be at least 1") argErr = true } - if *startFrame < 1 { - fmt.Println("start-frame must be 1 or higher") - argErr = true - } - if *startFrame > *endFrame { - fmt.Println("end-frame must be after start-frame") + if *frames == "" { + fmt.Println("frames is required") argErr = true } } @@ -155,6 +152,38 @@ func main() { } if sequenceCommand.Parsed() { + var framesToRender []int + for _, framesGroup := range strings.Split(*frames, ",") { + if !strings.Contains(framesGroup, "-") { + f, err := strconv.Atoi(framesGroup) + if err != nil { + panic(err) + } + framesToRender = append(framesToRender, f) + + continue + } + + r := strings.Split(framesGroup, "-") + + r1, err := strconv.Atoi(r[0]) + if err != nil { + panic(err) + } + + r2, err := strconv.Atoi(r[1]) + if err != nil { + panic(err) + } + + f, err := sequencer.GetSequence(r1, r2) + if err != nil { + panic(err) + } + + framesToRender = append(framesToRender, f...) + } + seq := sequencer.FrameSequencer{ Renderer: rf, Start: startTime, @@ -168,7 +197,7 @@ func main() { return ioutil.WriteFile(filename, b, 0644) }, } - seq.Sequence(*startFrame, *endFrame) + seq.Sequence(framesToRender...) } fmt.Printf("Completed all work in %f seconds", time.Since(started).Seconds()) diff --git a/pkg/sequencer/sequencer.go b/pkg/sequencer/sequencer.go index 39c8fed..abeffa3 100644 --- a/pkg/sequencer/sequencer.go +++ b/pkg/sequencer/sequencer.go @@ -1,6 +1,7 @@ package sequencer import ( + "errors" "fmt" "time" ) @@ -17,7 +18,7 @@ type SaveFunc func([]byte, int) error // Sequencer defines and manages a render sequence. type Sequencer interface { - Sequence(int, int) + Sequence(...int) } type FrameSequencer struct { @@ -50,12 +51,43 @@ type frame struct { start, end time.Time } -func (s *FrameSequencer) Sequence(start, end int) { +func getFrame(n int, s *FrameSequencer) frame { + frameStart := s.Start.Add(s.Interval * time.Duration(n-1)) + frameEnd := frameStart.Add(s.Interval) + + if frameStart.After(frameEnd) { + // This allows users to inverse the frame order + // by passing a negative interval. + oldFrameEnd := frameEnd + frameEnd = frameStart + frameStart = oldFrameEnd + } + + frameStart = frameStart.Add(s.StartPadding) + frameEnd = frameEnd.Add(s.EndPadding) + + return frame{ + num: n, + start: frameStart, + end: frameEnd, + } +} + +func GetSequence(start, end int) ([]int, error) { if start < 1 || start > end { - panic("malformed sequence") + return nil, errors.New("malformed sequence") + } + + var seq []int + for i := start; i <= end; i++ { + seq = append(seq, i) } - numFrames := 1 + end - start + return seq, nil +} + +func (s *FrameSequencer) Sequence(frames ...int) { + numFrames := len(frames) in := make(chan frame, numFrames) out := make(chan error, numFrames) @@ -67,22 +99,8 @@ func (s *FrameSequencer) Sequence(start, end int) { go s.renderWorker(i, in, out) } - for i := start; i <= end; i++ { - frameStart := s.Start.Add(s.Interval * time.Duration(i-1)) - frameEnd := frameStart.Add(s.Interval) - - if frameStart.After(frameEnd) { - // This allows users to inverse the frame order - // by passing a negative interval. - oldFrameEnd := frameEnd - frameEnd = frameStart - frameStart = oldFrameEnd - } - - frameStart = frameStart.Add(s.StartPadding) - frameEnd = frameEnd.Add(s.EndPadding) - - in <- frame{i, frameStart, frameEnd} + for _, i := range frames { + in <- getFrame(i, s) } close(in)