Add more data to rewind

This commit is contained in:
2025-12-26 01:08:10 +08:00
parent 4e9c5733d1
commit b193224a2c
4 changed files with 63 additions and 15 deletions

View File

@@ -34,7 +34,7 @@ public class AccountRewindService(
private async Task<SnRewindPoint> CreateRewindPoint(Guid accountId) private async Task<SnRewindPoint> CreateRewindPoint(Guid accountId)
{ {
var currentYear = DateTime.UtcNow.Year; const int currentYear = 2025;
var rewindRequest = new RequestRewindEvent { AccountId = accountId.ToString(), Year = currentYear}; var rewindRequest = new RequestRewindEvent { AccountId = accountId.ToString(), Year = currentYear};
var rewindEventTasks = new List<Task<RewindEvent>> var rewindEventTasks = new List<Task<RewindEvent>>

View File

@@ -23,19 +23,51 @@ public class PassRewindService(AppDatabase db)
.OrderBy(d => d) .OrderBy(d => d)
.ToListAsync(); .ToListAsync();
var maxCheckInStrike = 0; var maxCheckInStreak = 0;
if (checkInDates.Count != 0) if (checkInDates.Count != 0)
{ {
maxCheckInStrike = checkInDates maxCheckInStreak = checkInDates
.Select((d, i) => new { Date = d, Index = i }) .Select((d, i) => new { Date = d, Index = i })
.GroupBy(x => x.Date.Subtract(new TimeSpan(x.Index, 0, 0, 0))) .GroupBy(x => x.Date.Subtract(new TimeSpan(x.Index, 0, 0, 0)))
.Select(g => g.Count()) .Select(g => g.Count())
.Max(); .Max();
} }
var checkInCompleteness = checkInDates.Count / 365.0;
var actionDates = await db.ActionLogs
.Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate)
.Where(a => a.AccountId == accountId)
.Select(a => a.CreatedAt.ToDateTimeUtc().Date)
.ToListAsync();
var mostActiveDay = actionDates
.GroupBy(d => d)
.Select(g => new { Date = g.Key, Count = g.Count() })
.OrderByDescending(g => g.Count)
.FirstOrDefault();
var mostActiveWeekday = actionDates
.GroupBy(d => d.DayOfWeek)
.Select(g => new { Day = g.Key, Count = g.Count() })
.OrderByDescending(g => g.Count)
.FirstOrDefault();
var actionTimes = await db.ActionLogs
.Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate)
.Where(a => a.AccountId == accountId)
.Select(a => a.CreatedAt.ToDateTimeUtc())
.ToListAsync();
TimeSpan? latestActiveTime = actionTimes.Any() ? actionTimes.Max(dt => dt.TimeOfDay) : null;
var data = new Dictionary<string, object?> var data = new Dictionary<string, object?>
{ {
["max_check_in_strike"] = maxCheckInStrike, ["max_check_in_streak"] = maxCheckInStreak,
["check_in_completeness"] = checkInCompleteness,
["most_active_day"] = mostActiveDay?.Date.ToString("yyyy-MM-dd"),
["most_active_weekday"] = mostActiveWeekday?.Day.ToString(),
["latest_active_time"] = latestActiveTime?.ToString(@"hh\:mm"),
}; };
return new RewindEvent return new RewindEvent

View File

@@ -208,7 +208,7 @@ public class ChatRoomService(
{ {
var group = $"chatroom:subscribers:{roomId}"; var group = $"chatroom:subscribers:{roomId}";
var keys = (await cache.GetGroupKeysAsync(group)).ToList(); var keys = (await cache.GetGroupKeysAsync(group)).ToList();
var memberIds = new List<Guid>(keys.Count); var memberIds = new List<Guid>(keys.Count);
foreach (var key in keys) foreach (var key in keys)
{ {
@@ -218,7 +218,22 @@ public class ChatRoomService(
memberIds.Add(memberId); memberIds.Add(memberId);
} }
} }
return memberIds; return memberIds;
} }
public async Task<List<SnAccount>> GetTopActiveMembers(Guid roomId, Instant startDate, Instant endDate)
{
var topMembers = await db.ChatMessages
.Where(m => m.ChatRoomId == roomId && m.CreatedAt >= startDate && m.CreatedAt < endDate)
.GroupBy(m => m.SenderId)
.Select(g => new { SenderId = g.Key, MessageCount = g.Count() })
.OrderByDescending(g => g.MessageCount)
.Take(3)
.ToListAsync();
var accountIds = topMembers.Select(t => t.SenderId).ToList();
var accounts = await remoteAccounts.GetAccountBatch(accountIds);
return accounts.Select(SnAccount.FromProtoValue).ToList();
}
} }

