Skip to content

Commit

Permalink
Merge 28ecd20 into f52e219
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsDue authored Dec 10, 2024
2 parents f52e219 + 28ecd20 commit e2ffacb
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Diagnostics;
using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models;
using Energinet.DataHub.EDI.IncomingMessages.Domain.Abstractions;
using Energinet.DataHub.EDI.IncomingMessages.Domain.Validation;
using Energinet.DataHub.EDI.IncomingMessages.Domain.Validation.ValidationErrors;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.Repositories.MessageId;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.Repositories.TransactionId;
using Microsoft.Extensions.Logging;

namespace Energinet.DataHub.EDI.IncomingMessages.Application.UseCases;

Expand All @@ -28,7 +30,8 @@ public class ValidateIncomingMessage(
IMessageTypeValidator messageTypeValidator,
IProcessTypeValidator processTypeValidator,
IBusinessTypeValidator businessTypeValidator,
ITransactionIdRepository transactionIdRepository)
ITransactionIdRepository transactionIdRepository,
ILogger<ValidateIncomingMessage> logger)
{
private const int MaxMessageIdLength = 36;
private const int MaxTransactionIdLength = 36;
Expand All @@ -54,8 +57,11 @@ public async Task<Result> ValidateAsync(
// It is slightly annoying since all we do is FirstOrDefaultAsync which we await.
// And read access shouldn't be that much of a problem wrt concurrency.
// It is also a bit silly to have this as a separate call, but it works.
var stopwatch = Stopwatch.StartNew();
var transactionIdErrors =
await CheckTransactionIdsAsync(incomingMessage, cancellationToken).ConfigureAwait(false);
stopwatch.Stop();
logger.LogInformation($"ValidateIncomingMessage CheckTransactionIdsAsync execution time: {stopwatch.ElapsedMilliseconds} ms");

var allErrors = errors.Concat(transactionIdErrors).ToArray();

Expand All @@ -64,20 +70,26 @@ public async Task<Result> ValidateAsync(

private async Task<IReadOnlyCollection<ValidationError>> AuthorizeSenderAsync(IIncomingMessage message)
{
var stopwatch = Stopwatch.StartNew();
var allSeriesAreDelegated = message.Series.Count > 0 && message.Series.All(s => s.IsDelegated);

var result = await senderAuthorizer
.AuthorizeAsync(message, allSeriesAreDelegated)
.ConfigureAwait(false);
stopwatch.Stop();
logger.LogInformation($"ValidateIncomingMessage AuthorizeSenderAsync execution time: {stopwatch.ElapsedMilliseconds} ms");

return result.Errors;
}

private async Task<IReadOnlyCollection<ValidationError>> VerifyReceiverAsync(IIncomingMessage message)
{
var stopwatch = Stopwatch.StartNew();
var receiverVerification = await receiverValidator
.VerifyAsync(message.ReceiverNumber, message.ReceiverRoleCode)
.ConfigureAwait(false);
stopwatch.Stop();
logger.LogInformation($"ValidateIncomingMessage VerifyReceiverAsync execution time: {stopwatch.ElapsedMilliseconds} ms");

return receiverVerification.Errors;
}
Expand All @@ -86,6 +98,7 @@ private async Task<IReadOnlyCollection<ValidationError>> CheckMessageIdAsync(
IIncomingMessage message,
CancellationToken cancellationToken)
{
var stopwatch = Stopwatch.StartNew();
var errors = new List<ValidationError>();

if (string.IsNullOrEmpty(message.MessageId))
Expand All @@ -104,15 +117,21 @@ private async Task<IReadOnlyCollection<ValidationError>> CheckMessageIdAsync(
errors.Add(new DuplicateMessageIdDetected(message.MessageId));
}

stopwatch.Stop();
logger.LogInformation($"ValidateIncomingMessage CheckMessageIdAsync execution time: {stopwatch.ElapsedMilliseconds} ms");

return errors;
}

private async Task<IReadOnlyCollection<ValidationError>> CheckMessageTypeAsync(
IIncomingMessage message,
CancellationToken cancellationToken)
{
var stopwatch = Stopwatch.StartNew();
var result = await messageTypeValidator.ValidateAsync(message, cancellationToken)
.ConfigureAwait(false);
stopwatch.Stop();
logger.LogInformation($"ValidateIncomingMessage CheckMessageTypeAsync execution time: {stopwatch.ElapsedMilliseconds} ms");
return result.Errors;
}

Expand All @@ -121,17 +140,23 @@ private async Task<IReadOnlyCollection<ValidationError>> CheckBusinessReasonAsyn
DocumentFormat documentFormat,
CancellationToken cancellationToken)
{
var stopwatch = Stopwatch.StartNew();
var result = await processTypeValidator.ValidateAsync(message, documentFormat, cancellationToken)
.ConfigureAwait(false);
stopwatch.Stop();
logger.LogInformation($"ValidateIncomingMessage CheckBusinessReasonAsync execution time: {stopwatch.ElapsedMilliseconds} ms");
return result.Errors;
}

private async Task<IReadOnlyCollection<ValidationError>> CheckBusinessTypeAsync(
IIncomingMessage message,
CancellationToken cancellationToken)
{
var stopwatch = Stopwatch.StartNew();
var result = await businessTypeValidator.ValidateAsync(message.BusinessType, cancellationToken)
.ConfigureAwait(false);
stopwatch.Stop();
logger.LogInformation($"ValidateIncomingMessage CheckBusinessTypeAsync execution time: {stopwatch.ElapsedMilliseconds} ms");
return result.Errors;
}

Expand Down

0 comments on commit e2ffacb

Please sign in to comment.