Skip to content

Add merge reports option in report command#10

Merged
dimityrmirchev merged 16 commits intogardener:mainfrom
AleksandarSavchev:add-merged-reports
Sep 14, 2023
Merged

Add merge reports option in report command#10
dimityrmirchev merged 16 commits intogardener:mainfrom
AleksandarSavchev:add-merged-reports

Conversation

@AleksandarSavchev
Copy link
Copy Markdown
Member

@AleksandarSavchev AleksandarSavchev commented Sep 8, 2023

What this PR does / why we need it:
This PR add a new functionality to the diki report command which allows the merge of multiple json reports into a single html report. To use this feature the distinct-by flag should be set with unique attributes which would be used for each provider to distinct the different reports.

TODO: write documentation

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Release note:

The `diki report` command can now be used to merge multiple reports into a single report by setting the `--distinct-by` flag.

@gardener-robot gardener-robot added needs/review size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 8, 2023
@gardener-robot gardener-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 11, 2023
@ghost ghost removed the reviewed/ok-to-test label Sep 11, 2023
@ghost ghost removed the reviewed/ok-to-test label Sep 11, 2023
@AleksandarSavchev AleksandarSavchev marked this pull request as ready for review September 11, 2023 12:08
@AleksandarSavchev AleksandarSavchev requested a review from a team as a code owner September 11, 2023 12:08
Copy link
Copy Markdown
Member

@dimityrmirchev dimityrmirchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left some comments. Also it would be nice if we can have a simple test for the MergeReport function

Comment thread cmd/diki/app/app.go Outdated
if len(args) != 1 {
return errors.New("report requires a single filepath argument")
if len(args) == 0 {
return errors.New("report requires a minimum of one filepath arguments")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return errors.New("report requires a minimum of one filepath arguments")
return errors.New("report command requires a minimum of one filepath argument")

Comment thread cmd/diki/app/app.go Outdated
}

if len(args) > 1 && len(opts.distinctBy) == 0 {
return errors.New("report requires a single filepath argument when flag distinct-by is not set ")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return errors.New("report requires a single filepath argument when flag distinct-by is not set ")
return errors.New("report command requires a single filepath argument when the distinct-by flag is not set ")

Comment thread README.md Outdated
#### Report

Generate an html report
Diki report generates a human readable html report from the .json output of a `diki run` execution. Merged reports can be generated using the `distinct-by` flag, the value of this flag is a list of key:value pairs where the keys are the IDs of the providers we want to include in the merged report and the values are the unique metadata fields to be used for IDs of the different reports.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Diki report generates a human readable html report from the .json output of a `diki run` execution. Merged reports can be generated using the `distinct-by` flag, the value of this flag is a list of key:value pairs where the keys are the IDs of the providers we want to include in the merged report and the values are the unique metadata fields to be used for IDs of the different reports.
Diki can generate a human readable report from the output files of a `diki run` execution. Merged reports can be produced by setting the `distinct-by` flag. The value of this flag is a list of `key=value` pairs where the keys are the IDs of the providers we want to include in the merged report and the values are the unique metadata fields to be used as distinction values between different provider runs.

Comment thread cmd/diki/app/app.go Outdated
for _, arg := range args {
fileData, err := os.ReadFile(filepath.Clean(arg))
if err != nil {
return fmt.Errorf("failed to read file %s:%w", arg, err)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("failed to read file %s:%w", arg, err)
return fmt.Errorf("failed to read file %s: %w", arg, err)

Comment thread pkg/report/render.go Outdated
case *MergedReport:
return r.templates[tmplMergedReportName].Execute(w, rep)
default:
return fmt.Errorf("unsupported report type: %T", rep)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("unsupported report type: %T", rep)
return fmt.Errorf("unsupported report type: %T", report)

Comment thread pkg/report/merged_report.go Outdated
}

if _, ok := mergedProvider.Metadata[uniqueAttr]; ok {
return &MergedReport{}, fmt.Errorf("distinct attribute %s is not unique", mergedProvider.DistinctBy)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return &MergedReport{}, fmt.Errorf("distinct attribute %s is not unique", mergedProvider.DistinctBy)
return nil, fmt.Errorf("distinct attribute %s is not unique", mergedProvider.DistinctBy)

Comment thread pkg/report/merged_report.go Outdated
}

mergedProvider.Metadata[uniqueAttr] = report.Providers[idx].Metadata
mergedProvider.Metadata[uniqueAttr]["time"] = report.Time.Format("01-02-2006")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add the time of the report?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a good addition for a more detailed report. I have added HH-MM-SS now as well.

Comment thread pkg/report/merged_report.go Outdated
func numOfMergedRulesWithStatus(ruleset *MergedRuleset, status rule.Status) int {
num := 0
for _, rule := range ruleset.Rules {
for _, check := range rule.Checks {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use slices.ContainsFunc here?

Comment thread pkg/report/merged_report.go Outdated
}

func metadataTextForMergedProvider(mp MergedProvider) map[string]string {
metadataTexts := map[string]string{}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
metadataTexts := map[string]string{}
metadataTexts := make(map[string]string, len(mp.Metadata))

Comment thread pkg/report/merged_report.go Outdated
func metadataTextForMergedProvider(mp MergedProvider) map[string]string {
metadataTexts := map[string]string{}
for id, metadata := range mp.Metadata {
metadataText := "("
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good place to use strings.Builder

Comment thread pkg/report/merged_report.go Outdated
for key := range distinctByAttrs {
distinctByAttrsProviders = append(distinctByAttrsProviders, key)
}
sort.Strings(distinctByAttrsProviders)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use slices.Sort

Comment thread pkg/report/report_suite_test.go Outdated
Comment on lines +19 to +21
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})
logger := slog.New(handler)
testLogger = logger
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used

Copy link
Copy Markdown
Member

@dimityrmirchev dimityrmirchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
/lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants