Allow to import report history for a process

This commit is contained in:
Ingo Oppermann
2024-07-10 16:46:49 +02:00
parent 480dbb7f53
commit 7e90bb87ce
39 changed files with 2488 additions and 573 deletions

View File

@@ -40,8 +40,8 @@ type Parser interface {
// LastLogline returns the last parsed log line
LastLogline() string
// TransferReportHistory transfers the report history to another parser
TransferReportHistory(Parser) error
// ImportReportHistory imports a report history from another parser
ImportReportHistory([]ReportHistoryEntry)
}
// Config is the config for the Parser implementation
@@ -862,31 +862,6 @@ func (p *parser) ResetLog() {
p.lock.log.Unlock()
}
// Report represents a log report, including the prelude and the last log lines of the process.
type Report struct {
CreatedAt time.Time
Prelude []string
Log []process.Line
Matches []string
}
// ReportHistoryEntry represents an historical log report, including the exit status of the
// process and the last progress data.
type ReportHistoryEntry struct {
Report
ExitedAt time.Time
ExitState string
Progress Progress
Usage Usage
}
type ReportHistorySearchResult struct {
CreatedAt time.Time
ExitedAt time.Time
ExitState string
}
func (p *parser) SearchReportHistory(state string, from, to *time.Time) []ReportHistorySearchResult {
p.lock.logHistory.RLock()
defer p.lock.logHistory.RUnlock()
@@ -1006,26 +981,20 @@ func (p *parser) ReportHistory() []ReportHistoryEntry {
return history
}
func (p *parser) TransferReportHistory(dst Parser) error {
p.lock.logHistory.RLock()
defer p.lock.logHistory.RUnlock()
func (p *parser) ImportReportHistory(history []ReportHistoryEntry) {
p.lock.logHistory.Lock()
defer p.lock.logHistory.Unlock()
pp, ok := dst.(*parser)
if !ok {
return fmt.Errorf("the target parser is not of the required type")
historyLength := p.logHistoryLength + p.logMinimalHistoryLength
if historyLength <= 0 {
return
}
pp.lock.logHistory.Lock()
defer pp.lock.logHistory.Unlock()
p.logHistory = ring.New(historyLength)
p.logHistory.Do(func(l interface{}) {
if l == nil {
return
}
pp.logHistory.Value = l
pp.logHistory = pp.logHistory.Next()
})
return nil
for _, r := range history {
p.logHistory.Value = r
p.logHistory = p.logHistory.Next()
}
}

View File

@@ -22,11 +22,9 @@ func TestParserProgress(t *testing.T) {
d, _ := time.ParseDuration("3m58s440ms")
wantP := Progress{
Frame: 5968,
FPS: 25,
Quantizer: 19.4,
Size: 453632,
Time: d.Seconds(),
Bitrate: 5632,
Speed: 0.999,
Drop: 3522,
Dup: 87463,
@@ -202,6 +200,67 @@ func TestParserLogHistory(t *testing.T) {
}
}
func TestParserImportLogHistory(t *testing.T) {
parser := New(Config{
LogLines: 20,
LogHistory: 5,
}).(*parser)
for i := 0; i < 7; i++ {
parser.Parse("bla")
parser.prelude.done = true
parser.Parse("frame= 5968 fps= 25 q=19.4 size=443kB time=00:03:58.44 bitrate=5632kbits/s speed=0.999x skip=9733 drop=3522 dup=87463")
history := parser.ReportHistory()
require.Equal(t, int(math.Min(float64(i), 5)), len(history))
parser.Stop("finished", process.Usage{})
parser.ResetStats()
time.Sleep(time.Second)
}
history := parser.ReportHistory()
for i, h := range history {
h.Prelude[0] = "blubb"
h.ExitState = "nothing"
h.Progress.Frame = 42
history[i] = h
}
parser.ImportReportHistory(history[:3])
history = parser.ReportHistory()
require.Equal(t, 3, len(history))
for i := 0; i < 3; i++ {
require.Equal(t, "nothing", history[i].ExitState)
require.Equal(t, "bla", history[i].Log[0].Data)
require.Equal(t, "blubb", history[i].Prelude[0])
d, _ := time.ParseDuration("3m58s440ms")
require.Equal(t, Progress{
Started: true,
Frame: 42,
FPS: 0, // is calculated with averager
Quantizer: 19.4,
Size: 453632,
Time: d.Seconds(),
Bitrate: 0, // is calculated with averager
Speed: 0.999,
Drop: 3522,
Dup: 87463,
}, history[i].Progress)
if i != 0 {
require.Greater(t, history[i].CreatedAt, history[i-1].ExitedAt)
}
}
}
func TestParserLogHistoryLength(t *testing.T) {
parser := New(Config{
LogLines: 20,

View File

@@ -5,6 +5,7 @@ import (
"time"
"github.com/datarhei/core/v16/encoding/json"
"github.com/datarhei/core/v16/process"
)
// Duration represents a time.Duration
@@ -500,17 +501,21 @@ type AVstream struct {
}
type Usage struct {
CPU struct {
NCPU float64
Average float64
Max float64
Limit float64
}
Memory struct {
Average float64
Max uint64
Limit uint64
}
CPU UsageCPU
Memory UsageMemory
}
type UsageCPU struct {
NCPU float64
Average float64
Max float64
Limit float64
}
type UsageMemory struct {
Average float64
Max uint64
Limit uint64
}
type GraphElement struct {
@@ -542,3 +547,28 @@ type StreamMapping struct {
Graphs []GraphElement
Mapping []GraphMapping
}
// Report represents a log report, including the prelude and the last log lines of the process.
type Report struct {
CreatedAt time.Time
Prelude []string
Log []process.Line
Matches []string
}
// ReportHistoryEntry represents an historical log report, including the exit status of the
// process and the last progress data.
type ReportHistoryEntry struct {
Report
ExitedAt time.Time
ExitState string
Progress Progress
Usage Usage
}
type ReportHistorySearchResult struct {
CreatedAt time.Time
ExitedAt time.Time
ExitState string
}