Beispiel #1
0
        /// <summary>
        /// Returns RunModel for run
        /// </summary>
        /// <param name="run"></param>
        /// <param name="scenarioRepository"></param>
        /// <param name="passRepository"></param>
        /// <param name="tenantSettingsRepository"></param>
        /// <param name="allCampaigns"></param>
        /// <param name="functionalAreaRepository"></param>
        /// <returns></returns>
        public static RunModel MapToRunModel(Run run, IScenarioRepository scenarioRepository, IPassRepository passRepository,
                                             ITenantSettingsRepository tenantSettingsRepository, IMapper mapper,
                                             IAnalysisGroupRepository analysisGroupRepository,
                                             IFunctionalAreaRepository functionalAreaRepository = null,
                                             IScenarioCampaignMetricRepository scenarioCampaignMetricRepository = null,
                                             List <CampaignWithProductFlatModel> allCampaigns = null,
                                             List <Pass> passesFromDb = null, List <Scenario> scenariosFromDb = null)
        {
            // Get scenarios for run
            var scenarios = scenariosFromDb ?? scenarioRepository.FindByIds(run.Scenarios.Select(s => s.Id)).ToList();

            // Get passes for run
            var passes = passesFromDb ?? passRepository.FindByIds(scenarios.SelectMany(s => s.Passes).Select(p => p.Id).Distinct()).ToList();

            var analysisGroups = run.AnalysisGroupTargets.Any()
                ? analysisGroupRepository.GetByIds(run.AnalysisGroupTargets.Select(x => x.AnalysisGroupId).ToArray()).ToDictionary(x => x.Id)
                : new Dictionary <int, AnalysisGroupNameModel>();

            // Get default ScenarioId
            var defaultScenarioId = tenantSettingsRepository.GetDefaultScenarioId();

            if (run.RunStatus == RunStatus.NotStarted && allCampaigns != null && allCampaigns.Any())
            {
                // Since the run has not already started amend Run Scenarios
                // with CampaignPassPriorities for new Campaigns and remove
                // CampaignPassPriorities of deleted Campaigns
                CampaignPassPrioritiesServiceMapper.AmendCampaignPassPriorities(
                    scenarios,
                    passes,
                    allCampaigns,
                    passRepository,
                    mapper);
            }

            var runModel = mapper.Map <RunModel>(Tuple.Create(run, scenarios, passes, analysisGroups, defaultScenarioId));

            if (run.RunStatus == RunStatus.Complete)
            {
                ApplyFunctionalAreaFaultTypesToRunModel(runModel, run, functionalAreaRepository);
            }

            if (scenarioCampaignMetricRepository != null)
            {
                runModel.Scenarios.ForEach(s => ApplyKPIsToScenarioCampaigns(s, scenarioCampaignMetricRepository));
            }

            return(runModel);
        }
Beispiel #2
0
        public IHttpActionResult SearchLibraryItems([FromUri] SearchQueryModel query)
        {
            var searchQuery = new SearchQueryDto(query);

            IEnumerable <PassDigestListItem> passesContainingTitleFromQuery = new List <PassDigestListItem>();

            if (!string.IsNullOrEmpty(searchQuery.Title))
            {
                var passesSearchQuery = new SearchQueryDto(new SearchQueryModel()
                {
                    OrderBy        = query.OrderBy,
                    OrderDirection = query.OrderDirection,
                    Title          = query.Title,
                    Top            = int.MaxValue,
                    Skip           = 0
                });

                passesContainingTitleFromQuery = _passRepository.MinimalDataSearch(passesSearchQuery, false)
                                                 .Items
                                                 .Where(p =>
                                                        p.Name.IndexOf(searchQuery.Title, StringComparison.CurrentCultureIgnoreCase) >= 0
                                                        );
            }

            var scenariosWithMinimalData = _scenarioRepository.MinimalDataSearch(
                searchQuery,
                true,
                passesContainingTitleFromQuery
                .Select(p => p.Id)
                );

            if (scenariosWithMinimalData.TotalCount > 0)
            {
                var defaultScenarioId = _tenantSettingsRepository.GetDefaultScenarioId();
                var defaultScenario   = scenariosWithMinimalData.Items.FirstOrDefault(c => c.Id == defaultScenarioId);

                if (defaultScenario != null)
                {
                    defaultScenario.IsDefault = true;
                }

                var extractedPasses = ExtractMinimalPassesFromScenarios(scenariosWithMinimalData)
                                      .ToList();

                var passesFromDb = _passRepository.FindByIds(extractedPasses.Select(pass => pass.Id))
                                   .Select(p => (p.Id, p.Name, p.DateModified))
                                   .ToDictionary(p => p.Id);

                List <int> passIdsToRemove = new List <int>();
                UpdatePassesWithMinimalData(extractedPasses, passesFromDb, passIdsToRemove);

                RemoveDeletedPasses(scenariosWithMinimalData, passIdsToRemove);

                var minimalDataSearchResult = _mapper.Map <SearchResultModel <ScenarioDigestListItemModel> >(scenariosWithMinimalData);

                return(Ok(minimalDataSearchResult));
            }
            else
            {
                return(NotFound());
            }

            void UpdatePassesWithMinimalData(
                List <PassDigestListItem> passesWithMinimalData,
                Dictionary <int, (int Id, string Name, DateTime?DateModified)> passesFromDb,
                List <int> passIdsToRemove)
            {
                passesWithMinimalData.ForEach(minimalDataPass =>
                {
                    var pass = passesFromDb.FirstOrDefault(pass => pass.Key == minimalDataPass.Id).Value;

                    if (pass.Id == minimalDataPass.Id)
                    {
                        minimalDataPass.DateModified = pass.DateModified.Value;
                        minimalDataPass.Name         = pass.Name;
                    }
                    else
                    {
                        passIdsToRemove.Add(minimalDataPass.Id);
                    }
                });
            }

            void RemoveDeletedPasses(SearchResultModel <ScenarioDigestListItem> scenariosWithMinimalData, List <int> passIdsToRemove)
            {
                passIdsToRemove.ForEach(id =>
                {
                    scenariosWithMinimalData.Items.ForEach(scenario =>
                    {
                        var passToRemove = scenario.Passes.Find(p => p.Id == id);
                        if (passToRemove != null)
                        {
                            scenario.Passes.Remove(passToRemove);
                        }
                    });
                });
            }

            IEnumerable <PassDigestListItem> ExtractMinimalPassesFromScenarios(SearchResultModel <ScenarioDigestListItem> searchScenarios)
            {
                var passes = new List <PassDigestListItem>();

                foreach (var scenario in searchScenarios.Items)
                {
                    passes.AddRange(scenario.Passes);
                }

                return(passes);
            }
        }