Selaa lähdekoodia

Show instance stats on About page

This also moves the stats database logic out of nodeinfo.go and into
database.go.
compose-update
Matt Baer 5 vuotta sitten
vanhempi
commit
be2c7ef86b
5 muutettua tiedostoa jossa 43 lisäystä ja 5 poistoa
  1. +7
    -0
      app.go
  2. +18
    -0
      database.go
  3. +6
    -0
      instance.go
  4. +7
    -5
      nodeinfo.go
  5. +5
    -0
      pages/about.tmpl

+ 7
- 0
app.go Näytä tiedosto

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


+ 18
- 0
database.go Näytä tiedosto

@@ -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
- 0
instance.go Näytä tiedosto

@@ -0,0 +1,6 @@
package writefreely

type InstanceStats struct {
NumPosts int64
NumBlogs int64
}

+ 7
- 5
nodeinfo.go Näytä tiedosto

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

+ 5
- 0
pages/about.tmpl Näytä tiedosto

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


Ladataan…
Peruuta
Tallenna