Skip to content

Commit

Permalink
DB: history
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Oct 6, 2023
1 parent a0173f9 commit b8f7d3b
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 51 deletions.
51 changes: 26 additions & 25 deletions internal/db/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@ package db

import (
"fmt"
"log"
"os"

"github.com/aceberg/WatchYourLAN/internal/models"
)

// Create - create DB if not exists
func Create(path string) {
if _, err := os.Stat(path); err == nil {
log.Println("INFO: DB exists")
} else {
sqlStatement := `CREATE TABLE "now" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"NAME" TEXT NOT NULL,
"IP" TEXT,
"MAC" TEXT,
"HW" TEXT,
"DATE" TEXT,
"KNOWN" INTEGER DEFAULT 0,
"NOW" INTEGER DEFAULT 0
);`
dbExec(path, sqlStatement)
log.Println("INFO: Table created!")
}
}

// SetNow - mark all hosts as offline
func SetNow(path string) {
sqlStatement := `UPDATE "now" set NOW = '0';`
sqlStatement := `CREATE TABLE IF NOT EXISTS "now" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"NAME" TEXT NOT NULL,
"IP" TEXT,
"MAC" TEXT,
"HW" TEXT,
"DATE" TEXT,
"KNOWN" INTEGER DEFAULT 0,
"NOW" INTEGER DEFAULT 0
);`
dbExec(path, sqlStatement)

sqlStatement = `CREATE TABLE IF NOT EXISTS "history" (
"ID" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"HOST" INTEGER DEFAULT 0,
"NAME" TEXT NOT NULL,
"IP" TEXT,
"MAC" TEXT,
"HW" TEXT,
"DATE" TEXT,
"KNOWN" INTEGER DEFAULT 0,
"STATE" INTEGER DEFAULT 0
);`
dbExec(path, sqlStatement)
}

Expand All @@ -41,7 +42,7 @@ func Insert(path string, oneHost models.Host) {
sqlStatement := `INSERT INTO "now" (NAME, IP, MAC, HW, DATE, KNOWN, NOW)
VALUES ('%s','%s','%s','%s','%s','%d','%d');`
sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Name, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now)
//fmt.Println("Insert statement:", sqlStatement)

dbExec(path, sqlStatement)
}

