clistats

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 10 Imported by: 1

README

clistats

A command line statistics display library for golang.

Documentation

Overview

Package clistats implements a progress monitor functionality which exposes statistics in json format on a api endpoint bound to localhost

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	ListenPort: 63636,
	Web:        true,
}

DefaultOptions for clistats

Functions

func FmtDuration

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 calculate things like elapsed time, requests per seconds, etc.

func NewRequestsPerSecondCallback

func NewRequestsPerSecondCallback(options RequestPerSecondCallbackOptions) DynamicCallback

NewRequestsPerSecondCallback creates a request per second callback function.

type Options

type Options struct {
	ListenPort int
	Web        bool
}

Options to customize behavior

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 request sent 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 {
	Options *Options
	// 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 with default options

func NewWithOptions

func NewWithOptions(ctx context.Context, options *Options) (*Statistics, error)

NewWithOptions creates a new client with custom options

func (*Statistics) AddCounter

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

AddCounter adds an 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) GetStatResponse

func (s *Statistics) GetStatResponse(interval time.Duration, callback func(string, error) error)

GetStatResponse returns '/metrics' response for a given interval

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() 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() 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)

	//GetStatResponse returns '/metrics' response for a given interval
	GetStatResponse(interval time.Duration, callback func(string, error) error)
}

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

Jump to

Keyboard shortcuts

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