Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count results while printing them, when ignoring pagination #795

Merged
merged 11 commits into from
Oct 16, 2019
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed
- Add NULL check in nvts_feed_version_epoch [#768](https://github.com/greenbone/gvmd/pull/768)
- Faster counting in GET_REPORTS when ignoring pagination [#795](https://github.com/greenbone/gvmd/pull/795)
- Improve performance of GET_REPORTS [#797](https://github.com/greenbone/gvmd/pull/797)
- Consider results_trash when deleting users [#800](https://github.com/greenbone/gvmd/pull/800)

Expand Down
96 changes: 81 additions & 15 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -29245,7 +29245,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
int f_debugs, f_holes, f_infos, f_logs, f_warnings, f_false_positives;
int orig_f_debugs, orig_f_holes, orig_f_infos, orig_f_logs;
int orig_f_warnings, orig_f_false_positives, orig_filtered_result_count;
int search_phrase_exact, apply_overrides;
int search_phrase_exact, apply_overrides, count_filtered;
double severity, f_severity;
gchar *tz, *zone;
char *old_tz_override;
Expand Down Expand Up @@ -29483,8 +29483,12 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
"</delta>");
}

count_filtered = (delta == 0 && ignore_pagination && get->details);
Copy link
Member

Choose a reason for hiding this comment

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

This could perhaps also be used in any case where the number of rows is unlimited via a rows=-1 in the filter and the "Max Rows" limit is disabled.
If "Max Rows" is enabled it could also be compared against the unfiltered total or some other upper bound that's quick to query to see if using the number of returned results could give the wrong count.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good idea. Would like to leave it for a later PR though.


if (report)
{
/* Get total counts of full results. */

if (delta == 0)
{
int total_debugs, total_holes, total_infos, total_logs;
Expand All @@ -29502,10 +29506,29 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
get_data_reset (all_results_get);
free (all_results_get);
}
report_counts_id (report, &debugs, &holes, &infos, &logs, &warnings,
&false_positives, NULL, get, NULL);
filtered_result_count = debugs + holes + infos + logs + warnings
+ false_positives;

/* Get total counts of filtered results. */

if (count_filtered)
{
/* We're getting all the filtered results, so we can count them as we
* print them, to save time. */

filtered_result_count = 0;
}
else
{
/* Beware, we're using the full variables temporarily here, but
* report_counts_id counts the filtered results. */
report_counts_id (report, &debugs, &holes, &infos, &logs, &warnings,
&false_positives, NULL, get, NULL);

filtered_result_count = debugs + holes + infos + logs + warnings
+ false_positives;
}

/* Get report run status. */

report_scan_run_status (report, &run_status);
}

Expand Down Expand Up @@ -29894,11 +29917,25 @@ print_report_xml_start (report_t report, report_t delta, task_t task,

/* Prepare result counts. */

report_counts_id_full (report, &debugs, &holes, &infos, &logs,
&warnings, &false_positives, &severity,
get, NULL,
&f_debugs, &f_holes, &f_infos, &f_logs, &f_warnings,
&f_false_positives, &f_severity);
if (count_filtered)
{
/* We're getting all the filtered results, so we can count them as we
* print them, to save time. */

report_counts_id_full (report, &debugs, &holes, &infos, &logs,
&warnings, &false_positives, &severity,
get, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL);

f_debugs = f_holes = f_infos = f_logs = f_warnings = 0;
f_false_positives = f_severity = 0;
}
else
report_counts_id_full (report, &debugs, &holes, &infos, &logs,
&warnings, &false_positives, &severity,
get, NULL,
&f_debugs, &f_holes, &f_infos, &f_logs, &f_warnings,
&f_false_positives, &f_severity);

/* Results. */

Expand Down Expand Up @@ -30007,6 +30044,7 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
const char* level;
GHashTable *f_host_result_counts;
GString *buffer = g_string_new ("");
double result_severity;

buffer_results_xml (buffer,
&results,
Expand All @@ -30029,17 +30067,41 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
array_add_new_string (result_hosts,
result_iterator_host (&results));

result_severity = result_iterator_severity_double (&results);
if (result_severity > f_severity)
f_severity = result_severity;

level = result_iterator_level (&results);
if (strcasecmp (level, "log") == 0)
f_host_result_counts = f_host_logs;
{
f_host_result_counts = f_host_logs;
if (count_filtered)
f_logs++;
}
else if (strcasecmp (level, "high") == 0)
f_host_result_counts = f_host_holes;
{
f_host_result_counts = f_host_holes;
if (count_filtered)
f_holes++;
}
else if (strcasecmp (level, "medium") == 0)
f_host_result_counts = f_host_warnings;
{
f_host_result_counts = f_host_warnings;
if (count_filtered)
f_warnings++;
}
else if (strcasecmp (level, "low") == 0)
f_host_result_counts = f_host_infos;
{
f_host_result_counts = f_host_infos;
if (count_filtered)
f_infos++;
}
else if (strcasecmp (level, "false positive") == 0)
f_host_result_counts = f_host_false_positives;
{
f_host_result_counts = f_host_false_positives;
if (count_filtered)
f_false_positives++;
}
else
f_host_result_counts = NULL;

Expand Down Expand Up @@ -30088,6 +30150,10 @@ print_report_xml_start (report_t report, report_t delta, task_t task,
(strchr (levels, 'f') ? orig_f_false_positives : 0));
else
{
if (count_filtered)
filtered_result_count = f_debugs + f_holes + f_infos + f_logs
+ f_warnings + false_positives;

PRINT (out,
"<result_count>"
"%i"
Expand Down