Exemplo n.º 1
0
        protected override TableDefinition Content(ReportData reportData, Dictionary<string, string> options)
        {
            bool perModule = options.GetBoolOption("MODULES", false); // module or application mode
            bool showGrades = options.GetBoolOption("GRADE", true); // show/hide grades
            bool showCritical = options.GetBoolOption("CRITICAL", true); // show/hide critical rules
            bool showNonCritical = options.GetBoolOption("NONCRITICAL", false); // show/hide critical rules
            bool showAddedRemoved = options.GetBoolOption("ADDEDREMOVED", true); // show/hide added/removed
            bool showTotal = options.GetBoolOption("TOTAL", true); // show/hide total
            bool showFailedChecks = options.GetBoolOption("FAILED", false); // show/hide failed checks
            bool showSuccessfulChecks = options.GetBoolOption("SUCCESSFUL", false); // show/hide successfull checks
            bool showCompliance = options.GetBoolOption("COMPLIANCE", false); // show/hide compliance ration

            var headers = new HeaderDefinition();
            headers.Append(Labels.ModuleName, perModule);
            headers.Append(Labels.RuleName);
            headers.Append(Labels.Grade, showGrades);
            headers.Append(Labels.ViolationsCount, showFailedChecks);
            headers.Append(Labels.TotalOk, showSuccessfulChecks);
            headers.Append(Labels.TotalChecks, showTotal);
            headers.Append(Labels.Compliance, showCompliance);
            headers.Append(Labels.ViolationsAdded, showAddedRemoved);
            headers.Append(Labels.ViolationsRemoved, showAddedRemoved);
            headers.Append(Labels.Critical);

            var dataRow = headers.CreateDataRow();
            var data = new List<string>();

            if (reportData != null && reportData.CurrentSnapshot != null) {
                Dictionary<int, RuleDetails> targetRules =
                    reportData.RuleExplorer
                    .GetRulesDetails(reportData.CurrentSnapshot.DomainId, Constants.BusinessCriteria.TechnicalQualityIndex.GetHashCode(), reportData.CurrentSnapshot.Id)
                    .Where(rd => rd.Key.HasValue && ((showCritical && rd.Critical == true) || (showNonCritical && rd.Critical == false)))
                    .ToDictionary(rd => rd.Key.Value);

                var sourceResults = reportData.CurrentSnapshot.QualityRulesResults.Where(qr => targetRules.ContainsKey(qr.Reference.Key));

                var modules = (perModule ? reportData.CurrentSnapshot.Modules : new List<Module>(new Module[] { null }).AsEnumerable()).OrderBy(m => (m == null ? string.Empty : m.Name));

                foreach (var module in modules) {
                    if (perModule) {
                        dataRow.Set(Labels.ModuleName, module.Name.DashIfEmpty());
                    }

                    var query = sourceResults.Select(ar => new { Reference = ar.Reference, AppDetailResult = ar.DetailResult, ModDetailResult = GetModuleResult(ar, module) });

                    foreach (var result in query) {
                        var detailResult = perModule ? result.ModDetailResult : result.AppDetailResult;
                        if (detailResult != null && detailResult.Grade > 0) {
                            dataRow.Set(Labels.RuleName, result.Reference?.Name.DashIfEmpty());
                            if (showGrades) {
                                dataRow.Set(Labels.Grade, detailResult.Grade.ToString("N2"));
                            }
                            if (showFailedChecks) {
                                dataRow.Set(Labels.ViolationsCount, detailResult.ViolationRatio?.FailedChecks.DashIfEmpty());
                            }
                            if (showSuccessfulChecks) {
                                dataRow.Set(Labels.TotalOk, detailResult.ViolationRatio?.SuccessfulChecks.DashIfEmpty());
                            }
                            if (showTotal) {
                                dataRow.Set(Labels.TotalChecks, detailResult.ViolationRatio?.TotalChecks.DashIfEmpty());
                            }
                            if (showCompliance) {
                                dataRow.Set(Labels.Compliance, detailResult.ViolationRatio?.Ratio.FormatPercent(false));
                            }
                            if (showAddedRemoved) {
                                dataRow.Set(Labels.ViolationsAdded, detailResult.EvolutionSummary.AddedViolations.DashIfEmpty());
                                dataRow.Set(Labels.ViolationsRemoved, detailResult.EvolutionSummary?.RemovedViolations.DashIfEmpty());
                            }
                            var ruleId = result.Reference?.Key;
                            dataRow.Set(Labels.Critical, (ruleId.HasValue && targetRules.ContainsKey(ruleId.Value) && targetRules[ruleId.Value].Critical) ? "X" : "");

                            data.AddRange(dataRow);
                        }
                    }
                }
            }

            if (data.Count == 0) {
                dataRow.Reset();
                dataRow.Set(0, Labels.NoItem);
                data.AddRange(dataRow);
            }

            data.InsertRange(0, headers.Labels);

            return new TableDefinition {
                Data = data,
                HasColumnHeaders = true,
                HasRowHeaders = false,
                NbColumns = headers.Count,
                NbRows = data.Count / headers.Count
            };
        }
Exemplo n.º 2
0
        public override TableDefinition Content(ReportData reportData, Dictionary <string, string> options)
        {
            int  nbLimit              = options.GetIntOption("COUNT", reportData.Parameter.NbResultDefault); // Max number of rows; -1 correspond to all results
            bool perModule            = options.GetBoolOption("MODULES");                                    // module or application mode
            bool showGrades           = options.GetBoolOption("GRADE", true);                                // show/hide grades
            bool showCritical         = options.GetBoolOption("CRITICAL", true);                             // show/hide critical rules
            bool showNonCritical      = options.GetBoolOption("NONCRITICAL");                                // show/hide critical rules
            bool showAddedRemoved     = options.GetBoolOption("ADDEDREMOVED", true);                         // show/hide added/removed
            bool showTotal            = options.GetBoolOption("TOTAL", true);                                // show/hide total
            bool showFailedChecks     = options.GetBoolOption("FAILED");                                     // show/hide failed checks
            bool showSuccessfulChecks = options.GetBoolOption("SUCCESSFUL");                                 // show/hide successfull checks
            bool showCompliance       = options.GetBoolOption("COMPLIANCE");                                 // show/hide compliance ration

            var headers = new HeaderDefinition();

            headers.Append(Labels.ModuleName, perModule);
            headers.Append(Labels.RuleName);
            headers.Append(Labels.Grade, showGrades);
            headers.Append(Labels.ViolationsCount, showFailedChecks);
            headers.Append(Labels.TotalOk, showSuccessfulChecks);
            headers.Append(Labels.TotalChecks, showTotal);
            headers.Append(Labels.Compliance, showCompliance);
            headers.Append(Labels.ViolationsAdded, showAddedRemoved);
            headers.Append(Labels.ViolationsRemoved, showAddedRemoved);
            headers.Append(Labels.Critical);

            var dataRow = headers.CreateDataRow();
            var data    = new List <string>();

            int nbRow = 0;

            if (reportData.CurrentSnapshot != null)
            {
                Dictionary <int, RuleDetails> targetRules =
                    reportData.RuleExplorer
                    .GetRulesDetails(reportData.CurrentSnapshot.DomainId, Constants.BusinessCriteria.TechnicalQualityIndex.GetHashCode(), reportData.CurrentSnapshot.Id)
                    .Where(rd => rd.Key.HasValue && ((showCritical && rd.Critical) || (showNonCritical && rd.Critical == false)))
                    .ToDictionary(rd => rd.Key.Value);

                var sourceResults = reportData.CurrentSnapshot.QualityRulesResults.Where(qr => targetRules.ContainsKey(qr.Reference.Key)).ToList();

                var modules = (perModule ? reportData.CurrentSnapshot.Modules : new List <Module>(new Module[] { null }).AsEnumerable()).OrderBy(m => (m == null ? string.Empty : m.Name));

                foreach (var module in modules)
                {
                    if (perModule)
                    {
                        dataRow.Set(Labels.ModuleName, module.Name.NAIfEmpty());
                    }

                    var query = sourceResults.Select(ar => new { ar.Reference, AppDetailResult = ar.DetailResult, ModDetailResult = GetModuleResult(ar, module) });

                    foreach (var result in query)
                    {
                        if ((nbLimit != -1) && (nbRow >= nbLimit))
                        {
                            continue;
                        }
                        var detailResult = perModule ? result.ModDetailResult : result.AppDetailResult;
                        if (detailResult != null && detailResult.Grade > 0)
                        {
                            dataRow.Set(Labels.RuleName, result.Reference?.Name.NAIfEmpty());
                            if (showGrades)
                            {
                                dataRow.Set(Labels.Grade, detailResult.Grade?.ToString("N2"));
                            }
                            if (showFailedChecks)
                            {
                                dataRow.Set(Labels.ViolationsCount, (bool)detailResult.ViolationRatio?.FailedChecks.HasValue ? detailResult.ViolationRatio?.FailedChecks.Value.ToString("N0") : Constants.No_Value);
                            }
                            if (showSuccessfulChecks)
                            {
                                dataRow.Set(Labels.TotalOk, (bool)detailResult.ViolationRatio?.SuccessfulChecks.HasValue ? detailResult.ViolationRatio?.SuccessfulChecks.Value.ToString("N0") : Constants.No_Value);
                            }
                            if (showTotal)
                            {
                                dataRow.Set(Labels.TotalChecks, (bool)detailResult.ViolationRatio?.TotalChecks.HasValue ? detailResult.ViolationRatio?.TotalChecks.Value.ToString("N0") : Constants.No_Value);
                            }
                            if (showCompliance)
                            {
                                dataRow.Set(Labels.Compliance, detailResult.ViolationRatio?.Ratio.FormatPercent(false));
                            }
                            if (showAddedRemoved)
                            {
                                dataRow.Set(Labels.ViolationsAdded, detailResult.EvolutionSummary?.AddedViolations.NAIfEmpty());
                                dataRow.Set(Labels.ViolationsRemoved, detailResult.EvolutionSummary?.RemovedViolations.NAIfEmpty());
                            }
                            var ruleId = result.Reference?.Key;
                            dataRow.Set(Labels.Critical, (ruleId.HasValue && targetRules.ContainsKey(ruleId.Value) && targetRules[ruleId.Value].Critical) ? "X" : "");

                            data.AddRange(dataRow);
                        }
                        nbRow++;
                    }
                }
            }

            if (data.Count == 0)
            {
                dataRow.Reset();
                dataRow.Set(0, Labels.NoItem);
                data.AddRange(dataRow);
            }

            data.InsertRange(0, headers.Labels);

            return(new TableDefinition {
                Data = data,
                HasColumnHeaders = true,
                HasRowHeaders = false,
                NbColumns = headers.Count,
                NbRows = data.Count / headers.Count
            });
        }
