Skip to content

Commit e709f47

Browse files
authored
Merge pull request #24 from rdytech/AC-10426-dataset-creation-endpoint
AC-10426 Adds endpoint for dataset creation, route info
2 parents 47a8914 + cbf4489 commit e709f47

File tree

6 files changed

+112
-7
lines changed

6 files changed

+112
-7
lines changed

bin/console

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ unless ENV['SUPERSET_HOST'] && ENV['SUPERSET_API_USERNAME'] && ENV['SUPERSET_API
4949
exit
5050
end
5151

52+
puts "\n >>> Welcome to the Superset Ruby API Client <<< \n\n"
53+
puts "Your accessible Superset Database connections are: Superset::Database::List.call"
54+
55+
Superset::Database::List.call
56+
5257
Pry.start(__FILE__)
58+

lib/superset/chart/list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def filters
3939
# TODO filtering across all classes needs a refactor to support multiple options in a more flexible way
4040
filter_set = []
4141
filter_set << "(col:slice_name,opr:ct,value:'#{name_contains}')" if name_contains.present?
42-
filter_set << "(col:dashboards,opr:rel_m_m,value:#{dashboard_id_eq})" if dashboard_id_eq.present?
42+
filter_set << "(col:dashboards,opr:rel_m_m,value:#{dashboard_id_eq})" if dashboard_id_eq.present? # rel many to many
4343
filter_set << "(col:datasource_id,opr:eq,value:#{dataset_id_eq})" if dataset_id_eq.present?
4444

4545
unless filter_set.empty?

lib/superset/dataset/create.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# The API demands that the new_dataset_name be uniq within the schema it points to.
2+
# same behaviour as in the GUI
3+
4+
module Superset
5+
module Dataset
6+
class Create < Superset::Request
7+
8+
attr_reader :target_database_id, :new_dataset_name, :new_dataset_schema, :sql
9+
10+
def initialize(target_database_id: , new_dataset_name: , new_dataset_schema: 'public', sql: )
11+
@target_database_id = target_database_id
12+
@new_dataset_name = new_dataset_name
13+
@new_dataset_schema = new_dataset_schema
14+
@sql = sql
15+
end
16+
17+
def perform
18+
raise "Error: target_database_id integer is required" unless target_database_id.present? && target_database_id.is_a?(Integer)
19+
raise "Error: new_dataset_name string is required" unless new_dataset_name.present? && new_dataset_name.is_a?(String)
20+
raise "Error: Dataset Name #{new_dataset_name} is already in use in the schema: #{new_dataset_schema}. Suggest you add (COPY) as a suffix to the name" if new_dataset_name_already_in_use?
21+
raise "Error: sql string is required" unless sql.present? && sql.is_a?(String)
22+
23+
logger.info("Creating New Dataset #{new_dataset_name} in DB #{target_database_id} Schema #{new_dataset_schema}")
24+
25+
response
26+
{ id: response['id'], dataset_name: response['data']['datasource_name'] }
27+
end
28+
29+
def response
30+
@response ||= client.post(route, params)
31+
end
32+
33+
def params
34+
{
35+
"schema": new_dataset_schema,
36+
"sql": sql,
37+
"table_name": new_dataset_name,
38+
"database": target_database_id
39+
40+
# Optional Params .. pulled straight from the GUI swagger example
41+
42+
#"always_filter_main_dttm": false,
43+
#"external_url": "string",
44+
#"is_managed_externally": false,
45+
#"normalize_columns": false,
46+
# "owners": [ 0 ],
47+
}
48+
end
49+
50+
private
51+
52+
# The API demands that the new_dataset_name be uniq within the schema it points to.
53+
def new_dataset_name_already_in_use?
54+
Dataset::List.new(title_equals: new_dataset_name, schema_equals: new_dataset_schema, database_id_eq: target_database_id).result.any?
55+
end
56+
57+
def route
58+
"dataset/"
59+
end
60+
end
61+
end
62+
end

lib/superset/dataset/list.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module Superset
22
module Dataset
33
class List < Superset::Request
4-
attr_reader :title_contains, :title_equals, :schema_equals
4+
attr_reader :title_contains, :title_equals, :schema_equals, :database_id_eq
55

6-
def initialize(page_num: 0, title_contains: '', title_equals: '', schema_equals: '')
6+
def initialize(page_num: 0, title_contains: '', title_equals: '', schema_equals: '', database_id_eq: '')
77
@title_contains = title_contains
88
@title_equals = title_equals
99
@schema_equals = schema_equals
10+
@database_id_eq = database_id_eq
1011
super(page_num: page_num)
1112
end
1213

@@ -26,6 +27,7 @@ def filters
2627
filters << "(col:table_name,opr:ct,value:'#{title_contains}')" if title_contains.present?
2728
filters << "(col:table_name,opr:eq,value:'#{title_equals}')" if title_equals.present?
2829
filters << "(col:schema,opr:eq,value:'#{schema_equals}')" if schema_equals.present?
30+
filters << "(col:database,opr:rel_o_m,value:#{database_id_eq})" if database_id_eq.present? # rel one to many
2931
unless filters.empty?
3032
"filters:!(" + filters.join(',') + "),"
3133
end

lib/superset/route_info.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Superset
2+
class RouteInfo < Superset::Request
3+
alias result response
4+
5+
attr_reader :route
6+
7+
def initialize(route:)
8+
@route = route
9+
end
10+
11+
def perform
12+
validate_route
13+
response
14+
end
15+
16+
def response
17+
validate_route
18+
@response ||= client.get(route)
19+
end
20+
21+
def filters
22+
result['filters']
23+
end
24+
25+
private
26+
27+
def validate_route
28+
unless route.present? && route.is_a?(String)
29+
puts "Example Route: 'dashboard/_info' "
30+
raise "Error: route string is required" unless route.present? && route.is_a?(String)
31+
end
32+
end
33+
end
34+
end

lib/superset/sqllab/execute.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module Sqllab
33
class Execute < Superset::Request
44
class InvalidParameterError < StandardError; end
55

6-
attr_reader :database_id, :query, :schema
6+
attr_reader :database_id, :query, :schema, :query_limit
77

8-
def initialize(database_id: , query: , schema: 'public')
8+
def initialize(database_id: , query: , schema: 'public', query_limit: 1000)
99
@database_id = database_id
1010
@query = query
1111
@schema = schema
12+
@query_limit = query_limit
1213
end
1314

1415
def perform
@@ -36,7 +37,7 @@ def query_params
3637
database_id: database_id,
3738
sql: query,
3839
schema: schema,
39-
queryLimit: 1000,
40+
queryLimit: query_limit,
4041
runAsync: false,
4142
}
4243
end
@@ -48,4 +49,4 @@ def validate_constructor_args
4849
end
4950
end
5051
end
51-
end
52+
end

0 commit comments

Comments
 (0)