Skip to content

Commit 03b9a88

Browse files
committed
add default order, update specs
1 parent ee25652 commit 03b9a88

File tree

12 files changed

+247
-41
lines changed

12 files changed

+247
-41
lines changed

lib/superset/dashboard/list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Dashboard
77
class List < Superset::Request
88
attr_reader :title_contains, :title_equals, :tags_equal, :ids_not_in
99

10-
def initialize(page_num: 0, title_contains: '', title_equals: '', tags_equal: [], ids_not_in: [])
10+
def initialize(page_num: 0, title_contains: '', title_equals: '', tags_equal: [], ids_not_in: [], **extra_args)
1111
@title_contains = title_contains
1212
@title_equals = title_equals
1313
@tags_equal = tags_equal

lib/superset/dashboard/list_all.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Superset has a 100 result limit for requests
2+
# This is a wrapper for Superset::Dashboard::List to recursively list all dashboards
3+
4+
# TODO - would be very handy to create a parent class for this
5+
# to then be able to use the same pattern for other ::List classes
6+
7+
module Superset
8+
module Dashboard
9+
class ListAll
10+
include Display
11+
12+
def initialize(**kwargs)
13+
kwargs.each do |key, value|
14+
instance_variable_set("@#{key}", value)
15+
self.class.attr_reader key
16+
end
17+
end
18+
19+
def constructor_args
20+
instance_variables.each_with_object({}) do |var, hash|
21+
hash[var.to_s.delete('@').to_sym] = instance_variable_get(var)
22+
end
23+
end
24+
25+
def perform
26+
page_num = 0
27+
boards = []
28+
boards << next_group = Dashboard::List.new(page_num: page_num, **constructor_args).result
29+
while !next_group.empty?
30+
boards << next_group = Dashboard::List.new(page_num: page_num += 1, **constructor_args).result
31+
end
32+
@result = boards.flatten
33+
end
34+
35+
def result
36+
@result ||= []
37+
end
38+
39+
def rows
40+
result.map do |d|
41+
list_attributes.map do |la|
42+
la == :url ? "#{superset_host}#{d[la]}" : d[la]
43+
end
44+
end
45+
end
46+
47+
def ids
48+
result.map { |d| d[:id] }
49+
end
50+
51+
private
52+
53+
def list_attributes
54+
[:id, :dashboard_title, :status, :url]
55+
end
56+
57+
def superset_host
58+
ENV['SUPERSET_HOST']
59+
end
60+
end
61+
end
62+
end

lib/superset/request.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ def superset_host
3333
end
3434

3535
def query_params
36-
[filters, pagination].join
36+
[filters, pagination, order_by].join
37+
end
38+
39+
def order_by
40+
# by default, we will order by changed_on as per the GUI
41+
",order_column:changed_on,order_direction:desc"
3742
end
3843

3944
private

lib/superset/tag/add_to_object.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# frozen_string_literal: true
22

3+
# Add tags to an object
4+
# for object type options see ObjectType.to_a
5+
#
6+
# Usage:
7+
# Superset::Tag::AddToObject.new(object_type_id: ObjectType::DASHBOARD, object_id: new_dashboard.id, tags: tags).perform
8+
39
module Superset
410
module Tag
511
class AddToObject < Superset::Request
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# Delete tags from an object
4+
# for object type options see ObjectType.to_a
5+
#
6+
# Usage:
7+
# Superset::Tag::DeleteFromObject.new(object_type_id: ObjectType::DASHBOARD, object_id: 101, tag: 'test-tag').perform
8+
9+
module Superset
10+
module Tag
11+
class DeleteFromObject < Superset::Request
12+
13+
attr_reader :object_type_id, :object_id, :tag
14+
15+
def initialize(object_type_id:, object_id:, tag:)
16+
@object_type_id = object_type_id
17+
@object_id = object_id
18+
@tag = tag
19+
end
20+
21+
def perform
22+
validate_constructor_args
23+
24+
response
25+
end
26+
27+
def response
28+
@response ||= client.delete(route)
29+
end
30+
31+
def validate_constructor_args
32+
raise InvalidParameterError, "object_type_id integer is required" unless object_type_id.present? && object_type_id.is_a?(Integer)
33+
raise InvalidParameterError, "object_type_id is not a known value" unless ObjectType.list.include?(object_type_id)
34+
raise InvalidParameterError, "object_id integer is required" unless object_id.present? && object_id.is_a?(Integer)
35+
raise InvalidParameterError, "tag string is required" unless tag.present? && tag.is_a?(String)
36+
end
37+
38+
private
39+
40+
def route
41+
"tag/#{object_type_id}/#{object_id}/#{tag}/"
42+
end
43+
end
44+
end
45+
end