Exemplo n.º 3
0
        public override TableDefinition Content(ReportData reportData, Dictionary <string, string> options)
        {
            string standard = options.GetOption("STD");

            bool   vulnerability = options.GetOption("LBL", "vulnerabilities").ToLower().Equals("vulnerabilities");
            string lbltotal      = vulnerability ? Labels.TotalVulnerabilities : Labels.TotalViolations;
            string lbladded      = vulnerability ? Labels.AddedVulnerabilities : Labels.AddedViolations;
            string lblremoved    = vulnerability ? Labels.RemovedVulnerabilities : Labels.RemovedViolations;


            // cellProps will contains the properties of the cell (background color) linked to the data by position in the list stored with cellidx.
            List <CellAttributes> cellProps = new List <CellAttributes>();
            int cellidx = 0;

            var headers = new HeaderDefinition();

            headers.Append(standard);
            cellidx++;
            headers.Append(lbltotal);
            cellidx++;
            headers.Append(lbladded);
            cellidx++;
            headers.Append(lblremoved);
            cellidx++;

            var dataRow = headers.CreateDataRow();
            var data    = new List <string>();

            List <ApplicationResult> results = reportData.SnapshotExplorer.GetQualityStandardsTagsResults(reportData.CurrentSnapshot.Href, standard)?.FirstOrDefault()?.ApplicationResults?.ToList();

            if (results?.Count > 0)
            {
                foreach (var result in results)
                {
                    var detailResult = result.DetailResult;
                    if (detailResult == null)
                    {
                        continue;
                    }
                    int?   nbViolations = detailResult.EvolutionSummary?.TotalViolations;
                    string stdTagName   = result.Reference?.Name + " " + reportData.Application.StandardTags?.Where(_ => _.Key == result.Reference?.Name).FirstOrDefault()?.Name;
                    dataRow.Set(standard, stdTagName);
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    dataRow.Set(lbltotal, detailResult.EvolutionSummary?.TotalViolations.NAIfEmpty("N0"));
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    dataRow.Set(lbladded, detailResult.EvolutionSummary?.AddedViolations.NAIfEmpty("N0"));
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    dataRow.Set(lblremoved, detailResult.EvolutionSummary?.RemovedViolations.NAIfEmpty("N0"));
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    data.AddRange(dataRow);
                }
            }

            if (data.Count == 0)
            {
                dataRow.Reset();
                dataRow.Set(0, Labels.NoRules);
                data.AddRange(dataRow);
            }

            data.InsertRange(0, headers.Labels);

            return(new TableDefinition
            {
                Data = data,
                HasColumnHeaders = true,
                HasRowHeaders = false,
                NbColumns = headers.Count,
                NbRows = data.Count / headers.Count,
                CellsAttributes = cellProps
            });
        }
        public override TableDefinition Content(ReportData reportData, Dictionary <string, string> options)
        {
            List <string> metrics = options.GetOption("METRICS").Trim().Split('|').ToList();
            bool          critical;

            if (options == null || !options.ContainsKey("CRITICAL"))
            {
                critical = false;
            }
            else
            {
                critical = options.GetOption("CRITICAL").Equals("true");
            }
            bool displayCompliance  = options.GetBoolOption("COMPLIANCE");
            bool sortedByCompliance = displayCompliance && options.GetOption("SORTED", "TOTAL").Equals("COMPLIANCE");

            bool   vulnerability = options.GetOption("LBL", "vulnerabilities").ToLower().Equals("vulnerabilities");
            string lbltotal      = vulnerability ? Labels.TotalVulnerabilities : Labels.TotalViolations;
            string lbladded      = vulnerability ? Labels.AddedVulnerabilities : Labels.AddedViolations;
            string lblremoved    = vulnerability ? Labels.RemovedVulnerabilities : Labels.RemovedViolations;

            // cellProps will contains the properties of the cell (background color) linked to the data by position in the list stored with cellidx.
            List <CellAttributes> cellProps = new List <CellAttributes>();
            int cellidx = 0;

            var headers = new HeaderDefinition();

            headers.Append(Labels.CASTRules);
            cellidx++;
            headers.Append(lbltotal);
            cellidx++;
            headers.Append(lbladded);
            cellidx++;
            headers.Append(lblremoved);
            cellidx++;
            headers.Append(Labels.ComplianceScorePercent, displayCompliance);
            if (displayCompliance)
            {
                cellidx++;
            }

            var dataRow = headers.CreateDataRow();
            var data    = new List <string>();

            List <string> qualityRules = MetricsUtility.BuildRulesList(reportData, metrics, critical);

            List <ApplicationResult> results = sortedByCompliance ?
                                               reportData.CurrentSnapshot?.QualityRulesResults.Where(_ => qualityRules.Contains(_.Reference.Key.ToString())).OrderBy(_ => _.DetailResult.ViolationRatio.Ratio).ToList()
                    : reportData.CurrentSnapshot?.QualityRulesResults.Where(_ => qualityRules.Contains(_.Reference.Key.ToString())).OrderByDescending(_ => _.DetailResult.ViolationRatio.FailedChecks).ToList();

            if (results?.Count > 0)
            {
                foreach (var result in results)
                {
                    var detailResult = result.DetailResult;
                    if (detailResult == null)
                    {
                        continue;
                    }
                    int nbViolations = detailResult.ViolationRatio.FailedChecks ?? 0;

                    dataRow.Set(Labels.CASTRules, (result.Reference?.Name + " (" + result.Reference?.Key + ")").NAIfEmpty());
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    dataRow.Set(lbltotal, detailResult.ViolationRatio.FailedChecks.HasValue ? detailResult.ViolationRatio?.FailedChecks.Value.ToString("N0") : Constants.No_Value);
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    dataRow.Set(lbladded, detailResult.EvolutionSummary?.AddedViolations.NAIfEmpty("N0"));
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    dataRow.Set(lblremoved, detailResult.EvolutionSummary?.RemovedViolations.NAIfEmpty("N0"));
                    if (nbViolations > 0)
                    {
                        cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                    }
                    cellidx++;
                    if (displayCompliance)
                    {
                        dataRow.Set(Labels.ComplianceScorePercent, detailResult.ViolationRatio?.Ratio.FormatPercent(false));
                        if (nbViolations > 0)
                        {
                            cellProps.Add(new CellAttributes(cellidx, Color.Beige));
                        }
                        cellidx++;
                    }
                    data.AddRange(dataRow);
                }
            }

            if (data.Count == 0)
            {
                dataRow.Reset();
                dataRow.Set(0, Labels.NoRules);
                data.AddRange(dataRow);
            }

            data.InsertRange(0, headers.Labels);

            return(new TableDefinition
            {
                Data = data,
                HasColumnHeaders = true,
                HasRowHeaders = false,
                NbColumns = headers.Count,
                NbRows = data.Count / headers.Count,
                CellsAttributes = cellProps
            });
        }