diff --git a/modules/api_rest/api_rest.go b/modules/api_rest/api_rest.go index 28d66565..c3b6f082 100644 --- a/modules/api_rest/api_rest.go +++ b/modules/api_rest/api_rest.go @@ -28,6 +28,7 @@ type RestAPI struct { upgrader websocket.Upgrader quit chan bool + recClock int recording bool recTime int loading bool @@ -50,6 +51,7 @@ func NewRestAPI(s *session.Session) *RestAPI { ReadBufferSize: 1024, WriteBufferSize: 1024, }, + recClock: 1, recording: false, recTime: 0, loading: false, @@ -60,6 +62,7 @@ func NewRestAPI(s *session.Session) *RestAPI { } mod.State.Store("recording", &mod.recording) + mod.State.Store("rec_clock", &mod.recClock) mod.State.Store("replaying", &mod.replaying) mod.State.Store("loading", &mod.loading) mod.State.Store("load_progress", 0) @@ -122,6 +125,10 @@ func NewRestAPI(s *session.Session) *RestAPI { return mod.Stop() })) + mod.AddParam(session.NewIntParameter("api.rest.record.clock", + "1", + "Number of seconds to wait while recording with api.rest.record between one sample and the next one.")) + mod.AddHandler(session.NewModuleHandler("api.rest.record off", "", "Stop recording the session.", func(args []string) error { @@ -129,7 +136,7 @@ func NewRestAPI(s *session.Session) *RestAPI { })) mod.AddHandler(session.NewModuleHandler("api.rest.record FILENAME", `api\.rest\.record (.+)`, - "Start polling the rest API every second recording each sample as a session file that can be replayed.", + "Start polling the rest API periodically recording each sample in a compressed file that can be later replayed.", func(args []string) error { return mod.startRecording(args[0]) })) @@ -141,7 +148,7 @@ func NewRestAPI(s *session.Session) *RestAPI { })) mod.AddHandler(session.NewModuleHandler("api.rest.replay FILENAME", `api\.rest\.replay (.+)`, - "Start the rest API module in replay mode using FILENAME as the recorded session file.", + "Start the rest API module in replay mode using FILENAME as the recorded session file, will revert to normal mode once the replay is over.", func(args []string) error { return mod.startReplay(args[0]) })) diff --git a/modules/api_rest/api_rest_record.go b/modules/api_rest/api_rest_record.go index 22f03cd6..fd5d7c09 100644 --- a/modules/api_rest/api_rest_record.go +++ b/modules/api_rest/api_rest_record.go @@ -40,17 +40,21 @@ func (mod *RestAPI) recordState() error { } func (mod *RestAPI) recorder() { + clock := time.Duration(mod.recClock) * time.Second + mod.recTime = 0 mod.recording = true mod.replaying = false mod.record = NewRecord(mod.recordFileName, &mod.SessionModule) - mod.Info("started recording to %s ...", mod.recordFileName) + mod.Info("started recording to %s (clock %s) ...", mod.recordFileName, clock) mod.recordWait.Add(1) defer mod.recordWait.Done() tick := time.NewTicker(1 * time.Second) + lastSampled := time.Time{} + for range tick.C { if !mod.recording { break @@ -58,10 +62,13 @@ func (mod *RestAPI) recorder() { mod.recTime++ - if err := mod.recordState(); err != nil { - mod.Error("error while recording: %s", err) - mod.recording = false - break + if time.Since(lastSampled) >= clock { + lastSampled = time.Now() + if err := mod.recordState(); err != nil { + mod.Error("error while recording: %s", err) + mod.recording = false + break + } } } @@ -73,6 +80,8 @@ func (mod *RestAPI) startRecording(filename string) (err error) { return mod.errAlreadyRecording() } else if mod.replaying { return mod.errAlreadyReplaying() + } else if err, mod.recClock = mod.IntParam("api.rest.record.clock"); err != nil { + return err } else if mod.recordFileName, err = fs.Expand(filename); err != nil { return err }