spec/superset/chart/list_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"viz_type"=>"dist_bar"
7575
}]
7676
end
77+
let(:default_query_params) { "page:0,page_size:100,order_column:changed_on,order_direction:desc" }
7778

7879
before do
7980
allow(subject).to receive(:result).and_return(result)
@@ -106,14 +107,14 @@
106107

107108
describe '#query_params' do
108109
specify 'with defaults' do
109-
expect(subject.query_params).to eq("page:0,page_size:100")
110+
expect(subject.query_params).to eq(default_query_params)
110111
end
111112

112113
context 'with name_contains filters' do
113114
subject { described_class.new(name_contains: 'birth') }
114115

115116
specify do
116-
expect(subject.query_params).to eq("filters:!((col:slice_name,opr:ct,value:'birth')),page:0,page_size:100")
117+
expect(subject.query_params).to eq("filters:!((col:slice_name,opr:ct,value:'birth')),#{default_query_params}")
117118
end
118119
end
119120

@@ -125,7 +126,7 @@
125126
"filters:!(" \
126127
"(col:slice_name,opr:ct,value:'birth')," \
127128
"(col:dashboards,opr:rel_m_m,value:3)" \
128-
"),page:0,page_size:100")
129+
"),#{default_query_params}")
129130
end
130131
end
131132
end

spec/superset/dashboard/list_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
}
2020
]
2121
end
22+
let(:default_query_params) { "page:0,page_size:100,order_column:changed_on,order_direction:desc" }
2223

2324
before do
2425
allow(subject).to receive(:result).and_return(result)
@@ -62,15 +63,15 @@
6263
context 'for pagination' do
6364
context 'with defaults' do
6465
specify do
65-
expect(subject.query_params).to eq("page:0,page_size:100")
66+
expect(subject.query_params).to eq(default_query_params)
6667
end
6768
end
6869

6970
context 'with specifiec page' do
7071
subject { described_class.new(page_num: 5) }
7172

7273
specify do
73-
expect(subject.query_params).to eq("page:5,page_size:100")
74+
expect(subject.query_params).to eq(default_query_params.gsub('page:0', 'page:5'))
7475
end
7576
end
7677
end
@@ -79,7 +80,7 @@
7980
subject { described_class.new(title_contains: 'acme') }
8081

8182
specify do
82-
expect(subject.query_params).to eq("filters:!((col:dashboard_title,opr:ct,value:'acme')),page:0,page_size:100")
83+
expect(subject.query_params).to eq("filters:!((col:dashboard_title,opr:ct,value:'acme')),#{default_query_params}")
8384
end
8485
end
8586

@@ -91,7 +92,7 @@
9192
"filters:!(" \
9293
"(col:dashboard_title,opr:ct,value:'birth')," \
9394
"(col:tags,opr:dashboard_tags,value:'template')" \
94-
"),page:0,page_size:100")
95+
"),#{default_query_params}")
9596
end
9697
end
9798

@@ -106,7 +107,7 @@
106107
"(col:tags,opr:dashboard_tags,value:'template')," \
107108
"(col:tags,opr:dashboard_tags,value:'client:acme')," \
108109
"(col:tags,opr:dashboard_tags,value:'product:turbo-charged-feet')" \
109-
"),page:3,page_size:100")
110+
"),#{default_query_params.gsub('page:0', 'page:3')}")
110111
end
111112
end
112113
end

spec/superset/database/list_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
]
2121
}
2222
end
23+
let(:default_query_params) { "page:0,page_size:100,order_column:changed_on,order_direction:desc" }
2324

