Skip to content

Commit

Permalink
Replace --start-frame, --end-frame with --frames
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroPower committed Jun 27, 2021
1 parent 30b1897 commit cdce9db
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 33 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
47 changes: 38 additions & 9 deletions cmd/grafana-image-renderer-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/MacroPower/grafana-image-renderer-sdk-go/pkg/client"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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())
Expand Down
58 changes: 38 additions & 20 deletions pkg/sequencer/sequencer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sequencer

import (
"errors"
"fmt"
"time"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit cdce9db

Please sign in to comment.