Skip to content

Commit

Permalink
Fixes #1939 template db auto update (#2043)
Browse files Browse the repository at this point in the history
  • Loading branch information
msevestre authored Jan 12, 2022
1 parent ae5b129 commit 4893f39
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 54 deletions.
4 changes: 3 additions & 1 deletion src/PKSim.Core/CoreConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@ public static class Filter
public static readonly string TEMPLATE_USER_DATABASE_TEMPLATE = "PKSimTemplateDBUser.template";
public static readonly string REMOTE_TEMPLATE_SUMMARY = "templates.json";
public const string PRODUCT_NAME = "PK-Sim";
public const string TEMPLATES_PRODUCT_NAME = "BuildingBlockTemplates";
public static readonly string PRODUCT_NAME_WITH_TRADEMARK = "PK-Sim®";
public static readonly string DEFAULT_SKIN = "Office 2013 Light Gray";
public static readonly string VALUE_PROPERTY_NAME = "Value";
public static readonly string PROJECT_UNDEFINED = "Undefined";
public static readonly string VERSION_FILE_URL = "https://raw.githubusercontent.com/Open-Systems-Pharmacology/Suite/master/versions.json";
public static readonly string REMOTE_TEMPLATE_FILE_URL = "https://raw.githubusercontent.com/Open-Systems-Pharmacology/OSPSuite.BuildingBlockTemplates/main/templates.json";
public static readonly string ISSUE_TRACKER_URL = "https://github.com/open-systems-pharmacology/pk-sim/issues";

public static readonly string TEMPLATE_DATABASE_CONVERSION_WIKI_URL =
"https://github.com/Open-Systems-Pharmacology/OSPSuite.Documentation/wiki/Converting-User-Template-Database";

public const string APPLICATION_NAME_TEMPLATE = "Application_";

//tolerated precision to relativtely compare to double values
//tolerated precision to relatively compare to double values
public const double DOUBLE_RELATIVE_EPSILON = 1e-2;

public const char COMPOSITE_SEPARATOR = '-';
Expand Down
3 changes: 3 additions & 0 deletions src/PKSim.Core/IPKSimConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public interface IPKSimConfiguration : IApplicationConfiguration
/// </summary>
string RemoteTemplateSummaryPath { get; }

/// <summary>
/// Folder path where templates will be downloaded
/// </summary>
string RemoteTemplateFolderPath { get; }
}

Expand Down
1 change: 1 addition & 0 deletions src/PKSim.Core/Repositories/IRemoteTemplateRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface IRemoteTemplateRepository : IStartableRepository<RemoteTemplate
RemoteTemplate TemplateBy(TemplateType templateType, string name);
Task<T> LoadTemplateAsync<T>(RemoteTemplate remoteTemplate);
IReadOnlyList<RemoteTemplate> AllReferenceTemplatesFor<T>(RemoteTemplate remoteTemplate, T loadedTemplate);
Task UpdateLocalTemplateSummaryFile();
}
}
17 changes: 16 additions & 1 deletion src/PKSim.Core/Services/IVersionChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,24 @@ public interface IVersionChecker
/// </summary>
VersionInfo LatestVersion { get; }

/// <summary>
/// Returns the latest version available for the product named <paramref name="productName"/>
/// </summary>
VersionInfo LatestVersionFor(string productName);

/// <summary>
/// Downloads the latest version available for our software products
/// </summary>
Task DownloadLatestVersionInfoAsync();

/// <summary>
/// Returns true if a new version was found otherwise false. The latest version can be retrieved from LatestVersion
/// </summary>
Task<bool> NewVersionIsAvailableAsync();
bool NewVersionIsAvailable { get; }

/// <summary>
/// Returns true if a new version was found for product named <paramref name="productName" /> otherwise false.
/// </summary>
bool NewVersionIsAvailableFor(string productName, string currentVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public IReadOnlyList<RemoteTemplate> AllReferenceTemplatesFor<T>(RemoteTemplate
}
}

