Added statistics

This commit is contained in:
Eduard Urbach 2017-07-13 02:32:36 +02:00
parent df4b367c61
commit 33d6202041
6 changed files with 97 additions and 0 deletions

View File

@ -88,6 +88,7 @@ func configure(app *aero.Application) *aero.Application {
app.Ajax("/user/:nick/threads", profile.GetThreadsByUser)
app.Ajax("/user/:nick/posts", profile.GetPostsByUser)
app.Ajax("/user/:nick/tracks", profile.GetSoundTracksByUser)
app.Ajax("/user/:nick/stats", profile.GetStatsByUser)
app.Ajax("/user/:nick/animelist", animelist.Get)
app.Ajax("/user/:nick/animelist/watching", animelist.FilterByStatus(arn.AnimeListStatusWatching))
app.Ajax("/user/:nick/animelist/completed", animelist.FilterByStatus(arn.AnimeListStatusCompleted))

View File

@ -67,6 +67,10 @@ component ProfileNavigation(viewUser *arn.User, uri string)
a.button.tab.action(href="/+" + viewUser.Nick + "/tracks", data-action="diff", data-trigger="click")
Icon("music")
span.tab-text Tracks
a.button.tab.action(href="/+" + viewUser.Nick + "/stats", data-action="diff", data-trigger="click")
Icon("area-chart")
span.tab-text Stats
if strings.Contains(uri, "/animelist")
hr

64
pages/profile/stats.go Normal file
View File

@ -0,0 +1,64 @@
package profile
import (
"net/http"
"strconv"
"time"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
type stats map[string]float64
// GetStatsByUser shows statistics for a given user.
func GetStatsByUser(ctx *aero.Context) string {
nick := ctx.Get("nick")
viewUser, err := arn.GetUserByNick(nick)
userStats := utils.UserStats{}
ratings := stats{}
status := stats{}
types := stats{}
years := stats{}
if err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
}
animeList, err := arn.GetAnimeList(viewUser)
animeList.PrefetchAnime()
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Anime list not found", err)
}
for _, item := range animeList.Items {
duration := time.Duration(item.Episodes * item.Anime().EpisodeLength)
userStats.AnimeWatchingTime += duration * time.Minute
ratings[strconv.Itoa(int(item.Rating.Overall+0.5))]++
status[item.Status]++
types[item.Anime().Type]++
if item.Anime().StartDate != "" {
year := item.Anime().StartDate[:4]
if year < "2000" {
year = "Before 2000"
}
years[year]++
}
}
userStats.PieCharts = []*arn.PieChart{
arn.NewPieChart("Ratings", ratings),
arn.NewPieChart("Status", status),
arn.NewPieChart("Types", types),
arn.NewPieChart("Years", years),
}
return ctx.HTML(components.ProfileStats(&userStats, viewUser, utils.GetUser(ctx), ctx.URI()))
}

15
pages/profile/stats.pixy Normal file
View File

@ -0,0 +1,15 @@
component ProfileStats(stats *utils.UserStats, viewUser *arn.User, user *arn.User, uri string)
ProfileHeader(viewUser, user, uri)
.widgets
each pie in stats.PieCharts
.widget
h3.widget-title
Icon("pie-chart")
span= pie.Title
PieChart(pie.Slices)
.footer.text-center
span You spent
span= int(stats.AnimeWatchingTime / time.Hour / 24)
span days watching anime.

View File

@ -35,5 +35,8 @@ img
.hidden
display none !important
.text-center
text-align center
.spacer
flex 1

10
utils/UserStats.go Normal file
View File

@ -0,0 +1,10 @@
package utils
import "time"
import "github.com/animenotifier/arn"
// UserStats ...
type UserStats struct {
AnimeWatchingTime time.Duration
PieCharts []*arn.PieChart
}