Improvements, new data in rewind point

🐛 Fix most called rewind point unable to get real data
This commit is contained in:
2025-12-27 01:19:24 +08:00
parent 4443da5660
commit 50518351bc
4 changed files with 122 additions and 39 deletions

View File

@@ -1,6 +1,8 @@
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto; using DysonNetwork.Shared.Proto;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NodaTime; using NodaTime;
using NodaTime.TimeZones;
namespace DysonNetwork.Pass.Rewind; namespace DysonNetwork.Pass.Rewind;
@@ -15,6 +17,32 @@ public class PassRewindService(AppDatabase db)
var startDate = new LocalDate(year - 1, 12, 26).AtMidnight().InUtc().ToInstant(); var startDate = new LocalDate(year - 1, 12, 26).AtMidnight().InUtc().ToInstant();
var endDate = new LocalDate(year, 12, 26).AtMidnight().InUtc().ToInstant(); var endDate = new LocalDate(year, 12, 26).AtMidnight().InUtc().ToInstant();
var timeZone = (await db.AccountProfiles
.Where(p => p.AccountId == accountId)
.FirstOrDefaultAsync())?.TimeZone;
var zone = TimeZoneInfo.Utc;
if (!string.IsNullOrEmpty(timeZone))
try
{
zone = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
}
catch (DateTimeZoneNotFoundException)
{
// use UTC
}
var newFriendsCount = await db.AccountRelationships
.Where(r => r.CreatedAt >= startDate && r.CreatedAt < endDate)
.Where(r => r.AccountId == accountId)
.Where(r => r.Status == RelationshipStatus.Friends)
.CountAsync();
var newBlockedCount = await db.AccountRelationships
.Where(r => r.CreatedAt >= startDate && r.CreatedAt < endDate)
.Where(r => r.AccountId == accountId)
.Where(r => r.Status == RelationshipStatus.Blocked)
.CountAsync();
var checkInDates = await db.AccountCheckInResults var checkInDates = await db.AccountCheckInResults
.Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate) .Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate)
.Where(a => a.AccountId == accountId) .Where(a => a.AccountId == accountId)
@@ -53,13 +81,28 @@ public class PassRewindService(AppDatabase db)
.OrderByDescending(g => g.Count) .OrderByDescending(g => g.Count)
.FirstOrDefault(); .FirstOrDefault();
var actionTimes = await db.ActionLogs var actionTimes = actionDates
.Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate) .Select(a => TimeZoneInfo.ConvertTimeFromUtc(a, zone).TimeOfDay)
.Where(a => a.AccountId == accountId) .ToList();
.Select(a => a.CreatedAt.ToDateTimeUtc())
.ToListAsync();
TimeSpan? latestActiveTime = actionTimes.Any() ? actionTimes.Max(dt => dt.TimeOfDay) : null; TimeSpan? latestActiveTime = null;
if (actionTimes.Count != 0)
{
var timesBefore6Am = actionTimes.Where(t => t < TimeSpan.FromHours(6)).ToList();
latestActiveTime = timesBefore6Am.Count != 0 ? timesBefore6Am.Max() : actionTimes.Max();
}
var lotteriesQuery = db.Lotteries
.Where(l => l.CreatedAt >= startDate && l.CreatedAt < endDate)
.Where(l => l.AccountId == accountId)
.AsQueryable();
var lotteriesWins = await lotteriesQuery
.Where(l => l.MatchedRegionOneNumbers != null && l.MatchedRegionOneNumbers.Count > 0)
.CountAsync();
var lotteriesLosses = await lotteriesQuery
.Where(l => l.MatchedRegionOneNumbers == null || l.MatchedRegionOneNumbers.Count == 0)
.CountAsync();
var lotteriesWinRate = lotteriesWins / (double)(lotteriesWins + lotteriesLosses);
var data = new Dictionary<string, object?> var data = new Dictionary<string, object?>
{ {
@@ -68,6 +111,11 @@ public class PassRewindService(AppDatabase db)
["most_active_day"] = mostActiveDay?.Date.ToString("yyyy-MM-dd"), ["most_active_day"] = mostActiveDay?.Date.ToString("yyyy-MM-dd"),
["most_active_weekday"] = mostActiveWeekday?.Day.ToString(), ["most_active_weekday"] = mostActiveWeekday?.Day.ToString(),
["latest_active_time"] = latestActiveTime?.ToString(@"hh\:mm"), ["latest_active_time"] = latestActiveTime?.ToString(@"hh\:mm"),
["new_friends_count"] = newFriendsCount,
["new_blocked_count"] = newBlockedCount,
["lotteries_wins"] = lotteriesWins,
["lotteries_losses"] = lotteriesLosses,
["lotteries_win_rate"] = lotteriesWinRate,
}; };
return new RewindEvent return new RewindEvent

