Skip to content

Commit

Permalink
fix(ef-core): cleaning up issues with v10 pre-release builds
Browse files Browse the repository at this point in the history
  • Loading branch information
harlam357 committed May 23, 2022
1 parent b1cd7a3 commit 1f106a3
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 132 deletions.
105 changes: 105 additions & 0 deletions src/HFM.Core/Data/CleanWorkUnitContextPlatforms.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using HFM.Core.Logging;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace HFM.Core.Data;

public class CleanWorkUnitContextPlatforms
{
private readonly ILogger _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;

public CleanWorkUnitContextPlatforms(ILogger logger, IServiceScopeFactory serviceScopeFactory)
{
_logger = logger ?? NullLogger.Instance;
_serviceScopeFactory = serviceScopeFactory;
}

private int _count;
private int _total;
private int _lastPercent;

public async Task ExecuteAsync(IProgress<ProgressInfo> progress)
{
ReportProgressMessage(progress, "Cleaning platform data...");

await RemoveDuplicates(progress).ConfigureAwait(false);
await AddVersion().ConfigureAwait(false);
}

private async Task RemoveDuplicates(IProgress<ProgressInfo> progress)
{
using var scope = _serviceScopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<WorkUnitContext>();
var platforms = await context.Platforms.ToListAsync().ConfigureAwait(false);

_total = platforms.Count;

foreach (var p in platforms)
{
ReportPlatformProgress(progress);

var duplicates = platforms
.Where(x =>
x.ClientVersion == p.ClientVersion &&
x.OperatingSystem == p.OperatingSystem &&
x.Implementation == p.Implementation &&
x.Processor.StartsWith(p.Processor + " (", StringComparison.Ordinal) &&
x.Threads == p.Threads &&
x.DriverVersion == p.DriverVersion &&
x.ComputeVersion == p.ComputeVersion &&
x.CUDAVersion == p.CUDAVersion)
.ToList();

if (duplicates.Any())
{
var query = context.WorkUnits
.Where(x => duplicates
.Select(y => y.ID)
.Contains(x.PlatformID.GetValueOrDefault()));

foreach (var w in query)
{
w.PlatformID = p.ID;
}

foreach (var d in duplicates)
{
context.Platforms.Remove(d);
}
}
}

await context.SaveChangesAsync().ConfigureAwait(false);
}

private async Task AddVersion()
{
using var scope = _serviceScopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<WorkUnitContext>();
context.Versions.Add(new VersionEntity { Version = Application.Version });

await context.SaveChangesAsync().ConfigureAwait(false);
}

private static int CalculatePercent(int count, int total) => ((count * 200) + total) / (total * 2);

private void ReportProgressMessage(IProgress<ProgressInfo> progress, string message)
{
_logger.Info(message);
progress.Report(new ProgressInfo(_lastPercent, message));
}

private void ReportPlatformProgress(IProgress<ProgressInfo> progress)
{
int percent = CalculatePercent(++_count, _total);
if (percent != _lastPercent)
{
_lastPercent = percent;
string message = $"Cleaning platform {_count} of {_total}";
_logger.Info(message);
progress.Report(new ProgressInfo(_lastPercent, message));
}
}
}
134 changes: 2 additions & 132 deletions src/HFM/BootStrapper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using System.Globalization;

using HFM.Core.Data;
using HFM.Core.Logging;
using HFM.Core.Services;
using HFM.Core.WorkUnits;
using HFM.Forms.Models;
using HFM.Forms.Presenters;
using HFM.Forms.Views;
using HFM.Preferences;
using HFM.Proteins;