public async Task UpdateLocalTemplateSummaryFile()
{
var tempFile = FileHelper.GenerateTemporaryFileName();
try
{
await downloadRemoteFile(CoreConstants.REMOTE_TEMPLATE_FILE_URL, tempFile);
FileHelper.Copy(tempFile, _configuration.RemoteTemplateSummaryPath);
}
catch (Exception)
{
//could not download the file. Do nothing
}
}

private IReadOnlyList<RemoteTemplate> expressionProfileFor(RemoteTemplate remoteTemplate, ISimulationSubject simulationSubject)
{
//TODO Not implemented yet. It will be done with the profile defined for individual as separate building block
Expand Down
48 changes: 18 additions & 30 deletions src/PKSim.Infrastructure/Services/VersionChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,53 @@
using System.Net;
using System.Threading.Tasks;
using OSPSuite.Core.Domain;
using OSPSuite.Utility.Exceptions;
using PKSim.Core.Services;

namespace PKSim.Infrastructure.Services
{
public class VersionChecker : IVersionChecker
{
private readonly IJsonSerializer _jsonSerializer;
private IReadOnlyList<VersionInfo> _allVersions = new List<VersionInfo>();
public string ProductName { get; set; }
public string CurrentVersion { get; set; }
public string VersionFileUrl { get; set; }
public VersionInfo LatestVersion { get; private set; }

public VersionChecker(IJsonSerializer jsonSerializer)
{
_jsonSerializer = jsonSerializer;
}

public async Task<bool> NewVersionIsAvailableAsync()
public VersionInfo LatestVersion => LatestVersionFor(ProductName);

public VersionInfo LatestVersionFor(string productName) => _allVersions.FindByName(productName);

public async Task DownloadLatestVersionInfoAsync()
{
try
{
await retrieveLatestVersion();
return newVersionIsAvailable();

using (var wc = new WebClient())
{
var jsonContent = await wc.DownloadStringTaskAsync(VersionFileUrl);
_allVersions = await _jsonSerializer.DeserializeAsArrayFromString<VersionInfo>(jsonContent);
}
}
catch (Exception)
{
return false;
//we do nothing if we cannot download the file
}
}

private async Task retrieveLatestVersion()
{
using (var wc = new WebClient())
{
var jsonContent = await wc.DownloadStringTaskAsync(VersionFileUrl);
var versions = await _jsonSerializer.DeserializeAsArrayFromString<VersionInfo>(jsonContent);
LatestVersion = retrieveVersionFrom(versions);
}
}
public bool NewVersionIsAvailable => NewVersionIsAvailableFor(ProductName, CurrentVersion);

private bool newVersionIsAvailable()
public bool NewVersionIsAvailableFor(string productName, string currentVersion)
{
if (LatestVersion == null)
var latestVersion = LatestVersionFor(productName);
if (latestVersion == null)
return false;

var curVersion = new Version(CurrentVersion);
return curVersion.CompareTo(new Version(LatestVersion.Version)) < 0;
}


private VersionInfo retrieveVersionFrom(IEnumerable<VersionInfo> allVersionInfos)
{
var versionInfo = allVersionInfos.FindByName(ProductName);
if (versionInfo == null)
throw new OSPSuiteException($"{ProductName} node not available");

return versionInfo;
var curVersion = new Version(currentVersion);
return curVersion.CompareTo(new Version(latestVersion.Version)) < 0;
}
}
}
6 changes: 3 additions & 3 deletions src/PKSim.Presentation/Presenters/AboutPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace PKSim.Presentation.Presenters
{
public interface IAboutPresenter : IDisposablePresenter
{
Task CheckForUpdate();
void CheckForUpdate();
}

public class AboutPresenter : AbstractDisposablePresenter<IAboutView, IAboutPresenter>, IAboutPresenter
Expand All @@ -36,9 +36,9 @@ public override void Initialize()
_view.Display();
}

public async Task CheckForUpdate()
public void CheckForUpdate()
{
var newVersionAvailable = await _versionChecker.NewVersionIsAvailableAsync();
var newVersionAvailable = _versionChecker.NewVersionIsAvailable;

if (newVersionAvailable)
_dialogCreator.MessageBoxInfo(PKSimConstants.Information.NewVersionIsAvailable(_versionChecker.LatestVersion, Constants.PRODUCT_SITE_DOWNLOAD).RemoveHtml());
Expand Down
35 changes: 31 additions & 4 deletions src/PKSim.Presentation/Services/PostLaunchChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using OSPSuite.Core.Services;
using OSPSuite.Utility.Events;
using PKSim.Assets;
using PKSim.Core;
using PKSim.Core.Repositories;
using PKSim.Core.Services;
using PKSim.Presentation.Events;

Expand All @@ -16,39 +18,64 @@ public class PostLaunchChecker : IPostLaunchChecker
private readonly ITemplateDatabaseCreator _templateDatabaseCreator;
private readonly IEventPublisher _eventPublisher;
private readonly IUserSettings _userSettings;
private readonly IRemoteTemplateRepository _remoteTemplateRepository;

public PostLaunchChecker(
IVersionChecker versionChecker,
IWatermarkStatusChecker watermarkStatusChecker,
ITemplateDatabaseCreator templateDatabaseCreator,
IEventPublisher eventPublisher,
IUserSettings userSettings)
IUserSettings userSettings,
IRemoteTemplateRepository remoteTemplateRepository)
{
_versionChecker = versionChecker;
_watermarkStatusChecker = watermarkStatusChecker;
_templateDatabaseCreator = templateDatabaseCreator;
_eventPublisher = eventPublisher;
_userSettings = userSettings;
_remoteTemplateRepository = remoteTemplateRepository;
}

public async Task PerformPostLaunchCheckAsync()
{
await checkForNewVersionAsync();
await checkForNewSoftwareVersionAsync();

await checkForNewTemplateVersionAsync();

_watermarkStatusChecker.CheckWatermarkStatus();

_templateDatabaseCreator.CreateDefaultTemplateDatabase();

}

private async Task checkForNewVersionAsync()
private async Task checkForNewTemplateVersionAsync()
{
try
{
await _versionChecker.DownloadLatestVersionInfoAsync();
var hasNewVersion = _versionChecker.NewVersionIsAvailableFor(CoreConstants.TEMPLATES_PRODUCT_NAME, _remoteTemplateRepository.Version);
if (!hasNewVersion)
return;

await _remoteTemplateRepository.UpdateLocalTemplateSummaryFile();
}
catch (Exception)
{
//no need to do anything if version cannot be returned
return;
}


}
private async Task checkForNewSoftwareVersionAsync()
{
if (!_userSettings.ShowUpdateNotification)
return;

try
{
var hasNewVersion = await _versionChecker.NewVersionIsAvailableAsync();
await _versionChecker.DownloadLatestVersionInfoAsync();
var hasNewVersion = _versionChecker.NewVersionIsAvailable;
if (!hasNewVersion) return;
}
catch (Exception)
Expand Down
2 changes: 1 addition & 1 deletion templates
Submodule templates updated 1 files
+97 −12 templates.json
11 changes: 7 additions & 4 deletions tests/PKSim.Tests/Infrastructure/VersionCheckerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected override async Task Context()

protected override async Task Because()
{
_result = await sut.NewVersionIsAvailableAsync();
await sut.DownloadLatestVersionInfoAsync();
_result = sut.NewVersionIsAvailable;
}

[Observation]
Expand Down Expand Up @@ -61,7 +62,8 @@ protected override async Task Context()

protected override async Task Because()
{
_result = await sut.NewVersionIsAvailableAsync();
await sut.DownloadLatestVersionInfoAsync();
_result = sut.NewVersionIsAvailable;
}

[Observation]
Expand All @@ -85,7 +87,8 @@ protected override async Task Context()

protected override async Task Because()
{
_result = await sut.NewVersionIsAvailableAsync();
await sut.DownloadLatestVersionInfoAsync();
_result = sut.NewVersionIsAvailable;
}

[Observation]
Expand All @@ -107,7 +110,7 @@ protected override async Task Context()

protected override async Task Because()
{
await sut.NewVersionIsAvailableAsync();
await sut.DownloadLatestVersionInfoAsync();
}

[Observation]
Expand Down
Loading

0 comments on commit 4893f39

Please sign in to comment.