Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/482 add specific cherrypick method #483

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace COMET.Web.Common.Tests.Utilities.CherryPick
{
using NUnit.Framework;

using CDP4Common.SiteDirectoryData;
using SiteDirectory = CDP4Common.SiteDirectoryData.SiteDirectory;
using CDP4Common.DTO;

using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.Utilities.CherryPick;
Expand Down Expand Up @@ -61,12 +62,24 @@ public async Task VerifyProperties()
var propertyInfo = typeof(CherryPickRunner).GetProperty("IsCherryPicking", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

propertyInfo?.SetValue(this.viewModel, true, null);
await this.viewModel.RunCherryPick();
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<CDP4Common.DTO.Thing>>>()), Times.Never);
await this.viewModel.RunCherryPickAsync();
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<Thing>>>()), Times.Never);

propertyInfo?.SetValue(this.viewModel, false, null);
await this.viewModel.RunCherryPick();
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<CDP4Common.DTO.Thing>>>()), Times.Once);
await this.viewModel.RunCherryPickAsync();
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<Thing>>>()), Times.Once);

this.needCherryPickedData.Invocations.Clear();

var engineeringModelId = Guid.NewGuid();
var iterationId = Guid.NewGuid();
propertyInfo?.SetValue(this.viewModel, true, null);
await this.viewModel.RunCherryPickAsync(new[] { (engineeringModelId, iterationId)});
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<Thing>>>()), Times.Never);

