stub in preview for bulk revert
This commit is contained in:
41
app/logical/bulk_revert.rb
Normal file
41
app/logical/bulk_revert.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
class BulkRevert
|
||||
attr_reader :constraints
|
||||
|
||||
def initialize(constraints)
|
||||
@constraints = constraints
|
||||
end
|
||||
|
||||
def preview
|
||||
@_preview ||= find_post_versions
|
||||
end
|
||||
|
||||
def find_post_versions
|
||||
q = PostVersion.where("true")
|
||||
|
||||
if constraints[:user_name]
|
||||
constraints[:user_id] = User.find_by_name(constraints[:user_name]).try(:id)
|
||||
end
|
||||
|
||||
if constraints[:user_id]
|
||||
q = q.where("post_versions.updater_id = ?", constraints[:user_id])
|
||||
|
||||
if constraints[:added_tags] || constraints[:removed_tags]
|
||||
hash = CityHash.hash64("#{constraints[:added_tags]} #{constraints{removed_tags}}").to_s(36)
|
||||
sub_ids = Cache.get("br/fpv/#{hash}", 300) do
|
||||
sub_ids = GoogleBigQuery::PostVersion.new.find(constraints[:user_id], constraints[:added_tags], constraints[:removed_tags])
|
||||
end
|
||||
q = q.where("post_versions.id in (?)", sub_ids)
|
||||
end
|
||||
end
|
||||
|
||||
if constraints[:min_version_id].present?
|
||||
q = q.where("post_versions.id >= ?", constraints[:min_version_id])
|
||||
end
|
||||
|
||||
if constraints[:max_version_id].present?
|
||||
q = q.where("post_versions.id <= ?", constraints[:max_version_id])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
@@ -11,5 +11,35 @@ module GoogleBigQuery
|
||||
limit = limit.to_i
|
||||
query("select id, post_id, updated_at, updater_id, updater_ip_addr, tags, added_tags, removed_tags, parent_id, rating, source from [#{data_set}.post_versions] where regexp_match(added_tags, \"(?:^| )#{tag}(?:$| )\") order by updated_at desc limit #{limit}")
|
||||
end
|
||||
|
||||
def find(user_id, added_tags, removed_tags, limit = 1_000)
|
||||
constraints = []
|
||||
|
||||
constraints << "updater_id = #{user_id.to_i}"
|
||||
|
||||
if added_tags
|
||||
added_tags.scan(/\S+/).each do |tag|
|
||||
escaped = escape(tag)
|
||||
constraints << "regexp_match(added_tags, \"(?:^| )#{escaped}(?:$| )\")"
|
||||
end
|
||||
end
|
||||
|
||||
if removed_tags
|
||||
removed_tags.scan(/\S+/).each do |tag|
|
||||
escaped = escape(tag)
|
||||
constraints << "not regexp_match(added_tags, \"(?:^| )#{escaped}(?:$| )\")"
|
||||
end
|
||||
end
|
||||
|
||||
limit = limit.to_i
|
||||
sql = "select id from [#{data_set}.post_versions] where " + constraints.join(" and ") + " order by updated_at desc limit #{limit}"
|
||||
result = query(sql)
|
||||
|
||||
if result["rows"]
|
||||
result["rows"].map {|x| x["f"][0]["v"].to_i}
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user