gd_bam 0.0.15 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/README.md +313 -5
  2. data/bin/bam +126 -48
  3. data/lib/bam/version.rb +1 -1
  4. data/lib/bam.rb +51 -0
  5. data/lib/base/errors.rb +15 -0
  6. data/lib/base/flow.rb +37 -0
  7. data/lib/base/graph.rb +23 -0
  8. data/lib/base/metadata.rb +107 -0
  9. data/lib/base/project.rb +95 -0
  10. data/lib/base/repo.rb +35 -0
  11. data/lib/base/sink.rb +44 -0
  12. data/lib/base/step.rb +47 -0
  13. data/lib/base/tap.rb +167 -0
  14. data/lib/base/taps.rb +19 -0
  15. data/lib/cloud_connect/dsl/cc.rb +42 -0
  16. data/lib/cloud_connect/dsl/es_helpers.rb +49 -0
  17. data/lib/cloud_connect/dsl/helpers.rb +199 -0
  18. data/lib/{nodes → cloud_connect/dsl}/nodes.rb +106 -16
  19. data/lib/cloud_connect/dsl/sf_helpers.rb +39 -0
  20. data/lib/cloud_connect/dsl/structure_helpers.rb +94 -0
  21. data/lib/commands/commands.rb +110 -0
  22. data/lib/commands/deployment.rb +217 -0
  23. data/lib/commands/docs_commands.rb +41 -0
  24. data/lib/commands/gd_commands.rb +95 -0
  25. data/lib/commands/scaffold_commands.rb +103 -0
  26. data/lib/commands/sf_commands.rb +37 -0
  27. data/lib/commands/validators.rb +19 -0
  28. data/lib/compatibility.rb +19 -0
  29. data/lib/compiler/compiler.rb +76 -0
  30. data/lib/compiler/etl_visitor.rb +165 -0
  31. data/lib/dsl/dsl.rb +125 -0
  32. data/lib/generators/downloaders.rb +449 -0
  33. data/lib/generators/etl.rb +261 -0
  34. data/lib/generators/validators.rb +445 -0
  35. data/lib/graphs/docentize.grf +1 -1
  36. data/lib/graphs/dummy.grf +1 -1
  37. data/lib/graphs/goodsales_v2/docentize.grf +47 -0
  38. data/lib/graphs/goodsales_v2/dummy.grf +46 -0
  39. data/lib/graphs/goodsales_v2/load_history.grf +579 -0
  40. data/lib/graphs/goodsales_v2/process_account.grf +47 -0
  41. data/lib/graphs/goodsales_v2/process_activity.grf +222 -0
  42. data/lib/graphs/goodsales_v2/process_activity_dim.grf +88 -0
  43. data/lib/graphs/goodsales_v2/process_activity_owner.grf +48 -0
  44. data/lib/graphs/goodsales_v2/process_forecast.grf +20 -0
  45. data/lib/graphs/goodsales_v2/process_opp_records.grf +84 -0
  46. data/lib/graphs/goodsales_v2/process_opportunity.grf +46 -0
  47. data/lib/graphs/goodsales_v2/process_opportunity_line_item.grf +171 -0
  48. data/lib/graphs/goodsales_v2/process_opportunity_snapshot.grf +94 -0
  49. data/lib/graphs/goodsales_v2/process_owner.grf +48 -0
  50. data/lib/graphs/goodsales_v2/process_stage.grf +51 -0
  51. data/lib/graphs/goodsales_v2/process_stage_history.grf +184 -0
  52. data/lib/graphs/goodsales_v2/process_velocity_duration.grf +140 -0
  53. data/lib/graphs/process_account.grf +1 -1
  54. data/lib/graphs/process_activity.grf +1 -1
  55. data/lib/graphs/process_activity_dim.grf +1 -1
  56. data/lib/graphs/process_activity_owner.grf +1 -1
  57. data/lib/graphs/process_forecast.grf +1 -1
  58. data/lib/graphs/process_opp_records.grf +1 -1
  59. data/lib/graphs/process_opportunity.grf +1 -1
  60. data/lib/graphs/process_opportunity_line_item.grf +1 -1
  61. data/lib/graphs/process_opportunity_snapshot.grf +1 -1
  62. data/lib/graphs/process_owner.grf +1 -1
  63. data/lib/graphs/process_stage.grf +1 -1
  64. data/lib/graphs/process_stage_history.grf +1 -1
  65. data/lib/graphs/process_velocity_duration.grf +1 -1
  66. data/lib/nodes/clover_gen.rb +59 -946
  67. data/lib/nodes/dependency.rb +95 -96
  68. data/lib/runtime.rb +7 -648
  69. data/lib/utils/utils.rb +66 -0
  70. data/templates/flow.rb.erb +7 -6
  71. data/templates/join_template.grf.erb +1 -1
  72. data/templates/reformat_template.grf.erb +1 -1
  73. data/templates/sink.json.erb +28 -0
  74. data/templates/tap.json.erb +3 -5
  75. data/templates/workspace.prm.erb +4 -0
  76. metadata +50 -8
  77. data/lib/contract_checkers/contract_checkers.rb +0 -53
  78. data/lib/dsl/project_dsl.rb +0 -259
  79. data/lib/repo/1_config.json +0 -8
  80. data/templates/dataset.json.erb +0 -13
  81. data/templates/source.json.erb +0 -22
