75 lines
1.8 KiB
Go
Raw Normal View History

2017-07-13 00:32:36 +00:00
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{}
2017-12-02 18:34:32 +00:00
studios := 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-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))]++
status[item.Status]++
types[item.Anime().Type]++
2017-12-02 18:34:32 +00:00
for _, studio := range item.Anime().Studios() {
studios[studio.Name.English]++
}
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]++
}
}
userStats.PieCharts = []*arn.PieChart{
arn.NewPieChart("Ratings", ratings),
arn.NewPieChart("Status", status),
arn.NewPieChart("Types", types),
2017-12-02 18:34:32 +00:00
arn.NewPieChart("Studios", studios),
2017-07-13 00:32:36 +00:00
arn.NewPieChart("Years", years),
}
return ctx.HTML(components.ProfileStats(&userStats, viewUser, utils.GetUser(ctx), ctx.URI()))
}