2019-05-12 22:42:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-06-25 12:25:15 -04:00
|
|
|
# name: discourse-data-explorer
|
|
|
|
# about: Interface for running analysis SQL queries on the live database
|
2015-06-30 18:14:24 -04:00
|
|
|
# version: 0.2
|
2015-06-25 12:25:15 -04:00
|
|
|
# authors: Riking
|
|
|
|
# url: https://github.com/discourse/discourse-data-explorer
|
|
|
|
|
|
|
|
enabled_site_setting :data_explorer_enabled
|
2015-06-30 13:37:48 -04:00
|
|
|
register_asset 'stylesheets/explorer.scss'
|
2018-11-08 12:01:50 -05:00
|
|
|
|
2018-11-15 11:12:32 -05:00
|
|
|
if respond_to?(:register_svg_icon)
|
|
|
|
register_svg_icon "caret-down"
|
|
|
|
register_svg_icon "caret-right"
|
|
|
|
register_svg_icon "chevron-left"
|
|
|
|
register_svg_icon "exclamation-circle"
|
|
|
|
register_svg_icon "info"
|
|
|
|
register_svg_icon "pencil-alt"
|
|
|
|
register_svg_icon "upload"
|
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-25 13:43:05 -04:00
|
|
|
# route: /admin/plugins/explorer
|
|
|
|
add_admin_route 'explorer.title', 'explorer'
|
|
|
|
|
|
|
|
module ::DataExplorer
|
2020-02-20 13:04:15 -05:00
|
|
|
QUERY_RESULT_DEFAULT_LIMIT = 1000
|
|
|
|
QUERY_RESULT_MAX_LIMIT = 10000
|
2019-01-21 03:51:53 -05:00
|
|
|
|
2015-06-25 13:43:05 -04:00
|
|
|
def self.plugin_name
|
2015-06-25 14:58:14 -04:00
|
|
|
'discourse-data-explorer'.freeze
|
2015-06-25 13:43:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.pstore_get(key)
|
|
|
|
PluginStore.get(DataExplorer.plugin_name, key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pstore_set(key, value)
|
|
|
|
PluginStore.set(DataExplorer.plugin_name, key, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pstore_delete(key)
|
|
|
|
PluginStore.remove(DataExplorer.plugin_name, key)
|
|
|
|
end
|
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-25 13:43:05 -04:00
|
|
|
after_initialize do
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2019-09-11 10:09:41 -04:00
|
|
|
add_to_class(:guardian, :user_is_a_member_of_group?) do |group|
|
|
|
|
return false if !current_user
|
|
|
|
return true if current_user.admin?
|
|
|
|
return current_user.group_ids.include?(group.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_class(:guardian, :user_can_access_query?) do |group, query|
|
|
|
|
return false if !current_user
|
|
|
|
return true if current_user.admin?
|
|
|
|
return user_is_a_member_of_group?(group) &&
|
|
|
|
query.group_ids.include?(group.id.to_s)
|
|
|
|
end
|
|
|
|
|
2015-06-25 12:25:15 -04:00
|
|
|
module ::DataExplorer
|
|
|
|
class Engine < ::Rails::Engine
|
|
|
|
engine_name "data_explorer"
|
|
|
|
isolate_namespace DataExplorer
|
|
|
|
end
|
|
|
|
|
2019-01-21 05:24:21 -05:00
|
|
|
class ValidationError < StandardError
|
2015-07-08 16:45:13 -04:00
|
|
|
end
|
2015-06-25 14:58:14 -04:00
|
|
|
|
2015-08-05 19:40:00 -04:00
|
|
|
class SmallBadgeSerializer < ApplicationSerializer
|
|
|
|
attributes :id, :name, :badge_type, :description, :icon
|
|
|
|
end
|
|
|
|
|
|
|
|
class SmallPostWithExcerptSerializer < ApplicationSerializer
|
|
|
|
attributes :id, :topic_id, :post_number, :excerpt
|
2015-09-14 18:34:57 -04:00
|
|
|
attributes :username, :avatar_template
|
2015-08-05 19:40:00 -04:00
|
|
|
def excerpt
|
|
|
|
Post.excerpt(object.cooked, 70)
|
|
|
|
end
|
2019-01-21 05:24:21 -05:00
|
|
|
def username
|
|
|
|
object.user && object.user.username
|
|
|
|
end
|
|
|
|
def avatar_template
|
|
|
|
object.user && object.user.avatar_template
|
|
|
|
end
|
2015-08-05 19:40:00 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
# Run a data explorer query on the currently connected database.
|
|
|
|
#
|
|
|
|
# @param [DataExplorer::Query] query the Query object to run
|
|
|
|
# @param [Hash] params the colon-style query parameters to pass to AR
|
|
|
|
# @param [Hash] opts hash of options
|
|
|
|
# explain - include a query plan in the result
|
|
|
|
# @return [Hash]
|
|
|
|
# error - any exception that was raised in the execution. Check this
|
|
|
|
# first before looking at any other fields.
|
|
|
|
# pg_result - the PG::Result object
|
|
|
|
# duration_nanos - the query duration, in nanoseconds
|
|
|
|
# explain - the query
|
2017-08-02 01:42:49 -04:00
|
|
|
def self.run_query(query, req_params = {}, opts = {})
|
2015-06-25 14:58:14 -04:00
|
|
|
# Safety checks
|
2015-07-15 16:20:42 -04:00
|
|
|
# see test 'doesn't allow you to modify the database #2'
|
2015-06-25 14:58:14 -04:00
|
|
|
if query.sql =~ /;/
|
|
|
|
err = DataExplorer::ValidationError.new(I18n.t('js.errors.explorer.no_semicolons'))
|
2017-08-02 01:42:49 -04:00
|
|
|
return { error: err, duration_nanos: 0 }
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-07-14 16:22:53 -04:00
|
|
|
query_args = {}
|
|
|
|
begin
|
|
|
|
query_args = query.cast_params req_params
|
|
|
|
rescue DataExplorer::ValidationError => e
|
2017-08-02 01:42:49 -04:00
|
|
|
return { error: e, duration_nanos: 0 }
|
2015-06-30 22:51:38 -04:00
|
|
|
end
|
2015-07-14 16:22:53 -04:00
|
|
|
|
2015-06-25 17:53:03 -04:00
|
|
|
time_start, time_end, explain, err, result = nil
|
2015-06-25 14:58:14 -04:00
|
|
|
begin
|
|
|
|
ActiveRecord::Base.connection.transaction do
|
|
|
|
# Setting transaction to read only prevents shoot-in-foot actions like SELECT FOR UPDATE
|
2015-07-15 16:20:42 -04:00
|
|
|
# see test 'doesn't allow you to modify the database #1'
|
2018-07-12 22:42:11 -04:00
|
|
|
DB.exec "SET TRANSACTION READ ONLY"
|
2015-09-14 19:07:41 -04:00
|
|
|
# Set a statement timeout so we can't tie up the server
|
2018-07-12 22:42:11 -04:00
|
|
|
DB.exec "SET LOCAL statement_timeout = 10000"
|
2015-06-25 17:55:06 -04:00
|
|
|
|
2015-07-15 16:20:42 -04:00
|
|
|
# SQL comments are for the benefits of the slow queries log
|
|
|
|
sql = <<-SQL
|
2015-06-25 14:58:14 -04:00
|
|
|
/*
|
2015-06-25 17:55:06 -04:00
|
|
|
* DataExplorer Query
|
2015-06-30 22:51:38 -04:00
|
|
|
* Query: /admin/plugins/explorer?id=#{query.id}
|
2015-06-25 17:55:06 -04:00
|
|
|
* Started by: #{opts[:current_user]}
|
|
|
|
*/
|
2015-06-25 14:58:14 -04:00
|
|
|
WITH query AS (
|
|
|
|
#{query.sql}
|
|
|
|
) SELECT * FROM query
|
2020-02-20 13:04:15 -05:00
|
|
|
LIMIT #{opts[:limit] || DataExplorer::QUERY_RESULT_DEFAULT_LIMIT}
|
2015-06-25 14:58:14 -04:00
|
|
|
SQL
|
|
|
|
|
|
|
|
time_start = Time.now
|
2018-07-12 22:42:11 -04:00
|
|
|
|
|
|
|
# we probably want to rewrite this ... but for now reuse the working
|
|
|
|
# code we have
|
|
|
|
sql = DB.param_encoder.encode(sql, query_args)
|
|
|
|
|
|
|
|
result = ActiveRecord::Base.connection.raw_connection.async_exec(sql)
|
2015-07-09 15:02:47 -04:00
|
|
|
result.check # make sure it's done
|
2015-06-25 14:58:14 -04:00
|
|
|
time_end = Time.now
|
|
|
|
|
|
|
|
if opts[:explain]
|
2018-07-12 22:42:11 -04:00
|
|
|
explain = DB.query_hash("EXPLAIN #{query.sql}", query_args)
|
2017-08-02 01:42:49 -04:00
|
|
|
.map { |row| row["QUERY PLAN"] }.join "\n"
|
2015-06-25 14:58:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# All done. Issue a rollback anyways, just in case
|
2015-07-15 16:20:42 -04:00
|
|
|
# see test 'doesn't allow you to modify the database #1'
|
2015-06-25 14:58:14 -04:00
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
rescue Exception => ex
|
|
|
|
err = ex
|
|
|
|
time_end = Time.now
|
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
{
|
|
|
|
error: err,
|
|
|
|
pg_result: result,
|
2015-07-09 15:02:47 -04:00
|
|
|
duration_secs: time_end - time_start,
|
2015-06-25 14:58:14 -04:00
|
|
|
explain: explain,
|
2018-07-12 22:42:11 -04:00
|
|
|
params_full: query_args
|
2015-06-25 14:58:14 -04:00
|
|
|
}
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-08-05 19:40:00 -04:00
|
|
|
def self.extra_data_pluck_fields
|
|
|
|
@extra_data_pluck_fields ||= {
|
2017-08-02 01:42:49 -04:00
|
|
|
user: { class: User, fields: [:id, :username, :uploaded_avatar_id], serializer: BasicUserSerializer },
|
|
|
|
badge: { class: Badge, fields: [:id, :name, :badge_type_id, :description, :icon], include: [:badge_type], serializer: SmallBadgeSerializer },
|
|
|
|
post: { class: Post, fields: [:id, :topic_id, :post_number, :cooked, :user_id], include: [:user], serializer: SmallPostWithExcerptSerializer },
|
|
|
|
topic: { class: Topic, fields: [:id, :title, :slug, :posts_count], serializer: BasicTopicSerializer },
|
|
|
|
group: { class: Group, ignore: true },
|
|
|
|
category: { class: Category, ignore: true },
|
|
|
|
reltime: { ignore: true },
|
|
|
|
html: { ignore: true },
|
2015-08-05 19:40:00 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-08-25 23:48:19 -04:00
|
|
|
def self.column_regexes
|
|
|
|
@column_regexes ||=
|
|
|
|
extra_data_pluck_fields.map do |key, val|
|
|
|
|
if val[:class]
|
|
|
|
/(#{val[:class].to_s.downcase})_id$/
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2015-08-05 19:40:00 -04:00
|
|
|
def self.add_extra_data(pg_result)
|
|
|
|
needed_classes = {}
|
2019-05-13 00:41:37 -04:00
|
|
|
ret = {}
|
|
|
|
col_map = {}
|
2015-08-05 19:40:00 -04:00
|
|
|
|
|
|
|
pg_result.fields.each_with_index do |col, idx|
|
2018-10-22 16:24:55 -04:00
|
|
|
rgx = column_regexes.find { |r| r.match col }
|
2015-08-25 23:48:19 -04:00
|
|
|
if rgx
|
|
|
|
cls = (rgx.match col)[1].to_sym
|
|
|
|
needed_classes[cls] ||= []
|
|
|
|
needed_classes[cls] << idx
|
|
|
|
elsif col =~ /^(\w+)\$/
|
|
|
|
cls = $1.to_sym
|
|
|
|
needed_classes[cls] ||= []
|
|
|
|
needed_classes[cls] << idx
|
2019-05-13 00:41:37 -04:00
|
|
|
elsif col =~ /^\w+_url$/
|
|
|
|
col_map[idx] = "url"
|
2015-08-05 19:40:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
needed_classes.each do |cls, column_nums|
|
|
|
|
next unless column_nums.present?
|
2015-08-25 23:48:19 -04:00
|
|
|
support_info = extra_data_pluck_fields[cls]
|
|
|
|
next unless support_info
|
|
|
|
|
|
|
|
column_nums.each do |col_n|
|
|
|
|
col_map[col_n] = cls
|
|
|
|
end
|
|
|
|
|
|
|
|
if support_info[:ignore]
|
|
|
|
ret[cls] = []
|
|
|
|
next
|
|
|
|
end
|
2015-08-05 19:40:00 -04:00
|
|
|
|
|
|
|
ids = Set.new
|
|
|
|
column_nums.each do |col_n|
|
|
|
|
ids.merge(pg_result.column_values(col_n))
|
|
|
|
end
|
|
|
|
ids.delete nil
|
|
|
|
ids.map! &:to_i
|
|
|
|
|
|
|
|
object_class = support_info[:class]
|
2015-09-21 17:43:23 -04:00
|
|
|
all_objs = object_class
|
|
|
|
all_objs = all_objs.with_deleted if all_objs.respond_to? :with_deleted
|
|
|
|
all_objs = all_objs
|
2017-08-02 01:42:49 -04:00
|
|
|
.select(support_info[:fields])
|
|
|
|
.where(id: ids.to_a.sort)
|
|
|
|
.includes(support_info[:include])
|
|
|
|
.order(:id)
|
2015-08-05 19:40:00 -04:00
|
|
|
|
|
|
|
ret[cls] = ActiveModel::ArraySerializer.new(all_objs, each_serializer: support_info[:serializer])
|
|
|
|
end
|
2015-08-25 23:48:19 -04:00
|
|
|
[ret, col_map]
|
2015-08-05 19:40:00 -04:00
|
|
|
end
|
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
def self.sensitive_column_names
|
|
|
|
%w(
|
2018-07-12 22:42:11 -04:00
|
|
|
#_IP_Addresses
|
|
|
|
topic_views.ip_address
|
|
|
|
users.ip_address
|
|
|
|
users.registration_ip_address
|
|
|
|
incoming_links.ip_address
|
|
|
|
topic_link_clicks.ip_address
|
|
|
|
user_histories.ip_address
|
|
|
|
|
|
|
|
#_Emails
|
|
|
|
email_tokens.email
|
|
|
|
users.email
|
|
|
|
invites.email
|
|
|
|
user_histories.email
|
|
|
|
email_logs.to_address
|
|
|
|
posts.raw_email
|
|
|
|
badge_posts.raw_email
|
|
|
|
|
|
|
|
#_Secret_Tokens
|
|
|
|
email_tokens.token
|
|
|
|
email_logs.reply_key
|
|
|
|
api_keys.key
|
|
|
|
site_settings.value
|
|
|
|
|
|
|
|
users.auth_token
|
|
|
|
users.password_hash
|
|
|
|
users.salt
|
|
|
|
|
|
|
|
#_Authentication_Info
|
|
|
|
user_open_ids.email
|
|
|
|
oauth2_user_infos.uid
|
|
|
|
oauth2_user_infos.email
|
|
|
|
facebook_user_infos.facebook_user_id
|
|
|
|
facebook_user_infos.email
|
|
|
|
twitter_user_infos.twitter_user_id
|
|
|
|
github_user_infos.github_user_id
|
|
|
|
single_sign_on_records.external_email
|
|
|
|
single_sign_on_records.external_id
|
|
|
|
google_user_infos.google_user_id
|
|
|
|
google_user_infos.email
|
2015-07-08 16:45:13 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.schema
|
2015-07-28 14:18:22 -04:00
|
|
|
# No need to expire this, because the server processes get restarted on upgrade
|
2015-07-08 16:45:13 -04:00
|
|
|
# refer user to http://www.postgresql.org/docs/9.3/static/datatype.html
|
|
|
|
@schema ||= begin
|
2018-07-12 22:42:11 -04:00
|
|
|
results = DB.query_hash <<~SQL
|
|
|
|
select
|
|
|
|
c.column_name column_name,
|
|
|
|
c.data_type data_type,
|
|
|
|
c.character_maximum_length character_maximum_length,
|
|
|
|
c.is_nullable is_nullable,
|
|
|
|
c.column_default column_default,
|
|
|
|
c.table_name table_name,
|
|
|
|
pgd.description column_desc
|
|
|
|
from INFORMATION_SCHEMA.COLUMNS c
|
|
|
|
inner join pg_catalog.pg_statio_all_tables st on (c.table_schema = st.schemaname and c.table_name = st.relname)
|
|
|
|
left outer join pg_catalog.pg_description pgd on (pgd.objoid = st.relid and pgd.objsubid = c.ordinal_position)
|
|
|
|
where c.table_schema = 'public'
|
|
|
|
ORDER BY c.table_name, c.ordinal_position
|
|
|
|
SQL
|
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
by_table = {}
|
|
|
|
# Massage the results into a nicer form
|
|
|
|
results.each do |hash|
|
2015-07-09 18:46:19 -04:00
|
|
|
full_col_name = "#{hash['table_name']}.#{hash['column_name']}"
|
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
if hash['is_nullable'] == "YES"
|
|
|
|
hash['is_nullable'] = true
|
|
|
|
else
|
|
|
|
hash.delete('is_nullable')
|
|
|
|
end
|
|
|
|
clen = hash.delete 'character_maximum_length'
|
|
|
|
dt = hash['data_type']
|
2015-07-28 14:18:22 -04:00
|
|
|
if hash['column_name'] == 'id'
|
|
|
|
hash['data_type'] = 'serial'
|
|
|
|
hash['primary'] = true
|
|
|
|
elsif dt == 'character varying'
|
2015-07-08 16:45:13 -04:00
|
|
|
hash['data_type'] = "varchar(#{clen.to_i})"
|
|
|
|
elsif dt == 'timestamp without time zone'
|
|
|
|
hash['data_type'] = 'timestamp'
|
|
|
|
elsif dt == 'double precision'
|
|
|
|
hash['data_type'] = 'double'
|
|
|
|
end
|
|
|
|
default = hash['column_default']
|
|
|
|
if default.nil? || default =~ /^nextval\(/
|
|
|
|
hash.delete 'column_default'
|
|
|
|
elsif default =~ /^'(.*)'::(character varying|text)/
|
|
|
|
hash['column_default'] = $1
|
|
|
|
end
|
2015-07-27 19:05:10 -04:00
|
|
|
hash.delete('column_desc') unless hash['column_desc']
|
2015-07-08 16:45:13 -04:00
|
|
|
|
2015-07-09 18:46:19 -04:00
|
|
|
if sensitive_column_names.include? full_col_name
|
2015-07-08 16:45:13 -04:00
|
|
|
hash['sensitive'] = true
|
|
|
|
end
|
2015-07-09 18:46:19 -04:00
|
|
|
if enum_info.include? full_col_name
|
|
|
|
hash['enum'] = enum_info[full_col_name]
|
|
|
|
end
|
2015-07-28 14:18:22 -04:00
|
|
|
if denormalized_columns.include? full_col_name
|
|
|
|
hash['denormal'] = denormalized_columns[full_col_name]
|
|
|
|
end
|
2015-07-28 12:59:26 -04:00
|
|
|
fkey = fkey_info(hash['table_name'], hash['column_name'])
|
|
|
|
if fkey
|
|
|
|
hash['fkey_info'] = fkey
|
|
|
|
end
|
2015-07-08 16:45:13 -04:00
|
|
|
|
2015-07-28 12:59:26 -04:00
|
|
|
table_name = hash.delete('table_name')
|
|
|
|
by_table[table_name] ||= []
|
|
|
|
by_table[table_name] << hash
|
2015-07-08 16:45:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# this works for now, but no big loss if the tables aren't quite sorted
|
2015-07-09 17:22:54 -04:00
|
|
|
favored_order = %w(posts topics users categories badges groups notifications post_actions site_settings)
|
2015-07-08 16:45:13 -04:00
|
|
|
sorted_by_table = {}
|
2015-07-09 17:22:54 -04:00
|
|
|
favored_order.each do |tbl|
|
|
|
|
sorted_by_table[tbl] = by_table[tbl]
|
|
|
|
end
|
2015-07-08 16:45:13 -04:00
|
|
|
by_table.keys.sort.each do |tbl|
|
2015-07-09 17:22:54 -04:00
|
|
|
next if favored_order.include? tbl
|
2015-07-08 16:45:13 -04:00
|
|
|
sorted_by_table[tbl] = by_table[tbl]
|
|
|
|
end
|
|
|
|
sorted_by_table
|
|
|
|
end
|
|
|
|
end
|
2015-07-09 18:46:19 -04:00
|
|
|
|
|
|
|
def self.enums
|
2019-03-13 11:14:01 -04:00
|
|
|
return @enums if @enums
|
|
|
|
|
|
|
|
@enums = {
|
2019-12-08 22:20:34 -05:00
|
|
|
'application_requests.req_type': ApplicationRequest.req_types,
|
2017-08-02 01:42:49 -04:00
|
|
|
'badges.badge_type_id': Enum.new(:gold, :silver, :bronze, start: 1),
|
|
|
|
'category_groups.permission_type': CategoryGroup.permission_types,
|
2019-12-08 22:20:34 -05:00
|
|
|
'category_users.notification_level': CategoryUser.notification_levels,
|
2017-08-02 01:42:49 -04:00
|
|
|
'directory_items.period_type': DirectoryItem.period_types,
|
|
|
|
'groups.id': Group::AUTO_GROUPS,
|
2019-12-08 22:20:34 -05:00
|
|
|
'groups.mentionable_level': Group::ALIAS_LEVELS,
|
|
|
|
'groups.messageable_level': Group::ALIAS_LEVELS,
|
|
|
|
'groups.members_visibility_level': Group.visibility_levels,
|
|
|
|
'groups.visibility_level': Group.visibility_levels,
|
|
|
|
'groups.default_notification_level': GroupUser.notification_levels,
|
|
|
|
'group_users.notification_level': GroupUser.notification_levels,
|
2017-08-02 01:42:49 -04:00
|
|
|
'notifications.notification_type': Notification.types,
|
2019-12-08 22:20:34 -05:00
|
|
|
'polls.results': Poll.results,
|
|
|
|
'polls.status': Poll.statuses,
|
|
|
|
'polls.type': Poll.types,
|
|
|
|
'polls.visibility': Poll.visibilities,
|
|
|
|
'post_action_types.id': PostActionType.types,
|
|
|
|
'post_actions.post_action_type_id': PostActionType.types,
|
2017-08-02 01:42:49 -04:00
|
|
|
'posts.cook_method': Post.cook_methods,
|
|
|
|
'posts.hidden_reason_id': Post.hidden_reasons,
|
|
|
|
'posts.post_type': Post.types,
|
2019-12-08 22:20:34 -05:00
|
|
|
'reviewable_histories.reviewable_history_type': ReviewableHistory.types,
|
|
|
|
'reviewable_scores.status': ReviewableScore.statuses,
|
|
|
|
'screened_emails.action_type': ScreenedEmail.actions,
|
|
|
|
'screened_ip_addresses.action_type': ScreenedIpAddress.actions,
|
|
|
|
'screened_urls.action_type': ScreenedUrl.actions,
|
|
|
|
'search_logs.search_result_type': SearchLog.search_result_types,
|
|
|
|
'search_logs.search_type': SearchLog.search_types,
|
2017-08-02 01:42:49 -04:00
|
|
|
'site_settings.data_type': SiteSetting.types,
|
2019-12-08 22:20:34 -05:00
|
|
|
'skipped_email_logs.reason_type': SkippedEmailLog.reason_types,
|
|
|
|
'tag_group_permissions.permission_type': TagGroupPermission.permission_types,
|
|
|
|
'theme_settings.data_type': ThemeSetting.types,
|
|
|
|
'topic_timers.status_type': TopicTimer.types,
|
2017-08-02 01:42:49 -04:00
|
|
|
'topic_users.notification_level': TopicUser.notification_levels,
|
|
|
|
'topic_users.notifications_reason_id': TopicUser.notification_reasons,
|
|
|
|
'user_histories.action': UserHistory.actions,
|
2019-12-08 22:20:34 -05:00
|
|
|
'user_security_keys.factor_type': UserSecurityKey.factor_types,
|
2017-08-02 01:42:49 -04:00
|
|
|
'users.trust_level': TrustLevel.levels,
|
2019-12-08 22:20:34 -05:00
|
|
|
'web_hooks.content_type': WebHook.content_types,
|
|
|
|
'web_hooks.last_delivery_status': WebHook.last_delivery_statuses,
|
2015-07-09 18:46:19 -04:00
|
|
|
}.with_indifferent_access
|
2019-03-13 11:14:01 -04:00
|
|
|
|
|
|
|
# QueuedPost is removed in recent Discourse releases
|
|
|
|
@enums['queued_posts.state'] = QueuedPost.states if defined?(QueuedPost)
|
|
|
|
@enums['reviewables.status'] = Reviewable.statuses if defined?(Reviewable)
|
|
|
|
|
|
|
|
@enums
|
2015-07-09 18:46:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.enum_info
|
|
|
|
@enum_info ||= begin
|
|
|
|
enum_info = {}
|
2017-08-02 01:42:49 -04:00
|
|
|
enums.map do |key, enum|
|
2015-07-09 18:46:19 -04:00
|
|
|
# https://stackoverflow.com/questions/10874356/reverse-a-hash-in-ruby
|
|
|
|
enum_info[key] = Hash[enum.to_a.map(&:reverse)]
|
|
|
|
end
|
|
|
|
enum_info
|
|
|
|
end
|
|
|
|
end
|
2015-07-28 12:59:26 -04:00
|
|
|
|
|
|
|
def self.fkey_info(table, column)
|
|
|
|
full_name = "#{table}.#{column}"
|
|
|
|
|
|
|
|
if fkey_defaults[column]
|
|
|
|
fkey_defaults[column]
|
|
|
|
elsif column =~ /_by_id$/ || column =~ /_user_id$/
|
|
|
|
:users
|
|
|
|
elsif foreign_keys[full_name]
|
|
|
|
foreign_keys[full_name]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.foreign_keys
|
|
|
|
@fkey_columns ||= {
|
2017-08-02 01:42:49 -04:00
|
|
|
'posts.last_editor_id': :users,
|
|
|
|
'posts.version': :'post_revisions.number',
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'topics.featured_user1_id': :users,
|
|
|
|
'topics.featured_user2_id': :users,
|
|
|
|
'topics.featured_user3_id': :users,
|
|
|
|
'topics.featured_user4_id': :users,
|
|
|
|
'topics.featured_user5_id': :users,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'users.seen_notification_id': :notifications,
|
|
|
|
'users.uploaded_avatar_id': :uploads,
|
|
|
|
'users.primary_group_id': :groups,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'categories.latest_post_id': :posts,
|
|
|
|
'categories.latest_topic_id': :topics,
|
|
|
|
'categories.parent_category_id': :categories,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'badges.badge_grouping_id': :badge_groupings,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'post_actions.related_post_id': :posts,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'color_scheme_colors.color_scheme_id': :color_schemes,
|
|
|
|
'color_schemes.versioned_id': :color_schemes,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'incoming_links.incoming_referer_id': :incoming_referers,
|
|
|
|
'incoming_referers.incoming_domain_id': :incoming_domains,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'post_replies.reply_id': :posts,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'quoted_posts.quoted_post_id': :posts,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'topic_link_clicks.topic_link_id': :topic_links,
|
|
|
|
'topic_link_clicks.link_topic_id': :topics,
|
|
|
|
'topic_link_clicks.link_post_id': :posts,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'user_actions.target_topic_id': :topics,
|
|
|
|
'user_actions.target_post_id': :posts,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'user_avatars.custom_upload_id': :uploads,
|
|
|
|
'user_avatars.gravatar_upload_id': :uploads,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'user_badges.notification_id': :notifications,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
'user_profiles.card_image_badge_id': :badges,
|
2015-07-28 12:59:26 -04:00
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fkey_defaults
|
|
|
|
@fkey_defaults ||= {
|
2017-08-02 01:42:49 -04:00
|
|
|
user_id: :users,
|
2015-07-28 12:59:26 -04:00
|
|
|
# :*_by_id => :users,
|
|
|
|
# :*_user_id => :users,
|
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
category_id: :categories,
|
|
|
|
group_id: :groups,
|
|
|
|
post_id: :posts,
|
|
|
|
post_action_id: :post_actions,
|
|
|
|
topic_id: :topics,
|
|
|
|
upload_id: :uploads,
|
2015-07-28 12:59:26 -04:00
|
|
|
|
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
2015-07-28 14:18:22 -04:00
|
|
|
|
|
|
|
def self.denormalized_columns
|
|
|
|
{
|
2017-08-02 01:42:49 -04:00
|
|
|
'posts.reply_count': :post_replies,
|
|
|
|
'posts.quote_count': :quoted_posts,
|
|
|
|
'posts.incoming_link_count': :topic_links,
|
|
|
|
'posts.word_count': :posts,
|
|
|
|
'posts.avg_time': :post_timings,
|
|
|
|
'posts.reads': :post_timings,
|
|
|
|
'posts.like_score': :post_actions,
|
|
|
|
|
|
|
|
'posts.like_count': :post_actions,
|
|
|
|
'posts.bookmark_count': :post_actions,
|
|
|
|
'posts.vote_count': :post_actions,
|
|
|
|
'posts.off_topic_count': :post_actions,
|
|
|
|
'posts.notify_moderators_count': :post_actions,
|
|
|
|
'posts.spam_count': :post_actions,
|
|
|
|
'posts.illegal_count': :post_actions,
|
|
|
|
'posts.inappropriate_count': :post_actions,
|
|
|
|
'posts.notify_user_count': :post_actions,
|
|
|
|
|
|
|
|
'topics.views': :topic_views,
|
|
|
|
'topics.posts_count': :posts,
|
|
|
|
'topics.reply_count': :posts,
|
|
|
|
'topics.incoming_link_count': :topic_links,
|
|
|
|
'topics.moderator_posts_count': :posts,
|
|
|
|
'topics.participant_count': :posts,
|
|
|
|
'topics.word_count': :posts,
|
|
|
|
'topics.last_posted_at': :posts,
|
|
|
|
'topics.last_post_user_idt': :posts,
|
|
|
|
'topics.avg_time': :post_timings,
|
|
|
|
'topics.highest_post_number': :posts,
|
|
|
|
'topics.image_url': :posts,
|
|
|
|
'topics.excerpt': :posts,
|
|
|
|
|
|
|
|
'topics.like_count': :post_actions,
|
|
|
|
'topics.bookmark_count': :post_actions,
|
|
|
|
'topics.vote_count': :post_actions,
|
|
|
|
'topics.off_topic_count': :post_actions,
|
|
|
|
'topics.notify_moderators_count': :post_actions,
|
|
|
|
'topics.spam_count': :post_actions,
|
|
|
|
'topics.illegal_count': :post_actions,
|
|
|
|
'topics.inappropriate_count': :post_actions,
|
|
|
|
'topics.notify_user_count': :post_actions,
|
|
|
|
|
|
|
|
'categories.topic_count': :topics,
|
|
|
|
'categories.post_count': :posts,
|
|
|
|
'categories.latest_post_id': :posts,
|
|
|
|
'categories.latest_topic_id': :topics,
|
|
|
|
'categories.description': :posts,
|
|
|
|
'categories.read_restricted': :category_groups,
|
|
|
|
'categories.topics_year': :topics,
|
|
|
|
'categories.topics_month': :topics,
|
|
|
|
'categories.topics_week': :topics,
|
|
|
|
'categories.topics_day': :topics,
|
|
|
|
'categories.posts_year': :posts,
|
|
|
|
'categories.posts_month': :posts,
|
|
|
|
'categories.posts_week': :posts,
|
|
|
|
'categories.posts_day': :posts,
|
|
|
|
|
|
|
|
'badges.grant_count': :user_badges,
|
|
|
|
'groups.user_count': :group_users,
|
|
|
|
|
|
|
|
'directory_items.likes_received': :post_actions,
|
|
|
|
'directory_items.likes_given': :post_actions,
|
|
|
|
'directory_items.topics_entered': :user_stats,
|
|
|
|
'directory_items.days_visited': :user_stats,
|
|
|
|
'directory_items.posts_read': :user_stats,
|
|
|
|
'directory_items.topic_count': :topics,
|
|
|
|
'directory_items.post_count': :posts,
|
|
|
|
|
|
|
|
'post_search_data.search_data': :posts,
|
|
|
|
|
|
|
|
'top_topics.yearly_posts_count': :posts,
|
|
|
|
'top_topics.monthly_posts_count': :posts,
|
|
|
|
'top_topics.weekly_posts_count': :posts,
|
|
|
|
'top_topics.daily_posts_count': :posts,
|
|
|
|
'top_topics.yearly_views_count': :topic_views,
|
|
|
|
'top_topics.monthly_views_count': :topic_views,
|
|
|
|
'top_topics.weekly_views_count': :topic_views,
|
|
|
|
'top_topics.daily_views_count': :topic_views,
|
|
|
|
'top_topics.yearly_likes_count': :post_actions,
|
|
|
|
'top_topics.monthly_likes_count': :post_actions,
|
|
|
|
'top_topics.weekly_likes_count': :post_actions,
|
|
|
|
'top_topics.daily_likes_count': :post_actions,
|
|
|
|
'top_topics.yearly_op_likes_count': :post_actions,
|
|
|
|
'top_topics.monthly_op_likes_count': :post_actions,
|
|
|
|
'top_topics.weekly_op_likes_count': :post_actions,
|
|
|
|
'top_topics.daily_op_likes_count': :post_actions,
|
|
|
|
'top_topics.all_score': :posts,
|
|
|
|
'top_topics.yearly_score': :posts,
|
|
|
|
'top_topics.monthly_score': :posts,
|
|
|
|
'top_topics.weekly_score': :posts,
|
|
|
|
'top_topics.daily_score': :posts,
|
|
|
|
|
|
|
|
'topic_links.clicks': :topic_link_clicks,
|
|
|
|
'topic_search_data.search_data': :topics,
|
|
|
|
|
|
|
|
'topic_users.liked': :post_actions,
|
|
|
|
'topic_users.bookmarked': :post_actions,
|
|
|
|
|
|
|
|
'user_stats.posts_read_count': :post_timings,
|
|
|
|
'user_stats.topic_reply_count': :posts,
|
|
|
|
'user_stats.first_post_created_at': :posts,
|
|
|
|
'user_stats.post_count': :posts,
|
|
|
|
'user_stats.topic_count': :topics,
|
|
|
|
'user_stats.likes_given': :post_actions,
|
|
|
|
'user_stats.likes_received': :post_actions,
|
|
|
|
|
|
|
|
'user_search_data.search_data': :user_profiles,
|
|
|
|
|
|
|
|
'users.last_posted_at': :posts,
|
|
|
|
'users.previous_visit_at': :user_visits,
|
2015-07-28 14:18:22 -04:00
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 13:43:05 -04:00
|
|
|
# Reimplement a couple ActiveRecord methods, but use PluginStore for storage instead
|
2018-10-10 07:29:13 -04:00
|
|
|
require_dependency File.expand_path('../lib/queries.rb', __FILE__)
|
2015-06-25 13:43:05 -04:00
|
|
|
class DataExplorer::Query
|
2019-09-11 10:09:41 -04:00
|
|
|
attr_accessor :id, :name, :description, :sql, :created_by, :created_at, :group_ids, :last_run_at
|
2015-06-25 14:58:14 -04:00
|
|
|
|
2015-06-30 15:52:17 -04:00
|
|
|
def initialize
|
|
|
|
@name = 'Unnamed Query'
|
2018-08-19 00:54:53 -04:00
|
|
|
@description = ''
|
2015-06-30 15:52:17 -04:00
|
|
|
@sql = 'SELECT 1'
|
2019-09-11 10:09:41 -04:00
|
|
|
@group_ids = []
|
2015-06-25 14:58:14 -04:00
|
|
|
end
|
2015-06-25 13:43:05 -04:00
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
def slug
|
2018-05-09 16:22:39 -04:00
|
|
|
Slug.for(name).presence || "query-#{id}"
|
2015-06-25 14:58:14 -04:00
|
|
|
end
|
|
|
|
|
2015-07-14 16:22:53 -04:00
|
|
|
def params
|
|
|
|
@params ||= DataExplorer::Parameter.create_from_sql(sql)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_params!
|
2015-07-14 19:01:38 -04:00
|
|
|
DataExplorer::Parameter.create_from_sql(sql, strict: true)
|
2015-07-14 16:22:53 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_params(input_params)
|
|
|
|
result = {}.with_indifferent_access
|
|
|
|
self.params.each do |pobj|
|
|
|
|
result[pobj.identifier] = pobj.cast_to_ruby input_params[pobj.identifier]
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2019-09-11 10:09:41 -04:00
|
|
|
def can_be_run_by(group)
|
|
|
|
@group_ids.include?(group.id.to_s)
|
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
# saving/loading functions
|
|
|
|
# May want to extract this into a library or something for plugins to use?
|
2015-06-25 13:43:05 -04:00
|
|
|
def self.alloc_id
|
|
|
|
DistributedMutex.synchronize('data-explorer_query-id') do
|
|
|
|
max_id = DataExplorer.pstore_get("q:_id")
|
2015-06-25 17:53:03 -04:00
|
|
|
max_id = 1 unless max_id
|
2015-06-25 13:43:05 -04:00
|
|
|
DataExplorer.pstore_set("q:_id", max_id + 1)
|
|
|
|
max_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_hash(h)
|
|
|
|
query = DataExplorer::Query.new
|
2018-08-24 07:58:51 -04:00
|
|
|
[:name, :description, :sql, :created_by, :created_at, :last_run_at].each do |sym|
|
2018-05-09 16:22:39 -04:00
|
|
|
query.send("#{sym}=", h[sym].strip) if h[sym]
|
2015-06-25 14:58:14 -04:00
|
|
|
end
|
2019-10-16 06:43:10 -04:00
|
|
|
group_ids = (h[:group_ids] == "" || !h[:group_ids]) ? [] : h[:group_ids]
|
|
|
|
query.group_ids = group_ids
|
2018-05-09 16:22:39 -04:00
|
|
|
query.id = h[:id].to_i if h[:id]
|
2015-06-25 13:43:05 -04:00
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_hash
|
|
|
|
{
|
|
|
|
id: @id,
|
2015-06-30 15:52:17 -04:00
|
|
|
name: @name,
|
|
|
|
description: @description,
|
|
|
|
sql: @sql,
|
2018-08-12 12:08:25 -04:00
|
|
|
created_by: @created_by,
|
2018-08-24 07:58:51 -04:00
|
|
|
created_at: @created_at,
|
2019-09-11 10:09:41 -04:00
|
|
|
group_ids: @group_ids,
|
2018-08-24 07:58:51 -04:00
|
|
|
last_run_at: @last_run_at
|
2015-06-25 13:43:05 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
def self.find(id, opts = {})
|
2018-10-10 07:29:13 -04:00
|
|
|
if DataExplorer.pstore_get("q:#{id}").nil? && id < 0
|
|
|
|
hash = Queries.default[id.to_s]
|
|
|
|
hash[:id] = id
|
|
|
|
from_hash hash
|
|
|
|
else
|
|
|
|
unless hash = DataExplorer.pstore_get("q:#{id}")
|
|
|
|
return DataExplorer::Query.new if opts[:ignore_deleted]
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
from_hash hash
|
2015-06-30 15:52:17 -04:00
|
|
|
end
|
2015-06-25 13:43:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2015-07-14 16:22:53 -04:00
|
|
|
check_params!
|
2019-09-11 10:09:41 -04:00
|
|
|
return save_default_query if @id && @id < 0
|
|
|
|
|
2019-09-11 10:52:06 -04:00
|
|
|
@id = @id || self.class.alloc_id
|
2015-06-25 13:43:05 -04:00
|
|
|
DataExplorer.pstore_set "q:#{id}", to_hash
|
|
|
|
end
|
|
|
|
|
2018-10-10 07:29:13 -04:00
|
|
|
def save_default_query
|
|
|
|
check_params!
|
|
|
|
# Read from queries.rb again to pick up any changes and save them
|
|
|
|
query = Queries.default[id.to_s]
|
|
|
|
@id = query["id"]
|
|
|
|
@sql = query["sql"]
|
2019-09-11 10:09:41 -04:00
|
|
|
@group_ids = @group_ids || []
|
2018-10-10 07:29:13 -04:00
|
|
|
@name = query["name"]
|
|
|
|
@description = query["description"]
|
|
|
|
|
|
|
|
DataExplorer.pstore_set "q:#{id}", to_hash
|
|
|
|
end
|
|
|
|
|
2015-06-25 13:43:05 -04:00
|
|
|
def destroy
|
|
|
|
DataExplorer.pstore_delete "q:#{id}"
|
|
|
|
end
|
|
|
|
|
2015-06-25 17:53:03 -04:00
|
|
|
def read_attribute_for_serialization(attr)
|
|
|
|
self.send(attr)
|
|
|
|
end
|
|
|
|
|
2015-06-25 13:43:05 -04:00
|
|
|
def self.all
|
2015-06-25 17:53:03 -04:00
|
|
|
PluginStoreRow.where(plugin_name: DataExplorer.plugin_name)
|
2015-07-08 16:45:13 -04:00
|
|
|
.where("key LIKE 'q:%'")
|
|
|
|
.where("key != 'q:_id'")
|
|
|
|
.map do |psr|
|
2018-02-23 09:01:50 -05:00
|
|
|
DataExplorer::Query.from_hash PluginStore.cast_value(psr.type_name, psr.value)
|
|
|
|
end.sort_by { |query| query.name }
|
2015-06-25 13:43:05 -04:00
|
|
|
end
|
2015-07-15 15:23:56 -04:00
|
|
|
|
|
|
|
def self.destroy_all
|
|
|
|
PluginStoreRow.where(plugin_name: DataExplorer.plugin_name)
|
|
|
|
.where("key LIKE 'q:%'")
|
|
|
|
.destroy_all
|
|
|
|
end
|
2015-06-25 13:43:05 -04:00
|
|
|
end
|
|
|
|
|
2015-07-14 16:22:53 -04:00
|
|
|
class DataExplorer::Parameter
|
|
|
|
attr_accessor :identifier, :type, :default, :nullable
|
|
|
|
|
|
|
|
def initialize(identifier, type, default, nullable)
|
|
|
|
raise DataExplorer::ValidationError.new('Parameter declaration error - identifier is missing') unless identifier
|
|
|
|
raise DataExplorer::ValidationError.new('Parameter declaration error - type is missing') unless type
|
|
|
|
# process aliases
|
|
|
|
type = type.to_sym
|
|
|
|
if DataExplorer::Parameter.type_aliases[type]
|
|
|
|
type = DataExplorer::Parameter.type_aliases[type]
|
|
|
|
end
|
|
|
|
raise DataExplorer::ValidationError.new("Parameter declaration error - unknown type #{type}") unless DataExplorer::Parameter.types[type]
|
|
|
|
|
|
|
|
@identifier = identifier
|
|
|
|
@type = type
|
|
|
|
@default = default
|
|
|
|
@nullable = nullable
|
|
|
|
begin
|
|
|
|
cast_to_ruby default unless default.blank?
|
|
|
|
rescue DataExplorer::ValidationError
|
|
|
|
raise DataExplorer::ValidationError.new("Parameter declaration error - the default value is not a valid #{type}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_hash
|
|
|
|
{
|
|
|
|
identifier: @identifier,
|
|
|
|
type: @type,
|
|
|
|
default: @default,
|
|
|
|
nullable: @nullable,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(
|
|
|
|
# Normal types
|
2015-07-14 19:49:23 -04:00
|
|
|
:int, :bigint, :boolean, :string, :date, :time, :datetime, :double,
|
2015-07-14 16:22:53 -04:00
|
|
|
# Selection help
|
|
|
|
:user_id, :post_id, :topic_id, :category_id, :group_id, :badge_id,
|
|
|
|
# Arrays
|
2015-07-14 19:49:23 -04:00
|
|
|
:int_list, :string_list, :user_list
|
2015-07-14 16:22:53 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.type_aliases
|
|
|
|
@type_aliases ||= {
|
|
|
|
integer: :int,
|
|
|
|
text: :string,
|
2015-07-14 19:01:38 -04:00
|
|
|
timestamp: :datetime,
|
2015-07-14 16:22:53 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_to_ruby(string)
|
|
|
|
string = @default unless string
|
|
|
|
|
|
|
|
if string.blank?
|
|
|
|
if @nullable
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
raise DataExplorer::ValidationError.new("Missing parameter #{identifier} of type #{type}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if string.downcase == '#null'
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
def invalid_format(string, msg = nil)
|
2015-07-14 16:22:53 -04:00
|
|
|
if msg
|
|
|
|
raise DataExplorer::ValidationError.new("'#{string}' is an invalid #{type} - #{msg}")
|
|
|
|
else
|
|
|
|
raise DataExplorer::ValidationError.new("'#{string}' is an invalid value for #{type}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
value = nil
|
|
|
|
|
|
|
|
case @type
|
2017-08-02 01:42:49 -04:00
|
|
|
when :int
|
|
|
|
invalid_format string, 'Not an integer' unless string =~ /^-?\d+$/
|
|
|
|
value = string.to_i
|
2017-09-04 02:06:19 -04:00
|
|
|
invalid_format string, 'Too large' unless Integer === value
|
2017-08-02 01:42:49 -04:00
|
|
|
when :bigint
|
|
|
|
invalid_format string, 'Not an integer' unless string =~ /^-?\d+$/
|
|
|
|
value = string.to_i
|
|
|
|
when :boolean
|
|
|
|
value = !!(string =~ /t|true|y|yes|1/i)
|
|
|
|
when :string
|
|
|
|
value = string
|
|
|
|
when :time
|
|
|
|
begin
|
|
|
|
value = Time.parse string
|
|
|
|
rescue ArgumentError => e
|
|
|
|
invalid_format string, e.message
|
|
|
|
end
|
|
|
|
when :date
|
|
|
|
begin
|
|
|
|
value = Date.parse string
|
|
|
|
rescue ArgumentError => e
|
|
|
|
invalid_format string, e.message
|
|
|
|
end
|
|
|
|
when :datetime
|
|
|
|
begin
|
|
|
|
value = DateTime.parse string
|
|
|
|
rescue ArgumentError => e
|
|
|
|
invalid_format string, e.message
|
|
|
|
end
|
|
|
|
when :double
|
|
|
|
if string =~ /-?\d*(\.\d+)/
|
|
|
|
value = Float(string)
|
|
|
|
elsif string =~ /^(-?)Inf(inity)?$/i
|
|
|
|
if $1
|
|
|
|
value = -Float::INFINITY
|
|
|
|
else
|
|
|
|
value = Float::INFINITY
|
2015-07-14 16:22:53 -04:00
|
|
|
end
|
2017-08-02 01:42:49 -04:00
|
|
|
elsif string =~ /^(-?)NaN$/i
|
|
|
|
if $1
|
|
|
|
value = -Float::NAN
|
|
|
|
else
|
|
|
|
value = Float::NAN
|
2015-07-14 19:01:38 -04:00
|
|
|
end
|
2017-08-02 01:42:49 -04:00
|
|
|
else
|
|
|
|
invalid_format string
|
|
|
|
end
|
|
|
|
when :category_id
|
|
|
|
if string =~ /(.*)\/(.*)/
|
|
|
|
parent_name = $1
|
|
|
|
child_name = $2
|
|
|
|
parent = Category.query_parent_category(parent_name)
|
|
|
|
invalid_format string, "Could not find category named #{parent_name}" unless parent
|
|
|
|
object = Category.query_category(child_name, parent)
|
|
|
|
invalid_format string, "Could not find subcategory of #{parent_name} named #{child_name}" unless object
|
|
|
|
else
|
|
|
|
object = Category.where(id: string.to_i).first || Category.where(slug: string).first || Category.where(name: string).first
|
|
|
|
invalid_format string, "Could not find category named #{string}" unless object
|
|
|
|
end
|
|
|
|
|
|
|
|
value = object.id
|
|
|
|
when :user_id, :post_id, :topic_id, :group_id, :badge_id
|
|
|
|
if string.gsub(/[ _]/, '') =~ /^-?\d+$/
|
|
|
|
clazz_name = (/^(.*)_id$/.match(type.to_s)[1].classify.to_sym)
|
2015-07-14 19:01:38 -04:00
|
|
|
begin
|
2017-08-02 01:42:49 -04:00
|
|
|
object = Object.const_get(clazz_name).with_deleted.find(string.gsub(/[ _]/, '').to_i)
|
|
|
|
value = object.id
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
invalid_format string, "The specified #{clazz_name} was not found"
|
2015-07-14 19:01:38 -04:00
|
|
|
end
|
2017-08-02 01:42:49 -04:00
|
|
|
elsif type == :user_id
|
|
|
|
begin
|
|
|
|
object = User.find_by_username_or_email(string)
|
|
|
|
value = object.id
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
invalid_format string, "The user named #{string} was not found"
|
2015-07-15 15:23:56 -04:00
|
|
|
end
|
2017-08-02 01:42:49 -04:00
|
|
|
elsif type == :post_id
|
|
|
|
if string =~ /(\d+)\/(\d+)(\?u=.*)?$/
|
|
|
|
object = Post.with_deleted.find_by(topic_id: $1, post_number: $2)
|
|
|
|
invalid_format string, "The post at topic:#{$1} post_number:#{$2} was not found" unless object
|
|
|
|
value = object.id
|
2015-07-14 19:49:23 -04:00
|
|
|
end
|
2017-08-02 01:42:49 -04:00
|
|
|
elsif type == :topic_id
|
|
|
|
if string =~ /\/t\/[^\/]+\/(\d+)/
|
2015-07-14 16:22:53 -04:00
|
|
|
begin
|
2017-08-02 01:42:49 -04:00
|
|
|
object = Topic.with_deleted.find($1)
|
2015-08-04 02:35:02 -04:00
|
|
|
value = object.id
|
2015-07-14 16:22:53 -04:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2017-08-02 01:42:49 -04:00
|
|
|
invalid_format string, "The topic with id #{$1} was not found"
|
2015-07-14 16:22:53 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-02 01:42:49 -04:00
|
|
|
elsif type == :group_id
|
|
|
|
object = Group.where(name: string).first
|
|
|
|
invalid_format string, "The group named #{string} was not found" unless object
|
|
|
|
value = object.id
|
2015-07-14 16:22:53 -04:00
|
|
|
else
|
2017-08-02 01:42:49 -04:00
|
|
|
invalid_format string
|
|
|
|
end
|
|
|
|
when :int_list
|
|
|
|
value = string.split(',').map { |s| s.downcase == '#null' ? nil : s.to_i }
|
|
|
|
invalid_format string, "can't be empty" if value.length == 0
|
|
|
|
when :string_list
|
|
|
|
value = string.split(',').map { |s| s.downcase == '#null' ? nil : s }
|
|
|
|
invalid_format string, "can't be empty" if value.length == 0
|
|
|
|
when :user_list
|
|
|
|
value = string.split(',').map { |s| User.find_by_username_or_email(s) }
|
|
|
|
invalid_format string, "can't be empty" if value.length == 0
|
|
|
|
else
|
|
|
|
raise TypeError.new('unknown parameter type??? should not get here')
|
2015-07-14 16:22:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
def self.create_from_sql(sql, opts = {})
|
2015-07-14 16:22:53 -04:00
|
|
|
in_params = false
|
|
|
|
ret_params = []
|
2017-12-18 07:46:02 -05:00
|
|
|
sql.lines.find do |line|
|
|
|
|
line.chomp!
|
|
|
|
|
2015-07-14 16:22:53 -04:00
|
|
|
if in_params
|
|
|
|
# -- (ident) :(ident) (= (ident))?
|
|
|
|
|
|
|
|
if line =~ /^\s*--\s*([a-zA-Z_ ]+)\s*:([a-z_]+)\s*(?:=\s+(.*)\s*)?$/
|
|
|
|
type = $1
|
|
|
|
ident = $2
|
|
|
|
default = $3
|
|
|
|
nullable = false
|
|
|
|
if type =~ /^(null)?(.*?)(null)?$/i
|
2017-08-02 01:42:49 -04:00
|
|
|
if $1 || $3
|
2015-07-14 16:22:53 -04:00
|
|
|
nullable = true
|
|
|
|
end
|
|
|
|
type = $2
|
|
|
|
end
|
|
|
|
type = type.strip
|
|
|
|
|
2015-07-14 19:01:38 -04:00
|
|
|
begin
|
|
|
|
ret_params << DataExplorer::Parameter.new(ident, type, default, nullable)
|
|
|
|
rescue
|
|
|
|
if opts[:strict]
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-14 16:22:53 -04:00
|
|
|
false
|
|
|
|
elsif line =~ /^\s+$/
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
if line =~ /^\s*--\s*\[params\]\s*$/
|
|
|
|
in_params = true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2019-11-14 15:07:58 -05:00
|
|
|
ret_params
|
2015-07-14 16:22:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-25 12:25:15 -04:00
|
|
|
require_dependency 'application_controller'
|
2018-10-10 07:29:13 -04:00
|
|
|
require_dependency File.expand_path('../lib/queries.rb', __FILE__)
|
2015-06-25 16:26:31 -04:00
|
|
|
class DataExplorer::QueryController < ::ApplicationController
|
2015-06-25 14:58:14 -04:00
|
|
|
requires_plugin DataExplorer.plugin_name
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2017-09-04 01:59:06 -04:00
|
|
|
before_action :check_enabled
|
2019-09-11 10:52:06 -04:00
|
|
|
before_action :set_group, only: [:group_reports_index, :group_reports_show, :group_reports_run]
|
2019-09-11 10:09:41 -04:00
|
|
|
before_action :set_query, only: [:group_reports_show, :group_reports_run]
|
|
|
|
|
2019-09-11 10:52:06 -04:00
|
|
|
attr_reader :group, :query
|
2015-07-08 16:45:13 -04:00
|
|
|
|
|
|
|
def check_enabled
|
|
|
|
raise Discourse::NotFound unless SiteSetting.data_explorer_enabled?
|
|
|
|
end
|
|
|
|
|
2019-09-11 10:09:41 -04:00
|
|
|
def set_group
|
|
|
|
@group = Group.find_by(name: params["group_name"])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_query
|
|
|
|
@query = DataExplorer::Query.find(params[:id].to_i)
|
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
def index
|
|
|
|
# guardian.ensure_can_use_data_explorer!
|
|
|
|
queries = DataExplorer::Query.all
|
2018-10-10 07:29:13 -04:00
|
|
|
Queries.default.each do |params|
|
|
|
|
query = DataExplorer::Query.new
|
|
|
|
query.id = params.second["id"]
|
|
|
|
query.sql = params.second["sql"]
|
|
|
|
query.name = params.second["name"]
|
|
|
|
query.description = params.second["description"]
|
|
|
|
query.created_by = Discourse::SYSTEM_USER_ID.to_s
|
|
|
|
|
|
|
|
# don't render this query if query with the same id already exists in pstore
|
|
|
|
queries.push(query) unless DataExplorer.pstore_get("q:#{query.id}").present?
|
|
|
|
end
|
|
|
|
|
2015-06-25 16:26:31 -04:00
|
|
|
render_serialized queries, DataExplorer::QuerySerializer, root: 'queries'
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2017-09-04 01:59:06 -04:00
|
|
|
skip_before_action :check_xhr, only: [:show]
|
2015-06-25 12:25:15 -04:00
|
|
|
def show
|
2015-06-30 15:52:17 -04:00
|
|
|
check_xhr unless params[:export]
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
query = DataExplorer::Query.find(params[:id].to_i)
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
if params[:export]
|
2015-06-30 15:52:17 -04:00
|
|
|
response.headers['Content-Disposition'] = "attachment; filename=#{query.slug}.dcquery.json"
|
2015-06-25 14:58:14 -04:00
|
|
|
response.sending_file = true
|
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
# guardian.ensure_can_see! query
|
2015-06-30 13:37:48 -04:00
|
|
|
render_serialized query, DataExplorer::QuerySerializer, root: 'query'
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2019-11-14 13:39:55 -05:00
|
|
|
def groups
|
|
|
|
render_serialized(Group.all, BasicGroupSerializer)
|
|
|
|
end
|
|
|
|
|
2019-09-11 10:09:41 -04:00
|
|
|
def group_reports_index
|
2019-09-11 10:52:06 -04:00
|
|
|
return raise Discourse::NotFound unless guardian.user_is_a_member_of_group?(group)
|
2019-09-11 10:09:41 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render 'groups/show' }
|
|
|
|
format.json do
|
|
|
|
queries = DataExplorer::Query.all
|
2019-09-13 02:30:29 -04:00
|
|
|
queries.select! { |query| query.group_ids&.include?(group.id.to_s) }
|
2019-09-11 10:09:41 -04:00
|
|
|
render_serialized queries, DataExplorer::QuerySerializer, root: 'queries'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_reports_show
|
|
|
|
return raise Discourse::NotFound unless guardian.user_can_access_query?(group, query)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render 'groups/show' }
|
|
|
|
format.json do
|
|
|
|
render_serialized query, DataExplorer::QuerySerializer, root: 'query'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-21 04:46:15 -05:00
|
|
|
skip_before_action :check_xhr, only: [:group_reports_run]
|
2019-09-11 10:09:41 -04:00
|
|
|
def group_reports_run
|
|
|
|
return raise Discourse::NotFound unless guardian.user_can_access_query?(group, query)
|
|
|
|
|
|
|
|
run
|
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
def create
|
|
|
|
# guardian.ensure_can_create_explorer_query!
|
2018-09-16 12:28:16 -04:00
|
|
|
|
2015-06-29 15:10:24 -04:00
|
|
|
query = DataExplorer::Query.from_hash params.require(:query)
|
2018-08-24 07:58:51 -04:00
|
|
|
query.created_at = Time.now
|
2018-08-14 07:06:28 -04:00
|
|
|
query.created_by = current_user.id.to_s
|
2018-08-24 07:58:51 -04:00
|
|
|
query.last_run_at = Time.now
|
2015-06-30 15:52:17 -04:00
|
|
|
query.id = nil # json import will assign an id, which is wrong
|
2015-06-25 14:58:14 -04:00
|
|
|
query.save
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-30 15:52:17 -04:00
|
|
|
render_serialized query, DataExplorer::QuerySerializer, root: 'query'
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
def update
|
2015-06-30 15:52:17 -04:00
|
|
|
query = DataExplorer::Query.find(params[:id].to_i, ignore_deleted: true)
|
2015-06-29 15:10:24 -04:00
|
|
|
hash = params.require(:query)
|
2019-09-11 10:09:41 -04:00
|
|
|
hash[:group_ids] ||= []
|
2015-06-30 15:52:17 -04:00
|
|
|
|
|
|
|
# Undeleting
|
|
|
|
unless query.id
|
|
|
|
if hash[:id]
|
|
|
|
query.id = hash[:id].to_i
|
|
|
|
else
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-11 10:09:41 -04:00
|
|
|
[:name, :sql, :description, :created_by, :created_at, :group_ids, :last_run_at].each do |sym|
|
2015-06-30 13:20:22 -04:00
|
|
|
query.send("#{sym}=", hash[sym]) if hash[sym]
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
2015-07-14 19:01:38 -04:00
|
|
|
|
2015-07-14 16:22:53 -04:00
|
|
|
query.check_params!
|
2015-06-25 14:58:14 -04:00
|
|
|
query.save
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-30 18:56:09 -04:00
|
|
|
render_serialized query, DataExplorer::QuerySerializer, root: 'query'
|
2015-07-14 16:22:53 -04:00
|
|
|
rescue DataExplorer::ValidationError => e
|
|
|
|
render_json_error e.message
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
def destroy
|
|
|
|
query = DataExplorer::Query.find(params[:id].to_i)
|
|
|
|
query.destroy
|
2015-06-30 15:52:17 -04:00
|
|
|
|
2017-08-02 01:42:49 -04:00
|
|
|
render json: { success: true, errors: [] }
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-07-08 16:45:13 -04:00
|
|
|
def schema
|
2018-07-12 22:42:11 -04:00
|
|
|
schema_version = DB.query_single("SELECT max(version) AS tag FROM schema_migrations").first
|
2016-03-26 01:46:26 -04:00
|
|
|
if stale?(public: true, etag: schema_version, template: false)
|
2015-07-08 16:45:13 -04:00
|
|
|
render json: DataExplorer.schema
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-04 01:59:06 -04:00
|
|
|
skip_before_action :check_xhr, only: [:run]
|
2015-06-30 22:51:38 -04:00
|
|
|
# Return value:
|
|
|
|
# success - true/false. if false, inspect the errors value.
|
|
|
|
# errors - array of strings.
|
|
|
|
# params - hash. Echo of the query parameters as executed.
|
|
|
|
# duration - float. Time to execute the query, in milliseconds, to 1 decimal place.
|
|
|
|
# columns - array of strings. Titles of the returned columns, in order.
|
|
|
|
# explain - string. (Optional - pass explain=true in the request) Postgres query plan, UNIX newlines.
|
|
|
|
# rows - array of array of strings. Results of the query. In the same order as 'columns'.
|
2015-06-25 14:58:14 -04:00
|
|
|
def run
|
2015-07-09 15:02:47 -04:00
|
|
|
check_xhr unless params[:download]
|
2018-10-10 07:29:13 -04:00
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
query = DataExplorer::Query.find(params[:id].to_i)
|
2018-08-24 07:58:51 -04:00
|
|
|
query.last_run_at = Time.now
|
2018-10-10 07:29:13 -04:00
|
|
|
|
|
|
|
if params[:id].to_i < 0
|
|
|
|
query.created_by = Discourse::SYSTEM_USER_ID.to_s
|
|
|
|
query.save_default_query
|
|
|
|
else
|
|
|
|
query.save
|
|
|
|
end
|
2018-08-24 07:58:51 -04:00
|
|
|
|
2015-07-09 15:02:47 -04:00
|
|
|
if params[:download]
|
|
|
|
response.sending_file = true
|
|
|
|
end
|
|
|
|
|
2015-07-15 15:23:56 -04:00
|
|
|
params[:params] = params[:_params] if params[:_params] # testing workaround
|
|
|
|
query_params = {}
|
|
|
|
query_params = MultiJson.load(params[:params]) if params[:params]
|
2015-07-09 15:02:47 -04:00
|
|
|
|
2015-08-04 13:23:50 -04:00
|
|
|
opts = { current_user: current_user.username }
|
2015-06-30 18:56:09 -04:00
|
|
|
opts[:explain] = true if params[:explain] == "true"
|
2015-08-04 13:23:50 -04:00
|
|
|
|
2019-01-23 05:39:20 -05:00
|
|
|
opts[:limit] =
|
2020-02-24 00:23:48 -05:00
|
|
|
if params[:format] == "csv"
|
|
|
|
if params[:limit].present?
|
|
|
|
limit = params[:limit].to_i
|
|
|
|
limit = DataExplorer::QUERY_RESULT_MAX_LIMIT if limit > DataExplorer::QUERY_RESULT_MAX_LIMIT
|
|
|
|
limit
|
|
|
|
else
|
|
|
|
DataExplorer::QUERY_RESULT_MAX_LIMIT
|
|
|
|
end
|
|
|
|
elsif params[:limit].present?
|
2020-02-23 23:58:22 -05:00
|
|
|
params[:limit] == "ALL" ? "ALL" : params[:limit].to_i
|
2019-01-23 05:39:20 -05:00
|
|
|
end
|
2015-07-09 15:02:47 -04:00
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
result = DataExplorer.run_query(query, query_params, opts)
|
|
|
|
|
|
|
|
if result[:error]
|
|
|
|
err = result[:error]
|
|
|
|
|
|
|
|
# Pretty printing logic
|
|
|
|
err_class = err.class
|
|
|
|
err_msg = err.message
|
|
|
|
if err.is_a? ActiveRecord::StatementInvalid
|
|
|
|
err_class = err.original_exception.class
|
2015-07-09 15:02:47 -04:00
|
|
|
err_msg.gsub!("#{err_class}:", '')
|
2015-06-25 14:58:14 -04:00
|
|
|
else
|
|
|
|
err_msg = "#{err_class}: #{err_msg}"
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
success: false,
|
|
|
|
errors: [err_msg]
|
2015-07-15 15:51:33 -04:00
|
|
|
}, status: 422
|
2015-06-25 14:58:14 -04:00
|
|
|
else
|
|
|
|
pg_result = result[:pg_result]
|
|
|
|
cols = pg_result.fields
|
2015-08-03 18:07:29 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
if params[:download]
|
|
|
|
response.headers['Content-Disposition'] =
|
|
|
|
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.json"
|
|
|
|
end
|
|
|
|
json = {
|
|
|
|
success: true,
|
|
|
|
errors: [],
|
|
|
|
duration: (result[:duration_secs].to_f * 1000).round(1),
|
2018-09-16 12:28:16 -04:00
|
|
|
result_count: pg_result.values.length || 0,
|
2015-08-03 18:07:29 -04:00
|
|
|
params: query_params,
|
|
|
|
columns: cols,
|
2020-02-20 13:04:15 -05:00
|
|
|
default_limit: DataExplorer::QUERY_RESULT_DEFAULT_LIMIT
|
2015-08-03 18:07:29 -04:00
|
|
|
}
|
|
|
|
json[:explain] = result[:explain] if opts[:explain]
|
2019-01-24 02:16:02 -05:00
|
|
|
|
|
|
|
if !params[:download]
|
2019-05-13 00:41:37 -04:00
|
|
|
relations, colrender = DataExplorer.add_extra_data(pg_result)
|
|
|
|
json[:relations] = relations
|
|
|
|
json[:colrender] = colrender
|
2019-01-24 02:16:02 -05:00
|
|
|
end
|
2015-08-03 18:07:29 -04:00
|
|
|
|
|
|
|
json[:rows] = pg_result.values
|
|
|
|
|
|
|
|
render json: json
|
|
|
|
end
|
|
|
|
format.csv do
|
|
|
|
response.headers['Content-Disposition'] =
|
|
|
|
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.csv"
|
|
|
|
|
|
|
|
require 'csv'
|
|
|
|
text = CSV.generate do |csv|
|
|
|
|
csv << cols
|
|
|
|
pg_result.values.each do |row|
|
|
|
|
csv << row
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-03 14:58:28 -04:00
|
|
|
render plain: text
|
2015-08-03 18:07:29 -04:00
|
|
|
end
|
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-11 10:52:06 -04:00
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
class DataExplorer::QuerySerializer < ActiveModel::Serializer
|
2019-09-11 10:09:41 -04:00
|
|
|
attributes :id, :sql, :name, :description, :param_info, :created_by, :created_at, :username, :group_ids, :last_run_at
|
2015-07-14 16:22:53 -04:00
|
|
|
|
|
|
|
def param_info
|
|
|
|
object.params.map(&:to_hash) rescue nil
|
|
|
|
end
|
2018-08-14 07:51:59 -04:00
|
|
|
|
|
|
|
def username
|
|
|
|
User.find(created_by).username rescue nil
|
|
|
|
end
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
DataExplorer::Engine.routes.draw do
|
2015-06-25 16:26:31 -04:00
|
|
|
root to: "query#index"
|
2015-07-08 16:45:13 -04:00
|
|
|
get 'schema' => "query#schema"
|
2015-06-25 16:26:31 -04:00
|
|
|
get 'queries' => "query#index"
|
2019-11-14 13:39:55 -05:00
|
|
|
get 'groups' => "query#groups"
|
2015-06-29 15:10:24 -04:00
|
|
|
post 'queries' => "query#create"
|
2015-06-30 13:20:22 -04:00
|
|
|
get 'queries/:id' => "query#show"
|
|
|
|
put 'queries/:id' => "query#update"
|
|
|
|
delete 'queries/:id' => "query#destroy"
|
|
|
|
post 'queries/:id/run' => "query#run"
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 14:58:14 -04:00
|
|
|
Discourse::Application.routes.append do
|
2019-09-11 10:09:41 -04:00
|
|
|
get '/g/:group_name/reports' => 'data_explorer/query#group_reports_index'
|
|
|
|
get '/g/:group_name/reports/:id' => 'data_explorer/query#group_reports_show'
|
|
|
|
post '/g/:group_name/reports/:id/run' => 'data_explorer/query#group_reports_run'
|
|
|
|
|
2015-06-25 16:26:31 -04:00
|
|
|
mount ::DataExplorer::Engine, at: '/admin/plugins/explorer', constraints: AdminConstraint.new
|
2015-06-25 12:25:15 -04:00
|
|
|
end
|
2015-07-28 12:59:26 -04:00
|
|
|
end
|