Clean up job for search logs

This commit is contained in:
Robin Ward 2017-07-14 14:29:31 -04:00
parent d7f783ffed
commit 6b6ad9391b
5 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,9 @@
module Jobs
class CleanUpSearchLogs < Jobs::Scheduled
every 1.week
def execute(args)
SearchLog.clean_up
end
end
end

View File

@ -44,4 +44,11 @@ class SearchLog < ActiveRecord::Base
[:updated, rows[0]['id'].to_i]
end
end
def self.clean_up
search_id = SearchLog.order(:id).offset(SiteSetting.search_query_log_max_size).limit(1).pluck(:id)
if search_id.present?
SearchLog.where('id < ?', search_id[0]).delete_all
end
end
end

View File

@ -948,6 +948,7 @@ en:
search_prefer_recent_posts: "If searching your large forum is slow, this option tries an index of more recent posts first"
search_recent_posts_size: "How many recent posts to keep in the index"
log_search_queries: "Log search queries performed by users"
search_query_log_max_size: "Maximum amount of search queries to keep"
allow_uncategorized_topics: "Allow topics to be created without a category. WARNING: If there are any uncategorized topics, you must recategorize them before turning this off."
allow_duplicate_topic_titles: "Allow topics with identical, duplicate titles."
unique_posts_mins: "How many minutes before a user can make a post with the same content again"

View File

@ -1155,6 +1155,7 @@ search:
search_prefer_recent_posts: false
search_recent_posts_size: 100000
log_search_queries: true
search_query_log_max_size: 1000000
uncategorized:
version_checks:

View File

@ -137,6 +137,26 @@ RSpec.describe SearchLog, type: :model do
end
end
end
context "clean_up" do
it "will remove old logs" do
SearchLog.log(term: 'jawa', search_type: :header, ip_address: '127.0.0.1')
SearchLog.log(term: 'jedi', search_type: :header, ip_address: '127.0.0.1')
SearchLog.log(term: 'rey', search_type: :header, ip_address: '127.0.0.1')
SearchLog.log(term: 'finn', search_type: :header, ip_address: '127.0.0.1')
SiteSetting.search_query_log_max_size = 5
SearchLog.clean_up
expect(SearchLog.count).to eq(4)
SiteSetting.search_query_log_max_size = 2
SearchLog.clean_up
expect(SearchLog.count).to eq(2)
expect(SearchLog.where(term: 'rey').first).to be_present
expect(SearchLog.where(term: 'finn').first).to be_present
end
end