From 1e1f337ecd4342fecd7214989a6a5da467527a07 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Sun, 20 Oct 2024 18:31:12 +0700 Subject: [PATCH] API: status --- CHANGELOG.md | 4 +++ docs/API.md | 14 ++++++++++- internal/models/models.go | 9 +++++++ internal/web/api.go | 39 ++++++++++++++++++++++++++++-- internal/web/public/js/notify.js | 4 +-- internal/web/scan-routine.go | 2 +- internal/web/templates/config.html | 2 +- internal/web/webgui.go | 15 ++++++------ 8 files changed, 75 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0690ae6..678054c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. ## [v2.0.4] - 2024- +### Added +- Notification test [#147](https://github.com/aceberg/WatchYourLAN/issues/147) +- API status [#148](https://github.com/aceberg/WatchYourLAN/issues/148) + ### Fixed - [#101](https://github.com/aceberg/WatchYourLAN/issues/101) - The same problem for Theme, Color mode, Log level diff --git a/docs/API.md b/docs/API.md index c0bbcbb..61b2c50 100644 --- a/docs/API.md +++ b/docs/API.md @@ -39,4 +39,16 @@ Edit host with ID `id`. Can change `name`. `known` is optional, when set to `tog ```http GET /api/host/del/:id ``` -Remove host with ID `id`. \ No newline at end of file +Remove host with ID `id`. + + +```http +GET /api/notify_test +``` +Send test notification. + + +```http +GET /api/status/*iface +``` +Show status (Total number of hosts, online/offline, known/unknown). The `iface` parameter is optional and shows status for one interface only. For all interfaces just call `/api/status/`. \ No newline at end of file diff --git a/internal/models/models.go b/internal/models/models.go index 8069c72..a89c946 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -44,6 +44,15 @@ type Host struct { Now int `db:"NOW"` } +// Stat - status +type Stat struct { + Total int + Online int + Offline int + Known int + Unknown int +} + // GuiData - all data sent to html page type GuiData struct { Config Conf diff --git a/internal/web/api.go b/internal/web/api.go index b46fbee..bd88f00 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -90,10 +90,45 @@ func apiEdit(c *gin.Context) { c.IndentedJSON(http.StatusOK, "OK") } -func testNotifyHandler(c *gin.Context) { +func apiNotifyTest(c *gin.Context) { - msg := "Test notification from WatchYourLAN" + msg := "WatchYourLAN: test notification" notify.Shout(msg, appConfig.ShoutURL) c.Status(http.StatusOK) } + +func apiStatus(c *gin.Context) { + var status models.Stat + var searchHosts []models.Host + + iface := c.Param("iface") + iface = iface[1:] + + if iface != "" { + for _, host := range allHosts { + if iface == host.Iface { + searchHosts = append(searchHosts, host) + } + } + } else { + searchHosts = allHosts + } + + for _, host := range searchHosts { + status.Total = status.Total + 1 + + if host.Known > 0 { + status.Known = status.Known + 1 + } else { + status.Unknown = status.Unknown + 1 + } + if host.Now > 0 { + status.Online = status.Online + 1 + } else { + status.Offline = status.Offline + 1 + } + } + + c.IndentedJSON(http.StatusOK, status) +} diff --git a/internal/web/public/js/notify.js b/internal/web/public/js/notify.js index 5e9faaf..b3c7291 100644 --- a/internal/web/public/js/notify.js +++ b/internal/web/public/js/notify.js @@ -1,5 +1,5 @@ async function testNotifications() { - const url = '/api/test_notify' - await fetch(url, { method: 'post' }) + const url = '/api/notify_test'; + await fetch(url); } diff --git a/internal/web/scan-routine.go b/internal/web/scan-routine.go index 1dfa5af..0d9a0a9 100644 --- a/internal/web/scan-routine.go +++ b/internal/web/scan-routine.go @@ -77,7 +77,7 @@ func compareHosts(foundHosts []models.Host) { fHost.Name, fHost.DNS = updateDNS(fHost) - msg := fmt.Sprintf("Unknown host Names: '%s', IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.DNS, fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface) + msg := fmt.Sprintf("WatchYourLAN: unknown host found. Names: '%s', IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.DNS, fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface) slog.Warn(msg) notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr diff --git a/internal/web/templates/config.html b/internal/web/templates/config.html index 6b84498..1d001c0 100644 --- a/internal/web/templates/config.html +++ b/internal/web/templates/config.html @@ -44,7 +44,7 @@ - + diff --git a/internal/web/webgui.go b/internal/web/webgui.go index 345ddc5..908fb6d 100644 --- a/internal/web/webgui.go +++ b/internal/web/webgui.go @@ -46,13 +46,14 @@ func Gui(dirPath, nodePath string) { router.StaticFS("/fs/", http.FS(pubFS)) // public - router.GET("/api/all", apiAll) // api.go - router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go - router.GET("/api/history/*mac", apiHistory) // api.go - router.GET("/api/host/:id", apiHost) // api.go - router.GET("/api/host/del/:id", apiHostDel) // api.go - router.GET("/api/port/:addr/:port", apiPort) // api.go - router.POST("/api/test_notify", testNotifyHandler) // api.go + router.GET("/api/all", apiAll) // api.go + router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go + router.GET("/api/history/*mac", apiHistory) // api.go + router.GET("/api/host/:id", apiHost) // api.go + router.GET("/api/host/del/:id", apiHostDel) // api.go + router.GET("/api/notify_test", apiNotifyTest) // api.go + router.GET("/api/port/:addr/:port", apiPort) // api.go + router.GET("/api/status/*iface", apiStatus) // api.go router.GET("/", indexHandler) // index.go router.GET("/history/", historyHandler) // index.go