Files
danbooru/app/models/pool_archive.rb
evazion 59b277ead1 users: drop id_to_name, name_to_id caching.
Changes:

* Drop Users.id_to_name.
* Don't cache Users.name_to_id.
* Replace calls to name_to_id with find_by_name when possible.
* Don't autodefine creator_name in belongs_to_creator.
* Don't autodefine updater_name in belongs_to_updater.
* Instead manually define creator_name / updater_name only on models that need
  to return these fields in the api.

id_to_name was cached to reduce the impact of N+1 query patterns in
certain places, especially in api responses that return creator_name /
updater_name fields. But it still meant we were doing N calls to
memcache. Using `includes` to prefetch users avoids this N+1 pattern.

name_to_id had no need be cached, it was never used in any performance-
sensitive contexts.

Avoiding caching also avoids the need to keep these caches consistent.
2019-08-18 11:24:42 -05:00

102 lines
2.4 KiB
Ruby

class PoolArchive < ApplicationRecord
belongs_to :updater, :class_name => "User"
def self.enabled?
Danbooru.config.aws_sqs_archives_url.present?
end
establish_connection (ENV["ARCHIVE_DATABASE_URL"] || "archive_#{Rails.env}".to_sym) if enabled?
self.table_name = "pool_versions"
module SearchMethods
def default_order
order(updated_at: :desc)
end
def for_user(user_id)
where("updater_id = ?", user_id)
end
def search(params)
q = super
if params[:updater_id].present?
q = q.where(updater_id: params[:updater_id].split(",").map(&:to_i))
end
if params[:updater_name].present?
q = q.where("updater_id = ?", User.name_to_id(params[:updater_name]))
end
if params[:pool_id].present?
q = q.where(pool_id: params[:pool_id].split(",").map(&:to_i))
end
q.apply_default_order(params)
end
end
extend SearchMethods
def self.sqs_service
SqsService.new(Danbooru.config.aws_sqs_archives_url)
end
def self.queue(pool, updater, updater_ip_addr)
# queue updates to sqs so that if archives goes down for whatever reason it won't
# block pool updates
raise NotImplementedError.new("Archive service is not configured.") if !enabled?
json = {
pool_id: pool.id,
post_ids: pool.post_ids,
updater_id: updater.id,
updater_ip_addr: updater_ip_addr.to_s,
created_at: pool.created_at.try(:iso8601),
updated_at: pool.updated_at.try(:iso8601),
description: pool.description,
name: pool.name,
is_active: pool.is_active?,
is_deleted: pool.is_deleted?,
category: pool.category
}
msg = "add pool version\n#{json.to_json}"
sqs_service.send_message(msg, message_group_id: "pool:#{pool.id}")
end
def build_diff(other = nil)
diff = {}
prev = previous
if prev.nil?
diff[:added_post_ids] = added_post_ids
diff[:removed_post_ids] = removed_post_ids
diff[:added_desc] = description
else
diff[:added_post_ids] = post_ids - prev.post_ids
diff[:removed_post_ids] = prev.post_ids - post_ids
diff[:added_desc] = description
diff[:removed_desc] = prev.description
end
diff
end
def previous
PoolArchive.where("pool_id = ? and version < ?", pool_id, version).order("version desc").first
end
def pool
Pool.find(pool_id)
end
def updater
User.find(updater_id)
end
def pretty_name
name.tr("_", " ")
end
end