Module: GoodData::Bam::Generators::Etl

Includes:
GoodData::Bam, CloudConnect
Defined in:
lib/generators/etl.rb

Constant Summary

Constants included from GoodData::Bam

BAM_DEFINED_GRAPHS_ROOT, BAM_ROOT, GoodData::Bam::GENERATOR_ROOT, REPO_ROOT, TEMPLATE_ROOT

Class Method Summary collapse

Methods included from GoodData::Bam

flow

Class Method Details

.create_es_downloading_graph(file, tap, options = {}) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/generators/etl.rb', line 272

def self.create_es_downloading_graph(file, tap, options={})
  s3_backup = options[:params][:s3_backup]

   = Metadata.(tap)
   = Metadata.(tap)
  id = tap[:id]

  File.open(file, "w") do |file|
    builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
    builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
    builder.Graph({
      :name => "GoodData EventStore Downloader"
    }) do
      builder.Global do
        CloudConnect::Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})

        builder.Metadata({:id   => "es_metadata"}) do |builder|
          CloudConnect::Helpers::(builder, )
        end

        builder.Metadata({:id   => "output_metadata"}) do |builder|
          CloudConnect::Helpers::(builder, )
        end
      end

      builder.Phase(:number => 0) do
        stuff, read_map, time_frames = create_es_readmap(, tap)
        
        if time_frames.blank?
          Core::build_node2(builder, Nodes.es_reader2({:name => "ES Reader", :id => "es", :entityFieldsMapping => stuff.to_json , :readMap => read_map.to_json}))
        else
          Core::build_node2(builder, Nodes.es_reader2({:name => "ES Reader", :id => "es", :entityFieldsMapping => stuff.to_json , :readMap => read_map.to_json, :timeIntervals => time_frames.to_json}))
        end
        
        
        # Skipping the one row that is added by ES
        transformation_source = if Metadata::has_field?(, "Snapshot")
          "function integer transform() {\n" + (Tap.source_target_mappging(tap).map {|t| "$out.0.#{t.last} = $in.0.#{t.first};"}.join("\n")) + "\n$out.0.Snapshot = date2str(dateAdd(str2date(\"1900-01-01\", \"yyyy-MM-dd\"),  str2long($in.0.Snapshot), day), \"yyyy-MM-dd\");\nreturn OK;\n}"
        else
          "long count = 0;\nfunction integer transform() {\ncount = count + 1; if (count == 1) {return SKIP;}\n" + (Tap.source_target_mappging(tap).map {|t| "$out.0.#{t.last} = $in.0.#{t.first};"}.join("\n")) + "\nreturn OK;\n}"
        end
        Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "reformat", :transformation => transformation_source}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "reformat:0", :fromNode => "es:0", :metadata => "es_metadata"}))

        # Filter out deleted Records
        if tap[:has_deleted_records] == true
          filter_deleted_function = "function integer transform() {\nif ($in.0.IsDeleted == \"true\") {return SKIP;} else {$out.0.* = $in.0.*; return OK;};\n}"
          Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "filter_deleted", :transformation => filter_deleted_function}))
          Core::build_node2(builder, Nodes.edge2({:toNode => "filter_deleted:0", :fromNode => "reformat:0", :metadata => "output_metadata"}))
          Core::build_node2(builder, Nodes.edge2({:toNode => "dummy_reformat_1:0", :fromNode => "filter_deleted:0", :metadata => "output_metadata"}))
        else
          Core::build_node2(builder, Nodes.edge2({:toNode => "dummy_reformat_1:0", :fromNode => "reformat:0", :metadata => "output_metadata"}))
        end

        Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "dummy_reformat_1", :transformation => Nodes::MAP_ALL}))
        
        # Propagating a value from latest snapshot to all snapshots if necessary
        if tap[:propagate_latest_value_for] && !tap[:propagate_latest_value_for].empty?

          retain_transform = <<HEREDOC
#{tap[:propagate_latest_value_for].map {|f| "string " + f + " = \"\";"}.join("\n")}
string id = null;
string previousId = null;

  function integer transform() {
  	$out.0.* = $in.0.*;
  	id = $in.0.Id;
  	if (id != previousId) {
  		#{tap[:propagate_latest_value_for].map {|f| f + " = $in.0." + f + ";"}.join("\n")}
  	}
  	#{tap[:propagate_latest_value_for].map {|f| "$out.0." + f + " = " + f + ";"}.join("\n")}
  	previousId = id;
  	return ALL;
  }
