clistats

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2020 License: MIT Imports: 8 Imported by: 69

README

clistats

A command line statistics display library for golang.

Documentation

Overview

Package clistats implements a progress bar like functionality which displays periodic progress based on various rules.

Rather than rendering and maintaining a dynamic progress bar, statistics are displayed in individual lines either based on user keystrokes or using a display alogrithm that observes changes in statistics over time.

It is heavily inspired from nmap which is a very popular security scanning tool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FmtDuration added in v0.0.6

func FmtDuration(d time.Duration) string

FmtDuration formats the duration for the time elapsed

func String

func String(from interface{}) string

String returns the string representation of a few different types that are simple enough to be represented as a static metric for stats.

For everything else, it uses fmt.Sprint but it is very recommended to use only small and easy types.

Types

type DynamicCallback

type DynamicCallback func(client StatisticsClient) interface{}

DynamicCallback is called during statistics calculation for a dynamic field.

The value returned from this callback is displayed as the current value of a dynamic field. This can be utilised to calculated things like elapsed time, requests per seconds, etc.

func NewRequestsPerSecondCallback

func NewRequestsPerSecondCallback(options RequestPerSecondCallbackOptions) DynamicCallback

NewRequestsPerSecondCallback creates a request per second callback function.

type PrintCallback

type PrintCallback func(client StatisticsClient)

PrintCallback is used by clients to build and display a string on the screen.

type RequestPerSecondCallbackOptions

type RequestPerSecondCallbackOptions struct {
	// StartTimeFieldID is the ID of the start time field for the client.
	StartTimeFieldID string
	// RequestsCounterID is the ID of the sent request counter
	RequestsCounterID string
}

RequestPerSecondCallbackOptions returns a callback function which generates requests per second metrics based on total requests and time elapsed since the scan.

type Statistics

type Statistics struct {
	// contains filtered or unexported fields
}

Statistics is a client for showing statistics on the stdout.

func New

func New() (*Statistics, error)

New creates a new statistics client for cli stats printing.

func (*Statistics) AddCounter

func (s *Statistics) AddCounter(id string, value uint64)

AddCounter adds a uint64 counter field to the statistics client.

A counter is used to track an increasing quantity, like requests, errors etc.

func (*Statistics) AddDynamic

func (s *Statistics) AddDynamic(id string, Callback DynamicCallback)

AddDynamic adds a dynamic field to display whose value is retrieved by running a callback function.

The callback function performs some actions and returns the value to display. Generally this is used for calculating requests per seconds, elapsed time, etc.

func (*Statistics) AddStatic

func (s *Statistics) AddStatic(id string, value interface{})

AddStatic adds a static information field to the statistics.

The value for these metrics will remain constant throughout the lifecycle of the statistics client. All the values will be converted into string and displayed as such.

func (*Statistics) GetCounter

func (s *Statistics) GetCounter(id string) (uint64, bool)

GetCounter returns the current value of a counter.

func (*Statistics) GetDynamic

func (s *Statistics) GetDynamic(id string) (DynamicCallback, bool)

GetDynamic returns the dynamic field callback for data retrieval.

func (*Statistics) GetStatic

func (s *Statistics) GetStatic(id string) (interface{}, bool)

GetStatic returns the original value for a static field.

func (*Statistics) IncrementCounter

func (s *Statistics) IncrementCounter(id string, count int)

IncrementCounter increments the value of a counter by a count.

func (*Statistics) Start

func (s *Statistics) Start(printer PrintCallback, tickDuration time.Duration) error

Start starts the event loop of the stats client.

func (*Statistics) Stop

func (s *Statistics) Stop() error

Stop stops the event loop of the stats client

type StatisticsClient

type StatisticsClient interface {
	// Start starts the event loop of the stats client.
	Start(printer PrintCallback, tickDuration time.Duration) error
	// Stop stops the event loop of the stats client
	Stop() error

	// AddCounter adds a uint64 counter field to the statistics client.
	//
	// A counter is used to track an increasing quantity, like requests,
	// errors etc.
	AddCounter(id string, value uint64)

	// GetCounter returns the current value of a counter.
	GetCounter(id string) (uint64, bool)

	// IncrementCounter increments the value of a counter by a count.
	IncrementCounter(id string, count int)

	// AddStatic adds a static information field to the statistics.
	//
	// The value for these metrics will remain constant throughout the
	// lifecycle of the statistics client. All the values will be
	// converted into string and displayed as such.
	AddStatic(id string, value interface{})

	// GetStatic returns the original value for a static field.
	GetStatic(id string) (interface{}, bool)

	// AddDynamic adds a dynamic field to display whose value
	// is retrieved by running a callback function.
	//
	// The callback function performs some actions and returns the value
	// to display. Generally this is used for calculating requests per
	// seconds, elapsed time, etc.
	AddDynamic(id string, Callback DynamicCallback)

	// GetDynamic returns the dynamic field callback for data retrieval.
	GetDynamic(id string) (DynamicCallback, bool)
}

StatisticsClient is an interface implemented by a statistics client.

A unique ID is to be provided along with a description for the field to be displayed as output.

Multiple types of statistics are provided like Counters as well as static fields which display static information only.

A metric cannot be added once the client has been started. An error will be returned if the metric cannot be added. Already existing fields of same names are overwritten.

Directories

Path Synopsis
examples
basic command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL