apply fix on Models Enrichments ...
This commit is contained in:
@@ -1,215 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using xAiModels.Interfaces;
|
||||
using xAiModels.Interfaces.Dtos;
|
||||
using xAiModels.Interfaces.Enrichers;
|
||||
using xAiModels.Interfaces.Entities;
|
||||
using xAiModels.Models.Dtos;
|
||||
using xAiModels.Models.Entities;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityService.Interfaces;
|
||||
using xIdentityService.Extensions;
|
||||
using xModels.Dtos;
|
||||
using System.Linq;
|
||||
|
||||
namespace xAiModels.Providers.Enrichers
|
||||
{
|
||||
public class XAiConversationEnricher : IXAiConversationEnricher
|
||||
{
|
||||
private readonly IXIdentityProvider identityProvider;
|
||||
private readonly IXAiProjectRepositoryService projectRepositoryService;
|
||||
private readonly IXAiConversationRepository repository;
|
||||
private readonly IXAiProjectTitleResourceProvider projectTitleResourceProvider;
|
||||
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
||||
private readonly IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider;
|
||||
|
||||
public XAiConversationEnricher(
|
||||
IXIdentityProvider identityProvider,
|
||||
IXAiProjectRepositoryService projectRepositoryService,
|
||||
IXAiConversationTitleResourceProvider conversationTitleResourceProvider
|
||||
IXAiConversationRepository repository,
|
||||
IXAiProjectTitleResourceProvider projectTitleResourceProvider,
|
||||
IXAiConversationTitleResourceProvider conversationTitleResourceProvider,
|
||||
IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
this.repository = repository;
|
||||
this.identityProvider = identityProvider;
|
||||
this.projectRepositoryService = projectRepositoryService;
|
||||
this.projectTitleResourceProvider = projectTitleResourceProvider;
|
||||
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Signle Entity to Dto ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="destination"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task EnrichAsync(
|
||||
XAiConversationDto source,
|
||||
XAiConversationDto destination,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
await EnrichAsync(
|
||||
source: source,
|
||||
fillMessages: false,
|
||||
destination: destination,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Collection of Entities to Dtos ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="destinations"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task EnrichRangeAsync(
|
||||
IEnumerable<XAiConversationDto> sources,
|
||||
IEnumerable<XAiConversationDto> destinations,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
await EnrichRangeAsync(
|
||||
sources: sources,
|
||||
fillMessages: false,
|
||||
destinations: destinations,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Signle Entity to Dto ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="destination"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task EnrichAsync(
|
||||
XAiConversationDto source,
|
||||
XAiConversationDto destination,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Run independent async operations concurrently
|
||||
var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken);
|
||||
var projectTask = ResolveProjectAsync(source.ProjectId, cancellationToken);
|
||||
var titleLocalesTask = ResolveTitleLocalesAsync(source.Title, cancellationToken);
|
||||
var messagesTask = ResolveMessagesAsync(source.Id, cancellationToken);
|
||||
|
||||
//
|
||||
// wait until All Tasks Resolved ...
|
||||
Task[] tasks = [
|
||||
ownerTask,
|
||||
projectTask,
|
||||
titleLocalesTask
|
||||
];
|
||||
if (fillMessages)
|
||||
{
|
||||
tasks = [.. tasks, messagesTask];
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
//
|
||||
// Attached Resolved Info to Destination ...
|
||||
destination.Owner = ownerTask.Result;
|
||||
destination.TitleResource = titleLocalesTask.Result;
|
||||
var project = projectTask.Result;
|
||||
if (!project.IsNullOrDefault())
|
||||
{
|
||||
destination.ProjectTitle = project.Title;
|
||||
destination.ProjectDescription = project.Description;
|
||||
destination.ProjectTitleResource = project.TitleResource;
|
||||
destination.ProjectDescriptionResource = project.DescriptionResource;
|
||||
}
|
||||
if (!messagesTask.Result.IsNull())
|
||||
{
|
||||
destination.Messages = [.. messagesTask.Result];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Collection of Entities to Dtos ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="destinations"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task EnrichRangeAsync(
|
||||
IEnumerable<XAiConversationDto> sources,
|
||||
IEnumerable<XAiConversationDto> destinations,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Make Tasks Ready ...
|
||||
var tasks = sources.Zip(destinations, (src, dst) =>
|
||||
EnrichAsync(src, dst, cancellationToken)
|
||||
);
|
||||
|
||||
//
|
||||
await Task.WhenAll(tasks);
|
||||
this.projectDescriptionResourceProvider = projectDescriptionResourceProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
#region Enrichers ...
|
||||
/// <summary>
|
||||
/// Reading Owner User Info ...
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XPersonDto> ResolveOwnerAsync(
|
||||
string ownerId,
|
||||
public virtual async Task<XAiConversationDto> Enrich(
|
||||
XAiConversation source,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
XPersonDto result = null;
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
if (ownerId.IsNullOrEmpty())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Try to Retrieve User Info ...
|
||||
var device = identityProvider.GetDevice();
|
||||
var userInfo = await identityProvider.GetUserInfo(
|
||||
device: device,
|
||||
userSelectByParam: ownerId
|
||||
);
|
||||
result = userInfo.ToXPersonDto();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reading Title Locales ...
|
||||
/// </summary>
|
||||
/// <param name="title"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XResourceDto> ResolveTitleLocalesAsync(
|
||||
string title,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
XResourceDto result = null;
|
||||
if (title.IsNullOrEmpty())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = await conversationTitleResourceProvider.GetResource(
|
||||
owner: null,
|
||||
identifier: title,
|
||||
// Mapping Default ...
|
||||
var result = source.MapConvert<XAiConversationDto, XAiConversation>();
|
||||
result = await Enrich(
|
||||
source: result,
|
||||
fillMessages: fillMessages,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
@@ -218,58 +67,130 @@ namespace xAiModels.Providers.Enrichers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Project ...
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XAiProjectDto> ResolveProjectAsync(
|
||||
Guid projectId,
|
||||
public virtual async Task<XAiConversationDto> Enrich(
|
||||
XAiConversationDto source,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
XAiProjectDto result = null;
|
||||
if (projectId.IsNull() || projectId.IsDefaultGuid())
|
||||
// Handdle Owner ...
|
||||
XPersonDto owner = null;
|
||||
if (!source.OwnerId.IsNullOrEmpty())
|
||||
{
|
||||
return result;
|
||||
//
|
||||
// Try to Retrieve User Info ...
|
||||
var device = identityProvider.GetDevice();
|
||||
var userInfo = await identityProvider.GetUserInfo(
|
||||
device: device,
|
||||
userSelectByParam: source.OwnerId
|
||||
);
|
||||
owner = userInfo.ToXPersonDto();
|
||||
}
|
||||
|
||||
//
|
||||
result = await projectRepositoryService.GetAsync(
|
||||
id: projectId,
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
// Handle Title Resource ...
|
||||
XResourceDto titleResource = null;
|
||||
if (!source.Title.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
titleResource = await conversationTitleResourceProvider.GetResource(
|
||||
owner: source.OwnerId,
|
||||
identifier: source.Id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Project Info ...
|
||||
XResourceDto projectTitleResource = null;
|
||||
XResourceDto projectDescriptionResource = null;
|
||||
if (!source.ProjectId.IsDefaultGuid())
|
||||
{
|
||||
//
|
||||
projectTitleResource = await projectTitleResourceProvider.GetResource(
|
||||
owner: source.OwnerId,
|
||||
identifier: source.ProjectId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
projectDescriptionResource = await projectDescriptionResourceProvider.GetResource(
|
||||
owner: source.OwnerId,
|
||||
identifier: source.ProjectId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Attach Extracted Items ...
|
||||
var result = source.MapConvert<XAiConversationDto, XAiConversationDto>();
|
||||
result.Owner = owner;
|
||||
result.TitleResource = titleResource;
|
||||
result.ProjectTitleResource = projectTitleResource;
|
||||
result.ProjectDescriptionResource = projectDescriptionResource;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Conversation Messages ...
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<IEnumerable<XAiMessageDto>> ResolveMessagesAsync(
|
||||
Guid conversationId,
|
||||
public virtual async Task<IEnumerable<XAiConversationDto>> EnrichRange(
|
||||
IEnumerable<XAiConversation> sources,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new List<XAiMessageDto>();
|
||||
|
||||
//
|
||||
if (conversationId.IsDefaultGuid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
var tasks = sources
|
||||
.Select(x => Enrich(
|
||||
source: x,
|
||||
fillMessages: fillMessages,
|
||||
cancellationToken: cancellationToken
|
||||
));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<XAiConversationDto>> EnrichRange(
|
||||
IEnumerable<XAiConversationDto> sources,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var tasks = sources
|
||||
.Select(x => Enrich(
|
||||
source: x,
|
||||
fillMessages: fillMessages,
|
||||
cancellationToken: cancellationToken
|
||||
));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user