public async void OnExecution(object sender, CommandEventArgs command) { var bot = sender as Pieprz; var stopwatch = new Stopwatch(); stopwatch.Start(); await using var db = PerchikDB.GetContext(); var message = command.Message; var dateNow = DbConverter.ToEpochTime(DateTime.UtcNow.Date); var now = DbConverter.ToEpochTime(DateTime.UtcNow); var users = db.Users .AsNoTracking() .Select(x => new { x.Id, x.FirstName, x.LastName, activity = x.Messages.Where(m => m.Date > dateNow).Sum(m => m.Text.Length) / (double)db.Messages.Where(m => m.Date > dateNow).Sum(m => m.Text.Length) }) .ToList(); var usersDescending = users.OrderByDescending(x => x.activity); var msgString = "*Топ 10 по активности за сегодня:*\n"; for (var i = 0; i < 10 && i < users.Count; i++) { var user = usersDescending?.ElementAt(i); if (Math.Abs(user.activity) < 0.01) { continue; } var firstName = user.FirstName?.Replace('[', '<').Replace(']', '>'); var lastName = user.LastName?.Replace('[', '<').Replace(']', '>'); var fullName = $"`{firstName} {lastName}`"; msgString += $"{i + 1}. {fullName} -- {user.activity * 100:F2}%\n"; } stopwatch.Stop(); await bot.SendTextMessageAsync( chatId : message.Chat.Id, text : $"{msgString}\n`{stopwatch.ElapsedMilliseconds / 1000.0}сек`", parseMode : ParseMode.Markdown); }
public async void OnExecution(object sender, CommandEventArgs command) { var bot = sender as Pieprz; var msg = command.Message; await using var db = Db.PerchikDB.GetContext(); var pidr = db.Pidrs .AsNoTracking() .Where(p => p.ChatId == msg.Chat.Id && p.Date.Date == DbConverter.DateTimeUtc2.Date) .Select(x => new { x.UserId, x.User.FirstName }) .FirstOrDefault(); if (pidr == null) { await bot.SendChatActionAsync(msg.Chat.Id, ChatAction.Typing); await bot.SendTextMessageAsync( chatId : msg.Chat.Id, text : Program.strManager["PIDR_ONE"], parseMode : ParseMode.Markdown); await bot.SendChatActionAsync(msg.Chat.Id, ChatAction.Typing); await Task.Delay(2000); await bot.SendTextMessageAsync( chatId : msg.Chat.Id, text : Program.strManager["PIDR_TWO"], parseMode : ParseMode.Markdown); await bot.SendChatActionAsync(msg.Chat.Id, ChatAction.Typing); await Task.Delay(1000); await bot.SendTextMessageAsync( chatId : msg.Chat.Id, text : Program.strManager["PIDR_THREE"], parseMode : ParseMode.Markdown); await bot.SendChatActionAsync(msg.Chat.Id, ChatAction.Typing); await Task.Delay(5000); var lastday = DbConverter.ToEpochTime(DateTime.UtcNow.AddDays(-1).Date); var users = db.Users .AsNoTracking() .Where(u => u.Messages.Any(m => m.Date > lastday && m.ChatId == msg.Chat.Id)) .Select(x => new { x.Id, x.FirstName }).ToList(); var newPidr = users[new Random(DbConverter.DateTimeUtc2.Second).Next(0, users.Count)]; await bot.SendTextMessageAsync( chatId : msg.Chat.Id, text : string.Format(Program.strManager["PIDR_DONE"], newPidr.FirstName, newPidr.Id), parseMode : ParseMode.Markdown); await db.Pidrs.AddAsync(new Db.Tables.Pidr() { UserId = newPidr.Id, ChatId = msg.Chat.Id, Date = DbConverter.DateTimeUtc2 }); await db.SaveChangesAsync(); } else { await bot.SendTextMessageAsync( chatId : msg.Chat.Id, text : string.Format(Program.strManager["PIDR_EXIST"], pidr.FirstName), parseMode : ParseMode.Markdown); } }
private static string GetStatisticsText(string search) { var sw = new Stopwatch(); sw.Start(); using var db = PerchikDB.GetContext(); var name = search.Replace("@", ""); var today = DbConverter.ToEpochTime(DateTime.UtcNow.Date); var lastday = DbConverter.ToEpochTime(DateTime.UtcNow.AddDays(-1).Date); var user = db.Users .AsNoTracking() .Where(u => (u.FirstName.Contains(name, StringComparison.OrdinalIgnoreCase)) || (u.LastName.Contains(name, StringComparison.OrdinalIgnoreCase)) || (u.UserName.Contains(name, StringComparison.OrdinalIgnoreCase))) .Select(x => new { x.Id, x.Restricted, x.Description, x.FirstName, x.LastName, x.UserName, x.Restrictions.OrderByDescending(x => x.Until).FirstOrDefault().Until, msgLastday = x.Messages.Count(m => m.Date > lastday && m.Date < today), msgToday = x.Messages.Count(m => m.Date > today), msgTotal = x.Messages.Count, RestrictionCount = x.Restrictions.Count, activity = x.Messages.Where(m => m.Date > today).Sum(m => m.Text.Length) / (double)db.Messages.Where(m => m.Date > today).Sum(m => m.Text.Length) }) .FirstOrDefault(); if (user == null) { throw new Exception($"*Пользователя \"{search}\" нет в базе.*"); } var remaining = new TimeSpan(0); if (user.Restricted) { remaining = user.Until - DateTime.UtcNow; } sw.Stop(); return($"*Имя: {user.FirstName} {user.LastName}\n*" + $"*ID: {user.Id}\n*" + $"*Ник: {user.UserName}*\n\n" + string.Format("*Активность:* {0:F2}%\n", user.activity * 100) + $"*Сообщений сегодня:* { user.msgToday }\n" + $"*Сообщений вчера:* { user.msgLastday }\n" + $"*Всего сообщений:* { user.msgTotal }\n" + $"*Банов:* { user.RestrictionCount }\n\n" + (user.Description != null ? $"*О себе:* \n_{ user.Description }_\n\n" : "") + (remaining.Ticks != 0 ? $"💢`Сейчас забанен, осталось: { $"{remaining:hh\\:mm\\:ss}`\n" }" : "") + $"`{sw.ElapsedMilliseconds / 1000.0}сек`"); }