Skip to content
Snippets Groups Projects
Verified Commit f7aacb8c authored by Rajmund Hruška's avatar Rajmund Hruška
Browse files

Fix: Fetch last_seen beforehand and use it as the first fallback (#7812)

parent 2ab94d84
No related tags found
1 merge request!106Fix slow mentat-precache
Pipeline #15533 passed
......@@ -1569,6 +1569,12 @@ class EventStorageService:
:rtype: list
"""
enum_table = "enum_{}".format(column)
# Get last_seen first.
self.cursor.execute(psycopg2.sql.SQL("SELECT max(last_seen) FROM {}").format(psycopg2.sql.Identifier(enum_table)))
last_seen = self.cursor.fetchone()[0]
self.commit()
# Build and execute query for updating enumeration table.
enum_query = psycopg2.sql.SQL("INSERT INTO {} (SELECT * FROM (").format(psycopg2.sql.Identifier(enum_table))
if column not in ('eventclass', 'targetclass', 'eventseverity', 'targetseverity'):
......@@ -1577,10 +1583,13 @@ class EventStorageService:
enum_query += psycopg2.sql.SQL("SELECT {}").format(psycopg2.sql.Identifier(column))
enum_query += psycopg2.sql.SQL(
" AS data, max(storagetime) AS last_seen FROM events "
"WHERE storagetime >= COALESCE((SELECT max(last_seen) FROM {}), %s, (SELECT min(storagetime) FROM events)) "
"WHERE storagetime >= COALESCE(%s, %s, (SELECT min(storagetime) FROM events)) "
"GROUP BY data) AS enum WHERE data IS NOT NULL) ON CONFLICT (data) DO UPDATE SET last_seen = excluded.last_seen"
).format(psycopg2.sql.Identifier(enum_table))
self.cursor.execute(enum_query, (last_run_ts,))
)
# Use the time of the last run before the value of last_seen from the database.
# Some values may be quite rare and it doesn't make sense to always search all events
# since the time when this rare value appeared the last time.
self.cursor.execute(enum_query, (last_run_ts, last_seen,))
self.commit()
# Return all entries from recently updated enumeration table.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment