2017-07-07 00:07:34 +00:00
|
|
|
package statistics
|
|
|
|
|
|
|
|
import (
|
2017-10-28 01:53:12 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-07-07 00:07:34 +00:00
|
|
|
"github.com/aerogo/aero"
|
2017-10-28 01:53:12 +00:00
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
2017-07-07 00:07:34 +00:00
|
|
|
)
|
|
|
|
|
2017-10-28 01:53:12 +00:00
|
|
|
type stats map[string]float64
|
|
|
|
|
2017-07-07 00:07:34 +00:00
|
|
|
// Get ...
|
|
|
|
func Get(ctx *aero.Context) string {
|
2017-10-28 01:53:12 +00:00
|
|
|
pieCharts := getUserStats()
|
|
|
|
return ctx.HTML(components.Statistics(pieCharts))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserStats() []*arn.PieChart {
|
|
|
|
screenSize := stats{}
|
|
|
|
pixelRatio := stats{}
|
|
|
|
browser := stats{}
|
|
|
|
country := stats{}
|
|
|
|
gender := stats{}
|
|
|
|
os := stats{}
|
|
|
|
notifications := stats{}
|
|
|
|
avatar := stats{}
|
|
|
|
ip := stats{}
|
|
|
|
pro := stats{}
|
2017-11-19 13:44:02 +00:00
|
|
|
connectionType := stats{}
|
|
|
|
roundTripTime := stats{}
|
|
|
|
downLink := stats{}
|
2018-09-27 07:42:21 +00:00
|
|
|
theme := stats{}
|
2017-10-28 01:53:12 +00:00
|
|
|
|
|
|
|
for info := range arn.StreamAnalytics() {
|
|
|
|
user, err := arn.GetUser(info.UserID)
|
|
|
|
arn.PanicOnError(err)
|
|
|
|
|
|
|
|
if !user.IsActive() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pixelRatio[fmt.Sprintf("%.0f", info.Screen.PixelRatio)]++
|
|
|
|
|
2018-04-19 20:01:28 +00:00
|
|
|
size := fmt.Sprint(info.Screen.Width) + " x " + fmt.Sprint(info.Screen.Height)
|
2017-10-28 01:53:12 +00:00
|
|
|
screenSize[size]++
|
2017-11-19 13:44:02 +00:00
|
|
|
|
|
|
|
if info.Connection.EffectiveType != "" {
|
|
|
|
connectionType[info.Connection.EffectiveType]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Connection.DownLink != 0 {
|
|
|
|
downLink[fmt.Sprintf("%.0f Mb/s", info.Connection.DownLink)]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Connection.RoundTripTime != 0 {
|
|
|
|
roundTripTime[fmt.Sprintf("%.0f ms", info.Connection.RoundTripTime)]++
|
|
|
|
}
|
2017-10-28 01:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for user := range arn.StreamUsers() {
|
|
|
|
if !user.IsActive() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Gender != "" && user.Gender != "other" {
|
|
|
|
gender[user.Gender]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Browser.Name != "" {
|
|
|
|
browser[user.Browser.Name]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Location.CountryName != "" {
|
|
|
|
country[user.Location.CountryName]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.OS.Name != "" {
|
|
|
|
if strings.HasPrefix(user.OS.Name, "CrOS") {
|
|
|
|
user.OS.Name = "Chrome OS"
|
|
|
|
}
|
|
|
|
|
|
|
|
os[user.OS.Name]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(user.PushSubscriptions().Items) > 0 {
|
|
|
|
notifications["Enabled"]++
|
|
|
|
} else {
|
|
|
|
notifications["Disabled"]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Avatar.Source == "" {
|
|
|
|
avatar["none"]++
|
|
|
|
} else {
|
|
|
|
avatar[user.Avatar.Source]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if arn.IsIPv6(user.IP) {
|
|
|
|
ip["IPv6"]++
|
|
|
|
} else {
|
|
|
|
ip["IPv4"]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.IsPro() {
|
|
|
|
pro["PRO accounts"]++
|
|
|
|
} else {
|
|
|
|
pro["Free accounts"]++
|
|
|
|
}
|
2018-09-27 07:42:21 +00:00
|
|
|
|
|
|
|
settings := user.Settings()
|
|
|
|
theme[settings.Theme]++
|
2017-10-28 01:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []*arn.PieChart{
|
|
|
|
arn.NewPieChart("OS", os),
|
|
|
|
arn.NewPieChart("Screen size", screenSize),
|
|
|
|
arn.NewPieChart("Browser", browser),
|
|
|
|
arn.NewPieChart("Country", country),
|
|
|
|
arn.NewPieChart("Avatar", avatar),
|
|
|
|
arn.NewPieChart("Notifications", notifications),
|
|
|
|
arn.NewPieChart("Gender", gender),
|
|
|
|
arn.NewPieChart("Pixel ratio", pixelRatio),
|
2017-11-19 13:44:02 +00:00
|
|
|
arn.NewPieChart("Download speed", downLink),
|
2017-11-19 13:46:18 +00:00
|
|
|
arn.NewPieChart("Ping", roundTripTime),
|
|
|
|
arn.NewPieChart("Connection", connectionType),
|
2017-10-28 01:53:12 +00:00
|
|
|
arn.NewPieChart("IP version", ip),
|
|
|
|
arn.NewPieChart("PRO accounts", pro),
|
2018-09-27 07:42:21 +00:00
|
|
|
arn.NewPieChart("Theme", theme),
|
2017-10-28 01:53:12 +00:00
|
|
|
}
|
2017-07-07 00:07:34 +00:00
|
|
|
}
|