Skip to content

Commit

Permalink
Refactor the CherryPickRunner to allow for the possibility to CherryP…
Browse files Browse the repository at this point in the history
…ick on a specific engineering model and iteration.
  • Loading branch information
Robbware committed Oct 20, 2023
1 parent d10d4b4 commit 12ad6c9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 28 deletions.
104 changes: 79 additions & 25 deletions COMET.Web.Common/Utilities/CherryPick/CherryPickRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@

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

using COMET.Web.Common.Services.SessionManagement;

using Thing = CDP4Common.DTO.Thing;

/// <summary>
/// Utility class that could run CherryPick query for <see cref="INeedCherryPickedData" />
/// </summary>
Expand All @@ -37,20 +40,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,49 +64,100 @@ 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" />
/// Gets the defined category ids to be used as a filter
/// </summary>
/// <returns>A <see cref="Task" /></returns>
public async Task RunCherryPick()
/// <returns>A <see cref="IEnumerable{Guid}"/></returns> with the IDs of the categories to filter on
private IEnumerable<Guid> GetCategoryIdsForCherryPick()
{
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 categoriesName = this.needCherryPicked.SelectMany(x => x.CategoriesOfInterest).Distinct();
var categories = new List<Category>();

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

var availableEngineeringModelSetups = this.SessionService.GetParticipantModels().ToList();
var categoriesIds = categories.Select(x => x.Iid);
return categoriesIds;
}

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();
/// <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();
}

/// <summary>
/// Processes the retrieved cherry picked data
/// </summary>
/// <param name="cherryPickedData">The cherry picked data</param>
private void ProcessCherryPickedData(IReadOnlyCollection<IEnumerable<Thing>> cherryPickedData)
{
foreach (var needCherryPickedData in this.needCherryPicked)
{
needCherryPickedData.ProcessCherryPickedData(cherryPickedData);
}
}

/// <summary>
/// Runs the cherrypick features based on data required from <see cref="needCherryPicked" />
/// </summary>
/// <returns>A <see cref="Task" /></returns>
public async Task RunCherryPickAsync()
{
if (this.IsCherryPicking)
{
return;
}

this.IsCherryPicking = true;

var classKinds = this.GetClassKindsForCherryPick();
var categoriesIds = this.GetCategoryIdsForCherryPick();

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, categoriesIds)).ToList();
var results = (await Task.WhenAll(cherryPicks)).Where(x => x.Any()).ToList();

foreach (var needCherryPickedData in this.NeedCherryPicked)
this.ProcessCherryPickedData(results);
this.IsCherryPicking = false;
}

/// <summary>
/// Runs the cherrypick features for a specific Engineering Model and Iteration, based on data required from <see cref="needCherryPicked" />
/// </summary>
/// <param name="engineeringModelId">The engineering model Id that we want to cherry pick from</param>
/// <param name="iterationId">The iteration Id that we want to cherry pick from</param>
/// <returns>A <see cref="Task" /></returns>
public async Task RunSingleCherryPickAsync(Guid engineeringModelId, Guid iterationId)
{
if (this.IsCherryPicking)
{
needCherryPickedData.ProcessCherryPickedData(results);
return;
}

this.IsCherryPicking = true;

var classKinds = this.GetClassKindsForCherryPick();
var categoriesIds = this.GetCategoryIdsForCherryPick();

var cherryPicks = await this.sessionService.Session.CherryPick(engineeringModelId, iterationId, classKinds, categoriesIds);

this.ProcessCherryPickedData(new []{ cherryPicks });

this.IsCherryPicking = false;
}
}
Expand Down
14 changes: 11 additions & 3 deletions COMET.Web.Common/Utilities/CherryPick/ICherryPickRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ 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 for a specific Engineering Model and Iteration, based on data required from <see cref="NeedCherryPicked" />
/// </summary>
/// <param name="engineeringModelId">The engineering model Id that we want to cherry pick from</param>
/// <param name="iterationId">The iteration Id that we want to cherry pick from</param>
/// <returns>A <see cref="Task" /></returns>
Task RunSingleCherryPickAsync(Guid engineeringModelId, Guid iterationId);

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

0 comments on commit 12ad6c9

Please sign in to comment.