propertyInfo?.SetValue(this.viewModel, false, null);
await this.viewModel.RunCherryPickAsync(new[] { (engineeringModelId, iterationId)});
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<Thing>>>()), Times.Once);
}
}
}
81 changes: 56 additions & 25 deletions COMET.Web.Common/Utilities/CherryPick/CherryPickRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace COMET.Web.Common.Utilities.CherryPick
{
using CDP4Common.CommonData;
using CDP4Common.SiteDirectoryData;

using COMET.Web.Common.Services.SessionManagement;
Expand All @@ -37,20 +38,20 @@ public class CherryPickRunner : ICherryPickRunner
/// <summary>
/// Gets the collection of <see cref="INeedCherryPickedData" />
/// </summary>
private readonly List<INeedCherryPickedData> NeedCherryPicked = new();
private readonly List<INeedCherryPickedData> needCherryPicked = new();

/// <summary>
/// Gets the <see cref="ISessionService"/>
/// </summary>
protected readonly ISessionService SessionService;
private readonly ISessionService sessionService;

/// <summary>
/// Initializes a new <see cref="CherryPickRunner" />
/// </summary>
/// <param name="sessionService">The <see cref="ISessionService" /></param>
public CherryPickRunner(ISessionService sessionService)
{
this.SessionService = sessionService;
this.sessionService = sessionService;
}

/// <summary>
Expand All @@ -61,50 +62,80 @@ public CherryPickRunner(ISessionService sessionService)
/// <summary>
/// Initializes the internal properties
/// </summary>
/// <param name="needCherryPicked">A collection of <see cref="INeedCherryPickedData"/></param>
public void InitializeProperties(IEnumerable<INeedCherryPickedData> needCherryPicked)
/// <param name="needCherryPickedData">A collection of <see cref="INeedCherryPickedData"/></param>
public void InitializeProperties(IEnumerable<INeedCherryPickedData> needCherryPickedData)
{
this.NeedCherryPicked.Clear();
this.NeedCherryPicked.AddRange(needCherryPicked);
this.needCherryPicked.Clear();
this.needCherryPicked.AddRange(needCherryPickedData);
}


/// <summary>
/// Runs the cherrypick features based on data required from <see cref="needCherryPicked" /> for all the Engineering Models the user is participating on
/// </summary>
/// <returns>A <see cref="Task" /></returns>
public async Task RunCherryPickAsync()
{
var availableEngineeringModelSetups = this.sessionService.GetParticipantModels().ToList();
var engineeringModelAndIterationIdTuple = availableEngineeringModelSetups.Select(x => (x.Iid, x.IterationSetup.Single(c => c.FrozenOn == null).Iid));
await this.RunCherryPickAsync(engineeringModelAndIterationIdTuple);
}

/// <summary>
/// Runs the cherrypick features based on data required from <see cref="NeedCherryPicked" />
/// Runs the cherrypick features based on data required from <see cref="CherryPickRunner.NeedCherryPicked" /> and a particular set of EngineeringModelId and IterationId.
/// </summary>
/// <param name="ids">A <see cref="Tuple{Guid,Guid}"/> to run the cherry pick for a particular set of engineeringModelIds and iterationIds</param>
/// <returns>A <see cref="Task" /></returns>
public async Task RunCherryPick()
public async Task RunCherryPickAsync(IEnumerable<(Guid engineeringModelId, Guid iterationId)> ids)
{
if (this.IsCherryPicking)
{
return;
}

this.IsCherryPicking = true;
var classKinds = this.NeedCherryPicked.SelectMany(x => x.ClassKindsOfInterest).Distinct();
var categoriesName = this.NeedCherryPicked.SelectMany(x => x.CategoriesOfInterest).Distinct();
var classKinds = this.GetClassKindsForCherryPick();
var categoryIds = this.GetCategoryIdsForCherryPick();

var categories = new List<Category>();
var cherryPicks = ids.Select(pair => this.sessionService.Session.CherryPick(pair.engineeringModelId, pair.iterationId, classKinds, categoryIds))
.ToList();

foreach (var referenceDataLibrary in this.SessionService.Session.RetrieveSiteDirectory().AvailableReferenceDataLibraries())
var results = (await Task.WhenAll(cherryPicks)).Where(x => x.Any()).ToList();

foreach (var needCherryPickedData in this.needCherryPicked)
{
categories.AddRange(referenceDataLibrary.DefinedCategory
.Where(x => categoriesName.Contains(x.Name))
.ToList());
needCherryPickedData.ProcessCherryPickedData(results);
}

var availableEngineeringModelSetups = this.SessionService.GetParticipantModels().ToList();

var cherryPicks = availableEngineeringModelSetups.Select(engineeringModelSetup => this.SessionService.Session.CherryPick(engineeringModelSetup.EngineeringModelIid, engineeringModelSetup.IterationSetup.Single(x => x.FrozenOn == null).IterationIid, classKinds, categories.Select(x => x.Iid)))
.ToList();
this.IsCherryPicking = false;
}

var results = (await Task.WhenAll(cherryPicks)).Where(x => x.Any()).ToList();
/// <summary>
/// Gets the defined category ids to be used as a filter
/// </summary>
/// <returns>A <see cref="IEnumerable{Guid}"/></returns> with the IDs of the categories to filter on
private IEnumerable<Guid> GetCategoryIdsForCherryPick()
{
var categoriesName = this.needCherryPicked.SelectMany(x => x.CategoriesOfInterest).Distinct();
var categories = new List<Category>();

foreach (var needCherryPickedData in this.NeedCherryPicked)
foreach (var referenceDataLibrary in this.sessionService.Session.RetrieveSiteDirectory().AvailableReferenceDataLibraries())
{
needCherryPickedData.ProcessCherryPickedData(results);
categories.AddRange(referenceDataLibrary.DefinedCategory
.Where(x => categoriesName.Contains(x.Name))
.ToList());
}

this.IsCherryPicking = false;
var categoriesIds = categories.Select(x => x.Iid);
return categoriesIds;
}

/// <summary>
/// Gets the defined class kinds to be used as a filter
/// </summary>
/// <returns>A <see cref="IEnumerable{ClassKind}"/> with the <see cref="ClassKind"/> to filter on</returns>
private IEnumerable<ClassKind> GetClassKindsForCherryPick()
{
return this.needCherryPicked.SelectMany(x => x.ClassKindsOfInterest).Distinct();
}
}
}
15 changes: 11 additions & 4 deletions COMET.Web.Common/Utilities/CherryPick/ICherryPickRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ public interface ICherryPickRunner
/// <summary>
/// Initializes the internal properties
/// </summary>
/// <param name="needCherryPicked">A collection of <see cref="INeedCherryPickedData"/></param>
void InitializeProperties(IEnumerable<INeedCherryPickedData> needCherryPicked);
/// <param name="needCherryPickedData">A collection of <see cref="INeedCherryPickedData"/></param>
void InitializeProperties(IEnumerable<INeedCherryPickedData> needCherryPickedData);

/// <summary>
/// Runs the cherrypick features based on data required from <see cref="CherryPickRunner.NeedCherryPicked" />
/// Runs the cherrypick features based on data required from <see cref="CherryPickRunner.NeedCherryPicked"/>;
/// </summary>
/// <returns>A <see cref="Task" /></returns>
Task RunCherryPick();
Task RunCherryPickAsync();

/// <summary>
/// Runs the cherrypick features based on data required from <see cref="CherryPickRunner.NeedCherryPicked" /> and a particular set of EngineeringModelId and IterationId.
/// </summary>
/// <param name="ids">A <see cref="Tuple{Guid,Guid}"/> to run the cherry pick for a particular set of engineeringModelIds and iterationIds</param>
/// <returns>A <see cref="Task" /></returns>
Task RunCherryPickAsync(IEnumerable<(Guid engineeringModelId, Guid iterationId)> ids);
}
}
Loading