...

Package utils

import "golang.conradwood.net/go-easyops/utils"
Overview
Index

Overview ▾

Package utils provides a lot of utilities for files, strings, numbers, printing data, formatting and parsing time, rate calculation, sliding average calculation, (linux) console pretty printing and so on.

Index ▾

Constants
func Bail(txt string, err error)
func BooleanValue(boolvalue string) (bool, error)
func BooleanValueNoErr(boolvalue string) bool
func CallingFunction() string
func ChmodR(dir string, dirmask, filemask fs.FileMode) error
func DebugFlag(name string) *debugFlag
func DirHash(ddir string) (string, error)
func DirWalk(dir string, fn func(root string, relative_filename string) error) error
func DirWalkAllFiles(dir string) ([]string, error)
func ErrorString(err error) string
func FileExists(filename string) bool
func FindFile(name string) (string, error)
func FindFileInWorkingDir(name string) (string, error)
func FlagStrings(name, help string, values map[string]string) *flag_string_t
func GetFaultIndicatorClient() faultindicator.FaultIndicatorClient
func GetIPAndNet(ip_s string) (string, int, error)
func GetStack(format string, args ...interface{}) string
func GetStackLines() []string
func HexStr(buf []byte) string
func Hexdump(prefix string, buf []byte) string
func HexdumpWithLen(ln int, prefix string, buf []byte) string
func HomeDir() (string, error)
func IntArrayFlag(name string, help string) *intarrayFlag
func IsDir(name string) (bool, error)
func IsLettersOnly(txt string) bool
func IsLinkLocal(s string) bool
func IsLoopback(s string) bool
func IsOnlyChars(txt string, valid string) bool
func IsPrivateIP(ip_s string) (bool, error)
func LocalTime(ctx context.Context) (time.Time, error)
func LogFault(ctx context.Context, name, desc string)
func Mac2Str(mac uint64) string
func MakeSafeFilename(name string) string
func Marshal(req proto.Message) (string, error)
func MarshalBytes(req proto.Message) ([]byte, error)
func MarshalYaml(src interface{}) ([]byte, error)
func MaxInt(x, y int) int
func MaxInt32(x, y int32) int32
func MaxInt64(x, y int64) int64
func MinInt(x, y int) int
func MinInt32(x, y int32) int32
func MinInt64(x, y int64) int64
func MultiLinePrefix(input, prefix string, linelen int) string
func NotImpl(format string, args ...interface{})
func OpenWriteFile(filename string) (*os.File, error)
func ParseIP(ip_s string) (string, uint32, int, error)
func ParseTime(ts string) (time.Time, error)
func ParseTimeWithLocation(ts, tz string) (time.Time, error)
func ParseTimestamp(ts string) (uint32, error)
func ParseTimestampWithLocation(ts, tz string) (uint32, error)
func PrettyNumber(number uint64) string
func PrintStack(format string, args ...interface{})
func RandomInt(max int64) int
func RandomStall(minutes int)
func RandomString(n int) string
func ReadFile(filename string) ([]byte, error)
func ReadProto(filename string, req proto.Message) error
func ReadYaml(filename string, target interface{}) error
func RecreateSafely(dirname string) error
func RemoveAll(dir string) error
func SafelyWriteFile(filename string, content []byte) error
func SetConsoleTitle(title string)
func TimeString(t time.Time) string
func TimestampAgeString(timestamp uint32) string
func TimestampString(timestamp uint32) string
func Tribool(name, help string) *tribool_t
func Unmarshal(b64string string, req proto.Message) error
func UnmarshalBytes(pdata []byte, req proto.Message) error
func UnmarshalYaml(buf []byte, target interface{}) error
func WorkingDir() string
func WriteFile(filename string, content []byte) error
func WriteFileCreateDir(filename string, content []byte) error
func WriteProto(filename string, req proto.Message) error
func WriteYaml(filename string, data interface{}) error
type ArrayComparer
    func CompareArray(array1, array2 interface{}, comp func(i, j int) bool) *ArrayComparer
    func (ac *ArrayComparer) ElementsIn1ButNot2() []int
    func (ac *ArrayComparer) ElementsIn2ButNot1() []int
    func (ac *ArrayComparer) EqualElements() bool
type ByteStreamReceiver
    func NewByteStreamReceiver(path string) *ByteStreamReceiver
    func NewByteStreamReceiverWithFunction(newfile func(filename string, content []byte) error) *ByteStreamReceiver
    func (bsr *ByteStreamReceiver) Close() error
    func (bsr *ByteStreamReceiver) FileCount() int
    func (bsr *ByteStreamReceiver) Files() map[string]uint64
    func (bsr *ByteStreamReceiver) NewData(data StreamData) error
    func (bsr *ByteStreamReceiver) TotalBytesReceived() uint64
