mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 19:14:01 +03:00
Rework stats
This commit is contained in:
+41
-14
@@ -2,30 +2,57 @@ package stats
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
statsd "github.com/smira/go-statsd"
|
||||
)
|
||||
|
||||
type streamInfo struct {
|
||||
createdAt time.Time
|
||||
clientIP net.IP
|
||||
remoteIP net.IP
|
||||
dc int
|
||||
bytesSentToTelegram uint
|
||||
bytesRecvFromTelegram uint
|
||||
startTime time.Time
|
||||
tags map[string]string
|
||||
}
|
||||
|
||||
func (s *streamInfo) GetClientIPType() string {
|
||||
return s.getIPType(s.clientIP)
|
||||
func (s *streamInfo) SetStartTime(tme time.Time) {
|
||||
s.startTime = tme
|
||||
}
|
||||
|
||||
func (s *streamInfo) GetRemoteIPType() string {
|
||||
return s.getIPType(s.remoteIP)
|
||||
func (s *streamInfo) SetClientIP(ip net.IP) {
|
||||
if ip.To4() != nil {
|
||||
s.tags[TagIPFamily] = TagIPFamilyIPv4
|
||||
} else {
|
||||
s.tags[TagIPFamily] = TagIPFamilyIPv6
|
||||
}
|
||||
}
|
||||
|
||||
func (s *streamInfo) getIPType(ip net.IP) string {
|
||||
if ip.To4() == nil {
|
||||
return TagIPTypeIPv6
|
||||
func (s *streamInfo) SetTelegramIP(ip net.IP) {
|
||||
s.tags[TagTelegramIP] = ip.String()
|
||||
}
|
||||
|
||||
func (s *streamInfo) SetDC(dc int) {
|
||||
s.tags[TagDC] = strconv.Itoa(dc)
|
||||
}
|
||||
|
||||
func (s *streamInfo) V(key string) string {
|
||||
return s.tags[key]
|
||||
}
|
||||
|
||||
func (s *streamInfo) TV(key string) statsd.Tag {
|
||||
return statsd.StringTag(key, s.tags[key])
|
||||
}
|
||||
|
||||
func (s *streamInfo) Reset() {
|
||||
s.startTime = time.Time{}
|
||||
|
||||
for k := range s.tags {
|
||||
delete(s.tags, k)
|
||||
}
|
||||
}
|
||||
|
||||
func getDirection(isRead bool) string {
|
||||
if isRead { // for telegram
|
||||
return TagDirectionToClient
|
||||
}
|
||||
|
||||
return TagIPTypeIPv4
|
||||
return TagDirectionFromClient
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user