Skip to content

Commit 9209a21

Browse files
committed
updates report and spec
1 parent e74e49a commit 9209a21

File tree

2 files changed

+148
-13
lines changed

2 files changed

+148
-13
lines changed

lib/superset/services/dashboard_report.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,32 @@ class DashboardReport
1515
attr_reader :dashboard_ids, :report_on_data_sovereignty_only
1616

1717
def initialize(dashboard_ids: [], report_on_data_sovereignty_only: true)
18-
@dashboard_ids = dashboard_ids.presence || all_dashboard_ids
18+
@dashboard_ids = dashboard_ids
1919
@report_on_data_sovereignty_only = report_on_data_sovereignty_only
2020
end
2121

2222
def perform
23-
create_report
23+
create_dashboard_report
24+
load_data_sovereignty_issues
2425

2526
report_on_data_sovereignty_only ? display_data_sovereignty_report : @report
2627
end
2728

28-
def all_dashboard_ids
29-
Superset::Dashboard::List.new().rows.map{|d| d['id']}
30-
end
29+
private
3130

3231
def display_data_sovereignty_report
3332
# filter by dashboards where
3433
# 1. A filter dataset is not part of the dashboard datasets (might be ok for some cases)
35-
# 2. There is more than one distinct dataset schema (never ok for embedded dashboards)
34+
# 2. There is more than one distinct dataset schema (never ok for embedded dashboards where the expected schema num is only one)
3635

3736
puts "Data Sovereignty Report"
3837
puts "-----------------------"
39-
puts "Invalid Dashboards: #{data_sovereignty_issues.count}"
40-
data_sovereignty_issues
38+
puts "Possible Invalid Dashboards (CONFIRMATION REQUIRED): #{@data_sovereignty_issues.count}"
39+
@data_sovereignty_issues
4140
end
4241

4342
# possible data sovereignty issues
44-
def data_sovereignty_issues
43+
def load_data_sovereignty_issues
4544
@data_sovereignty_issues ||= begin
4645
@report.map do |dashboard|
4746
reasons = []
@@ -77,7 +76,7 @@ def unknown_dataset_details(unknown_datasets)
7776
end
7877
end
7978

80-
def create_report
79+
def create_dashboard_report
8180
@report ||= begin
8281
dashboard_ids.map do |dashboard_id|
8382
dashboard = dashboard_result(dashboard_id)
@@ -86,7 +85,7 @@ def create_report
8685
dashboard_id: dashboard_id,
8786
dashboard_title: dashboard['dashboard_title'],
8887
dashboard_url: dashboard['url'],
89-
dashboard_client_tags: dashboard_client_tags(dashboard),
88+
dashboard_tags: dashboard_tags(dashboard),
9089
filters: filter_details(dashboard),
9190
charts: chart_count(dashboard),
9291
datasets: dataset_details(dashboard_id),
@@ -124,8 +123,8 @@ def dataset_details(dashboard_id)
124123
}
125124
end
126125

127-
def dashboard_client_tags(dashboard)
128-
dashboard['tags'].map { |t| t['name'] if t['name'].include?('client') }.compact.join(',') || 'None'
126+
def dashboard_tags(dashboard)
127+
dashboard['tags'].map{|t| t['name']}.join('|')
129128
end
130129

