4 "github.com/EliasFleckenstein03/go-fscache"
10 func (s *MyListStats) setCachedTS(t time.Time) { s.Cached = t }
12 func (s *MyListStats) IsStale() bool {
13 if s == nil || time.Now().Sub(s.Cached) > MyListCacheDuration {
19 var _ cacheable = &MyListStats{}
21 func (u *User) Stats() *MyListStats {
26 if CacheGet(&s, "mylist-stats", u.UID) == nil {
32 func (adb *AniDB) MyListStats(user *User) <-chan *MyListStats {
33 ch := make(chan *MyListStats, 1)
34 if user == nil || user.UID < 1 {
40 key := []fscache.CacheKey{"mylist-stats", user.UID}
42 ic := make(chan notification, 1)
43 go func() { ch <- (<-ic).(*MyListStats) }()
44 if intentMap.Intent(ic, key...) {
50 defer intentMap.NotifyClose(stats, key...)
55 if adb.User() == nil {
58 intentMap.NotifyClose(stats, key...)
63 if user.UID != adb.User().UID {
64 intentMap.NotifyClose(stats, key...)
68 reply := <-adb.udp.SendRecv("MYLISTSTATS", nil)
71 parts := strings.Split(reply.Lines()[1], "|")
72 ints := make([]int64, len(parts))
73 for i := range parts {
74 ints[i], _ = strconv.ParseInt(parts[i], 10, 64)
79 Episodes: int(ints[1]),
81 Filesize: ints[3] * 1024 * 1024, // it comes in MB
83 AddedAnime: int(ints[4]),
84 AddedEpisodes: int(ints[5]),
85 AddedFiles: int(ints[6]),
86 AddedGroups: int(ints[7]),
88 Leech: float32(ints[8]) / 100,
89 Glory: float32(ints[9]) / 100,
91 ViewedPctDatabase: float32(ints[10]) / 100,
92 MyListPctDatabase: float32(ints[11]) / 100,
93 // ViewedPctMyList: float32(ints[12]) / 100, // we can calculate a more accurate value
94 ViewedEpisodes: int(ints[13]),
97 Reviews: int(ints[15]),
99 ViewedTime: time.Duration(ints[16]) * time.Minute,
101 stats.ViewedPctMyList = float32(stats.ViewedEpisodes) / float32(stats.Episodes)
103 if ac := AnimeCount(); ac > 0 {
104 stats.AnimePctDatabase = float32(stats.Anime) / float32(ac)
107 CacheSet(stats, key...)
109 intentMap.NotifyClose(stats, key...)