using LightInject;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace HFM
{
internal class BootStrapper
Expand Down Expand Up @@ -203,29 +197,8 @@ private MainForm InitializeMainForm(ICollection<Argument> arguments)
var mainForm = (MainForm)mainPresenter.Form;
try
{
string appDataPath = Preferences.Get<string>(Preference.ApplicationDataFolderPath);
string filePath = Path.Combine(appDataPath, WorkUnitRepository.DefaultFileName);

ILegacyWorkUnitSource legacyWorkUnitSource = new NullLegacyWorkUnitSource();
if (File.Exists(filePath))
{
var repository = Container.GetInstance<WorkUnitRepository>();
#if DEBUG
//var repository = new WorkUnitRepository(null, CreateProteinService());
#endif
repository.Initialize(filePath);
if (repository.RequiresUpgrade())
{
UpgradeWorkUnitRepository(repository);
}
legacyWorkUnitSource = repository;
}

string databaseVersion = GetDatabaseVersionFromWorkUnitContext();
if (ShouldMigrateToWorkUnitContext(databaseVersion))
{
MigrateToWorkUnitContext(legacyWorkUnitSource);
}
var migration = new WorkUnitContextMigration(Container, Logger, Preferences);
migration.Migrate();
}
catch (Exception ex)
{
Expand All @@ -236,109 +209,6 @@ private MainForm InitializeMainForm(ICollection<Argument> arguments)
return mainForm;
}

private static void UpgradeWorkUnitRepository(WorkUnitRepository repository)
{
using var dialog = new ProgressDialog((progress, _) =>
{
repository.Upgrade(progress);
return Task.CompletedTask;
}, false);

dialog.Text = Core.Application.NameAndVersion;
dialog.StartPosition = FormStartPosition.CenterScreen;
dialog.ShowDialog();
if (dialog.Exception != null)
{
throw dialog.Exception;
}
}

private string GetDatabaseVersionFromWorkUnitContext()
{
using (Container.BeginScope())
{
using var context = Container.GetInstance<WorkUnitContext>();
context.Database.Migrate();
return context.GetDatabaseVersion();
}
}

private static bool ShouldMigrateToWorkUnitContext(string databaseVersion) =>
databaseVersion is not null && Version.Parse(databaseVersion) < new Version(10, 0, 0);

private void MigrateToWorkUnitContext(ILegacyWorkUnitSource repository)
{
var toWorkUnitContext = new MigrateToWorkUnitContext(Logger, Container.GetInstance<IServiceScopeFactory>(), repository);

using var dialog = new ProgressDialog(async (progress, _) => await toWorkUnitContext.ExecuteAsync(progress).ConfigureAwait(true), false);
dialog.Text = Core.Application.NameAndVersion;
dialog.StartPosition = FormStartPosition.CenterScreen;
dialog.ShowDialog();
if (dialog.Exception != null)
{
throw dialog.Exception;
}
}

#if DEBUG
public static IProteinService CreateProteinService()
{
var collection = new List<Protein>();

var protein = new Protein();
protein.ProjectNumber = 6600;
protein.WorkUnitName = "WorkUnitName";
protein.Core = "GROGPU2";
protein.Credit = 450;
protein.KFactor = 0;
protein.Frames = 100;
protein.NumberOfAtoms = 5000;
protein.PreferredDays = 2;
protein.MaximumDays = 3;
collection.Add(protein);

protein = new Protein();
protein.ProjectNumber = 5797;
protein.WorkUnitName = "WorkUnitName2";
protein.Core = "GROGPU2";
protein.Credit = 675;
protein.KFactor = 2.3;
protein.Frames = 100;
protein.NumberOfAtoms = 7000;
protein.PreferredDays = 2;
protein.MaximumDays = 3;
collection.Add(protein);

protein = new Protein();
protein.ProjectNumber = 8011;
protein.WorkUnitName = "WorkUnitName3";
protein.Core = "GRO-A4";
protein.Credit = 106.6;
protein.KFactor = 0.75;
protein.Frames = 100;
protein.NumberOfAtoms = 9000;
protein.PreferredDays = 2.13;
protein.MaximumDays = 4.62;
collection.Add(protein);

protein = new Protein();
protein.ProjectNumber = 6903;
protein.WorkUnitName = "WorkUnitName4";
protein.Core = "GRO-A5";
protein.Credit = 22706;
protein.KFactor = 38.05;
protein.Frames = 100;
protein.NumberOfAtoms = 11000;
protein.PreferredDays = 5;
protein.MaximumDays = 12;
collection.Add(protein);

var dataContainer = new ProteinDataContainer();
dataContainer.Data = collection;
return new ProteinService(dataContainer, null, null);
}
#endif

public void RegisterForUnhandledExceptions()
{
Application.ThreadException += (s, e) => ShowThreadExceptionDialog(e);
Expand Down
Loading

0 comments on commit 1f106a3

Please sign in to comment.