From be2c7ef86bb4771325724eb9897e491bae9bc789 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 21 Nov 2018 14:05:44 -0500 Subject: [PATCH] Show instance stats on About page This also moves the stats database logic out of nodeinfo.go and into database.go. --- app.go | 7 +++++++ database.go | 18 ++++++++++++++++++ instance.go | 6 ++++++ nodeinfo.go | 12 +++++++----- pages/about.tmpl | 5 +++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 instance.go diff --git a/app.go b/app.go index 65939a9..172a00e 100644 --- a/app.go +++ b/app.go @@ -99,6 +99,8 @@ func handleTemplatedPage(app *app, w http.ResponseWriter, r *http.Request, t *te page.StaticPage Content template.HTML Updated string + + AboutStats *InstanceStats }{ StaticPage: pageForReq(app, r), } @@ -109,6 +111,11 @@ func handleTemplatedPage(app *app, w http.ResponseWriter, r *http.Request, t *te if r.URL.Path == "/about" { c, err = getAboutPage(app) + + // Fetch stats + p.AboutStats = &InstanceStats{} + p.AboutStats.NumPosts, _ = app.db.GetTotalPosts() + p.AboutStats.NumBlogs, _ = app.db.GetTotalCollections() } else { c, updated, err = getPrivacyPage(app) } diff --git a/database.go b/database.go index 0c79e10..468b04d 100644 --- a/database.go +++ b/database.go @@ -50,6 +50,8 @@ type writestore interface { GetCollections(u *User) (*[]Collection, error) GetPublishableCollections(u *User) (*[]Collection, error) GetMeStats(u *User) userMeStats + GetTotalCollections() (int64, error) + GetTotalPosts() (int64, error) GetTopPosts(u *User, alias string) (*[]PublicPost, error) GetAnonymousPosts(u *User) (*[]PublicPost, error) GetUserPosts(u *User) (*[]PublicPost, error) @@ -1541,6 +1543,22 @@ func (db *datastore) GetMeStats(u *User) userMeStats { return s } +func (db *datastore) GetTotalCollections() (collCount int64, err error) { + err = db.QueryRow(`SELECT COUNT(*) FROM collections`).Scan(&collCount) + if err != nil { + log.Error("Unable to fetch collections count: %v", err) + } + return +} + +func (db *datastore) GetTotalPosts() (postCount int64, err error) { + err = db.QueryRow(`SELECT COUNT(*) FROM posts`).Scan(&postCount) + if err != nil { + log.Error("Unable to fetch posts count: %v", err) + } + return +} + func (db *datastore) GetTopPosts(u *User, alias string) (*[]PublicPost, error) { params := []interface{}{u.ID} where := "" diff --git a/instance.go b/instance.go new file mode 100644 index 0000000..482e65d --- /dev/null +++ b/instance.go @@ -0,0 +1,6 @@ +package writefreely + +type InstanceStats struct { + NumPosts int64 + NumBlogs int64 +} diff --git a/nodeinfo.go b/nodeinfo.go index fd4b445..eab15cc 100644 --- a/nodeinfo.go +++ b/nodeinfo.go @@ -57,12 +57,14 @@ func (r nodeInfoResolver) IsOpenRegistration() (bool, error) { } func (r nodeInfoResolver) Usage() (nodeinfo.Usage, error) { - var collCount, postCount, activeHalfYear, activeMonth int - err := r.db.QueryRow(`SELECT COUNT(*) FROM collections`).Scan(&collCount) + var collCount, postCount int64 + var activeHalfYear, activeMonth int + var err error + collCount, err = r.db.GetTotalCollections() if err != nil { collCount = 0 } - err = r.db.QueryRow(`SELECT COUNT(*) FROM posts`).Scan(&postCount) + postCount, err = r.db.GetTotalPosts() if err != nil { log.Error("Unable to fetch post counts: %v", err) } @@ -88,10 +90,10 @@ WHERE collection_id IS NOT NULL return nodeinfo.Usage{ Users: nodeinfo.UsageUsers{ - Total: collCount, + Total: int(collCount), ActiveHalfYear: activeHalfYear, ActiveMonth: activeMonth, }, - LocalPosts: postCount, + LocalPosts: int(postCount), }, nil } diff --git a/pages/about.tmpl b/pages/about.tmpl index a47faf6..71a6223 100644 --- a/pages/about.tmpl +++ b/pages/about.tmpl @@ -6,6 +6,11 @@ {{.Content}} + {{if .Federation}} +
+

{{.SiteName}} is home to {{largeNumFmt .AboutStats.NumPosts}} {{pluralize "article" "articles" .AboutStats.NumPosts}} across {{largeNumFmt .AboutStats.NumBlogs}} {{pluralize "blog" "blogs" .AboutStats.NumBlogs}}.

+ {{end}} +

About WriteFreely

WriteFreely is a self-hosted, decentralized blogging platform for publishing beautiful, simple blogs.

It lets you publish a single blog, or host a community of writers who can create multiple blogs under one account. You can also enable federation, which allows people in the fediverse to follow your blog, bookmark your posts, and share them with others.