Skip to content

Commit 1fded46

Browse files
committed
[NEP-18090]: Adding en endpoint for warming up the dataset cache
Adding new loggers and spec cases
1 parent 951a925 commit 1fded46

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

lib/superset/dataset/warm_up_cache.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ def response
1717
dataset_details = fetch_dataset_details(dashboard_id)
1818
dataset_details.each do |dataset|
1919
begin
20-
client.put(route, params(dashboard_id, dataset["datasource_name"], dataset["name"]))
20+
logger.info("Hitting #{route} for warming up the cache for the dashboard #{dashboard_id.to_s} and for the dataset #{dataset["datasource_name"]}")
21+
api_response(dataset["datasource_name"], dataset["name"])
2122
rescue => e
22-
Rollbar.error(e.message)
23+
Rollbar.error("Warm up cache failed for the dashboard #{dashboard_id.to_s} and for the dataset #{dataset["datasource_name"]} - #{e.message}")
2324
end
2425
end
2526
end
2627

27-
def validate_dashboard_id
28-
raise InvalidParameterError, "dashboard_id must be present and must be an integer" unless dashboard_id.present? && dashboard_id.is_a?(Integer)
28+
def api_response(dataset_name, database_name)
29+
client.put(route, params(dashboard_id, dataset_name, database_name))
2930
end
3031

3132
def params(dashboard_id, dataset_name, db_name)
@@ -36,15 +37,23 @@ def params(dashboard_id, dataset_name, db_name)
3637
}
3738
end
3839

40+
private
41+
42+
def validate_dashboard_id
43+
raise InvalidParameterError, "dashboard_id must be present and must be an integer" unless dashboard_id.present? && dashboard_id.is_a?(Integer)
44+
end
45+
3946
def fetch_dataset_details(dashboard_id)
4047
Superset::Dashboard::Datasets::List.new(dashboard_id).datasets_details.map { |dataset| dataset['database'].slice('name').merge(dataset.slice('datasource_name'))}
4148
end
4249

43-
private
44-
4550
def route
4651
"dataset/warm_up_cache"
4752
end
53+
54+
def logger
55+
@logger ||= Superset::Logger.new
56+
end
4857
end
4958
end
5059
end

spec/superset/dataset/warm_up_cache_spec.rb

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,52 @@
55
let(:dashboard_id) { 1 }
66
let(:response) { nil }
77

8-
before do
9-
allow(subject).to receive(:response).and_return(response)
10-
end
11-
128
describe '.perform' do
13-
context 'when dashboard_id is not present' do
14-
let(:dashboard_id) { nil }
15-
16-
it 'raises an error' do
17-
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dashboard_id must be present and must be an integer")
18-
end
19-
end
20-
21-
context 'when dashboard_id is not an integer' do
22-
let(:dashboard_id) { 'string' }
23-
24-
it 'raises an error' do
25-
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dashboard_id must be present and must be an integer")
26-
end
27-
end
28-
29-
context 'when dashboard_id is an integer' do
30-
let(:response) { 'Dashboard warmed up' }
31-
32-
it 'warms up the dataset' do
33-
expect(subject.perform).to eq response
34-
end
9+
context "Dataset count is not considered" do
10+
before do
11+
allow(subject).to receive(:response).and_return(response)
12+
end
13+
context 'when dashboard_id is not present' do
14+
let(:dashboard_id) { nil }
15+
16+
it 'raises an error' do
17+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dashboard_id must be present and must be an integer")
18+
end
19+
end
20+
21+
context 'when dashboard_id is not an integer' do
22+
let(:dashboard_id) { 'string' }
23+
24+
it 'raises an error' do
25+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dashboard_id must be present and must be an integer")
26+
end
27+
end
28+
29+
context 'when dashboard_id is an integer' do
30+
let(:response) { 'Dashboard warmed up' }
31+
32+
it 'warms up the dashboard' do
33+
expect(subject.perform).to eq response
34+
end
35+
end
36+
end
37+
38+
context 'when dashboard has multiple datasets' do
39+
let(:dataset_details) do
40+
[
41+
{"database"=>"client database 1", "datasource_name"=>"datasource 101"},
42+
{"database"=>"client database 2", "datasource_name"=>"datasource 102"},
43+
]
44+
end
45+
let(:api_response) { "Dataset warmed up" }
46+
before do
47+
allow(subject).to receive(:fetch_dataset_details).with(1) { dataset_details }
48+
allow(subject).to receive(:api_response).and_return(api_response)
49+
end
50+
it 'warms up both the dataset' do
51+
subject.response
52+
expect(subject).to have_received(:api_response).twice
53+
end
3554
end
3655
end
3756
end

0 commit comments

Comments
 (0)