type ByteStreamSender
    func NewByteStreamSender(f1 Send_new_file_type, f2 Send_data_type) *ByteStreamSender
    func (bss *ByteStreamSender) FileCount() int
    func (bss *ByteStreamSender) SendBytes(key, filename string, b []byte) error
type Cell
    func (c *Cell) String() string
type ClientConnector
type DirHasher
    func (dh *DirHasher) AddEntry(root string, relname string) error
type Interpolator
    func (ip *Interpolator) AddReferencePoint(number, value float64)
    func (ip *Interpolator) AddReferencePoints(points map[float64]float64)
    func (ip *Interpolator) LinearInterpolate(number float64) float64
    func (ip *Interpolator) String() string
type LockedBool
    func (lb *LockedBool) Set(b bool)
    func (lb *LockedBool) Value() bool
type LockedInt
    func (li *LockedInt) Dec() int
    func (li *LockedInt) Inc() int
    func (li *LockedInt) Set(val int) int
    func (li *LockedInt) Value() int
type MiniTimeSeries
    func NewMiniTimeSeries(keep time.Duration) *MiniTimeSeries
    func (mt *MiniTimeSeries) Add(value float64)
    func (mt *MiniTimeSeries) AddWithTimestamp(ts time.Time, value float64)
    func (mt *MiniTimeSeries) All() map[int64]float64
    func (mt *MiniTimeSeries) Difference() float64
    func (mt *MiniTimeSeries) EarliestValue() (time.Time, float64)
    func (mt *MiniTimeSeries) GC()
    func (mt *MiniTimeSeries) LatestValue() (time.Time, float64)
type PacketReader
    func NewPacketReader(r io.Reader, start, escape, stop byte) (*PacketReader, error)
    func (pr *PacketReader) Close()
    func (pr *PacketReader) Read(buf []byte) (int, error)
type PacketWriter
    func NewPacketWriter(r io.Writer, start, escape, stop byte) (*PacketWriter, error)
    func (pr *PacketWriter) Close() error
    func (pr *PacketWriter) Write(buf []byte) (int, error)
type PeriodicTimer
    func NewPeriodicTimer(secs []time.Duration, cb func(pt *PeriodicTimer, secsLapsed time.Duration) error) *PeriodicTimer
    func (pt *PeriodicTimer) LastStarted() time.Time
    func (pt *PeriodicTimer) Secs() []time.Duration
    func (pt *PeriodicTimer) Start()
    func (pt *PeriodicTimer) Stop()
    func (pt *PeriodicTimer) Wait()
type ProgressReporter
    func (p *ProgressReporter) Add(a uint64)
    func (p *ProgressReporter) Eta() time.Time
    func (p *ProgressReporter) Inc()
    func (p *ProgressReporter) Print() bool
    func (p *ProgressReporter) PrintSingleLine() bool
    func (p *ProgressReporter) Rate() float64
    func (p *ProgressReporter) Set(a uint64)
    func (p *ProgressReporter) SetTotal(total uint64)
    func (p *ProgressReporter) String() string
type RateCalculator
    func NewRateCalculator(name string) RateCalculator
type Row
    func (r *Row) AddCell(cell *Cell)
    func (r *Row) Cells() []*Cell
    func (r *Row) Cols() int
    func (r *Row) GetCell(idx int) *Cell
type Send_data_type
type Send_new_file_type
type Shifter
    func NewShifter(buf []byte) *Shifter
    func (sh *Shifter) Array8() []byte
    func (sh *Shifter) Bytes() []byte
    func (sh *Shifter) Error() error
    func (sh *Shifter) RemainingBytes() []byte
    func (sh *Shifter) SetUint32(pos int, b uint32)
    func (sh *Shifter) Unshift_uint16() uint32
    func (sh *Shifter) Unshift_uint32() uint32
    func (sh *Shifter) Unshift_uint64() uint64
    func (sh *Shifter) Unshift_uint8() uint8
type SlidingAverage
    func NewSlidingAverage() *SlidingAverage
    func (sa *SlidingAverage) Add(counter int, a uint64)
    func (sa *SlidingAverage) GetAverage(counter int) float64
    func (sa *SlidingAverage) GetCounter(counter int) uint64
    func (sa *SlidingAverage) GetCounts(counter int) uint64
    func (sa *SlidingAverage) GetRate(counter int) float64
type StreamData
type StreamToPacket
    func NewStreamToPacket(start, esc, stop byte) (*StreamToPacket, error)
    func (stp *StreamToPacket) AddByte(b byte) bool
    func (stp *StreamToPacket) ReadPacket() []byte
