ping graph

This commit is contained in:
2026-02-20 06:28:31 +00:00
parent 87bdbc440a
commit 1dd3f09fed
3 changed files with 84 additions and 12 deletions

61
main.go
View File

@@ -10,7 +10,9 @@ import (
"github.com/diamondburned/gotk4/pkg/gio/v2"
"github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotk4/pkg/gdkpixbuf/v2"
"github.com/gen2brain/beeep"
"github.com/go-analyze/charts"
"path/filepath"
"github.com/BurntSushi/toml"
@@ -60,6 +62,7 @@ var mucmembers sync.Map
// stores devices of users
var userdevices sync.Map
var pingTimes = [][]float64{}
var clientAssets map[string]gdk.Paintabler = make(map[string]gdk.Paintabler)
@@ -79,6 +82,7 @@ func init() {
}
func main() {
pingTimes = append(pingTimes, []float64{})
p, err := ensureConfig()
if err != nil {
panic(err)
@@ -110,7 +114,7 @@ func main() {
Jid: loadedConfig.Username + "/lambda." + str,
Credential: xmpp.Password(loadedConfig.Password),
Insecure: loadedConfig.Insecure,
StreamLogger: os.Stdout,
// StreamLogger: os.Stdout,
StreamManagementEnable: true,
}
router := xmpp.NewRouter()
@@ -364,7 +368,9 @@ func main() {
_ = <-mychan
pingStatus.RemoveCSSClass("pending")
pingStatus.SetText(fmt.Sprintf("%d ms", time.Since(before)/time.Millisecond))
delay := time.Since(before) / time.Millisecond
pingStatus.SetText(fmt.Sprintf("%d ms", delay))
pingTimes[0] = append(pingTimes[0], float64(delay))
}
}()
@@ -652,7 +658,56 @@ func activate(app *gtk.Application) {
statBar.Append(cBox)
pBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
pBox.SetTooltipText("Ping between you and your XMPP server")
pBox.SetTooltipText("Ping between you and your XMPP server\nRight-click to see graph")
gesture := gtk.NewGestureClick()
gesture.SetButton(3)
gesture.Connect("pressed", func() {
opt := charts.NewLineChartOptionWithData(pingTimes)
opt.Title = charts.TitleOption{
Text: "Server latency",
}
/*
opt.XAxis.Labels = []string{
// The 7 labels here match to the 7 values above
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
}*/
opt.Legend = charts.LegendOption{
SeriesNames: []string{
"Ping (ms)",
},
}
opt.StrokeSmoothingTension = 0.9
p := charts.NewPainter(charts.PainterOptions{
Width: 600,
Height: 400,
})
err := p.LineChart(opt)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
loader := gdkpixbuf.NewPixbufLoader()
loader.Write(buf)
loader.Close()
i := gtk.NewPictureForPaintable(gdk.NewTextureForPixbuf(loader.Pixbuf()))
win := gtk.NewWindow()
win.SetDefaultSize(600, 400)
win.SetTitle("Server latency")
box := gtk.NewBox(gtk.OrientationVertical, 0)
box.Append(i)
win.SetChild(box)
win.SetVisible(true)
})
pBox.AddController(gesture)
i := (gtk.NewImageFromPaintable(clientAssets["chart_bar"]))
i.AddCSSClass("icon")
pBox.Append(i)