HEREDOC
          fields_to_propagate = tap[:propagate_latest_value_for]
          propagate_values_function = "string lastId;\nfunction integer transform() {\nif (lastId !=$in.0.Id) {$in.0.*=$out.0.*;} else {$in.0.*=$out.0.*; #{fields_to_propagate.map{|f| "$out.0.#{f} = \"XXXXX\""}.join("\n")}};lastId=$in.0.Id \nreturn OK;\n}"
          Core::build_node2(builder, Nodes.sort2({:sortKey => "Id(a);Snapshot(d)", :id => "sort_for_history_retain"}));
          Core::build_node2(builder, Nodes.edge2({:toNode => "sort_for_history_retain:0", :fromNode => "dummy_reformat_1:0", :metadata => "output_metadata"}))
          Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "retain_values_reformat", :transformation => retain_transform}))
          Core::build_node2(builder, Nodes.edge2({:toNode => "retain_values_reformat:0", :fromNode => "sort_for_history_retain:0", :metadata => "output_metadata"}))
          Core::build_node2(builder, Nodes.edge2({:toNode => "dummy_reformat_2:0", :fromNode => "retain_values_reformat:0", :metadata => "output_metadata"}))
        else
          Core::build_node2(builder, Nodes.edge2({:toNode => "dummy_reformat_2:0", :fromNode => "dummy_reformat_1:0", :metadata => "output_metadata"}))
        end
        
        Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "dummy_reformat_2", :transformation => Nodes::MAP_ALL}))
        
        Core::build_node2(builder, Nodes.copy2({:name => "Copy", :id => "copy"}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "copy:0", :fromNode => "dummy_reformat_2:0", :metadata => "output_metadata"}))
        Core::build_node2(builder, Nodes.writer2({:name => "CSV Writer", :id => "csv", :fileURL => "${PROJECT}/data/#{tap[:id]}.csv", :outputFieldNames => "true", :makeDirs => "true"}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "csv:0", :fromNode => "copy:0", :metadata => "output_metadata"}))
      end
    end
  end
end

.create_es_readmap(metadata, tap) ⇒ Object



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
# File 'lib/generators/etl.rb', line 24

def self.create_es_readmap(, tap)
  e = Es::Entity.new("x", {
    :file => "none",
    :fields => [:fields].map do |f|
      name = f[:name]
      if name == "Id"
        Es::Field.new('Id', 'recordid')
      elsif name == "Snapshot"
        Es::SnapshotField.new('Snapshot', 'snapshot')
      else
        Es::Field.new(name, 'attribute')
      end
    end,
    :timeframe => Es::Timeframe::parse("latest")
  })


  stuff = {
    :entityName => tap[:id],
    :fieldsMapping => tap[:fields].inject({}) do |memo, field|
      name = field[:name]
      memo[name] = name
      memo
    end,
    :eventStoreFieldToTypeMapping => tap[:fields].inject({}) do |memo, field|
      name = field[:name]
      if name == "Id"
        memo[name] = "recordid"
      elsif name == "Snapshot"
        memo[name] = "snapshot"
      else
        memo[name] = "attribute"
      end

      memo
    end,
    :outputMetadataName => "es_metadata"
  }

  read_map = {
    :columns => e.to_extract_fragment('pid')["readTask"]["readMap"].first[:columns],
    :populates => e.to_extract_fragment('pid')["readTask"]["readMap"].first[:populates]
  }
  if tap.has_key?(:time_intervals)
    [stuff, read_map, tap[:time_intervals]]
  else
    [stuff, read_map, nil]
  end
  
end

.create_file_downloading_graph(file, tap, options = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/generators/etl.rb', line 173

def self.create_file_downloading_graph(file, tap, options={})
  s3_backup = options[:params][:s3_backup]

  File.open(file, "w") do |file|
    builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
    builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
    builder.Graph({
      :name => "File Downloader"
    }) do
      builder.Global do
        CloudConnect::Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})

        builder.Metadata({:id   => "file_metadata"}) do |builder|
          CloudConnect::Helpers::(builder, Metadata.(tap))
        end
        builder.Metadata({:id   => "clover_metadata"}) do |builder|
          CloudConnect::Helpers::(builder, Metadata.(tap))
        end
      end

      builder.Phase(:number => 0) do
        Core::build_node2(builder, Nodes.reader2(Utils.merge_only_values({:name => "reader", :id => "reader", :fileURL => tap[:source]}, tap, [:charset])))

        Core::build_node2(builder, Nodes.copy2({:name => "copy", :id => "copy"}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "copy:0", :fromNode => "reader:0", :metadata => "file_metadata"}))

        transformation_source = "function integer transform() {\n" + (Tap.source_target_mappging(tap).map {|t| "$out.0.#{t.last} = $in.0.#{t.first};"}.join("\n")) + "\nreturn OK;\n}"
        Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "reformat", :transformation => transformation_source}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "reformat:0", :fromNode => "copy:0", :metadata => "file_metadata"}))

        Core::build_node2(builder, Nodes.writer2({:name => "CSV Writer", :id => "csv", :fileURL => "data/#{tap[:id]}.csv", :outputFieldNames => "true"}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "csv:0", :fromNode => "reformat:0", :metadata => "clover_metadata"}))

        if s3_backup then Core::build_node2(builder, Nodes.writer2({:name => "s3 Writer", :id => "s3", :fileURL => "bucket", :outputFieldNames => true})) end
        if s3_backup then Core::build_node2(builder, Nodes.edge2({:toNode => "s3:0", :fromNode => "copy:1", :metadata => "file_metadata"})) end
      end
    end
  end
