2019-01-28 23:09:52 +00:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-02-02 14:45:53 +00:00
|
|
|
"github.com/sqshq/sampler/console"
|
|
|
|
"github.com/sqshq/sampler/data"
|
2019-01-28 23:09:52 +00:00
|
|
|
"image"
|
2019-01-31 04:15:15 +00:00
|
|
|
"math"
|
2019-01-31 00:02:38 +00:00
|
|
|
"strconv"
|
2019-01-28 23:09:52 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
ui "github.com/sqshq/termui"
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
// TODO split into runchart, grid, legend files
|
2019-01-28 23:09:52 +00:00
|
|
|
const (
|
2019-02-04 04:03:59 +00:00
|
|
|
xAxisLegendWidth = 20
|
|
|
|
xAxisLabelsHeight = 1
|
|
|
|
xAxisLabelsWidth = 8
|
|
|
|
xAxisLabelsGap = 2
|
|
|
|
xAxisGridWidth = xAxisLabelsGap + xAxisLabelsWidth
|
|
|
|
yAxisLabelsHeight = 1
|
|
|
|
yAxisLabelsGap = 1
|
2019-02-12 03:49:57 +00:00
|
|
|
|
|
|
|
historyReserveHrs = 1
|
2019-02-08 03:47:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ScrollMode int
|
2019-02-04 04:03:59 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
const (
|
|
|
|
Auto ScrollMode = 0
|
|
|
|
Manual ScrollMode = 1
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
|
|
|
|
2019-01-31 01:41:51 +00:00
|
|
|
type RunChart struct {
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.Block
|
2019-02-08 03:47:43 +00:00
|
|
|
lines []TimeLine
|
|
|
|
grid ChartGrid
|
|
|
|
timescale time.Duration
|
|
|
|
mutex *sync.Mutex
|
|
|
|
scrollMode ScrollMode
|
|
|
|
selection time.Time
|
|
|
|
precision int
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
type ChartGrid struct {
|
|
|
|
timeRange TimeRange
|
|
|
|
timePerPoint time.Duration
|
|
|
|
valueExtrema ValueExtrema
|
2019-02-06 03:09:27 +00:00
|
|
|
linesCount int
|
|
|
|
maxTimeWidth int
|
|
|
|
minTimeWidth int
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
type TimePoint struct {
|
|
|
|
value float64
|
|
|
|
time time.Time
|
|
|
|
coordinate int
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
type TimeLine struct {
|
|
|
|
points []TimePoint
|
|
|
|
color ui.Color
|
|
|
|
label string
|
|
|
|
selection int
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
type TimeRange struct {
|
2019-01-31 01:41:51 +00:00
|
|
|
max time.Time
|
|
|
|
min time.Time
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
type ValueExtrema struct {
|
2019-01-31 01:41:51 +00:00
|
|
|
max float64
|
|
|
|
min float64
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
func NewRunChart(title string, precision int, refreshRateMs int) *RunChart {
|
2019-02-06 03:09:27 +00:00
|
|
|
block := *ui.NewBlock()
|
2019-01-31 04:15:15 +00:00
|
|
|
block.Title = title
|
|
|
|
return &RunChart{
|
2019-02-08 03:47:43 +00:00
|
|
|
Block: block,
|
|
|
|
lines: []TimeLine{},
|
|
|
|
timescale: calculateTimescale(refreshRateMs),
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
precision: precision,
|
|
|
|
scrollMode: Auto,
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
func (self *RunChart) newChartGrid() ChartGrid {
|
2019-01-31 01:41:51 +00:00
|
|
|
|
2019-02-04 04:03:59 +00:00
|
|
|
linesCount := (self.Inner.Max.X - self.Inner.Min.X - self.grid.minTimeWidth) / xAxisGridWidth
|
2019-02-08 03:47:43 +00:00
|
|
|
timeRange := self.getTimeRange(linesCount)
|
2019-02-06 03:09:27 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
return ChartGrid{
|
2019-02-06 03:09:27 +00:00
|
|
|
timeRange: timeRange,
|
2019-02-08 03:47:43 +00:00
|
|
|
timePerPoint: self.timescale / time.Duration(xAxisGridWidth),
|
2019-02-06 03:09:27 +00:00
|
|
|
valueExtrema: getValueExtrema(self.lines, timeRange),
|
|
|
|
linesCount: linesCount,
|
|
|
|
maxTimeWidth: self.Inner.Max.X,
|
|
|
|
minTimeWidth: self.getMaxValueLength(),
|
|
|
|
}
|
|
|
|
}
|
2019-01-31 01:41:51 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
func (self *RunChart) newTimePoint(value float64) TimePoint {
|
2019-02-06 03:09:27 +00:00
|
|
|
now := time.Now()
|
2019-02-08 03:47:43 +00:00
|
|
|
return TimePoint{
|
|
|
|
value: value,
|
|
|
|
time: now,
|
|
|
|
coordinate: self.calculateTimeCoordinate(now),
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
func (self *RunChart) Draw(buffer *ui.Buffer) {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Lock()
|
2019-02-03 03:30:45 +00:00
|
|
|
self.Block.Draw(buffer)
|
2019-01-31 01:41:51 +00:00
|
|
|
self.grid = self.newChartGrid()
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-29 14:34:15 +00:00
|
|
|
drawArea := image.Rect(
|
2019-02-02 04:39:34 +00:00
|
|
|
self.Inner.Min.X+self.grid.minTimeWidth+1, self.Inner.Min.Y,
|
2019-01-29 14:34:15 +00:00
|
|
|
self.Inner.Max.X, self.Inner.Max.Y-xAxisLabelsHeight-1,
|
|
|
|
)
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
self.renderAxes(buffer)
|
2019-02-05 03:28:17 +00:00
|
|
|
self.renderLines(buffer, drawArea)
|
|
|
|
self.renderLegend(buffer, drawArea)
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Unlock()
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
func (self *RunChart) ConsumeSample(sample data.Sample) {
|
|
|
|
|
|
|
|
float, err := strconv.ParseFloat(sample.Value, 64)
|
2019-01-31 00:02:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-02-06 03:09:27 +00:00
|
|
|
// TODO visual notification + check sample.Error
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Lock()
|
2019-02-03 03:30:45 +00:00
|
|
|
|
|
|
|
lineIndex := -1
|
2019-01-31 04:15:15 +00:00
|
|
|
|
2019-01-31 23:40:05 +00:00
|
|
|
for i, line := range self.lines {
|
2019-02-03 03:30:45 +00:00
|
|
|
if line.label == sample.Label {
|
|
|
|
lineIndex = i
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
if lineIndex == -1 {
|
2019-02-08 03:47:43 +00:00
|
|
|
line := &TimeLine{
|
|
|
|
points: []TimePoint{},
|
2019-02-03 03:30:45 +00:00
|
|
|
color: sample.Color,
|
|
|
|
label: sample.Label,
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-02-03 03:30:45 +00:00
|
|
|
self.lines = append(self.lines, *line)
|
|
|
|
lineIndex = len(self.lines) - 1
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
line := self.lines[lineIndex]
|
2019-02-06 03:09:27 +00:00
|
|
|
timePoint := self.newTimePoint(float)
|
2019-02-03 03:30:45 +00:00
|
|
|
line.points = append(line.points, timePoint)
|
|
|
|
self.lines[lineIndex] = line
|
|
|
|
|
2019-01-28 23:09:52 +00:00
|
|
|
self.trimOutOfRangeValues()
|
2019-01-31 04:15:15 +00:00
|
|
|
self.mutex.Unlock()
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
func (self *RunChart) renderLines(buffer *ui.Buffer, drawArea image.Rectangle) {
|
2019-02-02 04:39:34 +00:00
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
canvas := ui.NewCanvas()
|
|
|
|
canvas.Rectangle = drawArea
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
if len(self.lines) == 0 || len(self.lines[0].points) == 0 {
|
|
|
|
return
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
selectionCoordinate := self.calculateTimeCoordinate(self.selection)
|
|
|
|
selectionPoints := make(map[int]image.Point)
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
probe := self.lines[0].points[0]
|
2019-02-08 03:47:43 +00:00
|
|
|
delta := ui.AbsInt(self.calculateTimeCoordinate(probe.time) - probe.coordinate)
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-12 03:49:57 +00:00
|
|
|
for i, line := range self.lines {
|
2019-02-08 03:47:43 +00:00
|
|
|
|
|
|
|
xPoint := make(map[int]image.Point)
|
|
|
|
xOrder := make([]int, 0)
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
if line.selection != 0 {
|
|
|
|
line.selection -= delta
|
|
|
|
self.lines[i].selection = line.selection
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
for j, timePoint := range line.points {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
timePoint.coordinate -= delta
|
|
|
|
line.points[j] = timePoint
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
var y int
|
2019-02-06 03:09:27 +00:00
|
|
|
if self.grid.valueExtrema.max == self.grid.valueExtrema.min {
|
2019-02-02 04:39:34 +00:00
|
|
|
y = (drawArea.Dy() - 2) / 2
|
|
|
|
} else {
|
2019-02-06 03:09:27 +00:00
|
|
|
valuePerY := (self.grid.valueExtrema.max - self.grid.valueExtrema.min) / float64(drawArea.Dy()-2)
|
|
|
|
y = int(float64(timePoint.value-self.grid.valueExtrema.min) / valuePerY)
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
point := image.Pt(timePoint.coordinate, drawArea.Max.Y-y-1)
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
if _, exists := xPoint[point.X]; exists {
|
2019-01-31 04:15:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
if !point.In(drawArea) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
if line.selection == 0 {
|
|
|
|
if len(line.points) > j+1 && ui.AbsInt(timePoint.coordinate-selectionCoordinate) > ui.AbsInt(line.points[j+1].coordinate-selectionCoordinate) {
|
|
|
|
selectionPoints[i] = point
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if timePoint.coordinate == line.selection {
|
|
|
|
selectionPoints[i] = point
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xPoint[point.X] = point
|
|
|
|
xOrder = append(xOrder, point.X)
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
for i, x := range xOrder {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
currentPoint := xPoint[x]
|
2019-01-31 04:15:15 +00:00
|
|
|
var previousPoint image.Point
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
if i == 0 {
|
|
|
|
previousPoint = currentPoint
|
|
|
|
} else {
|
2019-02-08 03:47:43 +00:00
|
|
|
previousPoint = xPoint[xOrder[i-1]]
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
canvas.Line(
|
|
|
|
braillePoint(previousPoint),
|
|
|
|
braillePoint(currentPoint),
|
2019-02-03 03:30:45 +00:00
|
|
|
line.color,
|
2019-01-31 04:15:15 +00:00
|
|
|
)
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 05:07:25 +00:00
|
|
|
canvas.Draw(buffer)
|
2019-02-08 03:47:43 +00:00
|
|
|
|
|
|
|
if self.scrollMode == Manual {
|
|
|
|
for lineIndex, point := range selectionPoints {
|
|
|
|
buffer.SetCell(ui.NewCell(console.SymbolSelection, ui.NewStyle(self.lines[lineIndex].color)), point)
|
|
|
|
if self.lines[lineIndex].selection == 0 {
|
|
|
|
self.lines[lineIndex].selection = point.X
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
func (self *RunChart) renderAxes(buffer *ui.Buffer) {
|
2019-02-02 04:39:34 +00:00
|
|
|
// draw origin cell
|
|
|
|
buffer.SetCell(
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(ui.ColorWhite)),
|
2019-02-02 04:39:34 +00:00
|
|
|
image.Pt(self.Inner.Min.X+self.grid.minTimeWidth, self.Inner.Max.Y-xAxisLabelsHeight-1),
|
|
|
|
)
|
|
|
|
|
|
|
|
// draw x axis line
|
|
|
|
for i := self.grid.minTimeWidth + 1; i < self.Inner.Dx(); i++ {
|
|
|
|
buffer.SetCell(
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewCell(ui.HORIZONTAL_DASH, ui.NewStyle(ui.ColorWhite)),
|
2019-02-02 04:39:34 +00:00
|
|
|
image.Pt(i+self.Inner.Min.X, self.Inner.Max.Y-xAxisLabelsHeight-1),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw grid lines
|
|
|
|
for y := 0; y < self.Inner.Dy()-xAxisLabelsHeight-2; y = y + 2 {
|
|
|
|
for x := 1; x <= self.grid.linesCount; x++ {
|
|
|
|
buffer.SetCell(
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(console.ColorDarkGrey)),
|
2019-02-04 04:03:59 +00:00
|
|
|
image.Pt(self.grid.maxTimeWidth-x*xAxisGridWidth, y+self.Inner.Min.Y+1),
|
2019-02-02 04:39:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw y axis line
|
|
|
|
for i := 0; i < self.Inner.Dy()-xAxisLabelsHeight-1; i++ {
|
|
|
|
buffer.SetCell(
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(ui.ColorWhite)),
|
2019-02-02 04:39:34 +00:00
|
|
|
image.Pt(self.Inner.Min.X+self.grid.minTimeWidth, i+self.Inner.Min.Y),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw x axis time labels
|
|
|
|
for i := 1; i <= self.grid.linesCount; i++ {
|
2019-02-06 03:09:27 +00:00
|
|
|
labelTime := self.grid.timeRange.max.Add(time.Duration(-i) * self.timescale)
|
2019-02-02 04:39:34 +00:00
|
|
|
buffer.SetString(
|
|
|
|
labelTime.Format("15:04:05"),
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewStyle(ui.ColorWhite),
|
2019-02-04 04:03:59 +00:00
|
|
|
image.Pt(self.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), self.Inner.Max.Y-1),
|
2019-02-02 04:39:34 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw y axis labels
|
2019-02-06 03:09:27 +00:00
|
|
|
if self.grid.valueExtrema.max != self.grid.valueExtrema.min {
|
2019-02-02 04:39:34 +00:00
|
|
|
labelsCount := (self.Inner.Dy() - xAxisLabelsHeight - 1) / (yAxisLabelsGap + yAxisLabelsHeight)
|
2019-02-06 03:09:27 +00:00
|
|
|
valuePerY := (self.grid.valueExtrema.max - self.grid.valueExtrema.min) / float64(self.Inner.Dy()-xAxisLabelsHeight-3)
|
2019-02-02 04:39:34 +00:00
|
|
|
for i := 0; i < int(labelsCount); i++ {
|
2019-02-06 03:09:27 +00:00
|
|
|
value := self.grid.valueExtrema.max - (valuePerY * float64(i) * (yAxisLabelsGap + yAxisLabelsHeight))
|
2019-02-02 04:39:34 +00:00
|
|
|
buffer.SetString(
|
|
|
|
formatValue(value, self.precision),
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewStyle(ui.ColorWhite),
|
2019-02-02 04:39:34 +00:00
|
|
|
image.Pt(self.Inner.Min.X, 1+self.Inner.Min.Y+i*(yAxisLabelsGap+yAxisLabelsHeight)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer.SetString(
|
2019-02-06 03:09:27 +00:00
|
|
|
formatValue(self.grid.valueExtrema.max, self.precision),
|
|
|
|
ui.NewStyle(ui.ColorWhite),
|
2019-02-08 03:47:43 +00:00
|
|
|
image.Pt(self.Inner.Min.X, self.Inner.Min.Y+self.Inner.Dy()/2))
|
2019-02-02 04:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
func (self *RunChart) renderLegend(buffer *ui.Buffer, rectangle image.Rectangle) {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
for i, line := range self.lines {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-02-05 03:28:17 +00:00
|
|
|
extremum := getLineValueExtremum(line.points)
|
|
|
|
|
2019-02-01 05:07:25 +00:00
|
|
|
buffer.SetString(
|
2019-02-06 03:09:27 +00:00
|
|
|
string(ui.DOT),
|
|
|
|
ui.NewStyle(line.color),
|
2019-02-01 05:07:25 +00:00
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth-2, self.Inner.Min.Y+1+i*5),
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
2019-02-01 05:07:25 +00:00
|
|
|
buffer.SetString(
|
2019-02-03 03:30:45 +00:00
|
|
|
fmt.Sprintf("%s", line.label),
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewStyle(line.color),
|
2019-02-01 05:07:25 +00:00
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+1+i*5),
|
2019-01-28 23:09:52 +00:00
|
|
|
)
|
2019-02-05 03:28:17 +00:00
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("cur %s", formatValue(line.points[len(line.points)-1].value, self.precision)),
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewStyle(ui.ColorWhite),
|
2019-02-05 03:28:17 +00:00
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+2+i*5),
|
|
|
|
)
|
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("max %s", formatValue(extremum.max, self.precision)),
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewStyle(ui.ColorWhite),
|
2019-02-05 03:28:17 +00:00
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+3+i*5),
|
|
|
|
)
|
|
|
|
buffer.SetString(
|
|
|
|
fmt.Sprintf("min %s", formatValue(extremum.min, self.precision)),
|
2019-02-06 03:09:27 +00:00
|
|
|
ui.NewStyle(ui.ColorWhite),
|
2019-02-05 03:28:17 +00:00
|
|
|
image.Pt(self.Inner.Max.X-xAxisLegendWidth, self.Inner.Min.Y+4+i*5),
|
|
|
|
)
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 03:09:27 +00:00
|
|
|
func (self *RunChart) trimOutOfRangeValues() {
|
2019-02-12 03:49:57 +00:00
|
|
|
|
|
|
|
minRangeTime := self.grid.timeRange.min.Add(-time.Hour * time.Duration(historyReserveHrs))
|
|
|
|
|
|
|
|
for i, item := range self.lines {
|
|
|
|
lastOutOfRangeValueIndex := -1
|
|
|
|
|
|
|
|
for j, point := range item.points {
|
|
|
|
if point.time.Before(minRangeTime) {
|
|
|
|
lastOutOfRangeValueIndex = j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastOutOfRangeValueIndex > 0 {
|
|
|
|
item.points = append(item.points[:0], item.points[lastOutOfRangeValueIndex+1:]...)
|
|
|
|
self.lines[i] = item
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 03:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RunChart) calculateTimeCoordinate(t time.Time) int {
|
|
|
|
timeDeltaWithGridMaxTime := self.grid.timeRange.max.Sub(t).Nanoseconds()
|
|
|
|
timeDeltaToPaddingRelation := float64(timeDeltaWithGridMaxTime) / float64(self.timescale.Nanoseconds())
|
2019-02-08 03:47:43 +00:00
|
|
|
return self.grid.maxTimeWidth - int(math.Ceil(float64(xAxisGridWidth)*timeDeltaToPaddingRelation))
|
2019-02-06 03:09:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
func (self *RunChart) getMaxValueLength() int {
|
|
|
|
|
|
|
|
maxValueLength := 0
|
|
|
|
|
|
|
|
for _, line := range self.lines {
|
|
|
|
for _, point := range line.points {
|
2019-02-03 03:30:45 +00:00
|
|
|
l := len(formatValue(point.value, self.precision))
|
2019-02-02 04:39:34 +00:00
|
|
|
if l > maxValueLength {
|
|
|
|
maxValueLength = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxValueLength
|
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
func (self *RunChart) MoveSelection(shift int) {
|
|
|
|
|
|
|
|
if self.scrollMode == Auto {
|
|
|
|
self.scrollMode = Manual
|
|
|
|
self.selection = getMidRangeTime(self.grid.timeRange)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
self.selection = self.selection.Add(self.grid.timePerPoint * time.Duration(shift))
|
|
|
|
if self.selection.After(self.grid.timeRange.max) {
|
|
|
|
self.selection = self.grid.timeRange.max
|
|
|
|
} else if self.selection.Before(self.grid.timeRange.min) {
|
|
|
|
self.selection = self.grid.timeRange.min
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range self.lines {
|
|
|
|
self.lines[i].selection = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *RunChart) DisableSelection() {
|
|
|
|
if self.scrollMode == Manual {
|
|
|
|
self.scrollMode = Auto
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMidRangeTime(r TimeRange) time.Time {
|
|
|
|
delta := r.max.Sub(r.min)
|
|
|
|
return r.max.Add(-delta / 2)
|
|
|
|
}
|
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
func formatValue(value float64, precision int) string {
|
2019-02-03 03:30:45 +00:00
|
|
|
format := "%." + strconv.Itoa(precision) + "f"
|
2019-02-02 04:39:34 +00:00
|
|
|
return fmt.Sprintf(format, value)
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
func getValueExtrema(items []TimeLine, timeRange TimeRange) ValueExtrema {
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
if len(items) == 0 {
|
2019-02-08 03:47:43 +00:00
|
|
|
return ValueExtrema{0, 0}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
var max, min = -math.MaxFloat64, math.MaxFloat64
|
2019-01-28 23:09:52 +00:00
|
|
|
|
2019-01-31 04:15:15 +00:00
|
|
|
for _, item := range items {
|
2019-01-31 23:40:05 +00:00
|
|
|
for _, point := range item.points {
|
2019-02-06 03:09:27 +00:00
|
|
|
if point.value > max && timeRange.isInRange(point.time) {
|
2019-02-03 03:30:45 +00:00
|
|
|
max = point.value
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-02-06 03:09:27 +00:00
|
|
|
if point.value < min && timeRange.isInRange(point.time) {
|
2019-02-03 03:30:45 +00:00
|
|
|
min = point.value
|
2019-01-31 04:15:15 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
return ValueExtrema{max: max, min: min}
|
2019-02-06 03:09:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
func (r *TimeRange) isInRange(time time.Time) bool {
|
2019-02-06 03:09:27 +00:00
|
|
|
return time.After(r.min) && time.Before(r.max)
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
func getLineValueExtremum(points []TimePoint) ValueExtrema {
|
2019-02-01 05:07:25 +00:00
|
|
|
|
|
|
|
if len(points) == 0 {
|
2019-02-08 03:47:43 +00:00
|
|
|
return ValueExtrema{0, 0}
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var max, min = -math.MaxFloat64, math.MaxFloat64
|
|
|
|
|
|
|
|
for _, point := range points {
|
2019-02-03 03:30:45 +00:00
|
|
|
if point.value > max {
|
|
|
|
max = point.value
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
2019-02-03 03:30:45 +00:00
|
|
|
if point.value < min {
|
|
|
|
min = point.value
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
return ValueExtrema{max: max, min: min}
|
2019-02-01 05:07:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
func (self *RunChart) getTimeRange(linesCount int) TimeRange {
|
|
|
|
|
|
|
|
if self.scrollMode == Manual {
|
|
|
|
return self.grid.timeRange
|
|
|
|
}
|
|
|
|
|
|
|
|
width := time.Duration(self.timescale.Nanoseconds() * int64(linesCount))
|
|
|
|
max := time.Now()
|
|
|
|
|
|
|
|
return TimeRange{
|
|
|
|
max: max,
|
|
|
|
min: max.Add(-width),
|
2019-02-04 04:03:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 03:47:43 +00:00
|
|
|
// time duration between grid lines
|
2019-02-04 04:03:59 +00:00
|
|
|
func calculateTimescale(refreshRateMs int) time.Duration {
|
|
|
|
|
|
|
|
multiplier := refreshRateMs * xAxisGridWidth / 2
|
|
|
|
timescale := time.Duration(time.Millisecond * time.Duration(multiplier)).Round(time.Second)
|
|
|
|
|
|
|
|
if timescale.Seconds() == 0 {
|
|
|
|
return time.Second
|
|
|
|
} else {
|
|
|
|
return timescale
|
2019-01-31 01:41:51 +00:00
|
|
|
}
|
2019-01-28 23:09:52 +00:00
|
|
|
}
|