View File

@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto; using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry; using DysonNetwork.Shared.Registry;
using DysonNetwork.Sphere.Chat; using DysonNetwork.Sphere.Chat;
@@ -79,7 +79,7 @@ public class SphereRewindServiceGrpc(
.Where(m => m.Sender.AccountId == accountId) .Where(m => m.Sender.AccountId == accountId)
.AsQueryable(); .AsQueryable();
var mostMessagedChat = await messagesQuery var mostMessagedChat = await messagesQuery
.Where(m => m.ChatRoom.Type != Shared.Models.ChatRoomType.Group) .Where(m => m.ChatRoom.Type == Shared.Models.ChatRoomType.Group)
.GroupBy(m => m.ChatRoomId) .GroupBy(m => m.ChatRoomId)
.OrderByDescending(g => g.Count()) .OrderByDescending(g => g.Count())
.Select(g => g.First().ChatRoom) .Select(g => g.First().ChatRoom)
@@ -102,16 +102,17 @@ public class SphereRewindServiceGrpc(
.Where(c => c.Sender.AccountId == accountId) .Where(c => c.Sender.AccountId == accountId)
.AsQueryable(); .AsQueryable();
var now = SystemClock.Instance.GetCurrentInstant();
var mostCalledRoom = await callQuery var mostCalledRoom = await callQuery
.Where(c => c.Room.Type == Shared.Models.ChatRoomType.Group)
.GroupBy(c => c.RoomId) .GroupBy(c => c.RoomId)
.OrderByDescending(g => g.Count()) .OrderByDescending(g => g.Sum(c => c.CreatedAt.Minus(c.EndedAt ?? now).Seconds))
.Select(g => g.First().Room) .Select(g => g.First().Room)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (mostCalledRoom != null && mostCalledRoom.Type == Shared.Models.ChatRoomType.DirectMessage) List<SnAccount>? mostCalledChatTopMembers = null;
{ if (mostCalledRoom != null)
mostCalledRoom = await crs.LoadDirectMessageMembers(mostCalledRoom, accountId); mostCalledChatTopMembers = await crs.GetTopActiveMembers(mostCalledRoom.Id, startDate, endDate);
}
var mostCalledDirectRooms = await callQuery var mostCalledDirectRooms = await callQuery
.Where(c => c.Room.Type == Shared.Models.ChatRoomType.DirectMessage) .Where(c => c.Room.Type == Shared.Models.ChatRoomType.DirectMessage)
@@ -129,9 +130,8 @@ public class SphereRewindServiceGrpc(
if (otherMember != null) if (otherMember != null)
accountIds.Add(otherMember.AccountId); accountIds.Add(otherMember.AccountId);
} }
var mostCalledAccounts = await remoteAccounts.GetAccountBatch(accountIds); var mostCalledAccounts = await remoteAccounts.GetAccountBatch(accountIds);
var data = new Dictionary<string, object?> var data = new Dictionary<string, object?>
{ {
@@ -161,6 +161,7 @@ public class SphereRewindServiceGrpc(
["most_messaged_chat"] = mostMessagedChat, ["most_messaged_chat"] = mostMessagedChat,
["most_messaged_direct_chat"] = mostMessagedDirectChat, ["most_messaged_direct_chat"] = mostMessagedDirectChat,
["most_called_chat"] = mostCalledRoom, ["most_called_chat"] = mostCalledRoom,
["most_called_chat_top_members"] = mostCalledChatTopMembers,
["most_called_accounts"] = mostCalledAccounts, ["most_called_accounts"] = mostCalledAccounts,
}; };