-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone.rb
161 lines (147 loc) · 6.34 KB
/
clone.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# dependencies
require 'yaml'
require 'nanoid'
require 'slop'
require 'logger'
@log = Logger.new("log-#{Time.now}.txt")
@opts = Slop.parse do |o|
o.string '-y', '--yaml-files', 'the directory containing the yaml files', required: true
o.string '-s', '--source-datasource', 'the datasource to be replaced', required: true
o.string '-t', '--target-datasource', 'the datasource to replace with', required: true
o.string '-n', '--new-collection-name', 'the new name for the top collection', default: nil
o.bool '-d', '--duplicate', 'duplicate the collections', default: false
o.on '--help' do
puts o
exit
end
end
# Counters to keep track of how many entities were processed
@num_collections = 0
@num_dash = 0
@num_questions = 0
@num_models = 0
# A memory map of entity IDs
# If the duplicate param was provided, we need to remap all entity IDs
@entity_id_map = {}
def entity_id_for(entity_id, remap=true)
raise "Attempting to map for empty entity_id" if entity_id.nil? || entity_id.empty?
if @entity_id_map.has_key?(entity_id)
elsif remap
@entity_id_map[entity_id] = Nanoid.generate
@log.debug "Generated new entity ID: #{entity_id} -> #{@entity_id_map[entity_id]}"
else
# do not map to new entity id
@entity_id_map[entity_id] = entity_id
end
return @entity_id_map[entity_id]
end
def update_entity_ids(entity, type=nil, update_parent=true)
if type=="question" || type=="model" || type=="dashboard"
entity["entity_id"] = entity_id_for(entity["entity_id"]) if entity.has_key?("entity_id")
entity["serdes/meta"][0]["id"] = entity_id_for(entity["serdes/meta"][0]["id"]) if entity["serdes/meta"].is_a?(Array) && entity["serdes/meta"][0].has_key?("id")
entity["collection_id"] = entity_id_for(entity["collection_id"]) if entity.has_key?("collection_id")
end
if type=="question" || type=="model"
traverse(entity) do |node|
if node && node.is_a?(Hash) && node.has_key?("source-table") && node["source-table"].is_a?(String)
node["source-table"] = entity_id_for(node["source-table"])
end
end
end
if type=="dashboard"
# Update entity ids of dashcards
entity.has_key?("dashcards") && entity["dashcards"].each do |dashcard|
dashcard["entity_id"] = entity_id_for(dashcard["entity_id"]) if dashcard.has_key?("entity_id")
dashcard["card_id"] = entity_id_for(dashcard["card_id"]) if dashcard.has_key?("card_id") && !dashcard["card_id"].nil?
dashcard.has_key?("parameter_mappings") && dashcard["parameter_mappings"].each do |param_map|
param_map["card_id"] = entity_id_for(param_map["card_id"]) if param_map.has_key?("card_id")
traverse(param_map) do |node|
if node.is_a?(Array) && node[0] == @opts["source-datasource"]
node[0] = @opts["target-datasource"]
end
end
end
if dashcard["visualization_settings"].has_key?("click_behavior") && dashcard["visualization_settings"]["click_behavior"].has_key?("targetId")
dashcard["visualization_settings"]["click_behavior"]["targetId"] = entity_id_for(dashcard["visualization_settings"]["click_behavior"]["targetId"])
end
end
elsif type=="collection"
entity["entity_id"] = entity_id_for(entity["entity_id"]) if entity.has_key?("entity_id")
entity["serdes/meta"][0]["id"] = entity_id_for(entity["serdes/meta"][0]["id"]) if entity["serdes/meta"].is_a?(Array) && entity["serdes/meta"][0].has_key?("id")
entity["parent_id"] = entity_id_for(entity["parent_id"]) if update_parent && !entity["parent_id"].nil?
end
end
def update_datasource(entity)
@log.debug "Updating datasource for #{entity["entity_id"]}"
if entity.has_key?("database_id") && entity["database_id"] == @opts["source-datasource"]
entity["database_id"] = @opts["target-datasource"]
end
if entity.has_key?("dataset_query") && entity["dataset_query"].has_key?("database") && entity["dataset_query"]["database"] == @opts["source-datasource"]
entity["dataset_query"]["database"] = @opts["target-datasource"]
end
for key in ["table_id", "dataset_query"] do
if entity.has_key?(key) && entity[key].is_a?(Array) && entity[key][0] == @opts["source-datasource"]
entity[key][0] = @opts["target-datasource"]
end
end
traverse(entity) do |node|
if node.is_a?(Array) && node[0] == @opts["source-datasource"]
node[0] = @opts["target-datasource"]
end
end
end
def traverse(obj,parent=nil, &blk)
case obj
when Hash
blk.call(obj,parent) # alberto
obj.each do |k,v|
blk.call(k,parent)
# Pass hash key as parent
traverse(v,k, &blk)
end
when Array
blk.call(obj,parent) # alberto
obj.each {|v| traverse(v, parent, &blk) }
else
blk.call(obj,parent)
end
end
## Update top parent collection
Dir.glob("#{@opts["yaml-files"]}/collections/*/*.yaml") do |yaml_file|
@log.debug "Processing top level collection: #{yaml_file}"
entity = YAML.load_file(yaml_file)
update_entity_ids(entity, "collection", false) if @opts[:duplicate]
# Update name and slug
unless @opts["new-collection-name"].nil? || @opts["new-collection-name"].empty?
entity["name"] = @opts["new-collection-name"]
entity["slug"] = @opts["new-collection-name"].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
entity["serdes/meta"][0]["label"] = @opts["new-collection-name"].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
@num_collections+=1
File.write(yaml_file, entity.to_yaml)
@log.debug "Wrote #{yaml_file}"
end
## Update everything else
Dir.glob("#{@opts["yaml-files"]}/collections/*/*/**/*.yaml") do |yaml_file|
@log.debug "Processing #{yaml_file}"
entity = YAML.load_file(yaml_file)
if entity["type"]=="question"
update_entity_ids(entity, "question") if @opts[:duplicate]
update_datasource(entity)
@num_questions+=1
elsif entity["type"]=="model"
update_entity_ids(entity, "model") if @opts[:duplicate]
update_datasource(entity)
@num_models+=1
elsif entity["serdes/meta"][0]["model"]=="Dashboard"
update_entity_ids(entity, "dashboard") if @opts[:duplicate]
@num_dash+=1
elsif entity["serdes/meta"][0]["model"]=="Collection"
update_entity_ids(entity, "collection") if @opts[:duplicate]
@num_collections+=1
end
File.write(yaml_file, entity.to_yaml)
@log.debug "Wrote #{yaml_file}"
end
@log.debug "Processed #{@num_collections} collections, #{@num_dash} dashboards, #{@num_questions} questions."
@log.debug @entity_id_map.to_yaml