API: status

This commit is contained in:
aceberg 2024-10-20 18:31:12 +07:00
parent 9132401ce0
commit 1e1f337ecd
8 changed files with 75 additions and 14 deletions

View file

@ -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

View file

@ -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`.
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/`.

View file

@ -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

View file

@ -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)
}

View file

@ -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);
}

View file

@ -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

View file

@ -44,7 +44,7 @@
</tr>
<tr>
<td><button type="submit" class="btn btn-primary">Save</button></td>
<td><button type="button" style="float: right;" class="btn btn-success" onclick="testNotifications()">Test notifications</button></td>
<td><button type="button" style="float: right;" class="btn btn-info" onclick="testNotifications()">Test notification</button></td>
<td></td>
</tr>
</form>

View file

@ -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