/
stats.go
110 lines (97 loc) · 2.05 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package player
import (
"fmt"
"sync"
"time"
"github.com/wcharczuk/go-chart"
)
type Stats struct {
sync.RWMutex
PlaybackPosition time.Duration
Duration time.Duration
Bitrate float32
Size int
Speed float32
TimeAxis []float64
BiteRateAxis []float64
}
var whiteStyle = chart.Style{
Show: true,
StrokeColor: chart.ColorWhite,
FontColor: chart.ColorWhite,
}
func MinMax(array []float64) (float64, float64) {
var max float64 = array[0]
var min float64 = array[0]
for _, value := range array {
if max < value {
max = value
}
if min > value {
min = value
}
}
return min, max
}
func (s *Stats) GenerateChart() *chart.Chart {
s.RLock()
defer s.RUnlock()
if len(s.BiteRateAxis) < 2 {
return nil
}
min, max := MinMax(s.BiteRateAxis)
if min > 5 {
min -= 5
}
max += 5
cSeries := chart.ContinuousSeries{
Style: chart.Style{
Show: true,
FillColor: chart.GetDefaultColor(0).WithAlpha(50),
},
XValues: s.TimeAxis,
YValues: s.BiteRateAxis,
}
annotation := chart.LastValueAnnotation(cSeries)
annotation.Style = chart.Style{
Show: true,
FontColor: chart.ColorWhite,
FillColor: chart.GetDefaultColor(0).WithAlpha(50),
StrokeColor: chart.GetDefaultColor(0),
}
graph := chart.Chart{
Title: "Encoding Stats",
TitleStyle: whiteStyle,
Background: chart.Style{
Show: true,
FillColor: chart.ColorTransparent,
},
XAxis: chart.XAxis{
TickPosition: chart.TickPositionBetweenTicks,
Style: whiteStyle,
ValueFormatter: func(v interface{}) string {
return fmtDuration(time.Duration(v.(float64)))
},
},
YAxis: chart.YAxis{
AxisType: chart.YAxisSecondary,
Style: whiteStyle,
Range: &chart.ContinuousRange{
Min: min,
Max: max,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%.0f kB/s", v.(float64))
},
},
Canvas: chart.Style{
Show: true,
FillColor: chart.ColorTransparent,
},
Series: []chart.Series{
cSeries,
annotation,
},
}
return &graph
}