|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Altinn.Studio.Designer.Events; |
| 7 | +using Altinn.Studio.Designer.Models; |
| 8 | +using Altinn.Studio.Designer.Repository; |
| 9 | +using Altinn.Studio.Designer.Services.Interfaces; |
| 10 | +using Altinn.Studio.Designer.ViewModels.Request; |
| 11 | +using Altinn.Studio.Designer.ViewModels.Request.Enums; |
| 12 | +using MediatR; |
| 13 | +using Microsoft.AspNetCore.Hosting; |
| 14 | + |
| 15 | +namespace Altinn.Studio.Designer.EventHandlers.DeploymentPipelineCompleted; |
| 16 | + |
| 17 | +public class DeploymentPipelineCompletedStatisticsHandler : INotificationHandler<Events.DeploymentPipelineCompleted> |
| 18 | +{ |
| 19 | + private readonly IDeploymentRepository _deploymentRepository; |
| 20 | + private readonly IKafkaProducer _kafkaProducer; |
| 21 | + private readonly IWebHostEnvironment _hostingEnvironment; |
| 22 | + |
| 23 | + public DeploymentPipelineCompletedStatisticsHandler(IDeploymentRepository deploymentRepository, IWebHostEnvironment hostingEnvironment, IKafkaProducer kafkaProducer) |
| 24 | + { |
| 25 | + _deploymentRepository = deploymentRepository; |
| 26 | + _hostingEnvironment = hostingEnvironment; |
| 27 | + _kafkaProducer = kafkaProducer; |
| 28 | + } |
| 29 | + |
| 30 | + public async Task Handle(Events.DeploymentPipelineCompleted notification, CancellationToken cancellationToken) |
| 31 | + { |
| 32 | + var studioStatistics = await CalculateStudioStatistics(notification); |
| 33 | + await _kafkaProducer.ProduceAsync(studioStatistics, cancellationToken); |
| 34 | + } |
| 35 | + |
| 36 | + private async Task<StudioStatisticsModel> CalculateStudioStatistics(Events.DeploymentPipelineCompleted notification) |
| 37 | + { |
| 38 | + StudioStatisticsEvent studioEvent = await CalculateStudioStatisticsEvent(notification); |
| 39 | + return new StudioStatisticsModel |
| 40 | + { |
| 41 | + EventName = studioEvent.Name, |
| 42 | + Description = studioEvent.Description, |
| 43 | + Environment = notification.Environment, |
| 44 | + Org = notification.EditingContext.Org, |
| 45 | + App = notification.EditingContext.Repo, |
| 46 | + AdditionalData = new Dictionary<string, string> |
| 47 | + { |
| 48 | + { "studioEnvironment", _hostingEnvironment.EnvironmentName } |
| 49 | + } |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + private async Task<StudioStatisticsEvent> CalculateStudioStatisticsEvent(Events.DeploymentPipelineCompleted notification) |
| 54 | + { |
| 55 | + if (notification.PipelineType != PipelineType.Deploy && notification.PipelineType != PipelineType.Undeploy) |
| 56 | + { |
| 57 | + throw new ArgumentException("PipelineType must be Deploy or Undeploy"); |
| 58 | + } |
| 59 | + |
| 60 | + if (notification.PipelineType == PipelineType.Undeploy) |
| 61 | + { |
| 62 | + return notification.Succeeded ? StudioStatisticsEvent.AppDecommissioned : StudioStatisticsEvent.AppDecommissionFailed; |
| 63 | + } |
| 64 | + |
| 65 | + bool isUpdate = await IsAppUpdated(notification); |
| 66 | + |
| 67 | + if (isUpdate) |
| 68 | + { |
| 69 | + return notification.Succeeded ? StudioStatisticsEvent.AppUpdated : StudioStatisticsEvent.AppUpdateFailed; |
| 70 | + } |
| 71 | + |
| 72 | + return notification.Succeeded ? StudioStatisticsEvent.AppDeployed : StudioStatisticsEvent.AppDeployFailed; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Calculates if the app is updated based on the deployment history |
| 77 | + /// Current deploy is also stored in the database. |
| 78 | + /// If the current deploy is successful we're checking if there is one more successful deploy in the environment other than the current one. |
| 79 | + /// If the current deploy is unsuccessful we're checking if there was one successful deploy in the environment. |
| 80 | + /// </summary> |
| 81 | + private async Task<bool> IsAppUpdated(Events.DeploymentPipelineCompleted notification) |
| 82 | + { |
| 83 | + // if the current deployment is successful it will be contained in the list of successful deployments |
| 84 | + var deploymentsInEnvironment = (await _deploymentRepository.GetSucceeded( |
| 85 | + notification.EditingContext.Org, |
| 86 | + notification.EditingContext.Repo, |
| 87 | + notification.Environment, |
| 88 | + new DocumentQueryModel { SortDirection = SortDirection.Descending } |
| 89 | + )).ToList(); |
| 90 | + |
| 91 | + return (notification.Succeeded && deploymentsInEnvironment.Count > 1) || |
| 92 | + (!notification.Succeeded && deploymentsInEnvironment.Count > 0); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments