implement post change report
This commit is contained in:
36
app/logical/google_big_query/base.rb
Normal file
36
app/logical/google_big_query/base.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
require "big_query"
|
||||
|
||||
module GoogleBigQuery
|
||||
class Base
|
||||
def query(q)
|
||||
client.query(q)
|
||||
end
|
||||
|
||||
def escape(s)
|
||||
Regexp.escape(s).gsub(/\\/, '\0\0').gsub(/['"]/, '\\\\\0')
|
||||
end
|
||||
|
||||
def client
|
||||
@_client ||= BigQuery::Client.new(
|
||||
"json_key" => client_options[:google_key_path],
|
||||
"project_id" => google_config["project_id"],
|
||||
"dataset" => client_options[:google_data_set]
|
||||
)
|
||||
end
|
||||
|
||||
def client_options
|
||||
@_client_options ||= {
|
||||
google_key_path: Danbooru.config.google_api_json_key_path,
|
||||
google_data_set: "danbooru_#{Rails.env}"
|
||||
}
|
||||
end
|
||||
|
||||
def google_config
|
||||
@_google_config ||= JSON.parse(File.read(client_options[:google_key_path]))
|
||||
end
|
||||
|
||||
def data_set
|
||||
client_options[:google_data_set]
|
||||
end
|
||||
end
|
||||
end
|
||||
15
app/logical/google_big_query/post_version.rb
Normal file
15
app/logical/google_big_query/post_version.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module GoogleBigQuery
|
||||
class PostVersion < Base
|
||||
def find_removed(tag, limit = 1_000)
|
||||
tag = escape(tag)
|
||||
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(removed_tags, \"(?:^| )#{tag}(?:$| )\") order by updated_at desc limit #{limit}")
|
||||
end
|
||||
|
||||
def find_added(tag, limit = 1_000)
|
||||
tag = escape(tag)
|
||||
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
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user