Show instance stats on About page

This also moves the stats database logic out of nodeinfo.go and into
database.go.
pull/37/head
Matt Baer 6 years ago
parent b9d7d4ce24
commit be2c7ef86b
  1. 7
      app.go
  2. 18
      database.go
  3. 6
      instance.go
  4. 12
      nodeinfo.go
  5. 5
      pages/about.tmpl

@ -99,6 +99,8 @@ func handleTemplatedPage(app *app, w http.ResponseWriter, r *http.Request, t *te
page.StaticPage page.StaticPage
Content template.HTML Content template.HTML
Updated string Updated string
AboutStats *InstanceStats
}{ }{
StaticPage: pageForReq(app, r), 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" { if r.URL.Path == "/about" {
c, err = getAboutPage(app) c, err = getAboutPage(app)
// Fetch stats
p.AboutStats = &InstanceStats{}
p.AboutStats.NumPosts, _ = app.db.GetTotalPosts()
p.AboutStats.NumBlogs, _ = app.db.GetTotalCollections()
} else { } else {
c, updated, err = getPrivacyPage(app) c, updated, err = getPrivacyPage(app)
} }

@ -50,6 +50,8 @@ type writestore interface {
GetCollections(u *User) (*[]Collection, error) GetCollections(u *User) (*[]Collection, error)
GetPublishableCollections(u *User) (*[]Collection, error) GetPublishableCollections(u *User) (*[]Collection, error)
GetMeStats(u *User) userMeStats GetMeStats(u *User) userMeStats
GetTotalCollections() (int64, error)
GetTotalPosts() (int64, error)
GetTopPosts(u *User, alias string) (*[]PublicPost, error) GetTopPosts(u *User, alias string) (*[]PublicPost, error)
GetAnonymousPosts(u *User) (*[]PublicPost, error) GetAnonymousPosts(u *User) (*[]PublicPost, error)
GetUserPosts(u *User) (*[]PublicPost, error) GetUserPosts(u *User) (*[]PublicPost, error)
@ -1541,6 +1543,22 @@ func (db *datastore) GetMeStats(u *User) userMeStats {
return s 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) { func (db *datastore) GetTopPosts(u *User, alias string) (*[]PublicPost, error) {
params := []interface{}{u.ID} params := []interface{}{u.ID}
where := "" where := ""

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

@ -57,12 +57,14 @@ func (r nodeInfoResolver) IsOpenRegistration() (bool, error) {
} }
func (r nodeInfoResolver) Usage() (nodeinfo.Usage, error) { func (r nodeInfoResolver) Usage() (nodeinfo.Usage, error) {
var collCount, postCount, activeHalfYear, activeMonth int var collCount, postCount int64
err := r.db.QueryRow(`SELECT COUNT(*) FROM collections`).Scan(&collCount) var activeHalfYear, activeMonth int
var err error
collCount, err = r.db.GetTotalCollections()
if err != nil { if err != nil {
collCount = 0 collCount = 0
} }
err = r.db.QueryRow(`SELECT COUNT(*) FROM posts`).Scan(&postCount) postCount, err = r.db.GetTotalPosts()
if err != nil { if err != nil {
log.Error("Unable to fetch post counts: %v", err) log.Error("Unable to fetch post counts: %v", err)
} }
@ -88,10 +90,10 @@ WHERE collection_id IS NOT NULL
return nodeinfo.Usage{ return nodeinfo.Usage{
Users: nodeinfo.UsageUsers{ Users: nodeinfo.UsageUsers{
Total: collCount, Total: int(collCount),
ActiveHalfYear: activeHalfYear, ActiveHalfYear: activeHalfYear,
ActiveMonth: activeMonth, ActiveMonth: activeMonth,
}, },
LocalPosts: postCount, LocalPosts: int(postCount),
}, nil }, nil
} }

@ -6,6 +6,11 @@
{{.Content}} {{.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> <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><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> <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>

Loading…
Cancel
Save