Expand All @@ -54,12 +55,12 @@ func Update(path string, oneHost models.Host) {
KNOWN = '%d', NOW = '%d'
WHERE ID = '%d';`
sqlStatement = fmt.Sprintf(sqlStatement, oneHost.Name, oneHost.IP, oneHost.Mac, oneHost.Hw, oneHost.Date, oneHost.Known, oneHost.Now, oneHost.ID)
//fmt.Println("Update statement:", sqlStatement)

dbExec(path, sqlStatement)
}

// Delete - delete host from DB
func Delete(path string, id uint16) {
func Delete(path string, id int) {
sqlStatement := `DELETE FROM "now" WHERE ID='%d';`
sqlStatement = fmt.Sprintf(sqlStatement, id)
dbExec(path, sqlStatement)
Expand Down
31 changes: 31 additions & 0 deletions internal/db/hist-edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package db

import (
"fmt"

"github.com/aceberg/WatchYourLAN/internal/models"
)

// InsertHist - insert history into table
func InsertHist(path string, hist models.History) {
hist.Name = quoteStr(hist.Name)
hist.Hw = quoteStr(hist.Hw)
sqlStatement := `INSERT INTO "history" (HOST, NAME, IP, MAC, HW, DATE, KNOWN, STATE)
VALUES ('%d','%s','%s','%s','%s','%s','%d','%d');`
sqlStatement = fmt.Sprintf(sqlStatement, hist.Host, hist.Name, hist.IP, hist.Mac, hist.Hw, hist.Date, hist.Known, hist.State)
//fmt.Println("Insert statement:", sqlStatement)
dbExec(path, sqlStatement)
}

// DeleteHist - delete history from DB
func DeleteHist(path string, id int) {
sqlStatement := `DELETE FROM "history" WHERE ID='%d';`
sqlStatement = fmt.Sprintf(sqlStatement, id)
dbExec(path, sqlStatement)
}

// ClearHist - delete all history from table
func ClearHist(path string) {
sqlStatement := `DELETE FROM "history";`
dbExec(path, sqlStatement)
}
21 changes: 20 additions & 1 deletion internal/db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,27 @@ func Select(path string) (dbHosts []models.Host) {
mu.Unlock()

if err != nil {
log.Fatal("ERROR: db_select: ", err)
log.Fatal("ERROR: db.Select: ", err)
}

return dbHosts
}

// SelectHist - select all history
func SelectHist(path string) (hist []models.History) {

sqlStatement := `SELECT * FROM "history" ORDER BY DATE DESC`

mu.Lock()
db, _ := sqlx.Connect("sqlite", path)
defer db.Close()

err := db.Select(&hist, sqlStatement)
mu.Unlock()

if err != nil {
log.Fatal("ERROR: db.SelectHist: ", err)
}

return hist
}
29 changes: 16 additions & 13 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ type Conf struct {

// Host - one host
type Host struct {
ID uint16 `db:"ID"`
ID int `db:"ID"`
Name string `db:"NAME"`
IP string `db:"IP"`
Mac string `db:"MAC"`
Hw string `db:"HW"`
Date string `db:"DATE"`
Known uint16 `db:"KNOWN"`
Now uint16 `db:"NOW"`
Known int `db:"KNOWN"`
Now int `db:"NOW"`
}

// // History for hosts
// type History struct {
// ID int
// HostID int
// Name string
// IP string
// Date string
// State bool
// }
// History for hosts
type History struct {
ID int `db:"ID"`
Host int `db:"HOST"`
Name string `db:"NAME"`
IP string `db:"IP"`
Mac string `db:"MAC"`
Hw string `db:"HW"`
Date string `db:"DATE"`
Known int `db:"KNOWN"`
State int `db:"STATE"`
}

// GuiData - all data sent to html page
type GuiData struct {
Expand All @@ -50,5 +53,5 @@ type GuiData struct {
Themes []string
Version string
Auth auth.Conf
// HistLog []History
Hist []History
}
38 changes: 37 additions & 1 deletion internal/scan/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import (
"github.com/aceberg/WatchYourLAN/internal/notify"
)

func hostsCompare(appConfig models.Conf) {
func hostsCompare() {

for _, oneHost := range dbHosts {

host, exists := foundHostsMap[oneHost.Mac]

if exists && (appConfig.IgnoreIP == "yes" || host.IP == oneHost.IP) {

oneHost.Date = host.Date

if oneHost.Now == 0 {
histAdd(oneHost, 1)
}

oneHost.Now = 1

delete(foundHostsMap, oneHost.Mac)
Expand All @@ -26,6 +32,8 @@ func hostsCompare(appConfig models.Conf) {
} else if oneHost.Now == 1 {
oneHost.Now = 0
db.Update(appConfig.DbPath, oneHost)

histAdd(oneHost, 0)
}
}

Expand All @@ -36,5 +44,33 @@ func hostsCompare(appConfig models.Conf) {
notify.Shoutrrr(msg, appConfig.ShoutURL) // Notify through Shoutrrr

db.Insert(appConfig.DbPath, oneHost)

histAdd(oneHost, 1)
}
}

func histAdd(oneHost models.Host, state int) {
var history models.History

if oneHost.ID == 0 {
dbHosts = db.Select(appConfig.DbPath)

for _, host := range dbHosts {
if host.IP == oneHost.IP && host.Mac == oneHost.Mac {
oneHost.ID = host.ID
break
}
}
}

history.Host = oneHost.ID
history.Name = oneHost.Name
history.IP = oneHost.IP
history.Mac = oneHost.Mac
history.Hw = oneHost.Hw
history.Date = oneHost.Date
history.Known = oneHost.Known
history.State = state

db.InsertHist(appConfig.DbPath, history)
}
7 changes: 5 additions & 2 deletions internal/scan/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
"github.com/aceberg/WatchYourLAN/internal/models"
)

var appConfig models.Conf
var dbHosts, structHosts []models.Host
var foundHostsMap map[string]models.Host

// Start - start arp-scan goroutine
func Start(appConfig models.Conf, quit chan bool) {
func Start(config models.Conf, quit chan bool) {
var lastDate time.Time

appConfig = config
db.Create(appConfig.DbPath)
dbHosts = db.Select(appConfig.DbPath)

for {
select {
Expand All @@ -29,7 +32,7 @@ func Start(appConfig models.Conf, quit chan bool) {
dbHosts = db.Select(appConfig.DbPath)

toMap()
hostsCompare(appConfig) // compare.go
hostsCompare() // compare.go

lastDate = time.Now()
}
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions internal/web/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package web

import (
"net/http"

"github.com/aceberg/WatchYourLAN/internal/db"
"github.com/aceberg/WatchYourLAN/internal/models"
)

func historyHandler(w http.ResponseWriter, r *http.Request) {
var guiData models.GuiData

guiData.Config = AppConfig
guiData.Hist = db.SelectHist(AppConfig.DbPath)

execTemplate(w, "history", guiData)
}
6 changes: 2 additions & 4 deletions internal/web/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func delHandler(w http.ResponseWriter, r *http.Request) {

log.Println("INFO: delete host ID =", id)

db.Delete(AppConfig.DbPath, uint16(id))
db.Delete(AppConfig.DbPath, id)

http.Redirect(w, r, "/", 302)
}
Expand All @@ -35,10 +35,8 @@ func hostHandler(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(idStr)
check.IfError(err)

id16 := uint16(id)

for _, oneHost := range AllHosts {
if id16 == oneHost.ID {
if id == oneHost.ID {
host = oneHost
break
}
Expand Down
3 changes: 3 additions & 0 deletions internal/web/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<li class="nav-item">
<a class="nav-link active" href="/line?state=off">Offline</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/history/">History</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/config/">Config</a>
</li>
Expand Down
45 changes: 45 additions & 0 deletions internal/web/templates/history.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{{ define "history"}}
<!--Auto refresh (seconds)-->
<meta http-equiv="refresh" content="{{ .Config.Timeout }}">
<body>
<div class="container mt-3">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>IP</th>
<th>Mac</th>
<th>Hardware</th>
<th>Last seen</th>
<th>Known</th>
<th>State</th>
</tr>
{{ range .Hist }}
<tr>
<td><a href="/host?id={{ .Host }}">{{ .Host }}</a></td>
<td>{{ .Name }}</td>
<td><a href="http://{{ .IP }}" target="_blank">{{ .IP }}</a></td>
<td><a href="/host?id={{ .Host }}">{{ .Mac }}</a></td>
<td>{{ .Hw }}</td>
<td>{{ .Date }}</td>
<td>
{{ if eq .Known 1 }}
Yes
{{ else }}
No
{{ end }}
</td>
<td>
{{ if eq .State 1 }}
<i class="bi bi-circle-fill text-success"></i>
{{ else }}
<i class="bi bi-circle-fill text-danger"></i>
{{ end }}
</td>
</tr>
{{ end }}
</table>
</div>

{{ template "footer" }}
{{ end }}
2 changes: 1 addition & 1 deletion internal/web/templates/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<td>Domain name</td>
<td>
{{ range .Themes }}
{{ . }}
<a href="http://{{ . }}" target="_blank">{{ . }}</a>
{{ end }}
</td>
</tr>
Expand Down
Loading

0 comments on commit b8f7d3b

Please sign in to comment.