add new tag alias correction helper

This commit is contained in:
albert
2013-03-05 22:08:48 -05:00
parent e10f15d2db
commit d9a17d2378
11 changed files with 185 additions and 16 deletions

View File

@@ -40,7 +40,7 @@ class Cache
start_time = Time.now
value = MEMCACHE.get key.slice(0, 200)
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Get (%0.6f) %s' % [elapsed, key])
ActiveRecord::Base.logger.debug('MemCache Get (%0.6f) %s -> %s' % [elapsed, key, value])
if value.nil? and block_given? then
value = yield
MEMCACHE.set key, value, expiry
@@ -64,7 +64,7 @@ class Cache
start_time = Time.now
MEMCACHE.set key, value, expiry
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Set (%0.6f) %s' % [elapsed, key])
ActiveRecord::Base.logger.debug('MemCache Set (%0.6f) %s -> %s' % [elapsed, key, value])
value
rescue MemCache::MemCacheError => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"

View File

@@ -0,0 +1,60 @@
class TagAliasCorrection
attr_reader :tag_alias_id, :tag_alias, :hostname
delegate :antecedent_name, :consequent_name, :to => :tag_alias
def initialize(tag_alias_id, hostname = Socket.gethostname)
@tag_alias_id = tag_alias_id
@tag_alias = TagAlias.find(tag_alias_id)
@hostname = hostname
end
def to_json(options = {})
statistics_hash.to_json
end
def statistics_hash
@statistics_hash ||= {
"antecedent_cache" => Cache.get("ta:" + Cache.sanitize(tag_alias.antecedent_name)),
"consequent_cache" => Cache.get("ta:" + Cache.sanitize(tag_alias.consequent_name)),
"antecedent_count" => Tag.find_by_name(tag_alias.antecedent_name).try(:post_count),
"consequent_count" => Tag.find_by_name(tag_alias.consequent_name).try(:post_count)
}
end
def fill_hash!
Net::HTTP.start(hostname, 80) do |http|
http.request_get("/tag_aliases/#{tag_alias_id}/correction.json") do |res|
if res === Net::HTTPSuccess
json = JSON.parse(res.body)
statistics_hash["antecedent_cache"] = json["antecdent_cache"]
statistics_hash["consequent_cache"] = json["consequent_cache"]
end
end
end
end
def each_server
Danbooru.config.all_server_hosts.each do |host|
other = TagAliasCorrection.new(tag_alias_id, host)
if host != Socket.gethostname
other.fill_hash!
end
yield other
end
end
def clear_cache
tag_alias.clear_cache
end
def fix!
clear_cache
Post.raw_tag_match(tag_alias.antecedent_name).each do |post|
post.save
end
tag_alias.antecedent_tag.fix_post_count if tag_alias.antecedent_tag
tag_alias.consequent_tag.fix_post_count if tag_alias.consequent_tag
end
end