Skip to content

Commit

Permalink
Clean up job for search logs
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltrout committed Jul 14, 2017
1 parent d7f783f commit 6b6ad93
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/jobs/scheduled/clean_up_search_logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Jobs
class CleanUpSearchLogs < Jobs::Scheduled
every 1.week

def execute(args)
SearchLog.clean_up
end
end
end
7 changes: 7 additions & 0 deletions app/models/search_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ def self.log(term:, search_type:, ip_address:, user_id:nil)
[: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
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions config/site_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions spec/models/search_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@
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

Expand Down

1 comment on commit 6b6ad93

@discoursebot
Copy link

Choose a reason for hiding this comment

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

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/effectively-logging-search-queries/63838/4

Please sign in to comment.