feat: add bar for traffic usage

This commit is contained in:
Yiyang Kang 2022-11-21 15:17:02 +08:00
parent b429137d20
commit 3fc427fae4
2 changed files with 46 additions and 14 deletions

37
bot.go
View file

@ -2,10 +2,12 @@ package main
import (
"fmt"
"math"
"strings"
"time"
"github.com/go-errors/errors"
"github.com/samber/lo"
tele "gopkg.in/telebot.v3"
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/stats"
@ -96,11 +98,12 @@ func handleTrafficCmd(c tele.Context) error {
// This month's traffic, if present
if len(monthlyTraffic) > 0 {
row := monthlyTraffic[len(monthlyTraffic)-1]
responseParts = append(
responseParts,
fmt.Sprintf("This month so far:` %s`", fmtTraffic(row)),
)
bar := fmt.Sprintf("\n```\n%s\n```", drawBarForTrafficRecord(row))
responseParts = append(responseParts, bar)
}
var respText string
@ -114,12 +117,8 @@ func handleTrafficCmd(c tele.Context) error {
}
func fmtTraffic(r stats.VnstatTrafficRecord) string {
biggest := r.Rx
if r.Tx > biggest {
biggest = r.Tx
}
return fmt.Sprintf("%.2f GiB", float64(biggest)/1024/1024/1024)
effective := lo.Max([]uint64{r.Rx, r.Tx})
return fmt.Sprintf("%.2f GiB", float64(effective)/1024/1024/1024)
}
func handleUserInfoCmd(c tele.Context) error {
@ -174,3 +173,27 @@ func handleChatInfoCmd(c tele.Context) error {
}
return c.Reply(strings.Join(replyText, "\n"), &tele.SendOptions{ParseMode: tele.ModeMarkdown})
}
func drawBar(progress float64, length int) string {
barChars := []rune("░▒▓█")
if length <= 0 {
return ""
}
step := 1 / float64(length)
buf := make([]rune, length)
for i := 0; i < length; i++ {
fill := (progress - float64(i)*step) / step
fill = math.Min(math.Max(fill, 0), 1)
idx := int(math.Round(fill * float64(len(barChars)-1)))
buf[i] = barChars[idx]
}
return string(buf)
}
func drawBarForTrafficRecord(r stats.VnstatTrafficRecord) string {
effective := lo.Max([]uint64{r.Rx, r.Tx})
max := config.MonthlyTrafficLimitGiB
ratio := float64(effective) / 1024 / 1024 / 1024 / float64(max)
return fmt.Sprintf("%s %2.0f%%", drawBar(ratio, 16), ratio*100)
}