103 lines
2.2 KiB
Go
Raw Normal View History

2017-07-13 00:32:36 +00:00
package profile
import (
"net/http"
"strconv"
2018-03-14 19:21:12 +00:00
"strings"
2017-07-13 00:32:36 +00:00
"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{}
2017-12-02 18:34:32 +00:00
studios := stats{}
2017-12-02 18:55:16 +00:00
genres := stats{}
2018-03-14 19:21:12 +00:00
trackTags := stats{}
2017-07-13 00:32:36 +00:00
if err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
}
2017-07-17 20:43:26 +00:00
animeList, err := arn.GetAnimeList(viewUser.ID)
2017-07-13 00:32:36 +00:00
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Anime list not found", err)
}
2017-11-24 14:14:29 +00:00
animeList.Lock()
defer animeList.Unlock()
2017-07-13 00:32:36 +00:00
for _, item := range animeList.Items {
2017-12-03 15:06:20 +00:00
status[item.Status]++
if item.Status == arn.AnimeListStatusPlanned {
continue
}
2017-07-15 18:18:24 +00:00
currentWatch := item.Episodes * item.Anime().EpisodeLength
reWatch := item.RewatchCount * item.Anime().EpisodeCount * item.Anime().EpisodeLength
duration := time.Duration(currentWatch + reWatch)
2017-07-13 00:32:36 +00:00
userStats.AnimeWatchingTime += duration * time.Minute
ratings[strconv.Itoa(int(item.Rating.Overall+0.5))]++
types[item.Anime().Type]++
2017-12-02 18:34:32 +00:00
for _, studio := range item.Anime().Studios() {
studios[studio.Name.English]++
}
2017-12-02 18:55:16 +00:00
for _, genre := range item.Anime().Genres {
genres[genre]++
}
2017-07-13 00:32:36 +00:00
if item.Anime().StartDate != "" {
year := item.Anime().StartDate[:4]
if year < "2000" {
year = "Before 2000"
}
years[year]++
}
}
2018-03-14 19:21:12 +00:00
for track := range arn.StreamSoundTracks() {
if !track.LikedBy(viewUser.ID) {
continue
}
for _, tag := range track.Tags {
if strings.Contains(tag, ":") {
continue
}
trackTags[tag]++
}
}
2017-07-13 00:32:36 +00:00
userStats.PieCharts = []*arn.PieChart{
2017-12-02 18:55:16 +00:00
arn.NewPieChart("Genres", genres),
2017-12-02 18:34:32 +00:00
arn.NewPieChart("Studios", studios),
2017-07-13 00:32:36 +00:00
arn.NewPieChart("Years", years),
2017-12-02 18:55:16 +00:00
arn.NewPieChart("Ratings", ratings),
arn.NewPieChart("Types", types),
arn.NewPieChart("Status", status),
2018-03-14 19:21:12 +00:00
arn.NewPieChart("Soundtracks", trackTags),
2017-07-13 00:32:36 +00:00
}
return ctx.HTML(components.ProfileStats(&userStats, viewUser, utils.GetUser(ctx), ctx.URI()))
}