type Table
    func (t *Table) AddBool(b bool) *Table
    func (t *Table) AddFloat64(f float64) *Table
    func (t *Table) AddHeader(s string)
    func (t *Table) AddHeaders(s ...string)
    func (t *Table) AddInt(i int) *Table
    func (t *Table) AddInt64(i int64) *Table
    func (t *Table) AddString(s string) *Table
    func (t *Table) AddStrings(sts ...string) *Table
    func (t *Table) AddTimestamp(ts uint32) *Table
    func (t *Table) AddTimestampWithAge(ts uint32) *Table
    func (t *Table) AddUint32(i uint32) *Table
    func (t *Table) AddUint64(i uint64) *Table
    func (t *Table) DisableColumn(col int)
    func (t *Table) EnableAllColumns()
    func (t *Table) EnableColumn(col int)
    func (t *Table) GetMaxLen(col int) int
    func (t *Table) GetPrintingRows() []*Row
    func (t *Table) GetRowOrCreate(num int) *Row
    func (t *Table) NewRow()
    func (t *Table) SetMaxLen(col, width int)
    func (t *Table) ToCSV() string
    func (t *Table) ToPrettyString() string
type TerminalDimensions
    func TerminalSize() (*TerminalDimensions, error)
    func (td *TerminalDimensions) Columns() int
    func (td *TerminalDimensions) Rows() int
type TextFormatter
    func (tf *TextFormatter) ToPrettyString() string
type TextPositionFinder
    func NewTextPositionFinder(ct []byte) TextPositionFinder
type TimeoutLock
    func NewTimeoutLock(name string) TimeoutLock
type Value

Package files

array_comparer.go compare_boolean.go console.go debugflag.go dirhash.go dirwalker.go errors.go faults.go fileutils.go flag_intarray.go flags.go hexdump.go interpolator.go ip_helper.go localtime.go locked_bool.go locked_int.go macadr.go mini_timeseries.go multiline.go not_impl.go packet_reader.go packet_writer.go packetizer_helper.go periodictimer.go progressreporter.go proto.go recreate.go shifter.go sliding_average.go stream_receiver.go stream_sender.go stream_to_packet.go table.go table_formatter.go text_positionfinder.go time.go timeout_lock.go timeparser.go tribool.go utils.go yaml.go

Constants

const (
    COUNTERS = 10
)

func Bail

func Bail(txt string, err error)

never returns - if error != nil, print it and exit

func BooleanValue

func BooleanValue(boolvalue string) (bool, error)

return true or false if a boolean can be parsed. error if string is not something that is understood as a boolean. currently understands true/false,yes/no and on/off

func BooleanValueNoErr

func BooleanValueNoErr(boolvalue string) bool

if a valid boolean can parsed, return true, false in all other cases

func CallingFunction

func CallingFunction() string

returns a single line with the calling function immedialy preceding the function which invoked this one

func ChmodR

func ChmodR(dir string, dirmask, filemask fs.FileMode) error

recursively chmod a dir and files and subdirectories. applies 'dirmask' to dirs and 'filemask' to files

func DebugFlag

func DebugFlag(name string) *debugFlag

func DirHash

func DirHash(ddir string) (string, error)

return a hash across an entire directory tree, including the contents of all files

func DirWalk

func DirWalk(dir string, fn func(root string, relative_filename string) error) error

walk a directory tree and call function for each file (but not each dir)

func DirWalkAllFiles

func DirWalkAllFiles(dir string) ([]string, error)

returns all files relative to dir

func ErrorString

func ErrorString(err error) string

extracts the PRIVATE and possibly SENSITIVE debug error message from a string obsolete - use errors.ErrorString(err) the reason this is so convoluted with different types, is that different versions of grpc encapsulate status details in different messages.

func FileExists

func FileExists(filename string) bool

I can never remember how to do this, so here's a helper: return true if file exists, false otherwise

func FindFile

func FindFile(name string) (string, error)

search for a file in git repo and above. error if not found

func FindFileInWorkingDir

func FindFileInWorkingDir(name string) (string, error)

this will find a file or directory in the given working directory by traversing up the directory hierarchy but only upto the workingdir (not higher). the returned file is guaranteed to be within the workingdir. if it cannot be found, an error is returned.

func FlagStrings

func FlagStrings(name, help string, values map[string]string) *flag_string_t

very weird string flag thing

func GetFaultIndicatorClient

func GetFaultIndicatorClient() faultindicator.FaultIndicatorClient

get the faultindicator client.

func GetIPAndNet

func GetIPAndNet(ip_s string) (string, int, error)

splits things like so:

	172.29.1.0/24  => "172.29.1.0" and 24
	172.29.1.5:5000  => "172.29.1.5" and 32
        2a01:4b00:ab0f:5100:5::5/64 => "2a01:4b00:ab0f:5100:5::5" and 64

func GetStack

func GetStack(format string, args ...interface{}) string

get stack in a human readable format

func GetStackLines

func GetStackLines() []string

get stack, line by line in a human readable format

func HexStr

func HexStr(buf []byte) string

return a mini hexdump as single line string

func Hexdump

