From 1fb5b4210ed98d1930c2c0eeacda9191d48911d9 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 7 Jul 2017 03:45:39 +0200 Subject: [PATCH] More stats --- pages/statistics/statistics.go | 47 +++++++++++++++++++++++++++++----- utils/PieChart.go | 8 +++--- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/pages/statistics/statistics.go b/pages/statistics/statistics.go index 6d916c2c..c3063328 100644 --- a/pages/statistics/statistics.go +++ b/pages/statistics/statistics.go @@ -3,6 +3,7 @@ package statistics import ( "fmt" "net/http" + "strings" "github.com/aerogo/aero" "github.com/animenotifier/arn" @@ -10,6 +11,8 @@ import ( "github.com/animenotifier/notify.moe/utils" ) +type stats = map[string]float64 + // Get ... func Get(ctx *aero.Context) string { analytics, err := arn.AllAnalytics() @@ -18,21 +21,51 @@ func Get(ctx *aero.Context) string { return ctx.Error(http.StatusInternalServerError, "Couldn't retrieve analytics", err) } - screenSize := map[string]float64{} - platform := map[string]float64{} - pixelRatio := map[string]float64{} + screenSize := stats{} + // platform := stats{} + pixelRatio := stats{} + browser := stats{} + country := stats{} + gender := stats{} + os := stats{} for _, info := range analytics { - platform[info.System.Platform]++ + // platform[info.System.Platform]++ pixelRatio[fmt.Sprintf("%.1f", info.Screen.PixelRatio)]++ size := arn.ToString(info.Screen.Width) + " x " + arn.ToString(info.Screen.Height) screenSize[size]++ } + 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]++ + } + } + return ctx.HTML(components.Statistics( - utils.NewPieChart("Screen sizes", screenSize), - utils.NewPieChart("Platforms", platform), - utils.NewPieChart("Pixel ratios", pixelRatio), + utils.NewPieChart("OS", os), + // utils.NewPieChart("Platform", platform), + utils.NewPieChart("Screen size", screenSize), + utils.NewPieChart("Pixel ratio", pixelRatio), + utils.NewPieChart("Browser", browser), + utils.NewPieChart("Country", country), + utils.NewPieChart("Gender", gender), )) } diff --git a/utils/PieChart.go b/utils/PieChart.go index 28cab8fe..203cdad9 100644 --- a/utils/PieChart.go +++ b/utils/PieChart.go @@ -98,17 +98,17 @@ func ToPieChartSlices(data map[string]float64) []*PieChartSlice { slices := []*PieChartSlice{} current := 0.0 - hueOffset := 0.0 - hueScaling := 60.0 + hueOffset := 230.0 + hueScaling := -30.0 - for _, item := range dataSorted { + for i, item := range dataSorted { percentage := float64(item.Value) / sum slices = append(slices, &PieChartSlice{ From: current, To: current + percentage, Title: fmt.Sprintf("%s (%d%%)", item.Key, int(percentage*100+0.5)), - Color: fmt.Sprintf("hsl(%.2f, 75%%, 50%%)", current*hueScaling+hueOffset), + Color: fmt.Sprintf("hsl(%.2f, 75%%, 50%%)", float64(i)*hueScaling+hueOffset), }) current += percentage