/// <summary>
        /// 
        /// </summary>
        /// <param name="snapshot"></param>
        /// <param name="ruleExplorer"></param>
        /// <param name="businessCriteriasIds"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<RuleViolationResultDTO> GetNbViolationByRule(Snapshot snapshot, IRuleExplorer ruleExplorer, List<int> businessCriteriasIds, Int32 count)
        {
            if (snapshot == null || snapshot.TechnicalCriteriaResults == null) return null;

            //Get rules
            List<RuleDetails> rules = new List<RuleDetails>();
            foreach (var metricId in businessCriteriasIds)
            {
                var bcRules = ruleExplorer.GetRulesDetails(snapshot.DomainId, metricId.ToString(), snapshot.Id.ToString());

                rules.AddRange(bcRules);
            }

            rules = rules.GroupBy(_ => new { _.Key, _.Href, _.Name })
                         .Select(_ => new RuleDetails {
                                                         Key = _.Key.Key,
                                                         Href = _.Key.Href,
                                                         Name = _.Key.Name,
                                                         CompoundedWeight = _.Sum(x => x.CompoundedWeight),
                                                         Critical = _.Max(x => x.Critical)
                                                     })
                         .ToList();

            //Get result by technical criterias
            List<RuleViolationResultDTO> reslutByTechnicalCriterias = new List<RuleViolationResultDTO>();
            foreach (var rule in rules)
            {
                RuleViolationResultDTO ruleViolationResult = new RuleViolationResultDTO();

                var technicalCriterias = snapshot.TechnicalCriteriaResults
                                                 .Where(_ => _.RulesViolation!=null && _.RulesViolation.Where(p => p.Reference.Key.ToString() == rule.Key).Any())
                                                 .FirstOrDefault();

                if (technicalCriterias != null)
                {
                    ruleViolationResult.Rule = new RuleDetailsDTO { Name = rule.Name, Critical = rule.Critical, CompoundedWeight = rule.CompoundedWeight };

                    ruleViolationResult.Grade = technicalCriterias.DetailResult.Grade;

                    ruleViolationResult.TechnicalCriteraiName = technicalCriterias.Reference.Name;

                    var violationRatio = technicalCriterias.RulesViolation.Where(_ => _.Reference.Key.ToString() == rule.Key)
                                                                          .Select(_ => _.DetailResult.ViolationRatio)
                                                                          .FirstOrDefault();
                    if (violationRatio != null)
                    {
                        ruleViolationResult.TotalFailed = violationRatio.FailedChecks;
                        ruleViolationResult.TotalChecks = violationRatio.TotalChecks;
                    }

                    reslutByTechnicalCriterias.Add(ruleViolationResult);
                }
            }

            return reslutByTechnicalCriterias.OrderBy(_ => _.Rule.Name).Take(count).ToList();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="snapshot"></param>
        /// <param name="ruleExplorer"></param>
        /// <param name="businessCriteriasIds"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List <RuleViolationResultDTO> GetNbViolationByRule(Snapshot snapshot, IRuleExplorer ruleExplorer, List <int> businessCriteriasIds, int count)
        {
            if (snapshot?.TechnicalCriteriaResults == null)
            {
                return(null);
            }


            //Get rules
            List <RuleDetails> rules = new List <RuleDetails>();

            foreach (var metricId in businessCriteriasIds)
            {
                rules.AddRange(ruleExplorer.GetRulesDetails(snapshot.DomainId, metricId, snapshot.Id));
            }

            rules = rules.GroupBy(_ => new { _.Key, _.Href, _.Name })
                    .Select(_ => new RuleDetails {
                Key              = _.Key.Key,
                Href             = _.Key.Href,
                Name             = _.Key.Name,
                CompoundedWeight = _.Sum(x => x.CompoundedWeight),
                Critical         = _.Any(x => x.Critical)
            })
                    .ToList();

            //Get result by technical criterias
            List <RuleViolationResultDTO> reslutByTechnicalCriterias = new List <RuleViolationResultDTO>();

            foreach (var rule in rules)
            {
                // ruleViolationResult instanciation can not be out outside the loop, because the Add in reslutByTechnicalCriterias is by reference and list is corrupted
                RuleViolationResultDTO ruleViolationResult = new RuleViolationResultDTO();
                var technicalCriterias = snapshot.TechnicalCriteriaResults
                                         .FirstOrDefault(_ => _.RulesViolation != null && _.RulesViolation.Any(p => rule.Key.HasValue && p.Reference.Key == rule.Key.Value));

                if (technicalCriterias == null)
                {
                    continue;
                }

                ruleViolationResult.Rule = new RuleDetailsDTO {
                    Key = rule.Key ?? 0, Name = rule.Name, Critical = rule.Critical, CompoundedWeight = rule.CompoundedWeight ?? 0
                };
                ruleViolationResult.Grade = technicalCriterias.DetailResult.Grade;
                ruleViolationResult.TechnicalCriteraiName = technicalCriterias.Reference.Name;

                var violationRatio = technicalCriterias.RulesViolation.Where(_ => rule.Key.HasValue && _.Reference.Key == rule.Key.Value)
                                     .Select(_ => _.DetailResult?.ViolationRatio)
                                     .FirstOrDefault();
                if (violationRatio != null)
                {
                    ruleViolationResult.TotalFailed = violationRatio.FailedChecks;
                    ruleViolationResult.TotalChecks = violationRatio.TotalChecks;
                }

                reslutByTechnicalCriterias.Add(ruleViolationResult);
            }

            return(count == -1 ? reslutByTechnicalCriterias.OrderBy(_ => _.Rule.Name).ToList() : reslutByTechnicalCriterias.OrderBy(_ => _.Rule.Name).Take(count).ToList());
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="snapshot"></param>
        /// <param name="ruleExplorer"></param>
        /// <param name="businessCriteriasIds"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List <RuleViolationResultDTO> GetNbViolationByRule(Snapshot snapshot, IRuleExplorer ruleExplorer, List <int> businessCriteriasIds, Int32 count)
        {
            if (snapshot == null || snapshot.TechnicalCriteriaResults == null)
            {
                return(null);
            }


            //Get rules
            List <RuleDetails> rules = new List <RuleDetails>();

            foreach (var metricId in businessCriteriasIds)
            {
                var bcRules = ruleExplorer.GetRulesDetails(snapshot.DomainId, metricId.ToString(), snapshot.Id.ToString());

                rules.AddRange(bcRules);
            }

            rules = rules.GroupBy(_ => new { _.Key, _.Href, _.Name })
                    .Select(_ => new RuleDetails {
                Key              = _.Key.Key,
                Href             = _.Key.Href,
                Name             = _.Key.Name,
                CompoundedWeight = _.Sum(x => x.CompoundedWeight),
                Critical         = _.Max(x => x.Critical)
            })
                    .ToList();

            //Get result by technical criterias
            List <RuleViolationResultDTO> reslutByTechnicalCriterias = new List <RuleViolationResultDTO>();

            foreach (var rule in rules)
            {
                RuleViolationResultDTO ruleViolationResult = new RuleViolationResultDTO();

                var technicalCriterias = snapshot.TechnicalCriteriaResults
                                         .Where(_ => _.RulesViolation != null && _.RulesViolation.Where(p => p.Reference.Key.ToString() == rule.Key).Any())
                                         .FirstOrDefault();

                if (technicalCriterias != null)
                {
                    ruleViolationResult.Rule = new RuleDetailsDTO {
                        Name = rule.Name, Critical = rule.Critical, CompoundedWeight = rule.CompoundedWeight
                    };

                    ruleViolationResult.Grade = technicalCriterias.DetailResult.Grade;

                    ruleViolationResult.TechnicalCriteraiName = technicalCriterias.Reference.Name;

                    var violationRatio = technicalCriterias.RulesViolation.Where(_ => _.Reference.Key.ToString() == rule.Key)
                                         .Select(_ => _.DetailResult.ViolationRatio)
                                         .FirstOrDefault();
                    if (violationRatio != null)
                    {
                        ruleViolationResult.TotalFailed = violationRatio.FailedChecks;
                        ruleViolationResult.TotalChecks = violationRatio.TotalChecks;
                    }

                    reslutByTechnicalCriterias.Add(ruleViolationResult);
                }
            }

            return(reslutByTechnicalCriterias.OrderBy(_ => _.Rule.Name).Take(count).ToList());
        }