View File

@@ -226,13 +226,13 @@ public class ChatRoomService(
{ {
var topMembers = await db.ChatMessages var topMembers = await db.ChatMessages
.Where(m => m.ChatRoomId == roomId && m.CreatedAt >= startDate && m.CreatedAt < endDate) .Where(m => m.ChatRoomId == roomId && m.CreatedAt >= startDate && m.CreatedAt < endDate)
.GroupBy(m => m.SenderId) .GroupBy(m => m.Sender.AccountId)
.Select(g => new { SenderId = g.Key, MessageCount = g.Count() }) .Select(g => new { AccountId = g.Key, MessageCount = g.Count() })
.OrderByDescending(g => g.MessageCount) .OrderByDescending(g => g.MessageCount)
.Take(3) .Take(3)
.ToListAsync(); .ToListAsync();
var accountIds = topMembers.Select(t => t.SenderId).ToList(); var accountIds = topMembers.Select(t => t.AccountId).ToList();
var accounts = await remoteAccounts.GetAccountBatch(accountIds); var accounts = await remoteAccounts.GetAccountBatch(accountIds);
return accounts.Select(SnAccount.FromProtoValue).ToList(); return accounts.Select(SnAccount.FromProtoValue).ToList();
} }

View File

@@ -310,17 +310,19 @@ public class PublisherService(
var publisher = await db.Publishers.FirstOrDefaultAsync(e => e.Name == name); var publisher = await db.Publishers.FirstOrDefaultAsync(e => e.Name == name);
if (publisher is null) return null; if (publisher is null) return null;
var postsCount = await db.Posts.Where(e => e.Publisher.Id == publisher.Id).CountAsync(); var postsCount = await db.Posts.Where(e => e.PublisherId == publisher.Id).CountAsync();
var postsUpvotes = await db.PostReactions var postsUpvotes = await db.PostReactions
.Where(r => r.Post.Publisher.Id == publisher.Id && .Where(r => r.Post.PublisherId == publisher.Id &&
r.Attitude == Shared.Models.PostReactionAttitude.Positive) r.Attitude == Shared.Models.PostReactionAttitude.Positive)
.CountAsync(); .CountAsync();
var postsDownvotes = await db.PostReactions var postsDownvotes = await db.PostReactions
.Where(r => r.Post.Publisher.Id == publisher.Id && .Where(r => r.Post.PublisherId == publisher.Id &&
r.Attitude == Shared.Models.PostReactionAttitude.Negative) r.Attitude == Shared.Models.PostReactionAttitude.Negative)
.CountAsync(); .CountAsync();
var stickerPacksId = await db.StickerPacks.Where(e => e.Publisher.Id == publisher.Id).Select(e => e.Id) var stickerPacksId = await db.StickerPacks
.Where(e => e.PublisherId == publisher.Id)
.Select(e => e.Id)
.ToListAsync(); .ToListAsync();
var stickerPacksCount = stickerPacksId.Count; var stickerPacksCount = stickerPacksId.Count;

View File

@@ -6,6 +6,7 @@ using DysonNetwork.Sphere.Chat;
using Grpc.Core; using Grpc.Core;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NodaTime; using NodaTime;
using PostReactionAttitude = DysonNetwork.Shared.Models.PostReactionAttitude;
namespace DysonNetwork.Sphere.Rewind; namespace DysonNetwork.Sphere.Rewind;
@@ -28,7 +29,7 @@ public class SphereRewindServiceGrpc(
var mostLovedPublisherClue = var mostLovedPublisherClue =
await db.PostReactions await db.PostReactions
.Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate) .Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate)
.Where(p => p.AccountId == accountId && p.Attitude == Shared.Models.PostReactionAttitude.Positive) .Where(p => p.AccountId == accountId && p.Attitude == PostReactionAttitude.Positive)
.GroupBy(p => p.Post.PublisherId) .GroupBy(p => p.Post.PublisherId)
.OrderByDescending(g => g.Count()) .OrderByDescending(g => g.Count())
.Select(g => new { PublisherId = g.Key, ReactionCount = g.Count() }) .Select(g => new { PublisherId = g.Key, ReactionCount = g.Count() })
@@ -47,7 +48,7 @@ public class SphereRewindServiceGrpc(
await db.PostReactions await db.PostReactions
.Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate) .Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate)
.Where(pr => .Where(pr =>
pr.Attitude == Shared.Models.PostReactionAttitude.Positive && pr.Attitude == PostReactionAttitude.Positive &&
publishers.Contains(pr.Post.PublisherId)) publishers.Contains(pr.Post.PublisherId))
.GroupBy(pr => pr.AccountId) .GroupBy(pr => pr.AccountId)
.OrderByDescending(g => g.Count()) .OrderByDescending(g => g.Count())
@@ -62,6 +63,11 @@ public class SphereRewindServiceGrpc(
.Where(p => publishers.Contains(p.PublisherId)) .Where(p => publishers.Contains(p.PublisherId))
.AsQueryable(); .AsQueryable();
var postTotalCount = await posts.CountAsync(); var postTotalCount = await posts.CountAsync();
var postTotalUpvotes = await db.PostReactions
.Where(a => a.CreatedAt >= startDate && a.CreatedAt < endDate)
.Where(p => publishers.Contains(p.Post.PublisherId))
.Where(r => r.Attitude == PostReactionAttitude.Positive)
.CountAsync();
var mostPopularPost = await posts var mostPopularPost = await posts
.OrderByDescending(p => p.Upvotes - p.Downvotes) .OrderByDescending(p => p.Upvotes - p.Downvotes)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
@@ -78,20 +84,21 @@ public class SphereRewindServiceGrpc(
.Where(m => m.CreatedAt >= startDate && m.CreatedAt < endDate) .Where(m => m.CreatedAt >= startDate && m.CreatedAt < endDate)
.Where(m => m.Sender.AccountId == accountId) .Where(m => m.Sender.AccountId == accountId)
.AsQueryable(); .AsQueryable();
var mostMessagedChat = await messagesQuery var mostMessagedChatInfo = await messagesQuery
.Where(m => m.ChatRoom.Type == Shared.Models.ChatRoomType.Group) .Where(m => m.ChatRoom.Type == 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 => new { ChatRoom = g.First().ChatRoom, MessageCount = g.Count() })
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
var mostMessagedDirectChat = await messagesQuery var mostMessagedChat = mostMessagedChatInfo?.ChatRoom;
.Where(m => m.ChatRoom.Type == Shared.Models.ChatRoomType.DirectMessage) var mostMessagedDirectChatInfo = await messagesQuery
.Where(m => m.ChatRoom.Type == ChatRoomType.DirectMessage)
.GroupBy(m => m.ChatRoomId) .GroupBy(m => m.ChatRoomId)
.OrderByDescending(g => g.Count()) .OrderByDescending(g => g.Count())
.Select(g => g.First().ChatRoom) .Select(g => new { ChatRoom = g.First().ChatRoom, MessageCount = g.Count() })
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
mostMessagedDirectChat = mostMessagedDirectChat is not null var mostMessagedDirectChat = mostMessagedDirectChatInfo is not null
? await crs.LoadDirectMessageMembers(mostMessagedDirectChat, accountId) ? await crs.LoadDirectMessageMembers(mostMessagedDirectChatInfo.ChatRoom, accountId)
: null; : null;
// Call data // Call data
@@ -104,25 +111,28 @@ public class SphereRewindServiceGrpc(
var now = SystemClock.Instance.GetCurrentInstant(); var now = SystemClock.Instance.GetCurrentInstant();
var callDurations = await callQuery var callDurations = await callQuery
.Where(c => c.Room.Type == Shared.Models.ChatRoomType.Group) .Where(c => c.Room.Type == ChatRoomType.Group)
.Select(c => new { RoomId = c.RoomId, Duration = c.CreatedAt.Minus(c.EndedAt ?? now).Seconds }) .Select(c => new { c.RoomId, Duration = c.CreatedAt.Minus(c.EndedAt ?? now).Seconds })
.ToListAsync(); .ToListAsync();
var mostCalledRoomId = callDurations var mostCalledRoomInfo = callDurations
.GroupBy(c => c.RoomId) .GroupBy(c => c.RoomId)
.OrderByDescending(g => g.Sum(c => c.Duration)) .Select(g => new { RoomId = g.Key, TotalDuration = g.Sum(c => c.Duration) })
.Select(g => g.Key) .OrderByDescending(g => g.TotalDuration)
.FirstOrDefault(); .FirstOrDefault();
var mostCalledRoom = mostCalledRoomId != Guid.Empty ? await db.ChatRooms.FindAsync(mostCalledRoomId) : null; var mostCalledRoom = mostCalledRoomInfo != null && mostCalledRoomInfo.RoomId != Guid.Empty
? await db.ChatRooms.FindAsync(mostCalledRoomInfo.RoomId)
: null;
List<SnAccount>? mostCalledChatTopMembers = null; List<SnAccount>? mostCalledChatTopMembers = null;
if (mostCalledRoom != null) if (mostCalledRoom != null)
mostCalledChatTopMembers = await crs.GetTopActiveMembers(mostCalledRoom.Id, startDate, endDate); 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 == ChatRoomType.DirectMessage)
.GroupBy(c => c.RoomId) .GroupBy(c => c.RoomId)
.Select(g => new { ChatRoom = g.First().Room, CallCount = g.Count() }) .Select(g => new
.OrderByDescending(g => g.CallCount) { ChatRoom = g.First().Room, TotalDuration = g.Sum(c => c.CreatedAt.Minus(c.EndedAt ?? now).Seconds) })
.OrderByDescending(g => g.TotalDuration)
.Take(3) .Take(3)
.ToListAsync(); .ToListAsync();
@@ -134,12 +144,19 @@ public class SphereRewindServiceGrpc(
if (otherMember != null) if (otherMember != null)
accountIds.Add(otherMember.AccountId); accountIds.Add(otherMember.AccountId);
} }
var mostCalledAccounts = await remoteAccounts.GetAccountBatch(accountIds);
var accounts = await remoteAccounts.GetAccountBatch(accountIds);
var mostCalledAccounts = accounts.Zip(mostCalledDirectRooms,
(account, room) => new Dictionary<string, object?>
{ ["account"] = account, ["duration"] = room.TotalDuration }
)
.ToList();
var data = new Dictionary<string, object?> var data = new Dictionary<string, object?>
{ {
["total_count"] = postTotalCount, ["total_post_count"] = postTotalCount,
["total_upvote_count"] = postTotalUpvotes,
["most_popular_post"] = mostPopularPost, ["most_popular_post"] = mostPopularPost,
["most_productive_day"] = mostProductiveDay is not null ["most_productive_day"] = mostProductiveDay is not null
? new Dictionary<string, object?> ? new Dictionary<string, object?>
@@ -162,9 +179,25 @@ public class SphereRewindServiceGrpc(
["upvote_counts"] = mostLovedAudienceClue.ReactionCount, ["upvote_counts"] = mostLovedAudienceClue.ReactionCount,
} }
: null, : null,
["most_messaged_chat"] = mostMessagedChat, ["most_messaged_chat"] = mostMessagedChatInfo is not null
["most_messaged_direct_chat"] = mostMessagedDirectChat, ? new Dictionary<string, object?>
["most_called_chat"] = mostCalledRoom, {
["chat"] = mostMessagedChat,
["message_counts"] = mostMessagedChatInfo.MessageCount,
}
: null,
["most_messaged_direct_chat"] = mostMessagedDirectChatInfo is not null
? new Dictionary<string, object?>
{
["chat"] = mostMessagedDirectChat,
["message_counts"] = mostMessagedDirectChatInfo.MessageCount
}
: null,
["most_called_chat"] = new Dictionary<string, object?>
{
["chat"] = mostCalledRoom,
["duration"] = mostCalledRoomInfo?.TotalDuration
},
["most_called_chat_top_members"] = mostCalledChatTopMembers, ["most_called_chat_top_members"] = mostCalledChatTopMembers,
["most_called_accounts"] = mostCalledAccounts, ["most_called_accounts"] = mostCalledAccounts,
}; };