72 lines
1.5 KiB
Go
Raw Normal View History

2017-07-07 00:07:34 +00:00
package statistics
import (
2017-07-07 00:37:43 +00:00
"fmt"
2017-07-07 00:07:34 +00:00
"net/http"
2017-07-07 01:45:39 +00:00
"strings"
2017-07-07 00:07:34 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
2017-07-07 01:45:39 +00:00
type stats = map[string]float64
2017-07-07 00:07:34 +00:00
// Get ...
func Get(ctx *aero.Context) string {
analytics, err := arn.AllAnalytics()
if err != nil {
2017-07-07 08:57:48 +00:00
return ctx.Error(http.StatusInternalServerError, "Couldn't fetch analytics", err)
2017-07-07 00:07:34 +00:00
}
2017-07-07 01:45:39 +00:00
screenSize := stats{}
// platform := stats{}
pixelRatio := stats{}
browser := stats{}
country := stats{}
gender := stats{}
os := stats{}
2017-07-07 00:07:34 +00:00
for _, info := range analytics {
2017-07-07 01:45:39 +00:00
// platform[info.System.Platform]++
2017-07-07 01:22:29 +00:00
pixelRatio[fmt.Sprintf("%.1f", info.Screen.PixelRatio)]++
2017-07-07 00:07:34 +00:00
2017-07-07 01:22:29 +00:00
size := arn.ToString(info.Screen.Width) + " x " + arn.ToString(info.Screen.Height)
screenSize[size]++
2017-07-07 00:07:34 +00:00
}
2017-07-07 01:45:39 +00:00
for user := range arn.MustStreamUsers() {
if user.Gender != "" {
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]++
}
}
2017-07-07 01:22:29 +00:00
return ctx.HTML(components.Statistics(
2017-07-07 01:45:39 +00:00
utils.NewPieChart("OS", os),
// utils.NewPieChart("Platform", platform),
utils.NewPieChart("Screen size", screenSize),
utils.NewPieChart("Browser", browser),
utils.NewPieChart("Country", country),
utils.NewPieChart("Gender", gender),
2017-07-07 08:57:48 +00:00
utils.NewPieChart("Pixel ratio", pixelRatio),
2017-07-07 01:22:29 +00:00
))
2017-07-07 00:07:34 +00:00
}