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

Get class outside severity_in_level, to save time #801

Merged
merged 3 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Add setting "BPM Dashboard Configuration" [#764](https://github.com/greenbone/gvmd/pull/764)
- Faster SecInfo REF retrieval for GET_REPORTS [#793](https://github.com/greenbone/gvmd/pull/793)
- Improve performance of GET_REPORTS [#801](https://github.com/greenbone/gvmd/pull/801)

### Changed

Expand Down
27 changes: 19 additions & 8 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,16 +1787,10 @@ manage_create_sql_functions ()
sql_database ()))
{
sql ("CREATE OR REPLACE FUNCTION severity_in_level (double precision,"
" text,"
" text)"
" RETURNS boolean AS $$"
" SELECT CASE (SELECT value FROM settings"
" WHERE name = 'Severity Class'"
" AND ((owner IS NULL)"
" OR (owner = (SELECT id FROM users"
" WHERE users.uuid"
" = (SELECT uuid"
" FROM current_credentials))))"
" ORDER BY coalesce (owner, 0) DESC LIMIT 1)"
" SELECT CASE $3"
" WHEN 'pci-dss'"
" THEN (CASE lower ($2)"
" WHEN 'high'"
Expand Down Expand Up @@ -1827,6 +1821,23 @@ manage_create_sql_functions ()
" END;"
"$$ LANGUAGE SQL;");

sql ("CREATE OR REPLACE FUNCTION severity_in_level (double precision,"
" text)"
" RETURNS boolean AS $$"
" SELECT severity_in_level"
" ($1,"
" $2,"
" (SELECT value FROM settings"
" WHERE name = 'Severity Class'"
" AND ((owner IS NULL)"
" OR (owner = (SELECT id FROM users"
" WHERE users.uuid"
" = (SELECT uuid"
" FROM current_credentials))))"
" ORDER BY coalesce (owner, 0) DESC LIMIT 1))"
"$$ LANGUAGE SQL"
" STABLE;");

sql ("CREATE OR REPLACE FUNCTION severity_to_level (text, integer)"
" RETURNS text AS $$"
" SELECT CASE"
Expand Down
47 changes: 33 additions & 14 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -23111,6 +23111,7 @@ where_levels_auto (const char *levels, const char *new_severity_sql,
{
int count;
GString *levels_sql;
char *class;

/* Generate SQL for constraints on message type, according to levels. */

Expand All @@ -23126,16 +23127,26 @@ where_levels_auto (const char *levels, const char *new_severity_sql,
levels_sql = NULL;
count = 0;

class = sql_string ("SELECT value FROM settings"
" WHERE name = 'Severity Class'"
" AND ((owner IS NULL)"
" OR (owner = (SELECT id FROM users"
" WHERE users.uuid"
" = (SELECT uuid"
" FROM current_credentials))))"
" ORDER BY coalesce (owner, 0) DESC LIMIT 1;");

/* High. */
if (strchr (levels, 'h'))
{
count = 1;
// FIX handles dynamic "severity" in caller?
levels_sql = g_string_new ("");
g_string_append_printf (levels_sql,
" AND (((%s IS NULL) AND (severity_in_level (%s, 'high')",
" AND (((%s IS NULL) AND (severity_in_level (%s, 'high', '%s')",
auto_type_sql,
new_severity_sql);
new_severity_sql,
class);
}

/* Medium. */
Expand All @@ -23145,14 +23156,16 @@ where_levels_auto (const char *levels, const char *new_severity_sql,
{
levels_sql = g_string_new ("");
g_string_append_printf (levels_sql,
" AND (((%s IS NULL) AND (severity_in_level (%s, 'medium')",
" AND (((%s IS NULL) AND (severity_in_level (%s, 'medium', '%s')",
auto_type_sql,
new_severity_sql);
new_severity_sql,
class);
}
else
g_string_append_printf (levels_sql,
" OR severity_in_level (%s, 'medium')",
new_severity_sql);
" OR severity_in_level (%s, 'medium', '%s')",
new_severity_sql,
class);
count++;
}

Expand All @@ -23163,14 +23176,16 @@ where_levels_auto (const char *levels, const char *new_severity_sql,
{
levels_sql = g_string_new ("");
g_string_append_printf (levels_sql,
" AND (((%s IS NULL) AND (severity_in_level (%s, 'low')",
" AND (((%s IS NULL) AND (severity_in_level (%s, 'low', '%s')",
auto_type_sql,
new_severity_sql);
new_severity_sql,
class);
}
else
g_string_append_printf (levels_sql,
" OR severity_in_level (%s, 'low')",
new_severity_sql);
" OR severity_in_level (%s, 'low', '%s')",
new_severity_sql,
class);
count++;
}

Expand All @@ -23181,14 +23196,16 @@ where_levels_auto (const char *levels, const char *new_severity_sql,
{
levels_sql = g_string_new ("");
g_string_append_printf (levels_sql,
" AND (((%s IS NULL) AND (severity_in_level (%s, 'log')",
" AND (((%s IS NULL) AND (severity_in_level (%s, 'log', '%s')",
auto_type_sql,
new_severity_sql);
new_severity_sql,
class);
}
else
g_string_append_printf (levels_sql,
" OR severity_in_level (%s, 'log')",
new_severity_sql);
" OR severity_in_level (%s, 'log', '%s')",
new_severity_sql,
class);
count++;
}

Expand Down Expand Up @@ -23257,6 +23274,8 @@ where_levels_auto (const char *levels, const char *new_severity_sql,
" AND severity != " G_STRINGIFY (SEVERITY_ERROR));
}

free (class);

return levels_sql;
}

Expand Down