123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package analytic
- import (
- "context"
- "time"
- "github.com/uozi-tech/cosy/logger"
- )
- type Usage[T uint64 | float64] struct {
- Time time.Time `json:"x"`
- Usage T `json:"y"`
- }
- var (
- CpuUserRecord []Usage[float64]
- CpuTotalRecord []Usage[float64]
- NetRecvRecord []Usage[uint64]
- NetSentRecord []Usage[uint64]
- DiskWriteRecord []Usage[uint64]
- DiskReadRecord []Usage[uint64]
- LastDiskWrites uint64
- LastDiskReads uint64
- LastNetSent uint64
- LastNetRecv uint64
- )
- func init() {
- network, err := GetNetworkStat()
- if err != nil {
- logger.Error(err)
- }
- LastNetRecv = network.BytesRecv
- LastNetSent = network.BytesSent
- LastDiskReads, LastDiskWrites = getTotalDiskIO()
- now := time.Now()
- // init record slices
- for i := 100; i > 0; i-- {
- uf := Usage[float64]{Time: now.Add(time.Duration(-i) * time.Second), Usage: 0}
- CpuUserRecord = append(CpuUserRecord, uf)
- CpuTotalRecord = append(CpuTotalRecord, uf)
- u := Usage[uint64]{Time: now.Add(time.Duration(-i) * time.Second), Usage: 0}
- NetRecvRecord = append(NetRecvRecord, u)
- NetSentRecord = append(NetSentRecord, u)
- DiskWriteRecord = append(DiskWriteRecord, u)
- DiskReadRecord = append(DiskReadRecord, u)
- }
- }
- func RecordServerAnalytic(ctx context.Context) {
- logger.Info("RecordServerAnalytic Started")
- for {
- select {
- case <-ctx.Done():
- logger.Info("RecordServerAnalytic Stopped")
- return
- case <-time.After(1 * time.Second):
- now := time.Now()
- recordCpu(now) // this func will spend more than 1 second.
- recordNetwork(now)
- recordDiskIO(now)
- }
- }
- }
|