func Hexdump(prefix string, buf []byte) string

return the buffer as a hexdump with ascii. each line prefixed by 'prefix'

func HexdumpWithLen

func HexdumpWithLen(ln int, prefix string, buf []byte) string

func HomeDir

func HomeDir() (string, error)

get current home dir (from environment variable HOME, if that fails user.Current())

func IntArrayFlag

func IntArrayFlag(name string, help string) *intarrayFlag

a flag to allow for a list of uint64 and a convenient method to check if an integer is part of the flag

func IsDir

func IsDir(name string) (bool, error)

return true if file exists and is a directory (uses findfile)

func IsLettersOnly

func IsLettersOnly(txt string) bool

return true if string has letters only

func IsLinkLocal

func IsLinkLocal(s string) bool

return true if this is a IPv6 link local ip

func IsLoopback

func IsLoopback(s string) bool

returns true if this is an IPv4 or IPv6 loopback address

func IsOnlyChars

func IsOnlyChars(txt string, valid string) bool

true only if string "txt" is made up exclusively of characters in "valid"

func IsPrivateIP

func IsPrivateIP(ip_s string) (bool, error)

error if not a valid ip true if it is a non-routeable IP, such as link-local, loopback, or rfc 1918

func LocalTime

func LocalTime(ctx context.Context) (time.Time, error)

func LogFault

func LogFault(ctx context.Context, name, desc string)

This logs a fault to the faultindicator service.

func Mac2Str

func Mac2Str(mac uint64) string

func MakeSafeFilename

func MakeSafeFilename(name string) string

given an arbitrary string, will remove all unsafe characters. result may be safely used as a filename

func Marshal

func Marshal(req proto.Message) (string, error)

take a proto and convert it into a base64 string

func MarshalBytes

func MarshalBytes(req proto.Message) ([]byte, error)

func MarshalYaml

func MarshalYaml(src interface{}) ([]byte, error)

marshal 'src' to yaml as bytes

func MaxInt

func MaxInt(x, y int) int

func MaxInt32

func MaxInt32(x, y int32) int32

func MaxInt64

func MaxInt64(x, y int64) int64

func MinInt

func MinInt(x, y int) int

func MinInt32

func MinInt32(x, y int32) int32

func MinInt64

func MinInt64(x, y int64) int64

func MultiLinePrefix

func MultiLinePrefix(input, prefix string, linelen int) string

Adds a prefix to each line of a multi-line string. it hounours existing line breaks (\n) if line is longer than linelen it will add a linebreak empty trailing lines will be trimmed a single trailing \n will be retained.

func NotImpl

func NotImpl(format string, args ...interface{})

func OpenWriteFile

func OpenWriteFile(filename string) (*os.File, error)

like ioutil - but with open permissions to share

func ParseIP

func ParseIP(ip_s string) (string, uint32, int, error)

Parse an IPAddress. this may be either IPv4 or IPv6. The result is the IPaddress (perhaps normalised), the port (if any), the version or an error (if unparseable)

func ParseTime

func ParseTime(ts string) (time.Time, error)

Parse a time in various (uk) formats and return a time.Time

func ParseTimeWithLocation

func ParseTimeWithLocation(ts, tz string) (time.Time, error)

Parse a time in various (uk) formats and return a unix timestamp (in UTC)

func ParseTimestamp

func ParseTimestamp(ts string) (uint32, error)

Parse a time in various (uk) formats and return a unix timestamp

func ParseTimestampWithLocation

func ParseTimestampWithLocation(ts, tz string) (uint32, error)

Parse a time in various (uk) formats and return a unix timestamp

func PrettyNumber

func PrettyNumber(number uint64) string

func PrintStack

func PrintStack(format string, args ...interface{})

print a simpliefied stacktrace (filenames and linenumbers only)

func RandomInt

func RandomInt(max int64) int

return random integer between 0 and n

func RandomStall

func RandomStall(minutes int)

stall for a random amount of "upto" minutes

func RandomString

func RandomString(n int) string

func ReadFile

func ReadFile(filename string) ([]byte, error)

read file (uses some magic to find it too)

func ReadProto

func ReadProto(filename string, req proto.Message) error

read a proto from disk

func ReadYaml

func ReadYaml(filename string, target interface{}) error

reads a raml file and parses it (strict) into interface. error if unknown tags are encountered in yaml

func RecreateSafely

func RecreateSafely(dirname string) error

if dir does not exist: create it and create a file ".filelayouter" if dir DOES exist, check for existence of a file ".filelayouter", if so, recreate otherwise error

func RemoveAll

func RemoveAll(dir string) error

removes dir, changes permissions if needs to

func SafelyWriteFile

func SafelyWriteFile(filename string, content []byte) error

write a file to a temporary file, then rename it afterwards

func SetConsoleTitle

func SetConsoleTitle(title string)

func TimeString

func TimeString(t time.Time) string

format a timestring

func TimestampAgeString

func TimestampAgeString(timestamp uint32) string

format a timestamp as 'age'

func TimestampString

func TimestampString(timestamp uint32) string

format a timestamp

func Tribool

func Tribool(name, help string) *tribool_t

func Unmarshal

func Unmarshal(b64string string, req proto.Message) error

take a base64 string and convert it into the proto

func UnmarshalBytes

func UnmarshalBytes(pdata []byte, req proto.Message) error

func UnmarshalYaml

func UnmarshalYaml(buf []byte, target interface{}) error

interprets the bytes as yaml and decodes it (strict) into interface. error if unknown tags are encountered in yaml

func WorkingDir

func WorkingDir() string

return the "workingdir" (the one we were started in)

func WriteFile

func WriteFile(filename string, content []byte) error

like ioutil - but with open permissions to share

func WriteFileCreateDir

func WriteFileCreateDir(filename string, content []byte) error

like ioutil - but with open permissions to share. if necessary creates directories

func WriteProto

func WriteProto(filename string, req proto.Message) error

write a proto to disk

func WriteYaml

func WriteYaml(filename string, data interface{}) error

write 'data' as a yaml file to 'filename'

type ArrayComparer

compare two arrays. find Missing elements in one or the other

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

func CompareArray

func CompareArray(array1, array2 interface{}, comp func(i, j int) bool) *ArrayComparer

Compare an array. It uses a custom "compare" function which must return true if two elements are equal. i refers to array1[i] and j refers to array2[j] TODO: check if we can use reflect.ValueOf(array1).Index(i) to remove the need for external comp function

func (*ArrayComparer) ElementsIn1ButNot2

func (ac *ArrayComparer) ElementsIn1ButNot2() []int

returns indices of elements that are in array 1 but not array 2

func (*ArrayComparer) ElementsIn2ButNot1

func (ac *ArrayComparer) ElementsIn2ButNot1() []int

returns indices of elements that are in array 2 but not array 1

func (*ArrayComparer) EqualElements

func (ac *ArrayComparer) EqualElements() bool

returns true if both arrays contain the same elements (disregarding the order they are in)

type ByteStreamReceiver

type ByteStreamReceiver struct {
    sync.Mutex
    // contains filtered or unexported fields
}

func NewByteStreamReceiver

func NewByteStreamReceiver(path string) *ByteStreamReceiver

func NewByteStreamReceiverWithFunction

func NewByteStreamReceiverWithFunction(newfile func(filename string, content []byte) error) *ByteStreamReceiver

report each file to the function. This usually happens when Close() is called, prior to that the receiver has no means of telling if the file is completed. Perhaps a flaw in the protocol?

func (*ByteStreamReceiver) Close

func (bsr *ByteStreamReceiver) Close() error

func (*ByteStreamReceiver) FileCount

func (bsr *ByteStreamReceiver) FileCount() int

how many files were retrieved?

func (*ByteStreamReceiver) Files

func (bsr *ByteStreamReceiver) Files() map[string]uint64

func (*ByteStreamReceiver) NewData

func (bsr *ByteStreamReceiver) NewData(data StreamData) error

the result of srv.Recv()

func (*ByteStreamReceiver) TotalBytesReceived

func (bsr *ByteStreamReceiver) TotalBytesReceived() uint64

type ByteStreamSender

sends a bunch of bytes down a grpc stream

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

func NewByteStreamSender

func NewByteStreamSender(f1 Send_new_file_type, f2 Send_data_type) *ByteStreamSender

create a new bytestream sender. (key and filename are opaque to this sender) f1 - a function that sends a message on the stream to indicate start of a new file and key f2 - a function that sends a bunch of data on the stream

once stream sender is created, call SendBytes with filename and content. it will break the file into small pieces and call f2() with small arrays suitable for sending in a packet

func (*ByteStreamSender) FileCount

func (bss *ByteStreamSender) FileCount() int

how many files were sent?

func (*ByteStreamSender) SendBytes

func (bss *ByteStreamSender) SendBytes(key, filename string, b []byte) error

type Cell

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

func (*Cell) String

func (c *Cell) String() string

type ClientConnector

type ClientConnector interface {
    Connect(string) *grpc.ClientConn
}
var (

    // public as implementation detail
    Client_connector ClientConnector
)

type DirHasher

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

func (*DirHasher) AddEntry

func (dh *DirHasher) AddEntry(root string, relname string) error

type Interpolator

given some reference points will interpolate a value,

examples:

  1. reference points 5(50) and 10(100), interpolation: 7=70, 8=80,3=30 (0 is assumed), 11=100 (highest=max)
type Interpolator struct {
    sync.Mutex
    // contains filtered or unexported fields
}

func (*Interpolator) AddReferencePoint

func (ip *Interpolator) AddReferencePoint(number, value float64)

