Skip to content

Commit

Permalink
feat(dashboard): summarize new daily users/styles
Browse files Browse the repository at this point in the history
  • Loading branch information
vednoc committed Jul 15, 2021
1 parent 7c8c804 commit b4223a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions handlers/core/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"log"
"sort"
"time"

"github.com/gofiber/fiber/v2"

Expand All @@ -11,8 +12,15 @@ import (
"userstyles.world/modules/charts"
)

type stats struct {
NewUsers int
NewStyles int
}

func Dashboard(c *fiber.Ctx) error {
u, _ := jwt.User(c)
t := time.Now().Format("2006-01-02")
s := stats{}

// Don't allow regular users to see this page.
if u.Role < models.Moderator {
Expand All @@ -31,6 +39,13 @@ func Dashboard(c *fiber.Ctx) error {
})
}

// Summary of new styles.
for _, v := range styles {
if v.CreatedAt.Format("2006-01-02") == t {
s.NewStyles++
}
}

sort.Slice(styles, func(i, j int) bool {
return styles[i].ID > styles[j].ID
})
Expand All @@ -44,6 +59,13 @@ func Dashboard(c *fiber.Ctx) error {
})
}

// Summary of new users.
for _, v := range users {
if v.CreatedAt.Format("2006-01-02") == t {
s.NewUsers++
}
}

// Render user history.
var userHistory string
if len(users) > 0 {
Expand Down Expand Up @@ -77,6 +99,7 @@ func Dashboard(c *fiber.Ctx) error {
"User": u,
"Styles": styles,
"Users": users,
"Summary": s,
"DailyHistory": dailyHistory,
"TotalHistory": totalHistory,
"UserHistory": userHistory,
Expand Down
6 changes: 6 additions & 0 deletions views/core/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ <h1>Dashboard</h1>
<p class="fg:3">WIP functionality to help with moderation.</p>
</section>

<section class="summary">
<h2 class="td:d">Summary</h2>
<p>{{ len .Users }} users ({{ .Summary.NewUsers }} today)</p>
<p>{{ len .Styles }} styles ({{ .Summary.NewStyles }} today)</p>
</section>

<section class="history">
<h2 class="td:d">History</h2>
<p>Learn more about style statistics <a href="/docs/faq#how-do-view-install-update-statistics-work">in the FAQ</a>.</p>
Expand Down

0 comments on commit b4223a7

Please sign in to comment.