public IHttpActionResult GetCampaignsForAnalysisGroup(int id)
        {
            var existingAnalysisGroup = _analysisGroupRepository.Get(id);

            if (existingAnalysisGroup is null)
            {
                return(this.Error().ResourceNotFound($"Analysis Group with id {id} does not exist"));
            }

            var campaignIds = _groupCampaignQuery.GetAnalysisGroupCampaigns(existingAnalysisGroup.Filter);

            return(Ok(campaignIds));
        }
        /// <inheritdoc />
        public List <AnalysisGroupTargetMetric> CalculateAnalysisGroupKPIs(Guid runId)
        {
            _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Executing Run AnalysisGroup KPI processing (RunID={runId})"));

            var result = new List <AnalysisGroupTargetMetric>();

            var targets = _kpiCalculationContext.Snapshot.Run.Value.AnalysisGroupTargets;

            if (!targets.Any())
            {
                return(result);
            }

            var analysisGroupTragets = targets
                                       .GroupBy(c => c.AnalysisGroupId);

            foreach (var analysisGroupTraget in analysisGroupTragets)
            {
                var analysisGroup = _analysisGroupRepository.GetIncludingSoftDeleted(analysisGroupTraget.Key);

                var campaignsIds       = new HashSet <Guid>(_analysisGroupCampaignQuery.GetAnalysisGroupCampaigns(analysisGroup.Filter));
                var campaignsToInclude = _kpiCalculationContext.Snapshot
                                         .AllCampaigns
                                         .Value
                                         .Where(c => campaignsIds.Contains(c.Id));

                var campaignExternalIds = new HashSet <string>(
                    campaignsToInclude.Select(c => c.ExternalId)
                    );

                var kpis = CalculateAnalysisGroupCampaignKPIs(
                    campaignsToInclude,
                    _kpiCalculationContext.Recommendations.Where(c => campaignExternalIds.Contains(c.ExternalCampaignNumber))
                    );

                foreach (var item in analysisGroupTraget)
                {
                    double value = default;

                    if (string.Equals(item.KPI, nameof(AnalysisGroupKPI.DeliveryPercentage), StringComparison.OrdinalIgnoreCase))
                    {
                        value = kpis.DeliveryPercentage;
                    }
                    else if (string.Equals(item.KPI, nameof(AnalysisGroupKPI.PoolValue), StringComparison.OrdinalIgnoreCase))
                    {
                        value = kpis.PoolValue;
                    }
                    else if (string.Equals(item.KPI, nameof(AnalysisGroupKPI.RatingsDelivery), StringComparison.OrdinalIgnoreCase))
                    {
                        value = kpis.RatingsDelivery;
                    }
                    else if (string.Equals(item.KPI, nameof(AnalysisGroupKPI.RevenueBooked), StringComparison.OrdinalIgnoreCase))
                    {
                        value = kpis.RevenueBooked;
                    }
                    else if (string.Equals(item.KPI, nameof(AnalysisGroupKPI.Spots), StringComparison.OrdinalIgnoreCase))
                    {
                        value = kpis.Spots;
                    }
                    else if (string.Equals(item.KPI, nameof(AnalysisGroupKPI.ZeroRatedSpots), StringComparison.OrdinalIgnoreCase))
                    {
                        value = kpis.ZeroRatedSpots;
                    }
                    else
                    {
                        continue;
                    }

                    var metric = new AnalysisGroupTargetMetric
                    {
                        AnalysisGroupTargetId = item.Id,
                        Value = value
                    };

                    result.Add(metric);
                }
            }

            _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, $"Executed Run AnalysisGroup KPI processing (RunID={runId})"));

            return(result);
        }