func (*Interpolator) AddReferencePoints

func (ip *Interpolator) AddReferencePoints(points map[float64]float64)

func (*Interpolator) LinearInterpolate

func (ip *Interpolator) LinearInterpolate(number float64) float64

fast and quick interpolation using linear interpolation

func (*Interpolator) String

func (ip *Interpolator) String() string

type LockedBool

type LockedBool struct {
    sync.Mutex
    // contains filtered or unexported fields
}

func (*LockedBool) Set

func (lb *LockedBool) Set(b bool)

func (*LockedBool) Value

func (lb *LockedBool) Value() bool

type LockedInt

type LockedInt struct {
    sync.Mutex
    // contains filtered or unexported fields
}

func (*LockedInt) Dec

func (li *LockedInt) Dec() int

returns NEW value

func (*LockedInt) Inc

func (li *LockedInt) Inc() int

returns NEW value

func (*LockedInt) Set

func (li *LockedInt) Set(val int) int

returns PREVIOUS value

func (*LockedInt) Value

func (li *LockedInt) Value() int

type MiniTimeSeries

type MiniTimeSeries struct {
    sync.Mutex
    // contains filtered or unexported fields
}

func NewMiniTimeSeries

func NewMiniTimeSeries(keep time.Duration) *MiniTimeSeries

create a new rolling timeseries, which keeps values no longer than 'keep' parameter. (older ones will be discarded)

func (*MiniTimeSeries) Add

func (mt *MiniTimeSeries) Add(value float64)

add a value now

func (*MiniTimeSeries) AddWithTimestamp

func (mt *MiniTimeSeries) AddWithTimestamp(ts time.Time, value float64)

add a value with timestamp (backfilling)

func (*MiniTimeSeries) All

func (mt *MiniTimeSeries) All() map[int64]float64

get all values as a map timestamp->value

func (*MiniTimeSeries) Difference

func (mt *MiniTimeSeries) Difference() float64

total difference between first and last value (latest - earliest)

func (*MiniTimeSeries) EarliestValue

func (mt *MiniTimeSeries) EarliestValue() (time.Time, float64)

func (*MiniTimeSeries) GC

func (mt *MiniTimeSeries) GC()

called automatically occassionally to delete old values

func (*MiniTimeSeries) LatestValue

func (mt *MiniTimeSeries) LatestValue() (time.Time, float64)

type PacketReader

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

func NewPacketReader

func NewPacketReader(r io.Reader, start, escape, stop byte) (*PacketReader, error)

This is part of the Packetizer toolset.

A PacketReader reads from any stream (specifically, an io.Reader). It reads from the stream until any one of the following conditions occur:

  1. the io.Reader signals io.EOF
  2. the io.Reader signals some other error
  3. a complete packet, starting with the start byte and ending with the stop byte has been received through the io.Reader
  4. the start of a packet has been read, but its size exceeds the buffer size (specifically: the of the array passed to PacketReader.Read([]byte))

A packet is defined as the data between start and stop byte. As a consequence, Any start and stop bytes in the payload must be escaped with the escape byte

The data returned by the packet reader is guaranteed to be equal to the payload send by the PacketWriter. In other words: any escaping and wrapping and unescaping and unwrapping is handled by the reader and writer. see also NewPacketWriter

func (*PacketReader) Close

func (pr *PacketReader) Close()

closes underlying reader as well AND aborts a current read

func (*PacketReader) Read

func (pr *PacketReader) Read(buf []byte) (int, error)

this will return the number of bytes or an error. it is guaranteed to either return an error OR a non-zero number of bytes, but never both

type PacketWriter

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

func NewPacketWriter

func NewPacketWriter(r io.Writer, start, escape, stop byte) (*PacketWriter, error)

This is part of the Packetizer toolset.

A PacketWriter writes arbitrary data to an io.Writer. Each call to PacketWriter.Write is assumed to be one packet. The PacketWriter sends extra data to the io.Writer to allow a PacketReader to identify and reassemble each packet.

The algorithm to send any one packet can be summarised like so:

  1. Send one extra byte, the start- byte
  2. Send the payload upto, but not including any start, stop or escape bytes in the payload
  3. for any start, stop or escape byte, insert an extra escape byte
  4. repeat send until all bytes are send
  5. send one extra byte, the escape-byte

This algorithm is sufficient to send any data, 8-bit clean, across any io.Writer and reassemble it on the receiving side ot he stream. The canonical implementation to reassemble the packets is NewPacketReader.

func (*PacketWriter) Close

func (pr *PacketWriter) Close() error

func (*PacketWriter) Write

func (pr *PacketWriter) Write(buf []byte) (int, error)

write a packet (using buf as the payload)

type PeriodicTimer

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

func NewPeriodicTimer