@@ -0,0 +1,449 @@
1
+ module GoodData
2
+ module Bam
3
+ module Generators
4
+ module Downloaders
5
+
6
+ include GoodData::Bam
7
+ include GoodData::CloudConnect
8
+
9
+ def self.create_incremental_downloader_run_graph(file, taps, options={})
10
+
11
+ File.open(file, "w") do |file|
12
+ builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
13
+ builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
14
+ builder.Graph({
15
+ :name => "Run graph"
16
+ }) do
17
+ builder.Global do
18
+ Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})
19
+ Helpers::property_file(builder, {:id => "params_params", :fileURL => "params.prm"})
20
+ Helpers::create_trash_meta(builder)
21
+ Helpers::create_lookup_meta(builder)
22
+ Helpers::create_file_list_meta(builder)
23
+ Helpers::create_run_graph_failure_metadata(builder)
24
+
25
+ Core::build_node2(builder, Nodes.lookup2({:name => "gdLookup0", :id => "gdLookup0", :type => Nodes::GD_LOOKUP, :metadata => "lookup_metadata"}))
26
+
27
+ end
28
+ phase = 0
29
+ sf_taps = Taps.get_salesforce(taps)
30
+
31
+ sf_taps.each do |tap|
32
+ module_name = tap[:object]
33
+ file = tap[:id] || module_name
34
+ dataset = file || module_name
35
+
36
+
37
+ normalize_code = <<HEREDOC
38
+ boolean done = false;
39
+ function integer count() {
40
+
41
+ if (indexOf($in.0.key, "#{dataset}_LAST_RUN") != -1) {
42
+ return 4;
43
+ }
44
+ else {
45
+ return 0;
46
+ }
47
+ }
48
+
49
+ string last_run = null;
50
+ string end_date = null;
51
+
52
+ function integer transform(integer idx) {
53
+ if (last_run == null) {
54
+ last_run = $in.0.value;
55
+ }
56
+ if (end_date == null) {
57
+ end_date = jodaDate2str(today(), "yyyy-MM-dd'T'HH:mm:ss.SSSZZ", "en_US", 'UTC');
58
+ }
59
+
60
+
61
+ if (idx == 1) {
62
+ $out.0.all = "#{dataset}_TRUNCATE_DATE=" + jodaDate2str(jodaStr2date(last_run, ["yyyy-MM-dd'T'HH:mm:ss.SSSZZ"], 'en_US', 'UTC', 'UTC'), "yyyy-MM-dd HH:mm:ss", 'en_US', 'UTC');
63
+ } else if (idx == 2) {
64
+ $out.0.all = "#{dataset}_START=" + last_run;
65
+ } else if (idx == 3) {
66
+ $out.0.all = "#{dataset}_END=" + end_date;
67
+ } else {
68
+ $out.0.all = "#{dataset}_LAST_RUN=" + end_date;
69
+ }
70
+
71
+ return OK;
72
+ }
73
+
74
+ HEREDOC
75
+
76
+
77
+
78
+
79
+ builder.Phase(:number => phase += 1) do
80
+ generate_func = <<HEREDOC
81
+ function integer generate() {
82
+ $out.0.key = "#{dataset}_LAST_RUN";
83
+ $out.0.value = "1970-01-01T00:00:00.000+00:00";
84
+ return OK;
85
+ }
86
+ HEREDOC
87
+
88
+ join_func = <<HEREDOC
89
+ function integer transform() {
90
+ $out.0.key = nvl2($in.1.value, $in.1.key, $in.0.key);
91
+ $out.0.value = nvl2($in.1.value, $in.1.value, $in.0.value);
92
+ return OK;
93
+ }
94
+ HEREDOC
95
+
96
+ Core::build_node2(builder, Nodes.data_generator2({:name => "generator_#{dataset}", :id => "generator_#{dataset}", :generate => generate_func}))
97
+ Core::build_node2(builder, Nodes.lookup_reader_writer2({:lookupTable => "gdLookup0", :id => "gd_lookup_reader_#{dataset}" }))
98
+ Core::build_node2(builder, Nodes.hash_join2({:id => "join_#{dataset}", :joinType => "leftOuter", :joinKey => "$key=$key", :transformation => join_func}))
99
+
100
+ Core::build_node2(builder, Nodes.edge2({:toNode => "join_#{dataset}:0", :fromNode => "generator_#{dataset}:0", :metadata => "lookup_metadata"}))
101
+ Core::build_node2(builder, Nodes.edge2({:toNode => "join_#{dataset}:1", :fromNode => "gd_lookup_reader_#{dataset}:0", :metadata => "lookup_metadata"}))
102
+
103
+ Core::build_node2(builder, Nodes.normalizer2({:name => "normalizer_#{dataset}", :id => "normalizer_#{dataset}", :normalize => normalize_code }))
104
+ Core::build_node2(builder, Nodes.edge2({:toNode => "normalizer_#{dataset}:0", :fromNode => "join_#{dataset}:0", :metadata => "lookup_metadata"}))
105
+
106
+ Core::build_node2(builder, Nodes.writer2({:quotedStrings => "false", :name => "params_writer_#{dataset}", :id => "params_writer_#{dataset}", :fileURL => "params.prm", :outputFieldNames => "false", :append => "true"}))
107
+ Core::build_node2(builder, Nodes.edge2({:toNode => "params_writer_#{dataset}:0", :fromNode => "normalizer_#{dataset}:0", :metadata => "trash_metadata"}))
108
+ end
109
+ end
110
+ builder.Phase(:number => phase += 1) do
111
+ if !sf_taps.empty?
112
+ Core::build_node2(builder, Nodes.run_graph2({:guiName => "incremental", :name => "incremental_downloaders", :id => "downlaoders", :graphName => "graphs/incremental.grf"}))
113
+ end
114
+ end
115
+
116
+ file_taps = Taps.get_file(taps)
117
+
118
+ file_taps.each do |tap|
119
+ source = tap[:source]
120
+ id = tap[:id]
121
+
122
+
123
+ reformat_func = <<HEREDOC
124
+
125
+ function integer transform() {
126
+ $out.0.filePath = replace($in.0.filePath, "${GDC_WEBDAV_HOST}", replace(replace(\"${GD_LOGIN}\",\"@\",\"%40\"),\"\\\\+\",\"%2B\") + ":${GD_PASSWORD}@${GDC_WEBDAV_HOST}");
127
+ $out.0.fileName = $in.0.fileName;
128
+ return ALL;
129
+ }
130
+ HEREDOC
131
+
132
+ builder.Phase(:number => phase += 1) do
133
+ Core::build_node2(builder, Nodes.file_list2({:id => "#{id}_file_list", :name => "#{id}_file_list", :dataPolicy => "Strict", :baseURL => "#{tap[:source]}", :output_mapping => Nodes::MAP_ALL}))
134
+ Core::build_node2(builder, Nodes.reformat2({:name => "#{id} Reformat", :id => "#{id}_reformat", :transformation => reformat_func}))
135
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{id}_reformat:0", :fromNode => "#{id}_file_list:0", :metadata => "file_list"}))
136
+ Core::build_node2(builder, Nodes.writer2({:name => "PARAMS CSV Writer", :id => "#{id}_writer", :fileURL => "data/#{id}_files_to_read.csv"}))
137
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{id}_writer:0", :fromNode => "#{id}_reformat:0", :metadata => "file_list"}))
138
+ end
139
+
140
+ builder.Phase(:number => phase += 1) do
141
+ ctl = "function integer generate() {$out.0.all = \"#{id}_SKIP_LINES=0\";return OK;}"
142
+ Core::build_node2(builder, Nodes.data_generator2({:name => "#{id}_generator", :id => "#{id}_generator", :generate => ctl}))
143
+ Core::build_node2(builder, Nodes.writer2({:name => "PARAMS CSV Writer", :id => "#{id}_csv_writer", :fileURL => "#{id}_counter.prm", :outputFieldNames => "false", :quotedStrings => "false"}))
144
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{id}_csv_writer:0", :fromNode => "#{id}_generator:0", :metadata => "trash_metadata"}))
145
+ end
146
+
147
+ subgraph_reformat_func = <<HEREDOC
148
+
149
+ function integer transform() {
150
+ $out.0.all = "graphs/#{id}_loop.grf";
151
+ return ALL;
152
+ }
153
+ HEREDOC
154
+
155
+ fail_reformat = <<HEREDOC
156
+ function integer transform() {
157
+ raiseError("Loop failed");
158
+ }
159
+ HEREDOC
160
+
161
+
162
+ builder.Phase(:number => phase += 1) do
163
+ Core::build_node2(builder, Nodes.reformat2({:name => "#{id} Reformat graph", :id => "#{id}_reformat_graph", :transformation => subgraph_reformat_func}))
164
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{id}_reformat_graph:0", :fromNode => "#{id}_reformat:1", :metadata => "file_list"}))
165
+ Core::build_node2(builder, Nodes.run_graph2({
166
+ :guiName => id,
167
+ :name => id,
168
+ :id => "#{id}_run_graph"
169
+ }))
170
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{id}_run_graph:0", :fromNode => "#{id}_reformat_graph:0", :metadata => "trash_metadata"}))
171
+ Core::build_node2(builder, Nodes.reformat2({:name => "#{id} Reformat fail", :id => "#{id}_reformat_fail", :transformation => fail_reformat}))
172
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{id}_reformat_fail:0", :fromNode => "#{id}_run_graph:1", :metadata => "run_graph_failure_metadata"}))
173
+ Core::build_node2(builder, Nodes.trash2({:name => "#{id}_trash", :id => "#{id}_trash", :debugPrint => true}))
174
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{id}_trash:0", :fromNode => "#{id}_reformat_fail:0", :metadata => "run_graph_failure_metadata"}))
175
+ end
176
+ end
177
+
178
+
179
+ end
180
+ end
181
+ end
182
+
183
+ def self.create_incremental_sf_downloading_graph(file, taps, options={})
184
+ metadata = options[:metadata]
185
+ store = options[:store] || "${GDC_EVENTSTORE}"
186
+ s3_backup = options[:s3_backup]
187
+
188
+ File.open(file, "w") do |file|
189
+ builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
190
+ builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
191
+ builder.Graph({:name => "Goodsales incremental Downloader"}) do
192
+ builder.Global do
193
+ Helpers::property_file(builder, {:id => "params_params", :fileURL => "params.prm"})
194
+ Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})
195
+
196
+ Helpers::create_lookup_meta(builder)
197
+ taps.each do |tap|
198
+ module_name = tap[:object]
199
+ file = tap[:id] || module_name
200
+
201
+ builder.Metadata({:id => "#{file}_s3_metadata"}) do |builder|
202
+ Helpers::csv_metadata(builder, GoodData::Bam::Metadata.add_field(Metadata.get_target_metadata(Tap.prepare_for_s3_backup(tap)), {:name => "Timestamp"}))
203
+ end
204
+
205
+ tap = Tap.prepare_for_sf_downloader(tap)
206
+ builder.Metadata({:id => "#{file}_source_metadata"}) do |builder|
207
+ Helpers::csv_metadata(builder, Metadata.get_source_metadata(tap))
208
+ end
209
+
210
+ builder.Metadata({:id => "#{file}_es_metadata"}) do |builder|
211
+ Helpers::csv_metadata(builder, Metadata.add_timestamp_as_date(Metadata.get_target_metadata(tap)))
212
+ end
213
+
214
+ Core::build_node2(builder, Nodes.lookup2({:name => "gdLookup0", :id => "gdLookup0", :type => Nodes::GD_LOOKUP, :metadata => "lookup_metadata"}))
215
+
216
+ end
217
+
218
+ Helpers::sf_connection(builder, {})
219
+ Helpers::property(builder, {:id => "SFDC_CLIENT_ID", :value => "gooddata/gooddata/"})
220
+ Helpers::property(builder, {:id => "SFDC_LOGIN_HOSTNAME", :value => options[:sf_server] || "login.salesforce.com"})
221
+ Helpers::property(builder, {:id => "SFDC_NAME", :value => "Salesforce connection"})
222
+ Helpers::property(builder, {:id => "SFDC_PASSWORD", :value => options[:sf_password]})
223
+ Helpers::property(builder, {:id => "SFDC_TOKEN", :value => options[:sf_token]})
224
+ Helpers::property(builder, {:id => "SFDC_USERNAME", :value => options[:sf_login]})
225
+ Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})
226
+ end
227
+
228
+ phase = 1
229
+
230
+ taps.each do |tap|
231
+ module_name = tap[:object]
232
+ file = tap[:id]
233
+ dataset = file || module_name
234
+ source_file = tap[:source]
235
+
236
+ has_timestamp = Tap.has_output_field?(tap, "Timestamp")
237
+ timestamp_field = Tap.find_output_field(tap, "Timestamp")
238
+ id_field = Tap.find_output_field(tap, "Id")
239
+ tap = Tap.prepare_for_sf_downloader(tap)
240
+ builder.Phase(:number => phase += 1) do
241
+ Core::build_node2(builder, Nodes.es_truncate2({:guiName => dataset, :store => store, :entity => dataset, :timestamp => "${#{dataset}_TRUNCATE_DATE}", :name => "#{module_name} es truncate", :id => "#{module_name}_es_truncate"}))
242
+ end
243
+
244
+ builder.Phase(:number => phase += 1) do
245
+ fields = tap[:fields]
246
+ mapping = "{\"xmlFieldsMapping\":{\"xmlFields\":["
247
+ add = fields.map do |f|
248
+ "{\"xmlFieldMapping\":{\"name\":\"#{f[:name]}\",\"label\":\"#{f[:label]}\",\"xmlPath\":\"#{f[:name]}\",\"metadataField\":\"#{f[:name]}\"}}"
249
+ end
250
+ stuff = mapping + add.join(",") + "]}}"
251
+
252
+ mandatory_fields = fields.reject {|f| f[:is_mandatory] == false }.map {|f| f[:name] + ";"}.join
253
+
254
+ Core::build_node2(builder, Nodes.sfdc_reader2({:name => "#{file} SF Reader", :id => "#{file}_reader", :soql => Helpers::generate_incremental_select(tap), :sfdcConnection => "SFDC", :fieldsMapping => stuff, :mandatoryFields => mandatory_fields}))
255
+
256
+
257
+ es_transformation_source = if has_timestamp
258
+ "function integer transform() {\n$out.0.* = $in.0.*;\n$out.0.Id = $in.0.#{id_field[:name]};\n$out.0.Timestamp = str2date($in.0.#{timestamp_field[:name]},\"joda:yyyy-MM-dd'T'HH:mm:ss.SSSZZ\");;\nreturn OK;\n}"
259
+ else
260
+ "function integer transform() {\n$out.0.* = $in.0.*;\n$out.0.Id = $in.0.#{id_field[:name]};\n$out.0.Timestamp = long2date(${GRAPH_STARTED_TIMESTAMP});\nreturn OK;\n}"
261
+ end
262
+
263
+ Core::build_node2(builder, Nodes.copy2({:name => "#{file} copy", :id => "#{file}_copy"}))
264
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_copy:0", :fromNode => "#{file}_reader:0", :metadata => "#{file}_source_metadata"}))
265
+ Core::build_node2(builder, Nodes.writer2({:enabled => "disabled", :name => "#{file} CSV Writer", :id => "#{file}_csv", :fileURL => "data/#{dataset.downcase}.csv", :outputFieldNames => "true"}))
266
+
267
+ Core::build_node2(builder, Nodes.reformat2({:name => "#{file} Reformat", :id => "#{file}_es_reformat", :transformation => es_transformation_source}))
268
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_es_reformat:0", :fromNode => "#{file}_copy:1", :metadata => "#{file}_source_metadata"}))
269
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_csv:0", :fromNode => "#{file}_copy:0", :metadata => "#{file}_source_metadata"}))
270
+
271
+ if s3_backup then
272
+ if has_timestamp
273
+
274
+ Core::build_node2(builder, Nodes.writer2({:enabled => "enabled", :name => "#{file} s3 Writer", :id => "#{file}_s3", :fileURL => "https://${S3_ACCESS_KEY_ID}:\`replace(\"${S3_SECRET_ACCESS_KEY}\",\"/\",\"%2F\")\`@${S3_BUCKETNAME}.s3.amazonaws.com/${GDC_PROJECT_ID}/#{file}/#{file}_\`date2long(today())\`", :outputFieldNames => true}))
275
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_s3:0", :fromNode => "#{file}_copy:2", :metadata => "#{file}_source_metadata"}))
276
+
277
+ else
278
+ Core::build_node2(builder, Nodes.reformat2({:name => "#{file} Reformat", :id => "#{file}_s3_reformat", :transformation => "function integer transform() {\n$out.0.* = $in.0.*;\n$out.0.Timestamp = toString(${GRAPH_STARTED_TIMESTAMP});\nreturn OK;\n}"}))
279
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_s3_reformat:0", :fromNode => "#{file}_copy:2", :metadata => "#{file}_source_metadata"}))
280
+
281
+ Core::build_node2(builder, Nodes.writer2({:enabled => "enabled", :name => "#{file} s3 Writer", :id => "#{file}_s3", :fileURL => "https://${S3_ACCESS_KEY_ID}:\`replace(\"${S3_SECRET_ACCESS_KEY}\",\"/\",\"%2F\")\`@${S3_BUCKETNAME}.s3.amazonaws.com/${GDC_PROJECT_ID}/#{file}/#{file}_\`date2long(today())\`", :outputFieldNames => true}))
282
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_s3:0", :fromNode => "#{file}_s3_reformat:0", :metadata => "#{file}_s3_metadata"}))
283
+
284
+ end
285
+ end
286
+
287
+
288
+ Core::build_node2(builder, Nodes.sort2({:sortKey => "Timestamp(a)",:name => "#{file} es Sort", :id => "#{file}_es_sort"}))
289
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_es_sort:0", :fromNode => "#{file}_es_reformat:0", :metadata => "#{file}_es_metadata"}))
290
+
291
+ Core::build_node2(builder, Nodes.es_writer2({:name => "#{file} es Writer", :id => "#{file}_es", :store => store, :entityFieldsMapping => Helpers::create_es_write_json(Tap.add_timestamp_field(tap)).to_json}))
292
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_es:0", :fromNode => "#{file}_es_sort:0", :metadata => "#{file}_es_metadata"}))
293
+
294
+ end
295
+
296
+ builder.Phase(:number => phase += 1) do
297
+ generate_func = <<HEREDOC
298
+ function integer generate() {
299
+ date time_end = jodaStr2date("${#{dataset}_END}",["yyyy-MM-dd'T'HH:mm:ss.SSSZZ"], 'en_US', 'UTC', 'UTC');
300
+ $out.0.key = "#{dataset}_LAST_RUN";
301
+ $out.0.value = jodaDate2str(time_end,"yyyy-MM-dd'T'HH:mm:ss.SSSZZ", 'en_US', 'UTC');
302
+ return OK;
303
+ }
304
+ HEREDOC
305
+
306
+ Core::build_node2(builder, Nodes.data_generator2({:guiName => dataset, :name => "generator_#{phase}", :id => "generator_#{phase}", :generate => generate_func}))
307
+ Core::build_node2(builder, Nodes.lookup_reader_writer2({:guiName => dataset, :lookupTable => "gdLookup0", :id => "gd_lookup_reader_#{phase}" }))
308
+ Core::build_node2(builder, Nodes.edge2({:toNode => "gd_lookup_reader_#{phase}:0", :fromNode => "generator_#{phase}:0", :metadata => "lookup_metadata"}))
309
+
310
+ end
311
+ end
312
+ end
313
+ end
314
+ end
315
+
316
+ def self.generate_downloaders(home, project, params)
317
+ home = Pathname(home)
318
+
319
+ incremental_taps = Taps.get_incremental(project[:taps])
320
+
321
+ sf_taps = Taps.get_salesforce(incremental_taps)
322
+ file_taps = Taps.get_file(incremental_taps)
323
+
324
+ create_incremental_downloader_run_graph(home + "main.grf", incremental_taps)
325
+ create_incremental_sf_downloading_graph(home + "incremental.grf", sf_taps, params)
326
+
327
+ file_taps.each do |tap|
328
+ id = tap[:id]
329
+ Helpers::loop_over_file(home + "#{tap[:id]}_loop.grf", {
330
+ :token => id,
331
+ :file_to_loop => "data/#{id}_files_to_read.csv",
332
+ :graph_to_run => "graphs/#{id}_download.grf"
333
+ })
334
+ create_incremental_file_downloading_graph(home + "#{tap[:id]}_download.grf", [tap], params)
335
+
336
+ end
337
+ end
338
+
339
+ def self.create_incremental_file_downloading_graph(file, taps, options={})
340
+ metadata = options[:metadata]
341
+ store = options[:store] || "${GDC_EVENTSTORE}"
342
+ s3_backup = options[:s3_backup]
343
+ File.open(file, "w") do |file|
344
+ builder = Builder::XmlMarkup.new(:target=>file, :indent=>2)
345
+ builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
346
+ builder.Graph({:name => "Goodsales incremental Downloader"}) do
347
+ builder.Global do
348
+ Helpers::property_file(builder, {:id => "workspace_params", :fileURL => "workspace.prm"})
349
+ Helpers::create_lookup_meta(builder)
350
+ taps.each do |tap|
351
+ module_name = tap[:object]
352
+ file = tap[:id] || module_name
353
+
354
+ Helpers::property_file(builder, {:id => "params_params", :fileURL => "#{file}_item.prm"})
355
+
356
+ builder.Metadata({:id => "#{file}_s3_metadata"}) do |builder|
357
+ Helpers::csv_metadata(builder, GoodData::Bam::Metadata.add_field(Metadata.get_target_metadata(Tap.prepare_for_s3_backup(tap)), {:name => "Timestamp"}))
358
+ end
359
+
360
+ tap = Tap.prepare_for_sf_downloader(tap)
361
+ builder.Metadata({:id => "#{file}_source_metadata"}) do |builder|
362
+ Helpers::csv_metadata(builder, Metadata.get_source_metadata(tap))
363
+ end
364
+
365
+ builder.Metadata({:id => "#{file}_es_metadata"}) do |builder|
366
+ Helpers::csv_metadata(builder, GoodData::Bam::Metadata.add_timestamp_as_date(Metadata.get_target_metadata(tap)))
367
+ end
368
+ Core::build_node2(builder, Nodes.lookup2({:name => "gdLookup0", :id => "gdLookup0", :type => Nodes::GD_LOOKUP, :metadata => "lookup_metadata"}))
369
+ end
370
+ end
371
+
372
+ phase = 1
373
+
374
+ taps.each do |tap|
375
+ module_name = tap[:object]
376
+ file = tap[:id]
377
+ dataset = file || module_name
378
+ source_file = tap[:source]
379
+
380
+
381
+ has_timestamp = GoodData::Bam::Tap.has_output_field?(tap, "Timestamp")
382
+ timestamp_field = GoodData::Bam::Tap.find_output_field(tap, "Timestamp")
383
+ id_field = GoodData::Bam::Tap.find_output_field(tap, "Id")
384
+
385
+ tap = Tap.prepare_for_sf_downloader(tap)
386
+
387
+ builder.Phase(:number => phase += 1) do
388
+ fields = tap[:fields]
389
+ mapping = "{\"xmlFieldsMapping\":{\"xmlFields\":["
390
+ add = fields.map do |f|
391
+ "{\"xmlFieldMapping\":{\"name\":\"#{f[:name]}\",\"label\":\"#{f[:label]}\",\"xmlPath\":\"#{f[:name]}\",\"metadataField\":\"#{f[:name]}\"}}"
392
+ end
393
+ stuff = mapping + add.join(",") + "]}}"
394
+
395
+ mandatory_fields = fields.reject {|f| f[:is_mandatory] == false }.map {|f| f[:name] + ";"}.join
396
+
397
+ Core::build_node2(builder, Nodes.reader2({:name => "#{file} File Reader", :id => "#{file}_reader", :fileURL => "${FILE}"}))
398
+ es_transformation_source = if has_timestamp
399
+ "function integer transform() {\n$out.0.* = $in.0.*;\n$out.0.Id = $in.0.#{id_field[:name]};\n$out.0.Timestamp = str2date($in.0.#{timestamp_field[:name]},\"joda:yyyy-MM-dd'T'HH:mm:ss.SSSZZ\");;\nreturn OK;\n}"
400
+ else
401
+ "function integer transform() {\n$out.0.* = $in.0.*;\n$out.0.Id = $in.0.#{id_field[:name]};\n$out.0.Timestamp = long2date(${GRAPH_STARTED_TIMESTAMP});\nreturn OK;\n}"
402
+ end
403
+
404
+ # build_node2(builder, Nodes.reformat2({:name => "#{file} Reformat", :id => "#{file}_reformat", :transformation => transformation_source}))
405
+
406
+ Core::build_node2(builder, Nodes.copy2({:name => "#{file} copy", :id => "#{file}_copy"}))
407
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_copy:0", :fromNode => "#{file}_reader:0", :metadata => "#{file}_source_metadata"}))
408
+ Core::build_node2(builder, Nodes.writer2({:enabled => "disabled", :name => "#{file} CSV Writer", :id => "#{file}_csv", :fileURL => "data/#{dataset.downcase}.csv", :outputFieldNames => "true"}))
409
+
410
+ Core::build_node2(builder, Nodes.reformat2({:name => "#{file} Reformat", :id => "#{file}_es_reformat", :transformation => es_transformation_source}))
411
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_es_reformat:0", :fromNode => "#{file}_copy:1", :metadata => "#{file}_source_metadata"}))
412
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_csv:0", :fromNode => "#{file}_copy:0", :metadata => "#{file}_source_metadata"}))
413
+
414
+ if s3_backup
415
+ if has_timestamp
416
+ Core::build_node2(builder, Nodes.writer2({:enabled => "enabled", :name => "#{file} s3 Writer", :id => "#{file}_s3", :fileURL => "https://${S3_ACCESS_KEY_ID}:\`replace(\"${S3_SECRET_ACCESS_KEY}\",\"/\",\"%2F\")\`@${S3_BUCKETNAME}.s3.amazonaws.com/${GDC_PROJECT_ID}/#{file}/#{file}_\`date2long(today())\`", :outputFieldNames => true}))
417
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_s3:0", :fromNode => "#{file}_copy:2", :metadata => "#{file}_source_metadata"}))
418
+ else
419
+ Core::build_node2(builder, Nodes.reformat2({:name => "#{file} Reformat", :id => "#{file}_s3_reformat", :transformation => "function integer transform() {\n$out.0.* = $in.0.*;\n$out.0.Timestamp = toString(${GRAPH_STARTED_TIMESTAMP});\nreturn OK;\n}"}))
420
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_s3_reformat:0", :fromNode => "#{file}_copy:2", :metadata => "#{file}_source_metadata"}))
421
+
422
+ Core::build_node2(builder, Nodes.writer2({:enabled => "enabled", :name => "#{file} s3 Writer", :id => "#{file}_s3", :fileURL => "https://${S3_ACCESS_KEY_ID}:\`replace(\"${S3_SECRET_ACCESS_KEY}\",\"/\",\"%2F\")\`@${S3_BUCKETNAME}.s3.amazonaws.com/${GDC_PROJECT_ID}/#{file}/#{file}_\`date2long(today())\`", :outputFieldNames => true}))
423
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_s3:0", :fromNode => "#{file}_s3_reformat:0", :metadata => "#{file}_s3_metadata"}))
424
+ end
425
+ # Core::build_node2(builder, Nodes.writer2({:enabled => "enabled", :name => "#{file} s3 Writer", :id => "#{file}_s3", :fileURL => "https://${S3_ACCESS_KEY_ID}:\`replace(\"${S3_SECRET_ACCESS_KEY}\",\"/\",\"%2F\")\`@${S3_BUCKETNAME}.s3.amazonaws.com/${GDC_PROJECT_ID}/#{file}/#{file}_\`date2long(today())\`", :outputFieldNames => true}))
426
+ # Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_s3:0", :fromNode => "#{file}_copy:2", :metadata => "#{file}_source_metadata"}))
427
+ end
428
+
429
+
430
+ Core::build_node2(builder, Nodes.sort2({:sortKey => "Timestamp(a)",:name => "#{file} es Sort", :id => "#{file}_es_sort"}))
431
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_es_sort:0", :fromNode => "#{file}_es_reformat:0", :metadata => "#{file}_es_metadata"}))
432
+ Core::build_node2(builder, Nodes.es_writer2({:name => "#{file} es Writer", :id => "#{file}_es", :store => store, :entityFieldsMapping => Helpers::create_es_write_json(Tap.add_timestamp_field(tap)).to_json}))
433
+ Core::build_node2(builder, Nodes.edge2({:toNode => "#{file}_es:0", :fromNode => "#{file}_es_sort:0", :metadata => "#{file}_es_metadata"}))
434
+ end
435
+
436
+ builder.Phase(:number => phase += 1) do
437
+ Core::build_node2(builder, Nodes.file_delete2({:guiName => "#{file}_file_delete", :name => "#{file}_file_delete", :id => "#{file}_file_delete", :baseURL => "${FILE}"}))
438
+ end
439
+
440
+ end
441
+
442
+ end
443
+ end
444
+ end
445
+
446
+ end
447
+ end
448
+ end
449
+ end