feat(collectors) basic implementation of boring collectors; sender via http(s) to hard coded api route
This commit is contained in:
82
internal/aggregate/window.go
Normal file
82
internal/aggregate/window.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package aggregate
|
||||
|
||||
import "time"
|
||||
|
||||
type Window struct {
|
||||
start time.Time
|
||||
|
||||
cpuSum float64
|
||||
cpuCount uint64
|
||||
|
||||
memSum float64
|
||||
memCount uint64
|
||||
|
||||
diskSum float64
|
||||
diskCount uint64
|
||||
|
||||
uptime uint64
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
Timestamp time.Time
|
||||
|
||||
CPUAvg float64
|
||||
MemAvg float64
|
||||
DiskAvg float64
|
||||
|
||||
Uptime uint64
|
||||
}
|
||||
|
||||
func NewWindow() *Window {
|
||||
return &Window{
|
||||
start: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Window) AddCPU(value float64) {
|
||||
w.cpuSum += value
|
||||
w.cpuCount++
|
||||
}
|
||||
|
||||
func (w *Window) AddMemory(value float64) {
|
||||
w.memSum += value
|
||||
w.memCount++
|
||||
}
|
||||
|
||||
func (w *Window) SetUptime(value uint64) {
|
||||
w.uptime = value
|
||||
}
|
||||
|
||||
func (w *Window) Ready(windowDuration time.Duration) bool {
|
||||
return time.Since(w.start) >= windowDuration
|
||||
}
|
||||
|
||||
func (w *Window) Snapshot() Snapshot {
|
||||
var cpuAvg float64
|
||||
if w.cpuCount > 0 {
|
||||
cpuAvg = w.cpuSum / float64(w.cpuCount)
|
||||
}
|
||||
|
||||
var memAvg float64
|
||||
if w.memCount > 0 {
|
||||
memAvg = w.memSum / float64(w.memCount)
|
||||
}
|
||||
|
||||
var diskAvg float64
|
||||
if w.diskCount > 0 {
|
||||
diskAvg = w.diskSum / float64(w.diskCount)
|
||||
}
|
||||
|
||||
return Snapshot{
|
||||
Timestamp: time.Now(),
|
||||
CPUAvg: cpuAvg,
|
||||
MemAvg: memAvg,
|
||||
DiskAvg: diskAvg,
|
||||
Uptime: w.uptime,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Window) AddDisk(value float64) {
|
||||
w.diskSum += value
|
||||
w.diskCount++
|
||||
}
|
||||
Reference in New Issue
Block a user