2425
describe '#rows' do
2526
before do
@@ -40,15 +41,15 @@
4041
context 'for pagination' do
4142
context 'with defaults' do
4243
specify do
43-
expect(subject.query_params).to eq("page:0,page_size:100")
44+
expect(subject.query_params).to eq(default_query_params)
4445
end
4546
end
4647

4748
context 'with specifiec page' do
4849
subject { described_class.new(page_num: 5) }
4950

5051
specify do
51-
expect(subject.query_params).to eq("page:5,page_size:100")
52+
expect(subject.query_params).to eq(default_query_params.gsub('page:0', 'page:5'))
5253
end
5354
end
5455
end
@@ -57,15 +58,15 @@
5758
subject { described_class.new(title_contains: 'acme') }
5859

5960
specify do
60-
expect(subject.query_params).to eq("filters:!((col:database_name,opr:ct,value:'acme')),page:0,page_size:100")
61+
expect(subject.query_params).to eq("filters:!((col:database_name,opr:ct,value:'acme')),#{default_query_params}")
6162
end
6263
end
6364

6465
context 'with uuid_equals filters' do
6566
subject { described_class.new(uuid_equals: '123') }
6667

6768
specify do
68-
expect(subject.query_params).to eq("filters:!((col:uuid,opr:eq,value:'123')),page:0,page_size:100")
69+
expect(subject.query_params).to eq("filters:!((col:uuid,opr:eq,value:'123')),#{default_query_params}")
6970
end
7071
end
7172
end

spec/superset/dataset/list_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@
4343
]
4444
end
4545

46+
let(:default_query_params) { "page:0,page_size:100,order_column:changed_on,order_direction:desc" }
47+
4648
before do
4749
allow(subject).to receive(:result).and_return(result)
4850
allow(subject).to receive(:superset_host).and_return(superset_host)
4951
allow(subject).to receive(:response).and_return( { 'count': 1 } )
5052
end
5153

5254
describe '#rows' do
53-
#before { stub_const("Superset::Request::PAGE_SIZE", "3") }
54-
5555
specify do
5656
expect(subject.rows).to eq(
5757
[
@@ -64,22 +64,22 @@
6464

6565
describe '#query_params' do
6666
specify 'with defaults' do
67-
expect(subject.query_params).to eq("page:0,page_size:100")
67+
expect(subject.query_params).to eq("#{default_query_params}")
6868
end
6969

7070
context 'with title_contains filters' do
7171
subject { described_class.new(title_contains: 'birth') }
7272

7373
specify do
74-
expect(subject.query_params).to eq("filters:!((col:table_name,opr:ct,value:'birth')),page:0,page_size:100")
74+
expect(subject.query_params).to eq("filters:!((col:table_name,opr:ct,value:'birth')),#{default_query_params}")
7575
end
7676
end
7777

7878
context 'with title_contains filters' do
7979
subject { described_class.new(title_equals: 'birth_days') }
8080

8181
specify do
82-
expect(subject.query_params).to eq("filters:!((col:table_name,opr:eq,value:'birth_days')),page:0,page_size:100")
82+
expect(subject.query_params).to eq("filters:!((col:table_name,opr:eq,value:'birth_days')),#{default_query_params}")
8383
end
8484
end
8585

@@ -88,7 +88,7 @@
8888
subject { described_class.new(title_equals: 'birth_days', schema_equals: 'schema_one') }
8989

9090
specify do
91-
expect(subject.query_params).to eq("filters:!((col:table_name,opr:eq,value:'birth_days'),(col:schema,opr:eq,value:'schema_one')),page:0,page_size:100")
91+
expect(subject.query_params).to eq("filters:!((col:table_name,opr:eq,value:'birth_days'),(col:schema,opr:eq,value:'schema_one')),#{default_query_params}")
9292
end
9393
end
9494

spec/superset/request_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@
3838
expect(subject.superset_host).to eq(host)
3939
end
4040
end
41+
42+
describe '#query_params' do
43+
it 'returns the default query params' do
44+
expect(subject.send(:query_params)).to eq("page:0,page_size:100,order_column:changed_on,order_direction:desc")
45+
end
46+
end
4147
end

0 commit comments

Comments
 (0)