github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/client/statistics.go (about)

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