131130
def dashboard_result(dashboard_id)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Superset::Services::DashboardReport do
4+
let(:dashboard_ids) { [1, 2] }
5+
let(:service) { described_class.new(dashboard_ids: dashboard_ids) }
6+
7+
describe '#perform' do
8+
let(:dashboard_response) do
9+
{
10+
'dashboard_title' => 'Test Dashboard',
11+
'tags' => [{'name' => 'tag1'}, {'name' => 'tag2'}],
12+
'json_metadata' => {
13+
'native_filter_configuration' => [
14+
{
15+
'type' => 'NATIVE_FILTER',
16+
'targets' => [{'datasetId' => 100}]
17+
}
18+
],
19+
'chart_configuration' => [{}, {}] # 2 charts
20+
}.to_json
21+
}
22+
end
23+
24+
let(:datasets_response) do
25+
[
26+
{id: 1, schema: 'schema1', title: 'Dataset 1'},
27+
{id: 2, schema: 'schema1', title: 'Dataset 2'}
28+
]
29+
end
30+
31+
before do
32+
allow_any_instance_of(Superset::Dashboard::Get).to receive(:result).and_return(dashboard_response)
33+
allow_any_instance_of(Superset::Dashboard::Get).to receive(:url).and_return('http://example.com/dashboard/1')
34+
allow_any_instance_of(Superset::Dashboard::Datasets::List).to receive(:rows_hash).and_return(datasets_response)
35+
end
36+
37+
context 'when report_on_data_sovereignty_only is true' do
38+
it 'returns data sovereignty issues only' do
39+
result = service.perform
40+
binding.pry
41+
expect(result).to be_an(Array)
42+
expect(result).to all(include(:reasons, :dashboard))
43+
end
44+
end
45+
46+
context 'when report_on_data_sovereignty_only is false' do
47+
let(:service) { described_class.new(dashboard_ids: dashboard_ids, report_on_data_sovereignty_only: false) }
48+
49+
it 'returns full dashboard report' do
50+
result = service.perform
51+
expect(result).to be_an(Array)
52+
expect(result.first).to include(
53+
:dashboard_id,
54+
:dashboard_title,
55+
:dashboard_url,
56+
:dashboard_tags,
57+
:filters,
58+
:charts,
59+
:datasets
60+
)
61+
end
62+
end
63+
end
64+
65+
describe '#data_sovereignty_issues' do
66+
context 'when filter dataset is not in chart datasets' do
67+
let(:dashboard_response) do
68+
{
69+
'dashboard_title' => 'Test Dashboard',
70+
'tags' => [],
71+
'json_metadata' => {
72+
'native_filter_configuration' => [
73+
{
74+
'type' => 'NATIVE_FILTER',
75+
'targets' => [{'datasetId' => 999}]
76+
}
77+
],
78+
'chart_configuration' => []
79+
}.to_json
80+
}
81+
end
82+
83+
let(:datasets_response) do
84+
[
85+
{id: 1, schema: 'schema1', title: 'Dataset 1'}
86+
]
87+
end
88+
89+
before do
90+
allow_any_instance_of(Superset::Dashboard::Get).to receive(:result).and_return(dashboard_response)
91+
allow_any_instance_of(Superset::Dashboard::Get).to receive(:url).and_return('http://example.com/dashboard/1')
92+
allow_any_instance_of(Superset::Dashboard::Datasets::List).to receive(:rows_hash).and_return(datasets_response)
93+
allow_any_instance_of(Superset::Dataset::Get).to receive(:result)
94+
allow_any_instance_of(Superset::Dataset::Get).to receive(:id).and_return(999)
95+
allow_any_instance_of(Superset::Dataset::Get).to receive(:title).and_return('Unknown Dataset')
96+
end
97+
98+
it 'reports warning for unknown filter datasets' do
99+
result = service.perform
100+
expect(result.first[:reasons]).to include(a_string_matching(/WARNING: One or more filter datasets/))
101+
end
102+
end
103+
104+
context 'when multiple schemas are found' do
105+
let(:datasets_response) do
106+
[
107+
{id: 1, schema: 'schema1', title: 'Dataset 1'},
108+
{id: 2, schema: 'schema2', title: 'Dataset 2'}
109+
]
110+
end
111+
112+
before do
113+
allow_any_instance_of(Superset::Dashboard::Get).to receive(:result).and_return(
114+
{
115+
'dashboard_title' => 'Test Dashboard',
116+
'tags' => [],
117+
'json_metadata' => {
118+
'native_filter_configuration' => [],
119+
'chart_configuration' => [{}, {}]
120+
}.to_json
121+
}
122+
)
123+
allow_any_instance_of(Superset::Dashboard::Get).to receive(:url).and_return('http://example.com/dashboard/1')
124+
allow_any_instance_of(Superset::Dashboard::Datasets::List).to receive(:rows_hash).and_return(datasets_response)
125+
end
126+
127+
it 'reports error for multiple schemas' do
128+
result = service.perform
129+
expect(result.first[:reasons]).to include(a_string_matching(/ERROR: Multiple distinct chart dataset schemas/))
130+
end
131+
end
132+
end
133+
134+
135+
136+
end

0 commit comments

Comments
 (0)