Skip to content

Commit

Permalink
Merge pull request #714 from Blazam-App/v1-Dev
Browse files Browse the repository at this point in the history
Memory Optimizations
  • Loading branch information
jacobsen9026 authored Dec 22, 2024
2 parents a07472a + fff83af commit 27b79ba
Show file tree
Hide file tree
Showing 28 changed files with 217 additions and 83 deletions.
2 changes: 1 addition & 1 deletion BLAZAM/BLAZAM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<ServerGarbageCollection>false</ServerGarbageCollection>
<AssemblyVersion>1.2.3</AssemblyVersion>
<Version>2024.12.19.2303</Version>
<Version>2024.12.22.1816</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RootNamespace>BLAZAM</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
Expand Down
2 changes: 1 addition & 1 deletion BLAZAM/Middleware/ApplicationStatusRedirectMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ApplicationStatusRedirectMiddleware(
_monitor = monitor;
}

public async Task InvokeAsync(HttpContext context, IAppDatabaseFactory factory)
public async Task InvokeAsync(HttpContext context, IUserDatabaseFactory factory)
{
intendedUri = context.Request.Path.ToUriComponent();
if (!InIgnoreList(intendedUri))
Expand Down
4 changes: 2 additions & 2 deletions BLAZAM/Pages/API/v1/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ApiController : Controller
/// <summary>
/// A factory for <see cref="IDatabaseContext"/> connections
/// </summary>
protected readonly IAppDatabaseFactory DbFactory;
protected readonly IUserDatabaseFactory DbFactory;
/// <summary>
/// The API audit logger
/// </summary>
Expand All @@ -45,7 +45,7 @@ public class ApiController : Controller
/// </summary>
protected IApplicationUserState? CurrentUserState { get; }

public ApiController(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory)
public ApiController(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory)
{
//User = httpContextAccessor.HttpContext.User;
AuditLogger = audit;
Expand Down
2 changes: 1 addition & 1 deletion BLAZAM/Pages/API/v1/Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace BLAZAM.Pages.API.v1
[Produces("application/json")]
public class Search : ApiController
{
public Search(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory)
public Search(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory)
{
}

Expand Down
4 changes: 2 additions & 2 deletions BLAZAM/Pages/API/v1/Templates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ namespace BLAZAM.Pages.API.v1
/// </summary>
public class Templates : ApiController
{
private IAppDatabaseFactory _appDatabaseFactory;
private IUserDatabaseFactory _appDatabaseFactory;
private IStringLocalizer<AppLocalization> AppLocalization;
private EmailService EmailService;
private NotificationGenerationService OUNotificationService;

public Templates(NotificationGenerationService ouNotificationService, EmailService email, IApplicationUserStateService applicationUserStateService, IStringLocalizer<AppLocalization> localizer, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory)
public Templates(NotificationGenerationService ouNotificationService, EmailService email, IApplicationUserStateService applicationUserStateService, IStringLocalizer<AppLocalization> localizer, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory)
{
AppLocalization = localizer;
EmailService = email;
Expand Down
2 changes: 1 addition & 1 deletion BLAZAM/Pages/API/v1/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace BLAZAM.Pages.API.v1
{
public class Test : ApiController
{
public Test(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IAppDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory)
public Test(IApplicationUserStateService applicationUserStateService, AuditLogger audit, IUserDatabaseFactory appDatabaseFactory, IHttpContextAccessor httpContextAccessor, IActiveDirectoryContextFactory adFactory) : base(applicationUserStateService, audit, appDatabaseFactory, httpContextAccessor, adFactory)
{
}

Expand Down
2 changes: 1 addition & 1 deletion BLAZAM/Pages/Benchmark.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inject IAppDatabaseFactory factory
@inject IUserDatabaseFactory factory
@inject IActiveDirectoryContextFactory directoryFactory
@inherits AppComponentBase
@page "/benchmark"
Expand Down
2 changes: 1 addition & 1 deletion BLAZAM/Pages/Error/UnhandledExceptionPage.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@inject EmailService email
@inject ApplicationInfo ApplicationInfo
@inject IAppDatabaseFactory factory
@inject IUserDatabaseFactory factory
@inject NavigationManager Nav
<PageTitle>Error</PageTitle>

Expand Down
4 changes: 2 additions & 2 deletions BLAZAM/Pages/PWAManifest.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class PWAManifest
[Produces("application/json")]
public class PWAManifestModel : PageModel
{
private readonly IAppDatabaseFactory _factory;
private readonly IUserDatabaseFactory _factory;

public PWAManifestModel(IAppDatabaseFactory factory)
public PWAManifestModel(IUserDatabaseFactory factory)
{
_factory = factory;
}
Expand Down
2 changes: 1 addition & 1 deletion BLAZAM/Pages/Static.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class StaticModel : PageModel
protected IDatabaseContext Context { get; private set; }


public StaticModel(IAppDatabaseFactory factory)
public StaticModel(IUserDatabaseFactory factory)
{
Context = factory.CreateDbContext();

Expand Down
2 changes: 1 addition & 1 deletion BLAZAM/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static void SetupKestrel(WebApplicationBuilder builder)
{

var _programDbFactory = new AppDatabaseFactory(Configuration);
var kestrelContext = _programDbFactory.CreateDbContext();
using var kestrelContext = _programDbFactory.CreateDbContext();


if (!ApplicationInfo.isUnderIIS && !Debugger.IsAttached)
Expand Down
3 changes: 2 additions & 1 deletion BLAZAM/ProgramHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public static WebApplicationBuilder InjectServices(this WebApplicationBuilder bu


builder.Services.AddSingleton<IAppDatabaseFactory, AppDatabaseFactory>();
builder.Services.AddScoped<IUserDatabaseFactory, UserDatabaseFactory>();

//Provide an Http client as a service with custom construction via api service class
builder.Services.AddHttpClient();
Expand Down Expand Up @@ -407,7 +408,7 @@ public static void PreRun(this WebApplication application)
//Setup Seq logging if allowed by admin
try
{
var context = Program.AppInstance.Services.GetRequiredService<IAppDatabaseFactory>().CreateDbContext();
using var context = Program.AppInstance.Services.GetRequiredService<IAppDatabaseFactory>().CreateDbContext();
if (context != null && context.AppSettings.FirstOrDefault()?.SendLogsToDeveloper != null)
{
Loggers.SendToSeqServer = context.AppSettings.FirstOrDefault().SendLogsToDeveloper;
Expand Down
38 changes: 38 additions & 0 deletions BLAZAMCommon/Data/ApplicationStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Extensions.ObjectPool;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLAZAM.Common.Data
{
public static class ApplicationStatistics
{
public static int ADContextCount { get; private set; }
public static void AddADContext()
{
ADContextCount++;
}
public static void RemoveADContext()
{
if (ADContextCount > 0)
ADContextCount--;

}


public static int DBContextCount { get; private set; }
public static void AddDBContext()
{
DBContextCount++;
}
public static void RemoveDBContext()
{
if (DBContextCount > 0)
DBContextCount--;

}
}
}
11 changes: 7 additions & 4 deletions BLAZAMDatabase/Context/AppDatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public AppDatabaseFactory(IConfiguration configuration)
/// <exception cref="NotImplementedException"></exception>
private void SeedData()
{
var seedContext = this.CreateDbContext();
using var seedContext = this.CreateDbContext();


SetupDenyAll(seedContext);
Expand Down Expand Up @@ -177,7 +177,10 @@ private bool CheckInstallation()
/// Async call to <see cref="CreateDbContext"/>
/// </summary>
/// <returns></returns>
public async Task<IDatabaseContext> CreateDbContextAsync() => await Task.Run(() => { return CreateDbContext(); });
public async Task<IDatabaseContext> CreateDbContextAsync()
{
return await Task.Run(() => { return CreateDbContext(); });
}
public DatabaseType DatabaseType
{
get
Expand All @@ -190,7 +193,7 @@ public DatabaseType DatabaseType
{

case "sql":
return DatabaseType.SQL;
return DatabaseType.SQL;
case "sqlite":

return DatabaseType.SQLite;
Expand All @@ -217,7 +220,7 @@ public DatabaseType DatabaseType
/// <exception cref="Exception">Thrown for unexpected exceptions</exception>
public IDatabaseContext CreateDbContext()
{

IDatabaseContext? databaseContext = null;
switch (DatabaseType)
{
Expand Down
12 changes: 12 additions & 0 deletions BLAZAMDatabase/Context/DatabaseContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace BLAZAM.Database.Context
public class DatabaseContextBase : DbContext, IDatabaseContext
{

public override void Dispose()
{
ApplicationStatistics.RemoveDBContext();

base.Dispose();
}




Expand Down Expand Up @@ -76,12 +83,15 @@ public enum DatabaseStatus
public DatabaseContextBase()
{
ConnectionString = new("");
ApplicationStatistics.AddDBContext();

}


public DatabaseContextBase(DatabaseConnectionString databaseConnectionString) : base()
{
ConnectionString = databaseConnectionString;
ApplicationStatistics.AddDBContext();

}

Expand All @@ -93,6 +103,8 @@ public DatabaseContextBase(DatabaseConnectionString databaseConnectionString) :

public DatabaseContextBase(DbContextOptions options) : base(options)
{
ApplicationStatistics.AddDBContext();

}

//App Settings
Expand Down
14 changes: 14 additions & 0 deletions BLAZAMDatabase/Context/IUserDatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using BLAZAM.Common.Data.Database;
using BLAZAM.Database.Exceptions;

namespace BLAZAM.Database.Context
{
/// <summary>
/// The primary database factory for BLAZAM.
/// Creates <see cref="IDatabaseContext"/> types
/// that can have a number of database type backings
/// </summary>
public interface IUserDatabaseFactory : IAppDatabaseFactory
{
}
}
52 changes: 52 additions & 0 deletions BLAZAMDatabase/Context/UserDatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using BLAZAM.Common.Data.Database;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLAZAM.Database.Context
{
public class UserDatabaseFactory : IUserDatabaseFactory, IDisposable
{
private readonly IAppDatabaseFactory _appDatabaseFactory;

public UserDatabaseFactory(IAppDatabaseFactory appDatabaseFactory)
{
_appDatabaseFactory = appDatabaseFactory;
}

public DatabaseType DatabaseType => _appDatabaseFactory.DatabaseType;

public bool ApplyDatabaseMigrations(bool force = false) => _appDatabaseFactory.ApplyDatabaseMigrations(force);

public Task<bool> ApplyDatabaseMigrationsAsync(bool force = false) => _appDatabaseFactory.ApplyDatabaseMigrationsAsync(force);

private List<IDatabaseContext> _userContexts = new();

public IDatabaseContext CreateDbContext()
{
var ctx= _appDatabaseFactory.CreateDbContext();
_userContexts.Add(ctx);

return ctx;
}

public async Task<IDatabaseContext> CreateDbContextAsync()
{
var ctx = await _appDatabaseFactory.CreateDbContextAsync();
_userContexts.Add(ctx);

return ctx;
}

public void Dispose()
{
foreach(var ctx in _userContexts)
{
ctx.Dispose();
}
}
}
}
2 changes: 1 addition & 1 deletion BLAZAMGui/Layouts/LoginLayout.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@inherits LayoutComponentBase

@inject IActiveDirectoryContext directory
@inject IAppDatabaseFactory DbFactory
@inject IUserDatabaseFactory DbFactory
@inject ConnMonitor monitor


Expand Down
2 changes: 1 addition & 1 deletion BLAZAMGui/Layouts/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@inject NavigationManager Nav
@inject IActiveDirectoryContext directory
@inject IApplicationUserStateService UserStateService
@inject IAppDatabaseFactory DbFactory
@inject IUserDatabaseFactory DbFactory
@inject AppSnackBarService SnackBarService
@inject ApplicationInfo ApplicationInfo
@inject ConnMonitor monitor
Expand Down
2 changes: 1 addition & 1 deletion BLAZAMGui/UI/AppComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected bool LoadingData
}
//protected IDatabaseContext? Context;
[Inject]
protected IAppDatabaseFactory DbFactory { get; set; }
protected IUserDatabaseFactory DbFactory { get; set; }


protected override void OnInitialized()
Expand Down
Loading

0 comments on commit 27b79ba

Please sign in to comment.