func NewPeriodicTimer(secs []time.Duration, cb func(pt *PeriodicTimer, secsLapsed time.Duration) error) *PeriodicTimer

A "PeriodicTimer" executes a callback at certain intervals over a certain period of time. For example, a periodictimer, defined as NewPeriodicTimer([]uint32{20,15,5}) will run for 20 seconds and call the callback after 5 seconds, 10 seconds and 20 seconds. The callback will be retried every second if it returns an error until it returns no error callback will also be called each time "Start()" is called.

Note: this code is 'experimental' (at best). it is neither optimised for efficiency nor intended to be used for high-performance requirements. it is intented to be used for small, simple periodic tasks. a typical secs array may contain 5 entries. anything over 10 is considered large and untested.

func (*PeriodicTimer) LastStarted

func (pt *PeriodicTimer) LastStarted() time.Time

func (*PeriodicTimer) Secs

func (pt *PeriodicTimer) Secs() []time.Duration

func (*PeriodicTimer) Start

func (pt *PeriodicTimer) Start()

func (*PeriodicTimer) Stop

func (pt *PeriodicTimer) Stop()

func (*PeriodicTimer) Wait

func (pt *PeriodicTimer) Wait()

wait for timer to either stop or expire

type ProgressReporter

* This is a simple command line "progress reporter". * It's usage is (intentionally) very simple. * Example: * pr := utils.ProgressReporter{} * pr.SetTotal(1000) * for i:=0;i<1000;i++ { * // do something slow * DoSomethingSlow() * pr.Inc() * pr.Print() * } * The above sippet will print a rate and an ETA once a second.

type ProgressReporter struct {
    RawPrint bool

    Prefix string
    // contains filtered or unexported fields
}

func (*ProgressReporter) Add

func (p *ProgressReporter) Add(a uint64)

func (*ProgressReporter) Eta

func (p *ProgressReporter) Eta() time.Time

func (*ProgressReporter) Inc

func (p *ProgressReporter) Inc()

func (*ProgressReporter) Print

func (p *ProgressReporter) Print() bool

func (*ProgressReporter) PrintSingleLine

func (p *ProgressReporter) PrintSingleLine() bool

return true if it actually printed stuff

func (*ProgressReporter) Rate

func (p *ProgressReporter) Rate() float64

func (*ProgressReporter) Set

func (p *ProgressReporter) Set(a uint64)

func (*ProgressReporter) SetTotal

func (p *ProgressReporter) SetTotal(total uint64)

func (*ProgressReporter) String

func (p *ProgressReporter) String() string

type RateCalculator

type RateCalculator interface {
    Add(a uint64)
    Rate() float64
    Reset()
    String() string
}

func NewRateCalculator

func NewRateCalculator(name string) RateCalculator

type Row

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

func (*Row) AddCell

func (r *Row) AddCell(cell *Cell)

func (*Row) Cells

func (r *Row) Cells() []*Cell

return all cells (considering the col<->idx mapping)

func (*Row) Cols

func (r *Row) Cols() int

return # of cells (considering the col<->idx mapping)

func (*Row) GetCell

func (r *Row) GetCell(idx int) *Cell

return a cell (considering the col<->idx mapping)

type Send_data_type

type Send_data_type func(b []byte) error

type Send_new_file_type

type Send_new_file_type func(key, filename string) error

type Shifter

serialise/deserialise a bunch of variables

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

func NewShifter

func NewShifter(buf []byte) *Shifter

A "Shifter" shifts bytes from a larger number into an array and unshifts it again

func (*Shifter) Array8

func (sh *Shifter) Array8() []byte

next byte is length, followed by uint8s

func (*Shifter) Bytes

func (sh *Shifter) Bytes() []byte

return ALL bytes

func (*Shifter) Error

func (sh *Shifter) Error() error

func (*Shifter) RemainingBytes

func (sh *Shifter) RemainingBytes() []byte

return remaining bytes

func (*Shifter) SetUint32

func (sh *Shifter) SetUint32(pos int, b uint32)

modify the underlying byte array to set a uint32 at a particular address

func (*Shifter) Unshift_uint16

func (sh *Shifter) Unshift_uint16() uint32

func (*Shifter) Unshift_uint32

func (sh *Shifter) Unshift_uint32() uint32

LSB first, MSB last. e.g. 0xAABBCCDD will be shifted into a byte array like so: []byte{0xDD,0xCC,0xBB,0xAA}

func (*Shifter) Unshift_uint64

func (sh *Shifter) Unshift_uint64() uint64

func (*Shifter) Unshift_uint8

func (sh *Shifter) Unshift_uint8() uint8

type SlidingAverage

it is often useful to take the most recent period of time average, for example last minute average. however, it is useful to double-buffer this so that there is always a full sample available. that is where this struct helps. That is, it is guaranteed that the average is always calculated over at least MinAge. Periodically old average numbers are "dropped", so the average is also reflective of fresh values.

