From ba62db97e95e87b011d9cc68aade162ef7c8d750 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Wed, 27 Nov 2019 19:33:37 +0100 Subject: [PATCH] result: move code into separate result.go --- submitqueue/result.go | 54 ++++++++++++++++++++++++++++++++++++++ submitqueue/submitqueue.go | 48 --------------------------------- 2 files changed, 54 insertions(+), 48 deletions(-) create mode 100644 submitqueue/result.go diff --git a/submitqueue/result.go b/submitqueue/result.go new file mode 100644 index 000000000..a5ca66201 --- /dev/null +++ b/submitqueue/result.go @@ -0,0 +1,54 @@ +package submitqueue + +import ( + "time" + + "github.com/sirupsen/logrus" +) + +// Problem: no inspection during the run +// Problem: record the state + +// Result contains all data necessary to inspect a previous run +// This includes the Series from that run, and all Log Entries collected. +// It also implements the interface required for logrus.Hook. +type Result struct { + LogEntries []*logrus.Entry + Series []Serie + Error error + startTime time.Time +} + +// MakeResult produces a new Result struct, +// and initializes startTime with the current time. +func MakeResult() *Result { + return &Result{ + startTime: time.Now(), + } +} + +// StartTime returns the startTime +func (r Result) StartTime() time.Time { + return r.startTime +} + +// EndTime returns the time of the latest log entry +func (r Result) EndTime() time.Time { + if len(r.LogEntries) == 0 { + return r.startTime + } + return r.LogEntries[len(r.LogEntries)-1].Time +} + +// Fire is called by logrus on each log event, +// we collect all log entries in the struct variable +func (r *Result) Fire(entry *logrus.Entry) error { + r.LogEntries = append(r.LogEntries, entry) + return nil +} + +// Levels is called by logrus to determine whether to Fire the handler. +// As we want to collect all log entries, we return logrus.AllLevels +func (r *Result) Levels() []logrus.Level { + return logrus.AllLevels +} diff --git a/submitqueue/submitqueue.go b/submitqueue/submitqueue.go index e0dbbd1da..bd0ee230a 100644 --- a/submitqueue/submitqueue.go +++ b/submitqueue/submitqueue.go @@ -2,7 +2,6 @@ package submitqueue import ( "fmt" - "time" "github.com/tweag/gerrit-queue/gerrit" @@ -156,53 +155,6 @@ func (s *SubmitQueue) DoRebase(log *logrus.Logger) error { return nil } -// Problem: no inspection during the run -// Problem: record the state - -// Result all data necessary to inspect a previous run -// This includes the Series from that run, and all Log Entries collected. -// It also implements the interface required for logrus.Hook. -type Result struct { - LogEntries []*logrus.Entry - Series []Serie - Error error - startTime time.Time -} - -// MakeResult produces a new Result struct, -// and initializes startTime with the current time. -func MakeResult() *Result { - return &Result{ - startTime: time.Now(), - } -} - -// StartTime returns the startTime -func (r Result) StartTime() time.Time { - return r.startTime -} - -// EndTime returns the time of the latest log entry -func (r Result) EndTime() time.Time { - if len(r.LogEntries) == 0 { - return r.startTime - } - return r.LogEntries[len(r.LogEntries)-1].Time -} - -// Fire is called by logrus on each log event, -// we collect all log entries in the struct variable -func (r *Result) Fire(entry *logrus.Entry) error { - r.LogEntries = append(r.LogEntries, entry) - return nil -} - -// Levels is called by logrus to determine whether to Fire the handler. -// As we want to collect all log entries, we return logrus.AllLevels -func (r *Result) Levels() []logrus.Level { - return logrus.AllLevels -} - // Run starts the submit and rebase logic. func (s *SubmitQueue) Run(fetchOnly bool) *Result { r := MakeResult()