Ejemplo n.º 1
0
        public async Task <SearchTagBO> SearchRulesAsync(List <Guid> collectionsSelected, List <Guid> containersSelected, List <int> tagsSelected, string name, string diagnostic, int?detailsDestination, int?severityId)
        {
            SearchTagBO result   = new SearchTagBO();
            List <Rule> allRules = await _ruleQueries
                                   .GetByCollectionOrContainerOrTagAsync(collectionsSelected, containersSelected, tagsSelected
                                                                         .ToList());

            if (!string.IsNullOrEmpty(name))
            {
                allRules = allRules
                           .Where(rec => rec.Name.ToLower().Contains(name.ToLower()))
                           .ToList();
            }

            if (!string.IsNullOrEmpty(diagnostic))
            {
                allRules = allRules
                           .Where(rec => rec.DiagnosticSql.ToLower().Contains(diagnostic.ToLower()))
                           .ToList();
            }

            if (detailsDestination != null)
            {
                var collectionsByDestination = await _collectionQueries.GetByRulesDestinationIdAsync(detailsDestination.Value);

                if (collectionsByDestination != null && collectionsByDestination.Any())
                {
                    List <Guid> containersByDestination = new List <Guid>();
                    foreach (var collection in collectionsByDestination)
                    {
                        if (collection.ChildContainers != null && collection.ChildContainers.Any())
                        {
                            containersByDestination.AddRange(collection.ChildContainers.Select(rec => rec.Id));
                        }
                    }

                    if (containersByDestination.Any())
                    {
                        containersByDestination = containersByDestination.Distinct().ToList();
                        allRules = allRules
                                   .Where(rec => containersByDestination.Contains(rec.ContainerId))
                                   .ToList();
                    }
                }
            }

            if (severityId != null)
            {
                allRules = allRules
                           .Where(rec => rec.ErrorSeverityLevel == severityId.Value)
                           .ToList();
            }

            result.Rules = allRules
                           .Select(rec => MapEntityToModel(rec))
                           .ToList();

            if (result.Rules != null && result.Rules.Count > 0)
            {
                var listEnvironmentType = await _catalogQueries.GetByTypeAsync("EnvironmentType");

                List <Guid> containersParents = result
                                                .Rules
                                                .Select(rec => rec.ContainerId)
                                                .ToList();

                containersParents = containersParents.Distinct().ToList();

                List <Container> allContainersParents = await _collectionQueries
                                                        .GetByListAsync(containersParents);

                List <Container> allContainersMain = await _collectionQueries
                                                     .GetByListAsync(allContainersParents.Select(rec => rec.ParentContainerId.Value).ToList());

                foreach (RuleBO rule in result.Rules)
                {
                    Container existParent = allContainersParents.FirstOrDefault(rec => rec.Id == rule.ContainerId);
                    if (existParent != null && existParent.ParentContainerId != null)
                    {
                        Container existParentMain = allContainersMain.FirstOrDefault(rec => rec.Id == existParent.ParentContainerId.Value);
                        if (existParentMain != null)
                        {
                            Catalog existType = listEnvironmentType.FirstOrDefault(rec => rec.Id == existParentMain.EnvironmentType);
                            if (existType != null)
                            {
                                rule.ParentContainer         = existParentMain.Id;
                                rule.EnvironmentTypeText     = existType.Name;
                                rule.CollectionContainerName = $"{existParentMain.Name}/{existParent.Name}";
                                rule.ContainerName           = existParent.Name;
                                rule.CollectionName          = existParentMain.Name;
                            }
                        }
                    }
                }

                result.Rules = result.Rules.OrderBy(rec => rec.CollectionContainerName).ToList();
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <SearchTagBO> SearchByTags(List <TagBO> tags)
        {
            SearchTagBO result = new SearchTagBO();

            var entitiesFound = await _TagQueries
                                .GetByTagsAsync(tags.Select(rec => rec.Id)
                                                .ToList());

            if (entitiesFound != null && entitiesFound.Any())
            {
                List <Guid> containersList = entitiesFound
                                             .Where(rec => rec.ContainerId != null)
                                             .Select(rec => rec.ContainerId.Value)
                                             .ToList();

                List <Guid> rulesList = entitiesFound
                                        .Where(rec => rec.RuleId != null)
                                        .Select(rec => rec.RuleId.Value)
                                        .ToList();

                List <Container> containers = await _collectionQueries
                                              .GetByListAsync(containersList);

                List <Rule> rules = await _ruleQueries
                                    .GetByListAsync(rulesList);

                rules.AddRange((await _ruleQueries
                                .GetByContainerListAsync(containers
                                                         .Select(rec => rec.Id)
                                                         .ToList())));

                rules = rules
                        .Distinct()
                        .ToList();

                result.Collections = containers
                                     .Where(rec => rec.ParentContainerId == null)
                                     .Select(rec => new ContainerBO
                {
                    Name           = rec.Name,
                    Description    = rec.Name,
                    IsDefault      = rec.IsDefault,
                    TagIsInherited = false
                })
                                     .ToList();

                result.Containers = containers
                                    .Where(rec => rec.ParentContainerId != null)
                                    .Select(rec => new ContainerBO
                {
                    Name              = rec.Name,
                    Description       = rec.Name,
                    IsDefault         = rec.IsDefault,
                    ParentContainerId = rec.ParentContainerId,
                    TagIsInherited    = entitiesFound.FirstOrDefault(aux => aux.ContainerId == rec.Id) == null
                })
                                    .ToList();

                result.Rules = rules
                               .Select(rec => new RuleBO {
                    RuleIdentification = rec.RuleIdentification,
                    Name           = rec.Name,
                    Description    = rec.Description,
                    ContainerId    = rec.ContainerId,
                    TagIsInherited = entitiesFound.FirstOrDefault(aux => aux.RuleId == rec.Id) == null
                })
                               .ToList();

                result.TagsSelected = tags;
            }

            return(result);
        }