From 33d62020410538be1e984504ceab530cb02fff39 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 13 Jul 2017 02:32:36 +0200 Subject: [PATCH] Added statistics --- main.go | 1 + pages/profile/profile.pixy | 4 +++ pages/profile/stats.go | 64 ++++++++++++++++++++++++++++++++++++++ pages/profile/stats.pixy | 15 +++++++++ styles/base.scarlet | 3 ++ utils/UserStats.go | 10 ++++++ 6 files changed, 97 insertions(+) create mode 100644 pages/profile/stats.go create mode 100644 pages/profile/stats.pixy create mode 100644 utils/UserStats.go diff --git a/main.go b/main.go index 2d47d72e..80d7d10c 100644 --- a/main.go +++ b/main.go @@ -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)) diff --git a/pages/profile/profile.pixy b/pages/profile/profile.pixy index d16aa2c3..90da6855 100644 --- a/pages/profile/profile.pixy +++ b/pages/profile/profile.pixy @@ -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 diff --git a/pages/profile/stats.go b/pages/profile/stats.go new file mode 100644 index 00000000..05b79b81 --- /dev/null +++ b/pages/profile/stats.go @@ -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())) +} diff --git a/pages/profile/stats.pixy b/pages/profile/stats.pixy new file mode 100644 index 00000000..199fbf3c --- /dev/null +++ b/pages/profile/stats.pixy @@ -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. \ No newline at end of file diff --git a/styles/base.scarlet b/styles/base.scarlet index 6892df79..c2eb31d3 100644 --- a/styles/base.scarlet +++ b/styles/base.scarlet @@ -35,5 +35,8 @@ img .hidden display none !important +.text-center + text-align center + .spacer flex 1 \ No newline at end of file diff --git a/utils/UserStats.go b/utils/UserStats.go new file mode 100644 index 00000000..d4e5c2d7 --- /dev/null +++ b/utils/UserStats.go @@ -0,0 +1,10 @@ +package utils + +import "time" +import "github.com/animenotifier/arn" + +// UserStats ... +type UserStats struct { + AnimeWatchingTime time.Duration + PieCharts []*arn.PieChart +}