Skip to content

Commit 4cb755f

Browse files
authored
Merge pull request #28 from rdytech/NEP-18090-new_endpoint_for_warm_up_cache
[NEP-18090]: Adding a class for warming up the dataset cache
2 parents d7aff94 + 51f411b commit 4cb755f

File tree

8 files changed

+165
-2
lines changed

8 files changed

+165
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Change Log
22

3+
## 0.1.6 - 2024-07-10
4+
5+
* added a class **WarmUpCache** to hit the 'api/v1/dataset/warm_up_cache' endpoint to warm up the cache of all the datasets for a particular dashaboard being passed to the class - https://github.com/rdytech/superset-client/pull/28
6+
37
## 0.1.5 - 2024-05-10
48

59
* add multi config for multi env creds https://github.com/rdytech/superset-client/pull/22

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GIT
1414
PATH
1515
remote: .
1616
specs:
17-
superset (0.1.5)
17+
superset (0.1.6)
1818
dotenv (~> 2.7)
1919
enumerate_it (~> 1.7.0)
2020
faraday (~> 1.0)

bin/console

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require "terminal-table"
1111
require "yaml"
1212
require "enumerate_it"
1313
require "superset"
14+
require "rollbar"
1415

1516
Dir["./lib/**/*.rb"].each { |f| require f }
1617
# You can add fixtures and/or initialization code here to make experimenting
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Superset
2+
module Dashboard
3+
class WarmUpCache < Superset::Request
4+
5+
attr_reader :dashboard_id
6+
7+
def initialize(dashboard_id:)
8+
@dashboard_id = dashboard_id
9+
end
10+
11+
def perform
12+
validate_dashboard_id
13+
response
14+
end
15+
16+
def response
17+
dataset_details = fetch_dataset_details(dashboard_id)
18+
dataset_details.each do |dataset|
19+
begin
20+
warm_up_dataset(dataset["datasource_name"], dataset["name"])
21+
rescue => e
22+
Rollbar.error("Warm up cache failed for the dashboard #{dashboard_id.to_s} and for the dataset #{dataset["datasource_name"]} - #{e}")
23+
end
24+
end
25+
end
26+
27+
def warm_up_dataset(dataset_name, db_name)
28+
Superset::Dataset::WarmUpCache.new(dashboard_id: dashboard_id, table_name: dataset_name, db_name: db_name).perform
29+
end
30+
31+
private
32+
33+
def validate_dashboard_id
34+
raise InvalidParameterError, "dashboard_id must be present and must be an integer" unless dashboard_id.present? && dashboard_id.is_a?(Integer)
35+
end
36+
37+
def fetch_dataset_details(dashboard_id)
38+
Superset::Dashboard::Datasets::List.new(dashboard_id).datasets_details.map { |dataset| dataset['database'].slice('name').merge(dataset.slice('datasource_name'))}
39+
end
40+
end
41+
end
42+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Superset
2+
module Dataset
3+
class WarmUpCache < Superset::Request
4+
5+
attr_reader :dashboard_id, :table_name, :db_name
6+
7+
def initialize(dashboard_id:, table_name:, db_name:)
8+
@dashboard_id = dashboard_id
9+
@table_name = table_name
10+
@db_name = db_name
11+
end
12+
13+
def perform
14+
response
15+
end
16+
17+
def response
18+
logger.info("Hitting #{route} for warming up the cache for the dashboard #{dashboard_id.to_s} and for the dataset #{table_name}")
19+
client.put(route, params(dashboard_id, table_name, db_name))
20+
end
21+
22+
def params(dashboard_id, table_name, db_name)
23+
{
24+
"dashboard_id" => dashboard_id,
25+
"table_name" => table_name,
26+
"db_name" => db_name
27+
}
28+
end
29+
30+
private
31+
32+
def route
33+
"dataset/warm_up_cache"
34+
end
35+
36+
def logger
37+
@logger ||= Superset::Logger.new
38+
end
39+
end
40+
end
41+
end

lib/superset/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Superset
4-
VERSION = "0.1.5"
4+
VERSION = "0.1.6"
55
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Superset::Dashboard::WarmUpCache do
4+
subject { described_class.new(dashboard_id: dashboard_id) }
5+
let(:dashboard_id) { 1 }
6+
7+
describe '.perform' do
8+
context "Dataset count is not considered" do
9+
let(:response) { nil }
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+
{"name"=>"client database 1", "datasource_name"=>"datasource 101"},
42+
{"name"=>"client database 2", "datasource_name"=>"datasource 102"},
43+
]
44+
end
45+
let(:api_response) { "Datasets warmed up" }
46+
before do
47+
allow(subject).to receive(:fetch_dataset_details).with(dashboard_id) { dataset_details }
48+
allow(subject).to receive(:warm_up_dataset).and_return(api_response)
49+
end
50+
it 'warms up all the datasets' do
51+
subject.response
52+
expect(subject).to have_received(:warm_up_dataset).exactly(dataset_details.count).times
53+
end
54+
end
55+
end
56+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Superset::Dataset::WarmUpCache do
4+
subject { described_class.new(dashboard_id: dashboard_id, table_name: table_name, db_name: db_name) }
5+
let(:dashboard_id) { 1 }
6+
let(:table_name) { "Dataset 101"}
7+
let(:db_name) { "Client Database 1" }
8+
before do
9+
allow(subject).to receive(:response).and_return(response)
10+
end
11+
12+
describe '.perform' do
13+
let(:response) { 'Dataset warmed up' }
14+
15+
it 'warms up the dataset' do
16+
expect(subject.perform).to eq response
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)