github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/client/statistics.go (about)

     1  package client
     2  
     3  // from https://github.com/Mrs4s/MiraiGo/blob/master/client/statistics.go
     4  
     5  import (
     6  	"bytes"
     7  	"strconv"
     8  	"sync/atomic"
     9  )
    10  
    11  type Statistics struct {
    12  	PacketReceived  atomic.Uint64
    13  	PacketSent      atomic.Uint64
    14  	PacketLost      atomic.Uint64
    15  	MessageReceived atomic.Uint64
    16  	MessageSent     atomic.Uint64
    17  	LastMessageTime atomic.Int64
    18  	DisconnectTimes atomic.Uint32
    19  	LostTimes       atomic.Uint32
    20  }
    21  
    22  func (c *QQClient) GetStatistics() *Statistics {
    23  	return &c.stat
    24  }
    25  
    26  // MarshalJSON encodes the wrapped statistics into JSON.
    27  func (m *Statistics) MarshalJSON() ([]byte, error) {
    28  	var w bytes.Buffer
    29  	w.Grow(256)
    30  	w.WriteString(`{"packet_received":`)
    31  	w.WriteString(strconv.FormatUint(m.PacketReceived.Load(), 10))
    32  	w.WriteString(`,"packet_sent":`)
    33  	w.WriteString(strconv.FormatUint(m.PacketSent.Load(), 10))
    34  	w.WriteString(`,"packet_lost":`)
    35  	w.WriteString(strconv.FormatUint(m.PacketLost.Load(), 10))
    36  	w.WriteString(`,"message_received":`)
    37  	w.WriteString(strconv.FormatUint(m.MessageReceived.Load(), 10))
    38  	w.WriteString(`,"message_sent":`)
    39  	w.WriteString(strconv.FormatUint(m.MessageSent.Load(), 10))
    40  	w.WriteString(`,"disconnect_times":`)
    41  	w.WriteString(strconv.FormatUint(uint64(m.DisconnectTimes.Load()), 10))
    42  	w.WriteString(`,"lost_times":`)
    43  	w.WriteString(strconv.FormatUint(uint64(m.LostTimes.Load()), 10))
    44  	w.WriteString(`,"last_message_time":`)
    45  	w.WriteString(strconv.FormatInt(m.LastMessageTime.Load(), 10))
    46  	w.WriteByte('}')
    47  	return w.Bytes(), nil
    48  }