Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a03baca498 | |||
| 1904bfd265 | |||
| 2d488ffdcc | |||
| f4c5834f0c | |||
| db72e8fb7d | |||
| 6f93f2ff50 | |||
| 2913d510bd | |||
| 8bd2c87fc6 | |||
| ccf373b863 | |||
| c5957e38db | |||
| 5be599589a |
@@ -90,3 +90,4 @@ changelog:
|
||||
- '^docs:'
|
||||
- '^test:'
|
||||
- '^[Bb]ump'
|
||||
- '^[Cc]lean'
|
||||
|
||||
@@ -32,6 +32,11 @@ A self-hosted bookmark database with full-text page content search.
|
||||
mountpoint.
|
||||
* Run `docker-compose up -d`
|
||||
|
||||
To upgrade:
|
||||
|
||||
* `docker-compose pull`
|
||||
* `docker-compose up -d`
|
||||
|
||||
## Packages (deb/rpm)
|
||||
|
||||
* Download the .deb or .rpm from the releases
|
||||
|
||||
@@ -25,6 +25,7 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
bmm := db.NewBookmarkManager(&dbh)
|
||||
cmm := db.NewConfigManager(&dbh)
|
||||
|
||||
@@ -35,6 +36,17 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// update stats every 5 minutes
|
||||
go func() {
|
||||
for {
|
||||
err := dbh.UpdateBookmarkStats()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
time.Sleep(time.Minute * 5)
|
||||
}
|
||||
}()
|
||||
|
||||
log.Printf("linkwallet version %s starting", version.VersionInfo.Local.Tag)
|
||||
|
||||
server := web.Create(bmm, cmm)
|
||||
|
||||
@@ -100,7 +100,6 @@ func (m *BookmarkManager) SaveBookmark(bm *entity.Bookmark) error {
|
||||
func (m *BookmarkManager) LoadBookmarkByID(id uint64) entity.Bookmark {
|
||||
// log.Printf("loading %v", ids)
|
||||
ret := entity.Bookmark{}
|
||||
log.Printf("loading id %d", id)
|
||||
err := m.db.store.Get(id, &ret)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -179,6 +178,8 @@ func (m *BookmarkManager) Search(opts SearchOptions) ([]entity.Bookmark, error)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m.db.IncrementSearches()
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -205,7 +206,6 @@ func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error {
|
||||
func (m *BookmarkManager) UpdateIndexForBookmark(bm *entity.Bookmark) {
|
||||
words := content.Words(bm)
|
||||
words = append(words, bm.Tags...)
|
||||
log.Printf("index for %d %s (%d words)", bm.ID, bm.URL, len(words))
|
||||
m.db.UpdateIndexForWordsByID(words, bm.ID)
|
||||
}
|
||||
|
||||
@@ -279,3 +279,12 @@ func (m *BookmarkManager) UpdateContent() {
|
||||
time.Sleep(time.Second * 5)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *BookmarkManager) Stats() (entity.DBStats, error) {
|
||||
stats := entity.DBStats{}
|
||||
err := m.db.store.Get("stats", &stats)
|
||||
if err != nil && err != bolthold.ErrNotFound {
|
||||
return stats, fmt.Errorf("could not load stats: %s", err)
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
86
db/db.go
86
db/db.go
@@ -2,7 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/tardisx/linkwallet/entity"
|
||||
bolthold "github.com/timshannon/bolthold"
|
||||
@@ -28,8 +28,84 @@ func (db *DB) Close() {
|
||||
db.store.Close()
|
||||
}
|
||||
|
||||
func (db *DB) Dumpy() {
|
||||
res := make([]entity.Bookmark, 0, 0)
|
||||
db.store.Find(&res, &bolthold.Query{})
|
||||
log.Printf("%v", res)
|
||||
// func (db *DB) Dumpy() {
|
||||
// res := make([]entity.Bookmark, 0, 0)
|
||||
// db.store.Find(&res, &bolthold.Query{})
|
||||
// log.Printf("%v", res)
|
||||
// }
|
||||
|
||||
// IncrementSearches increments the number of searches we have ever performed by one.
|
||||
func (db *DB) IncrementSearches() error {
|
||||
txn, err := db.store.Bolt().Begin(true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not start transaction for increment searches: %s", err)
|
||||
}
|
||||
|
||||
stats := entity.DBStats{}
|
||||
err = db.store.TxGet(txn, "stats", &stats)
|
||||
if err != nil && err != bolthold.ErrNotFound {
|
||||
txn.Rollback()
|
||||
return fmt.Errorf("could not get stats for incrementing searches: %s", err)
|
||||
}
|
||||
|
||||
stats.Searches += 1
|
||||
err = db.store.TxUpsert(txn, "stats", &stats)
|
||||
if err != nil {
|
||||
txn.Rollback()
|
||||
return fmt.Errorf("could not upsert stats for incrementing searches: %s", err)
|
||||
}
|
||||
err = txn.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not commit increment searches transaction: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateBookmarkStats updates the history on the number of bookmarks and words indexed.
|
||||
func (db *DB) UpdateBookmarkStats() error {
|
||||
|
||||
txn, err := db.store.Bolt().Begin(true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not start transaction for update stats: %s", err)
|
||||
}
|
||||
// count bookmarks and words indexed
|
||||
bmI := entity.Bookmark{}
|
||||
wiI := entity.WordIndex{}
|
||||
bookmarkCount, err := db.store.TxCount(txn, &bmI, &bolthold.Query{})
|
||||
if err != nil {
|
||||
txn.Rollback()
|
||||
return fmt.Errorf("could not get bookmark count: %s", err)
|
||||
}
|
||||
indexWordCount, err := db.store.TxCount(txn, &wiI, &bolthold.Query{})
|
||||
if err != nil {
|
||||
txn.Rollback()
|
||||
return fmt.Errorf("could not get index word count: %s", err)
|
||||
}
|
||||
|
||||
// bucket these stats by day
|
||||
now := time.Now().Truncate(time.Hour * 24)
|
||||
|
||||
stats := entity.DBStats{}
|
||||
err = db.store.TxGet(txn, "stats", &stats)
|
||||
if err != nil && err != bolthold.ErrNotFound {
|
||||
txn.Rollback()
|
||||
return fmt.Errorf("could not get stats: %s", err)
|
||||
}
|
||||
if stats.History == nil {
|
||||
stats.History = make(map[time.Time]entity.BookmarkInfo)
|
||||
}
|
||||
stats.History[now] = entity.BookmarkInfo{Bookmarks: bookmarkCount, IndexedWords: indexWordCount}
|
||||
err = db.store.TxUpsert(txn, "stats", &stats)
|
||||
if err != nil {
|
||||
txn.Rollback()
|
||||
return fmt.Errorf("could not upsert stats: %s", err)
|
||||
}
|
||||
|
||||
err = txn.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not commit stats transaction: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
15
db/index.go
15
db/index.go
@@ -20,7 +20,20 @@ func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
|
||||
}
|
||||
db.store.TxForEach(txn, &bolthold.Query{}, func(wi *entity.WordIndex) error {
|
||||
delete(wi.Bitmap, id)
|
||||
db.store.TxUpdate(txn, "word_index_"+wi.Word, wi)
|
||||
// if the index is now completely empty, nuke it entirely
|
||||
empty := true
|
||||
for _, v := range wi.Bitmap {
|
||||
if v {
|
||||
empty = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if empty {
|
||||
db.store.TxDelete(txn, "word_index_"+wi.Word, wi)
|
||||
} else {
|
||||
db.store.TxUpdate(txn, "word_index_"+wi.Word, wi)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
47
entity/meta.go
Normal file
47
entity/meta.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DBStats struct {
|
||||
History map[time.Time]BookmarkInfo
|
||||
Searches int
|
||||
}
|
||||
|
||||
type BookmarkInfo struct {
|
||||
Bookmarks int
|
||||
IndexedWords int
|
||||
}
|
||||
|
||||
func (stats DBStats) String() string {
|
||||
out := fmt.Sprintf("searches: %d\n", stats.Searches)
|
||||
|
||||
dates := []time.Time{}
|
||||
|
||||
for k := range stats.History {
|
||||
dates = append(dates, k)
|
||||
}
|
||||
|
||||
sort.Slice(dates, func(i, j int) bool { return dates[i].Before(dates[j]) })
|
||||
|
||||
for _, k := range dates {
|
||||
out += fmt.Sprintf("%s - %d bookmarks, %d words indexed\n", k, stats.History[k].Bookmarks, stats.History[k].IndexedWords)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (stats DBStats) MostRecentBookmarkInfo() BookmarkInfo {
|
||||
mostRecent := time.Time{}
|
||||
for k := range stats.History {
|
||||
if k.After(mostRecent) {
|
||||
mostRecent = k
|
||||
}
|
||||
}
|
||||
if !mostRecent.IsZero() {
|
||||
return stats.History[mostRecent]
|
||||
}
|
||||
return BookmarkInfo{}
|
||||
}
|
||||
14
meta/meta.go
Normal file
14
meta/meta.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package meta
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func MemInfo() string {
|
||||
stats := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&stats)
|
||||
|
||||
return fmt.Sprintf("%.3fMb", float64(stats.Alloc)/1024.0/1024.0)
|
||||
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"golang.org/x/mod/semver"
|
||||
)
|
||||
|
||||
const Tag = "v0.0.28"
|
||||
const Tag = "v0.0.32"
|
||||
|
||||
type Info struct {
|
||||
Local struct {
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<div class="top-bar-right">
|
||||
<ul class="menu">
|
||||
<li>
|
||||
<a href="/releaseinfo">{{ version.Local.Tag }}
|
||||
<a href="/info">{{ version.Local.Tag }}
|
||||
{{ if version.UpgradeAvailable }}
|
||||
❗
|
||||
{{ end }}
|
||||
@@ -63,8 +63,8 @@
|
||||
{{ template "config.html" . }}
|
||||
{{ else if eq .page "edit" }}
|
||||
{{ template "edit.html" . }}
|
||||
{{ else if eq .page "releaseinfo" }}
|
||||
{{ template "release_info.html" . }}
|
||||
{{ else if eq .page "info" }}
|
||||
{{ template "info.html" . }}
|
||||
{{ end }}
|
||||
{{/* template "foundation_sample.html" . */}}
|
||||
</div>
|
||||
|
||||
@@ -30,7 +30,10 @@
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
<button type="button" hx-confirm="Delete this bookmark permanently?" hx-delete="/edit/{{.bookmark.ID}}" class="alert button">delete</button>
|
||||
<button type="button" class="button" hx-post="/edit/{{.bookmark.ID}}">save</button>
|
||||
<button type="button" hx-confirm="Delete this bookmark permanently?" hx-delete="/edit/{{.bookmark.ID}}" class="alert button">Delete</button>
|
||||
<button type="button" class="button" hx-indicator="#saving" hx-post="/edit/{{.bookmark.ID}}"> {{ if .saved }} Saved {{ else }} Save {{ end }}</button>
|
||||
<span id="saving" class="htmx-indicator">
|
||||
<img style="height:1em;" src="/assets/image/beating.gif" /> Saving...
|
||||
</span>
|
||||
</p>
|
||||
</form>
|
||||
@@ -1,6 +1,13 @@
|
||||
<div class="grid-x grid-padding-x">
|
||||
<div class="large-12 cell">
|
||||
|
||||
<h5>System information</h5>
|
||||
<table>
|
||||
<tr><th>Memory in use</th><td>{{ meminfo }}</td></tr>
|
||||
<tr><th>Bookmarks</th><td>{{ .stats.MostRecentBookmarkInfo.Bookmarks }}</td></tr>
|
||||
<tr><th>Words Indexed</th><td>{{ .stats.MostRecentBookmarkInfo.IndexedWords }}</td></tr>
|
||||
</table>
|
||||
|
||||
<h5>Release info</h5>
|
||||
{{ if not version.Remote.Valid }}
|
||||
<p>GitHub version information not yet fetched.</p>
|
||||
19
web/web.go
19
web/web.go
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"github.com/tardisx/linkwallet/db"
|
||||
"github.com/tardisx/linkwallet/entity"
|
||||
"github.com/tardisx/linkwallet/meta"
|
||||
"github.com/tardisx/linkwallet/version"
|
||||
|
||||
"github.com/gomarkdown/markdown"
|
||||
@@ -82,6 +83,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
||||
"niceURL": niceURL,
|
||||
"join": strings.Join,
|
||||
"version": func() *version.Info { return &version.VersionInfo },
|
||||
"meminfo": meta.MemInfo,
|
||||
"markdown": func(s string) template.HTML { return template.HTML(string(markdown.ToHTML([]byte(s), nil, nil))) },
|
||||
}).ParseFS(templateFiles, "templates/*.html"))
|
||||
|
||||
@@ -185,6 +187,13 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
||||
r.POST("/search", func(c *gin.Context) {
|
||||
query := c.PostForm("query")
|
||||
|
||||
// no query, return an empty response
|
||||
if len(query) == 0 {
|
||||
c.Status(http.StatusNoContent)
|
||||
c.Writer.Write([]byte{})
|
||||
return
|
||||
}
|
||||
|
||||
sr, err := bmm.Search(db.SearchOptions{Query: query})
|
||||
data := gin.H{
|
||||
"results": sr,
|
||||
@@ -369,7 +378,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
||||
bmm.SaveBookmark(&bookmark)
|
||||
bmm.UpdateIndexForBookmark(&bookmark) // because title may have changed
|
||||
|
||||
meta := gin.H{"page": "edit", "bookmark": bookmark, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}}
|
||||
meta := gin.H{"page": "edit", "bookmark": bookmark, "saved": true, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}}
|
||||
|
||||
c.HTML(http.StatusOK,
|
||||
"edit_form.html", meta,
|
||||
@@ -394,8 +403,12 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
||||
)
|
||||
})
|
||||
|
||||
r.GET("/releaseinfo", func(c *gin.Context) {
|
||||
meta := gin.H{"page": "releaseinfo", "config": config}
|
||||
r.GET("/info", func(c *gin.Context) {
|
||||
dbStats, err := bmm.Stats()
|
||||
if err != nil {
|
||||
panic("could not load stats for info page")
|
||||
}
|
||||
meta := gin.H{"page": "info", "stats": dbStats, "config": config}
|
||||
c.HTML(http.StatusOK,
|
||||
"_layout.html", meta,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user