The InitialAge value may be set to start providing averages faster than MinAge upon startup.

type SlidingAverage struct {
    InitialAge time.Duration // time to wait before providing the first averages (after startup)
    MinAge     time.Duration // minimum age before a counter is valid
    MinSamples uint64        // minimum number of samples before a counter is valid
    // contains filtered or unexported fields
}

func NewSlidingAverage

func NewSlidingAverage() *SlidingAverage

func (*SlidingAverage) Add

func (sa *SlidingAverage) Add(counter int, a uint64)

func (*SlidingAverage) GetAverage

func (sa *SlidingAverage) GetAverage(counter int) float64

func (*SlidingAverage) GetCounter

func (sa *SlidingAverage) GetCounter(counter int) uint64

get a counter

func (*SlidingAverage) GetCounts

func (sa *SlidingAverage) GetCounts(counter int) uint64

get number of counts

func (*SlidingAverage) GetRate

func (sa *SlidingAverage) GetRate(counter int) float64

per second added rate

type StreamData

the proto must be compatible with this interface

type StreamData interface {
    GetFilename() string
    GetData() []byte
}

type StreamToPacket

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

func NewStreamToPacket

func NewStreamToPacket(start, esc, stop byte) (*StreamToPacket, error)

func (*StreamToPacket) AddByte

func (stp *StreamToPacket) AddByte(b byte) bool

returns true if a complete packet is in the buf

func (*StreamToPacket) ReadPacket

func (stp *StreamToPacket) ReadPacket() []byte

type Table

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

func (*Table) AddBool

func (t *Table) AddBool(b bool) *Table

func (*Table) AddFloat64

func (t *Table) AddFloat64(f float64) *Table

func (*Table) AddHeader

func (t *Table) AddHeader(s string)

func (*Table) AddHeaders

func (t *Table) AddHeaders(s ...string)

func (*Table) AddInt

func (t *Table) AddInt(i int) *Table

func (*Table) AddInt64

func (t *Table) AddInt64(i int64) *Table

func (*Table) AddString

func (t *Table) AddString(s string) *Table

func (*Table) AddStrings

func (t *Table) AddStrings(sts ...string) *Table

func (*Table) AddTimestamp

func (t *Table) AddTimestamp(ts uint32) *Table

func (*Table) AddTimestampWithAge

func (t *Table) AddTimestampWithAge(ts uint32) *Table

func (*Table) AddUint32

func (t *Table) AddUint32(i uint32) *Table

func (*Table) AddUint64

func (t *Table) AddUint64(i uint64) *Table

func (*Table) DisableColumn

func (t *Table) DisableColumn(col int)

column 0..n

func (*Table) EnableAllColumns

func (t *Table) EnableAllColumns()

column 0..n

func (*Table) EnableColumn

func (t *Table) EnableColumn(col int)

column 0..n

func (*Table) GetMaxLen

func (t *Table) GetMaxLen(col int) int

func (*Table) GetPrintingRows

func (t *Table) GetPrintingRows() []*Row

gets "printing" rows. Multi-line text or text that is wrapped will create an extra row

func (*Table) GetRowOrCreate

func (t *Table) GetRowOrCreate(num int) *Row

func (*Table) NewRow

func (t *Table) NewRow()

create a new row (writing will commence at a new row

func (*Table) SetMaxLen

func (t *Table) SetMaxLen(col, width int)

func (*Table) ToCSV

func (t *Table) ToCSV() string

func (*Table) ToPrettyString

func (t *Table) ToPrettyString() string

type TerminalDimensions

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

func TerminalSize

func TerminalSize() (*TerminalDimensions, error)

get the size of the current xterm

func (*TerminalDimensions) Columns

func (td *TerminalDimensions) Columns() int

func (*TerminalDimensions) Rows

func (td *TerminalDimensions) Rows() int

type TextFormatter

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

func (*TextFormatter) ToPrettyString

func (tf *TextFormatter) ToPrettyString() string

type TextPositionFinder

type TextPositionFinder interface {
    // advance position to line after pattern found. return true if found
    FindLineContaining(s string) bool
    // insert line at position
    AddLine(s string)
    // return current content
    Content() []byte
}

func NewTextPositionFinder

func NewTextPositionFinder(ct []byte) TextPositionFinder

a position finder helps finding certain positions in pieces of text. for example: find the '}' after the first occurence of line "foo" and "bar". Each call advances the position further. AddLine inserts a new line at current position.

type TimeoutLock

type TimeoutLock interface {
    Unlock()
    Lock()
    LockWithTimeout(time.Duration) bool // true if lock was acquired
}

func NewTimeoutLock

func NewTimeoutLock(name string) TimeoutLock

create a new lock which can be used like sync.Mutex but also adds LockWithTimeout()

type Value

type Value struct {
    Integer int
}