end

.create_file_uploading_graph(file, sink, input_name, state = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/generators/etl.rb', line 75

def self.create_file_uploading_graph(file, sink, input_name, state={})
  File.open(file, "w") do |file|
    builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
    builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
    builder.Graph({
      :name => "GoodData Data loader"
    }) do
      builder.Global do
        CloudConnect::Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})
        builder.Metadata({:id => "load_metadata"}) do |builder| 
          CloudConnect::Helpers::(builder, state[:metadata][input_name])
        end
      end
      builder.Phase(:number => 0) do
        gd_dataset = sink[:gd_name] || sink[:id]
        to_svinstvo = GoodData::CloverGenerator::build_gd_dataset_loader_json(sink)
        Core::build_node2(builder, Nodes.reader2({:name => "CSV Loader", :id => "loader", :fileURL => "${PROJECT}/data/#{input_name}.csv"}))
        Core::build_node2(builder, Nodes.writer2({:name => "GD Loader", :id => "writer", :fileURL => sink[:target]}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "writer:0", :fromNode => "loader:0", :metadata => "load_metadata"}))
      end
    end
  end
end

.create_gd_uploading_graph(file, sink, input_name, state = {}) ⇒ Object



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
162
163
164
165
166
167
168
169
170
171
# File 'lib/generators/etl.rb', line 99

def self.create_gd_uploading_graph(file, sink, input_name, state={})
  File.open(file, "w") do |file|
    builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
    builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
    builder.Graph({
      :name => "GoodData Data loader"
    }) do
      builder.Global do
        CloudConnect::Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})
        builder.Metadata({:id => "load_metadata"}) do |builder| 
          CloudConnect::Helpers::(builder, state[:metadata][input_name])
        end
        builder.Metadata({:id => "gd_sink_metadata"}) do |builder| 
          CloudConnect::Helpers::(builder, CloudConnect::Helpers::(state[:metadata][input_name], sink))
        end
        
      end

      builder.Phase(:number => 0) do
        gd_dataset = sink[:gd_name] || sink[:id]
        to_svinstvo = GoodData::CloverGenerator::build_gd_dataset_loader_json(sink)
        Core::build_node2(builder, Nodes.reader2({:name => "CSV Loader", :id => "loader", :fileURL => "${PROJECT}/data/#{input_name}.csv"}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "gd_load:0", :fromNode => "reformat:0", :metadata => "gd_sink_metadata"}))

        transform = <<HEREDOC
function string gd_date_compliance(string a_date) {
  if (a_date != null && a_date != "" && left(a_date, 4) < "1950") {
    return '1950-01-01';
  }
  if (a_date != null && a_date != "" && left(a_date, 4) > "2049") {
    return '2049-01-01';
  }
  return a_date;
}

function decimal deal_with_scientific_notation(long a_number) {
  if (isnull(a_number)) {
    return null;
  } else {
    return str2decimal(num2str(a_number));
  }
}


function decimal deal_with_scientific_notation(string a_number) {
  if (isnull(a_number) || a_number == "") {
    return null;
  } else {
    return floor(str2decimal(a_number)*1000D)/1000D;
  }
}

function integer transform() {
    $out.0.* = $in.0.*;
    #{Sink.get_date_fields(sink).map do |f|
      "$out.0.#{f[:meta]} = gd_date_compliance($in.0.#{f[:meta]});"
    end.join("\n")}

    #{Sink.get_fact_fields(sink).map do |f|
      "$out.0.#{f[:meta]} = deal_with_scientific_notation($in.0.#{f[:meta]});"
    end.join("\n")}

    return ALL;
}
HEREDOC

        Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "reformat", :transformation => transform}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "reformat:0", :fromNode => "loader:0", :metadata => "load_metadata"}))
        Core::build_node2(builder, Nodes.gd_loader2({:name => "GD Loader", :id => "gd_load", :dataset => "dataset.#{gd_dataset}", :datasetFieldMappings => to_svinstvo.to_json}))
      end
    end
  end
