|
| 1 | +# A validation checker for comparing dashboards. |
| 2 | +# This class is used to compare two dashboards by their datasets, charts, native filters and cross filters. |
| 3 | +# Output is displayed in a table format to the ruby console |
| 4 | +# |
| 5 | +# Usage: Superset::Dashboard::Compare.new(first_dashboard_id: 322, second_dashboard_id: 347).perform |
| 6 | +# |
| 7 | +module Superset |
| 8 | + module Dashboard |
| 9 | + class Compare |
| 10 | + |
| 11 | + attr_reader :first_dashboard_id, :second_dashboard_id |
| 12 | + |
| 13 | + def initialize(first_dashboard_id: , second_dashboard_id: ) |
| 14 | + @first_dashboard_id = first_dashboard_id |
| 15 | + @second_dashboard_id = second_dashboard_id |
| 16 | + end |
| 17 | + |
| 18 | + |
| 19 | + def perform |
| 20 | + raise "Error: first_dashboard_id integer is required" unless first_dashboard_id.present? && first_dashboard_id.is_a?(Integer) |
| 21 | + raise "Error: second_dashboard_id integer is required" unless second_dashboard_id.present? && second_dashboard_id.is_a?(Integer) |
| 22 | + |
| 23 | + list_datasets |
| 24 | + list_charts |
| 25 | + list_native_filters |
| 26 | + list_cross_filters |
| 27 | + |
| 28 | + end |
| 29 | + |
| 30 | + def first_dashboard |
| 31 | + @first_dashboard ||= Get.new(first_dashboard_id).result |
| 32 | + end |
| 33 | + |
| 34 | + def second_dashboard |
| 35 | + @second_dashboard ||= Get.new(second_dashboard_id).result |
| 36 | + end |
| 37 | + |
| 38 | + def list_datasets |
| 39 | + puts "\n ====== DASHBOARD DATASETS ====== " |
| 40 | + Superset::Dashboard::Datasets::List.new(first_dashboard_id).list |
| 41 | + Superset::Dashboard::Datasets::List.new(second_dashboard_id).list |
| 42 | + end |
| 43 | + |
| 44 | + def list_charts |
| 45 | + puts "\n ====== DASHBOARD CHARTS ====== " |
| 46 | + Superset::Dashboard::Charts::List.new(first_dashboard_id).list |
| 47 | + puts '' |
| 48 | + Superset::Dashboard::Charts::List.new(second_dashboard_id).list |
| 49 | + end |
| 50 | + |
| 51 | + def list_native_filters |
| 52 | + puts "\n ====== DASHBOARD NATIVE FILTERS ====== " |
| 53 | + list_native_filters_for(first_dashboard) |
| 54 | + puts '' |
| 55 | + list_native_filters_for(second_dashboard) |
| 56 | + end |
| 57 | + |
| 58 | + def list_cross_filters |
| 59 | + puts "\n ====== DASHBOARD CROSS FILTERS ====== " |
| 60 | + list_cross_filters_for(first_dashboard) |
| 61 | + puts '' |
| 62 | + list_cross_filters_for(second_dashboard) |
| 63 | + end |
| 64 | + |
| 65 | + def native_filter_configuration(dashboard_result) |
| 66 | + rows = [] |
| 67 | + JSON.parse(dashboard_result['json_metadata'])['native_filter_configuration'].each do |filter| |
| 68 | + filter['targets'].each {|t| rows << [ t['column']['name'], t['datasetId'] ] } |
| 69 | + end |
| 70 | + rows |
| 71 | + end |
| 72 | + |
| 73 | + def list_native_filters_for(dashboard_result) |
| 74 | + puts Terminal::Table.new( |
| 75 | + title: [dashboard_result['id'], dashboard_result['dashboard_title']].join(' - '), |
| 76 | + headings: ['Filter Name', 'Dataset Id'], |
| 77 | + rows: native_filter_configuration(dashboard_result) |
| 78 | + ) |
| 79 | + end |
| 80 | + |
| 81 | + def cross_filter_configuration(dashboard_result) |
| 82 | + JSON.parse(dashboard_result['json_metadata'])['chart_configuration'].map {|k, v| [ v['id'], v['crossFilters'].to_s ] } |
| 83 | + end |
| 84 | + |
| 85 | + def list_cross_filters_for(dashboard_result) |
| 86 | + puts Terminal::Table.new( |
| 87 | + title: [dashboard_result['id'], dashboard_result['dashboard_title']].join(' - '), |
| 88 | + headings: ['Chart Id', 'Cross Filter Config'], |
| 89 | + rows: cross_filter_configuration(dashboard_result) |
| 90 | + ) |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments