mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-13 04:24:09 +08:00
optimize stats
This commit is contained in:
@@ -9,9 +9,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"golang.org/x/text/language"
|
|
||||||
"golang.org/x/text/message"
|
|
||||||
|
|
||||||
"github.com/xjasonlyu/tun2socks/common/log"
|
"github.com/xjasonlyu/tun2socks/common/log"
|
||||||
"github.com/xjasonlyu/tun2socks/common/stats"
|
"github.com/xjasonlyu/tun2socks/common/stats"
|
||||||
)
|
)
|
||||||
@@ -48,14 +45,13 @@ func (s *simpleSessionStater) Start() error {
|
|||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
p := message.NewPrinter(language.English)
|
|
||||||
tablePrint := func(w io.Writer, sessions []*stats.Session) {
|
tablePrint := func(w io.Writer, sessions []*stats.Session) {
|
||||||
// Sort by session start time.
|
// Sort by session start time.
|
||||||
sort.Slice(sessions, func(i, j int) bool {
|
sort.Slice(sessions, func(i, j int) bool {
|
||||||
return sessions[i].SessionStart.Sub(sessions[j].SessionStart) < 0
|
return sessions[i].SessionStart.Sub(sessions[j].SessionStart) < 0
|
||||||
})
|
})
|
||||||
_, _ = fmt.Fprintf(w, "<table style=\"border=4px solid\">")
|
_, _ = fmt.Fprintf(w, "<table style=\"border=4px solid\">")
|
||||||
_, _ = fmt.Fprintf(w, "<tr><td>Process Name</td><td>Network</td><td>Date</td><td>Duration</td><td>Client Addr</td><td>Target Addr</td><td>Upload Bytes</td><td>Download Bytes</td></tr>")
|
_, _ = fmt.Fprintf(w, "<tr><td>Process Name</td><td>Network</td><td>Date</td><td>Duration</td><td>Client Addr</td><td>Target Addr</td><td>Upload</td><td>Download</td></tr>")
|
||||||
sort.Slice(sessions, func(i, j int) bool {
|
sort.Slice(sessions, func(i, j int) bool {
|
||||||
return sessions[i].SessionStart.After(sessions[j].SessionStart)
|
return sessions[i].SessionStart.After(sessions[j].SessionStart)
|
||||||
})
|
})
|
||||||
@@ -68,8 +64,8 @@ func (s *simpleSessionStater) Start() error {
|
|||||||
duration(sess.SessionStart, sess.SessionClose),
|
duration(sess.SessionStart, sess.SessionClose),
|
||||||
sess.ClientAddr,
|
sess.ClientAddr,
|
||||||
sess.TargetAddr,
|
sess.TargetAddr,
|
||||||
p.Sprintf("%d", atomic.LoadInt64(&sess.UploadBytes)),
|
byteCountSI(atomic.LoadInt64(&sess.UploadBytes)),
|
||||||
p.Sprintf("%d", atomic.LoadInt64(&sess.DownloadBytes)),
|
byteCountSI(atomic.LoadInt64(&sess.DownloadBytes)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
_, _ = fmt.Fprintf(w, "</table>")
|
_, _ = fmt.Fprintf(w, "</table>")
|
||||||
|
@@ -110,3 +110,31 @@ func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func byteCountSI(b int64) string {
|
||||||
|
const unit = 1000
|
||||||
|
if b < unit {
|
||||||
|
return fmt.Sprintf("%d B", b)
|
||||||
|
}
|
||||||
|
div, exp := int64(unit), 0
|
||||||
|
for n := b / unit; n >= unit; n /= unit {
|
||||||
|
div *= unit
|
||||||
|
exp++
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.1f %cB",
|
||||||
|
float64(b)/float64(div), "kMGTPE"[exp])
|
||||||
|
}
|
||||||
|
|
||||||
|
func byteCountIEC(b int64) string {
|
||||||
|
const unit = 1024
|
||||||
|
if b < unit {
|
||||||
|
return fmt.Sprintf("%d B", b)
|
||||||
|
}
|
||||||
|
div, exp := int64(unit), 0
|
||||||
|
for n := b / unit; n >= unit; n /= unit {
|
||||||
|
div *= unit
|
||||||
|
exp++
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.1f %ciB",
|
||||||
|
float64(b)/float64(div), "KMGTPE"[exp])
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user