end

.create_sf_downloading_graph(file, tap, options = {}) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/generators/etl.rb', line 213

def self.create_sf_downloading_graph(file, tap, options={})
  s3_backup = options[:params][:s3_backup]

  File.open(file, "w") do |file|
    builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
    builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
    builder.Graph({
      :name => "Goodsales Salesforce Downloader"
    }) do
      builder.Global do

        builder.Metadata({:id  => "clover_metadata"}) do |builder|
          CloudConnect::Helpers::(builder, Metadata.(tap))
        end

        builder.Metadata({:id   => "sf_metadata"}) do |builder|
          CloudConnect::Helpers::(builder, Metadata.(tap))
        end

        CloudConnect::Helpers::sf_connection(builder, {})
        CloudConnect::Helpers::property(builder, {:id => "SFDC_CLIENT_ID", :value => "gooddata/gooddata/"})
        CloudConnect::Helpers::property(builder, {:id => "SFDC_LOGIN_HOSTNAME", :value => options[:params][:sf_server] || "login.salesforce.com"})
        CloudConnect::Helpers::property(builder, {:id => "SFDC_NAME", :value => "Salesforce connection"})
        CloudConnect::Helpers::property(builder, {:id => "SFDC_PASSWORD", :value => options[:params][:sf_password]})
        CloudConnect::Helpers::property(builder, {:id => "SFDC_TOKEN", :value => options[:params][:sf_token]})
        CloudConnect::Helpers::property(builder, {:id => "SFDC_USERNAME", :value => options[:params][:sf_login]})
        CloudConnect::Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})
      end

      builder.Phase(:number => 0) do
        fields = tap[:fields]
        mapping = "{\"xmlFieldsMapping\":{\"xmlFields\":["
        add = fields.map do |f|
          "{\"xmlFieldMapping\":{\"name\":\"#{f[:name]}\",\"label\":\"#{f[:label]}\",\"xmlPath\":\"#{f[:name]}\",\"metadataField\":\"#{f[:name]}\"}}"
        end

        stuff = mapping + add.join(",") + "]}}"
        mandatory_fields = fields.reject {|f| f[:is_mandatory] == false }.map {|f| f[:name] + ";"}.join

        Core::build_node2(builder, Nodes.sfdc_reader2({:name => "SF Writer", :id => "sf", :soql => CloudConnect::Helpers::generate_select(tap), :sfdcConnection => "SFDC", :fieldsMapping => stuff, :mandatoryFields => mandatory_fields}))

        Core::build_node2(builder, Nodes.copy2({:name => "copy", :id => "copy"}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "copy:0", :fromNode => "sf:0", :metadata => "sf_metadata"}))

        transformation_source = "function integer transform() {\n" + (Tap.source_target_mappging(tap).map {|t| "$out.0.#{t.last} = $in.0.#{t.first};"}.join("\n")) + "\nreturn OK;\n}"
        Core::build_node2(builder, Nodes.reformat2({:name => "Reformat", :id => "reformat", :transformation => transformation_source}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "reformat:0", :fromNode => "copy:0", :metadata => "sf_metadata"}))

        Core::build_node2(builder, Nodes.writer2({:name => "CSV Writer", :id => "csv", :fileURL => "data/#{tap[:id]}.csv", :outputFieldNames => "true"}))
        Core::build_node2(builder, Nodes.edge2({:toNode => "csv:0", :fromNode => "reformat:0", :metadata => "clover_metadata"}))
        if s3_backup
          Core::build_node2(builder, Nodes.writer2({:name => "s3 Writer", :id => "s3", :fileURL => "bucket", :outputFieldNames => true}))
          Core::build_node2(builder, Nodes.edge2({:toNode => "s3:0", :fromNode => "copy:1", :metadata => "sf_metadata"}))
        end
      end
    end
  end
end

.generate(home, params = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/etl.rb', line 10

def self.generate(home, params={})

  project = GoodData::Bam::Project.build_project(home, params)
  fail "There are no flows to generate from" if project[:flows].empty?

  params = params.merge({
    :home => home
  })

  v = Compiler::EtlVisitor.new
  v.visit(project, params)

end