Skip to content

Commit

Permalink
Remove debug prints and prevent activities from being registered in r…
Browse files Browse the repository at this point in the history
…ead only mode
  • Loading branch information
Slendy committed May 13, 2024
1 parent 27cbb14 commit 180cac5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public ActivityController(DatabaseContext database)
this.database = database;
}

/// <summary>
/// This method is only used for LBP2 so we exclude playlists
/// </summary>
private async Task<IQueryable<ActivityDto>> GetFilters
(
IQueryable<ActivityDto> dtoQuery,
Expand Down Expand Up @@ -123,8 +120,6 @@ private async Task<IQueryable<ActivityDto>> GetFilters
dto.Activity.Type != EventType.AddLevelToPlaylist);
}

Logger.Debug(predicate.ToString(), LogArea.Activity);

dtoQuery = dtoQuery.Where(predicate);

return dtoQuery;
Expand Down Expand Up @@ -344,8 +339,6 @@ public async Task<IActionResult> LocalActivity(string? slotType, int slotId, str

List<OuterActivityGroup> outerGroups = groups.ToOuterActivityGroups();

PrintOuterGroups(outerGroups);

long oldestTimestamp = GetOldestTime(groups, times.Start).ToUnixTimeMilliseconds();

await this.CacheEntities(outerGroups);
Expand Down
21 changes: 16 additions & 5 deletions ProjectLighthouse/Database/ActivityInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ private class CustomTrackedEntity
public required object OldEntity { get; init; }
}

private readonly ConcurrentDictionary<(Type Type, int HashCode), CustomTrackedEntity> unsavedEntities;
private struct TrackedEntityKey
{
public Type Type { get; set; }
public int HashCode { get; set; }
public Guid ContextId { get; set; }
}

private readonly ConcurrentDictionary<TrackedEntityKey, CustomTrackedEntity> unsavedEntities;
private readonly IEntityEventHandler eventHandler;

public ActivityInterceptor(IEntityEventHandler eventHandler)
{
this.eventHandler = eventHandler;
this.unsavedEntities = new ConcurrentDictionary<(Type Type, int HashCode), CustomTrackedEntity>();
this.unsavedEntities = new ConcurrentDictionary<TrackedEntityKey, CustomTrackedEntity>();
}

#region Hooking stuff
Expand Down Expand Up @@ -78,8 +85,12 @@ private void SaveNewEntities(DbContextEventData eventData)
if (entry.Metadata.Name.Contains("Token")) continue;

if (entry.State is not (EntityState.Added or EntityState.Deleted or EntityState.Modified)) continue;

this.unsavedEntities.TryAdd((entry.Entity.GetType(), entry.Entity.GetHashCode()),
this.unsavedEntities.TryAdd(new TrackedEntityKey
{
ContextId = context.ContextId.InstanceId,
Type = entry.Entity.GetType(),
HashCode = entry.Entity.GetHashCode(),
},
new CustomTrackedEntity
{
State = entry.State,
Expand All @@ -97,7 +108,7 @@ private void ParseInsertedEntities(DbContextEventData eventData)

List<EntityEntry> entries = context.ChangeTracker.Entries().ToList();

foreach (KeyValuePair<(Type Type, int HashCode), CustomTrackedEntity> kvp in this.unsavedEntities)
foreach (KeyValuePair<TrackedEntityKey, CustomTrackedEntity> kvp in this.unsavedEntities)
{
EntityEntry entry = entries.FirstOrDefault(e =>
e.Metadata.ClrType == kvp.Key.Type && e.Entity.GetHashCode() == kvp.Key.HashCode);
Expand Down
29 changes: 4 additions & 25 deletions ProjectLighthouse/Types/Activity/ActivityEntityEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Logging;
Expand All @@ -13,18 +14,13 @@
using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Types.Logging;
using Microsoft.EntityFrameworkCore;
#if DEBUG
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
#endif

namespace LBPUnion.ProjectLighthouse.Types.Activity;

public class ActivityEntityEventHandler : IEntityEventHandler
{
public void OnEntityInserted<T>(DatabaseContext database, T entity) where T : class
{
Logger.Debug($@"OnEntityInserted: {entity.GetType().Name}", LogArea.Activity);
ActivityEntity? activity = entity switch
{
SlotEntity slot => slot.Type switch
Expand All @@ -50,7 +46,7 @@ public void OnEntityInserted<T>(DatabaseContext database, T entity) where T : cl
},
_ => null,
},
CommentType.Profile => new UserCommentActivityEntity()
CommentType.Profile => new UserCommentActivityEntity
{
Type = EventType.CommentOnUser,
CommentId = comment.CommentId,
Expand Down Expand Up @@ -199,6 +195,8 @@ private static void InsertActivity(DatabaseContext database, ActivityEntity? act
{
if (activity == null) return;

if (ServerConfiguration.Instance.UserGeneratedContentLimits.ReadOnlyMode) return;

Logger.Debug("Inserting activity: " + activity.GetType().Name, LogArea.Activity);

RemoveDuplicateEvents(database, activity);
Expand All @@ -210,24 +208,6 @@ private static void InsertActivity(DatabaseContext database, ActivityEntity? act

public void OnEntityChanged<T>(DatabaseContext database, T origEntity, T currentEntity) where T : class
{
#if DEBUG
foreach (PropertyInfo propInfo in currentEntity.GetType().GetProperties())
{
if (!propInfo.CanRead || !propInfo.CanWrite) continue;

if (propInfo.CustomAttributes.Any(c => c.AttributeType == typeof(NotMappedAttribute))) continue;

object? origVal = propInfo.GetValue(origEntity);
object? newVal = propInfo.GetValue(currentEntity);
if ((origVal == null && newVal == null) || (origVal != null && newVal != null && origVal.Equals(newVal))) continue;

Logger.Debug($@"Value for {propInfo.Name} changed", LogArea.Activity);
Logger.Debug($@"Orig val: {origVal?.ToString() ?? "null"}", LogArea.Activity);
Logger.Debug($@"New val: {newVal?.ToString() ?? "null"}", LogArea.Activity);
}
Logger.Debug($@"OnEntityChanged: {currentEntity.GetType().Name}", LogArea.Activity);
#endif

ActivityEntity? activity = null;
switch (currentEntity)
{
Expand Down Expand Up @@ -354,7 +334,6 @@ public void OnEntityChanged<T>(DatabaseContext database, T origEntity, T current

public void OnEntityDeleted<T>(DatabaseContext database, T entity) where T : class
{
Logger.Debug($@"OnEntityDeleted: {entity.GetType().Name}", LogArea.Activity);
ActivityEntity? activity = entity switch
{
HeartedLevelEntity heartedLevel => heartedLevel.Slot.Type switch
Expand Down

0 comments on commit 180cac5

Please sign in to comment.