ppy / osu-server-spectator

osu! spectator server
MIT License
64 stars 13 forks source link

Add tracking and reporting of users-per-build #138

Closed peppy closed 9 months ago

peppy commented 2 years ago

As brought up in https://github.com/ppy/osu/discussions/18590#discussioncomment-2932477, we need to report counts of users per release so it can be shown in places like the changelog header.

Here's bancho's implementation of this for reference. The important part is the UPDATE statement (the INSERT one is not relevant for the newer implemetation) but I've included the whole stats method for context.

        private static bool isUpdatingStats;

        private static void updateStats()
        {
            if (isUpdatingStats || !ReportClientCounts) return;

            var countOsu = UserManager.CountOsu;

            // avoid reporting low stats by accident
            if (countOsu < 32) return;

            isUpdatingStats = true;

            RunThread(() =>
            {
                try
                {
                    Database.RunNonQueryLowPriority(
                        "INSERT INTO osu_banchostats (users_irc, users_osu, multiplayer_games) VALUES (@irc, @osu, @mp)",
                        new MySqlParameter("irc", UserManager.CountIrc),
                        new MySqlParameter("osu", countOsu),
                        new MySqlParameter("mp", Lobby.Matches.Count));

                    SortedDictionary<string, int> versionCounts = new SortedDictionary<string, int>();

                    foreach (Client c in UserManager.Clients)
                    {
                        NetClient nc = c as NetClient;
                        if (nc == null) continue;

                        string version = Regex.Replace(nc.Version, "[^0-9.]", string.Empty);

                        if (versionCounts.ContainsKey(version))
                            versionCounts[version]++;
                        else
                            versionCounts[version] = 1;
                    }

                    foreach (var v in versionCounts)
                    {
                        Database.RunNonQueryLowPriority("UPDATE osu_builds SET users = @users WHERE version = @version",
                            new MySqlParameter("users", v.Value),
                            new MySqlParameter("version", v.Key)
                        );
                    }
                }
                catch (Exception e)
                {
                    Bacon.WriteLine("Database connection failed!");
                    Bacon.WriteLine(e.ToString());
                }

                isUpdatingStats = false;
            });
        }
bdach commented 9 months ago

Implementation needs more tweaks (see https://discord.com/channels/188630481301012481/188630652340404224/1184782370105200753)