address linter warnings

This commit is contained in:
sqshq 2019-07-27 00:15:35 -04:00
parent 34cfaae794
commit 5d730b5fec
19 changed files with 84 additions and 70 deletions

View File

@ -19,7 +19,7 @@ const (
jsonContentType = "application/json"
)
// Backend client is used to verify license and to send telemetry reports
// BackendClient is used to verify license and to send telemetry reports
// for analyses (anonymous usage data statistics and crash reports)
type BackendClient struct {
client http.Client

View File

@ -12,6 +12,7 @@ import (
"strings"
)
// AsciiBox represents a component with ascii-style text
type AsciiBox struct {
*ui.Block
*data.Consumer

View File

@ -17,17 +17,18 @@ const (
barIndent int = 1
)
// BarChart presents categorical data with rectangular bars
type BarChart struct {
*ui.Block
*data.Consumer
bars []Bar
bars []bar
scale int
maxValue float64
count int64
palette console.Palette
}
type Bar struct {
type bar struct {
label string
color ui.Color
value float64
@ -39,14 +40,14 @@ func NewBarChart(c config.BarChartConfig, palette console.Palette) *BarChart {
chart := BarChart{
Block: component.NewBlock(c.Title, true, palette),
Consumer: data.NewConsumer(),
bars: []Bar{},
bars: []bar{},
scale: *c.Scale,
maxValue: -math.MaxFloat64,
palette: palette,
}
for _, i := range c.Items {
chart.AddBar(*i.Label, *i.Color)
chart.addBar(*i.Label, *i.Color)
}
go func() {
@ -68,13 +69,14 @@ func (b *BarChart) consumeSample(sample *data.Sample) {
b.count++
float, err := util.ParseFloat(sample.Value)
if err != nil {
b.HandleConsumeFailure("Failed to parse a number", err, sample)
return
} else {
b.HandleConsumeSuccess()
}
b.HandleConsumeSuccess()
index := -1
for i, bar := range b.bars {
if bar.label == sample.Label {
@ -97,8 +99,8 @@ func (b *BarChart) consumeSample(sample *data.Sample) {
}
}
func (b *BarChart) AddBar(label string, color ui.Color) {
b.bars = append(b.bars, Bar{label: label, color: color, value: 0})
func (b *BarChart) addBar(label string, color ui.Color) {
b.bars = append(b.bars, bar{label: label, color: color, value: 0})
}
func (b *BarChart) reselectMaxValue() {
@ -111,6 +113,7 @@ func (b *BarChart) reselectMaxValue() {
b.maxValue = maxValue
}
// Draw renders the barchart
func (b *BarChart) Draw(buffer *ui.Buffer) {
b.Block.Draw(buffer)

View File

@ -17,6 +17,7 @@ const (
CurValueLabel = "cur"
)
// Gauge displays cur value between specified min and max values
type Gauge struct {
*ui.Block
*data.Consumer

View File

@ -13,6 +13,7 @@ import (
"time"
)
// Layout represents component arrangement on the screen
type Layout struct {
ui.Block
Components []*component.Component

View File

@ -10,7 +10,7 @@ import (
const defaultValueLength = 4
type ChartGrid struct {
type chartGrid struct {
timeRange TimeRange
timePerPoint time.Duration
valueExtrema ValueExtrema
@ -19,12 +19,12 @@ type ChartGrid struct {
minTimeWidth int
}
func (c *RunChart) newChartGrid() ChartGrid {
func (c *RunChart) newChartGrid() chartGrid {
linesCount := (c.Inner.Max.X - c.Inner.Min.X - c.grid.minTimeWidth) / xAxisGridWidth
timeRange := c.getTimeRange(linesCount)
return ChartGrid{
return chartGrid{
timeRange: timeRange,
timePerPoint: c.timescale / time.Duration(xAxisGridWidth),
valueExtrema: getLocalExtrema(c.lines, timeRange),

View File

@ -18,7 +18,7 @@ const (
timeFormat = "15:04:05.000"
)
type Legend struct {
type legend struct {
Enabled bool
Details bool
}
@ -102,15 +102,13 @@ func getColumnWidth(mode Mode, lines []TimeLine, scale int) int {
func getDiffWithPreviousValue(line TimeLine) float64 {
if len(line.points) < 2 {
return 0
} else {
return line.points[len(line.points)-1].value - line.points[len(line.points)-2].value
}
return line.points[len(line.points)-1].value - line.points[len(line.points)-2].value
}
func getCurrentValue(line TimeLine) float64 {
if len(line.points) == 0 {
return 0
} else {
return line.points[len(line.points)-1].value
}
return line.points[len(line.points)-1].value
}

View File

@ -38,17 +38,18 @@ const (
CommandMoveSelection = "MOVE_SELECTION"
)
// RunChart displays observed data in a time sequence
type RunChart struct {
*ui.Block
*data.Consumer
lines []TimeLine
grid ChartGrid
grid chartGrid
timescale time.Duration
mutex *sync.Mutex
mode Mode
selection time.Time
scale int
legend Legend
legend legend
palette console.Palette
}
@ -87,7 +88,7 @@ func NewRunChart(c config.RunChartConfig, palette console.Palette) *RunChart {
mutex: &sync.Mutex{},
scale: *c.Scale,
mode: ModeDefault,
legend: Legend{Enabled: c.Legend.Enabled, Details: c.Legend.Details},
legend: legend{Enabled: c.Legend.Enabled, Details: c.Legend.Details},
palette: palette,
}
@ -159,10 +160,10 @@ func (c *RunChart) consumeSample(sample *data.Sample) {
if err != nil {
c.HandleConsumeFailure("Failed to parse a number", err, sample)
return
} else {
c.HandleConsumeSuccess()
}
c.HandleConsumeSuccess()
c.mutex.Lock()
index := -1
@ -318,13 +319,13 @@ func (c *RunChart) moveSelection(shift int) {
c.mode = ModePinpoint
c.selection = getMidRangeTime(c.grid.timeRange)
return
} else {
c.selection = c.selection.Add(c.grid.timePerPoint * time.Duration(shift))
if c.selection.After(c.grid.timeRange.max) {
c.selection = c.grid.timeRange.max
} else if c.selection.Before(c.grid.timeRange.min) {
c.selection = c.grid.timeRange.min
}
}
c.selection = c.selection.Add(c.grid.timePerPoint * time.Duration(shift))
if c.selection.After(c.grid.timeRange.max) {
c.selection = c.grid.timeRange.max
} else if c.selection.Before(c.grid.timeRange.min) {
c.selection = c.grid.timeRange.min
}
for i := range c.lines {
@ -352,9 +353,9 @@ func calculateTimescale(rateMs int) time.Duration {
if timescale.Seconds() == 0 {
return time.Second
} else {
return timescale
}
return timescale
}
func braillePoint(point image.Point) image.Point {

View File

@ -11,6 +11,7 @@ import (
"sync"
)
// SparkLine displays general shape of a measurement variation over time
type SparkLine struct {
*ui.Block
*data.Consumer

View File

@ -9,6 +9,7 @@ import (
"image"
)
// TextBox represents a component with regular text
type TextBox struct {
*ui.Block
*data.Consumer

View File

@ -2,7 +2,6 @@ package util
import (
"image"
"math"
)
func GetRectLeftSideCenter(rect image.Rectangle) image.Point {
@ -33,12 +32,6 @@ func GetRectBottomSideCenter(rect image.Rectangle) image.Point {
}
}
func GetDistance(p1 image.Point, p2 image.Point) float64 {
x := math.Abs(float64(p1.X - p2.X))
y := math.Abs(float64(p1.Y - p2.Y))
return math.Sqrt(x*x + y*y)
}
func GetRectCoordinates(area image.Rectangle, width int, height int) (int, int, int, int) {
x1 := area.Min.X + area.Dx()/2 - width/2
y1 := area.Min.Y + area.Dy()/2 - height

View File

@ -189,9 +189,8 @@ func countEmptyCellsBelow(grid [console.RowsCount][console.ColumnsCount]int, row
for r := row; r < console.RowsCount; r++ {
if grid[r][column] == 1 {
return count
} else {
count++
}
count++
}
return count
}

View File

@ -36,9 +36,12 @@ func (c *ComponentConfig) GetSize() Size {
func (c *ComponentConfig) GetRectangle() image.Rectangle {
if c.Position == nil || len(c.Position) == 0 {
return image.ZR
} else {
return image.Rect(c.Position[0][0], c.Position[0][1], c.Position[0][0]+c.Position[1][0], c.Position[0][1]+c.Position[1][1])
}
return image.Rect(
c.Position[0][0],
c.Position[0][1],
c.Position[0][0]+c.Position[1][0],
c.Position[0][1]+c.Position[1][1])
}
type TriggerConfig struct {

View File

@ -1,5 +1,6 @@
package config
// Options with cli flags
type Options struct {
ConfigFile *string `short:"c" long:"config" description:"Path to YAML config file"`
LicenseKey *string `short:"l" long:"license" description:"License key. Visit www.sampler.dev for details"`

View File

@ -10,6 +10,7 @@ import (
"time"
)
// BasicInteractiveShell represents non-PTY interactive shell sampling metadata
type BasicInteractiveShell struct {
item *Item
variables []string
@ -93,7 +94,7 @@ func (s *BasicInteractiveShell) execute() (string, error) {
_ = s.cmd.Wait()
s.item.basicShell = nil // restart session
}
return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err))
return "", fmt.Errorf("failed to execute command: %s", err)
}
timeout := make(chan bool, 1)
@ -121,9 +122,8 @@ func (s *BasicInteractiveShell) execute() (string, error) {
case <-timeout:
if errorText.Len() > 0 {
return "", errors.New(errorText.String())
} else {
return s.item.transform(resultText.String())
}
return s.item.transform(resultText.String())
}
}
}

View File

@ -1,8 +1,9 @@
//+build !windows
package data
import (
"bufio"
"errors"
"fmt"
"github.com/kr/pty"
"github.com/lunixbochs/vtclean"
@ -12,6 +13,7 @@ import (
"time"
)
// PtyInteractiveShell represents PTY interactive shell sampling metadata
type PtyInteractiveShell struct {
item *Item
variables []string
@ -73,7 +75,7 @@ func (s *PtyInteractiveShell) execute() (string, error) {
_ = s.file.Close()
s.item.ptyShell = nil // restart session
}
return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err))
return "", fmt.Errorf("failed to execute command: %s", err)
}
softTimeout := make(chan bool, 1)

View File

@ -74,14 +74,17 @@ func (i *Item) execute(variables []string, script string) (string, error) {
}
func (i *Item) initInteractiveShell(v []string) error {
timeout := time.Duration(i.rateMs) * time.Millisecond * 3 / 4
if i.pty {
i.ptyShell = &PtyInteractiveShell{item: i, variables: v, timeout: timeout}
return i.ptyShell.init()
} else {
i.basicShell = &BasicInteractiveShell{item: i, variables: v, timeout: timeout}
return i.basicShell.init()
}
i.basicShell = &BasicInteractiveShell{item: i, variables: v, timeout: timeout}
return i.basicShell.init()
}
func (i *Item) transform(sample string) (string, error) {

View File

@ -23,20 +23,21 @@ const (
const licenseFileName = "license.yml"
func GetLicense() *License {
if !fileExists(licenseFileName) {
return nil
} else {
file := readStorageFile(getPlatformStoragePath(licenseFileName))
license := new(License)
err := yaml.Unmarshal(file, license)
if err != nil {
log.Fatalf("Failed to read license file: %v", err)
}
return license
}
file := readStorageFile(getPlatformStoragePath(licenseFileName))
license := new(License)
err := yaml.Unmarshal(file, license)
if err != nil {
log.Fatalf("Failed to read license file: %v", err)
}
return license
}
func SaveLicense(license License) {

View File

@ -9,7 +9,7 @@ import (
"runtime"
)
// Anonymous usage data, which we collect for analyses and improvements
// Statistics represents anonymous usage data, which we collect for analyses and improvements
// User can disable it, along with crash reports, using --telemetry flag
type Statistics struct {
Version string
@ -22,6 +22,7 @@ type Statistics struct {
const statisticsFileName = "statistics.yml"
// PersistStatistics in file
func PersistStatistics(config *config.Config) *Statistics {
statistics := new(Statistics)
@ -65,7 +66,9 @@ func PersistStatistics(config *config.Config) *Statistics {
return statistics
}
// GetStatistics from file
func GetStatistics(cfg *config.Config) *Statistics {
if !fileExists(statisticsFileName) {
return &Statistics{
Version: console.AppVersion,
@ -75,15 +78,17 @@ func GetStatistics(cfg *config.Config) *Statistics {
WindowHeight: 0,
ComponentsCount: countComponentsPerType(cfg),
}
} else {
file := readStorageFile(getPlatformStoragePath(statisticsFileName))
license := new(Statistics)
err := yaml.Unmarshal(file, license)
if err != nil {
log.Fatalf("Failed to read statistics file: %v", err)
}
return license
}
file := readStorageFile(getPlatformStoragePath(statisticsFileName))
license := new(Statistics)
err := yaml.Unmarshal(file, license)
if err != nil {
log.Fatalf("Failed to read statistics file: %v", err)
}
return license
}
func countComponentsPerType(config *config.Config) map[string]int {