Show instance stats on About page

This also moves the stats database logic out of nodeinfo.go and into
database.go.
This commit is contained in:
Matt Baer 2018-11-21 14:05:44 -05:00
parent b9d7d4ce24
commit be2c7ef86b
5 changed files with 43 additions and 5 deletions

7
app.go
View file

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

View file

@ -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 := ""

6
instance.go Normal file
View file

@ -0,0 +1,6 @@
package writefreely
type InstanceStats struct {
NumPosts int64
NumBlogs int64
}

View file

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

View file

@ -6,6 +6,11 @@
{{.Content}}
{{if .Federation}}
<hr style="margin:1.5em 0;" />
<p><em>{{.SiteName}}</em> is home to <strong>{{largeNumFmt .AboutStats.NumPosts}}</strong> {{pluralize "article" "articles" .AboutStats.NumPosts}} across <strong>{{largeNumFmt .AboutStats.NumBlogs}}</strong> {{pluralize "blog" "blogs" .AboutStats.NumBlogs}}.</p>
{{end}}
<h2 style="margin-top:2em">About WriteFreely</h2>
<p><a href="https://writefreely.org">WriteFreely</a> is a self-hosted, decentralized blogging platform for publishing beautiful, simple blogs.</p>
<p>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.</p>