Module: Es::Commands

Defined in:
lib/commands.rb

Class Method Summary collapse

Class Method Details

.create(options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/commands.rb', line 4

def self.create(options)
  pid     = options[:pid]
  es_name = options[:es_name]

  begin
    GoodData.post "/gdc/projects/#{pid}/eventStore/stores", {:store => {:storeId => es_name}}
  rescue RestClient::BadRequest
    puts "Seems like eventstore with name #{es_name} already exists"
    exit 1
  end
end

.delete(options) ⇒ Object



16
17
18
19
20
# File 'lib/commands.rb', line 16

def self.delete(options)
  pid     = options[:pid]
  es_name = options[:es_name]
  GoodData.delete "/gdc/projects/#{pid}/eventStore/stores/#{es_name}"
end

.extract(options) ⇒ Object



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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/commands.rb', line 233

def self.extract(options)
  base_dir      = options[:basedir]
  extract_dir   = options[:extractdir]
  pid           = options[:pid]
  es_name       = options[:es_name]
  now           = options[:now]
  args          = options[:args]
  logger        = options[:logger]

  if base_dir.nil? && extract_dir.nil?
    fail "Provide path to the loading configuration as a first argument" if args.first.nil?
    load_config_files = [args.first]
    fail "Provide path to the extract configuration as a second argument" if args[1].nil?
    extract_config_files = [args[1]]
  else
    load_config_files = Dir::glob("#{base_dir}/gen_load*.json")
    extract_config_files = Dir::glob("#{extract_dir}/gen_extract*.json")
  end

  # build one giant load config
  load_entities = load_config_files.reduce([]) do |memo, filename|
    fail "File #{filename} cannot be found" unless File.exist?(filename)
    load_config = Es::Helpers.load_config(filename)
    load = Es::Load.parse(load_config)
    memo.concat(load.entities)
  end
  hyper_load = Es::Load.new(load_entities)

  extract_config_files.each do |extract_config_file|
    fail "File #{extract_config_file} cannot be found" unless File.exist?(extract_config_file)
    extract_config = Es::Helpers.load_config(extract_config_file)
    extract = Es::Extract.parse(extract_config, hyper_load, :now => now)

    extract.entities.each do |entity|
      next if options[:only] && entity.name != options[:only]

      logger.info "Extracting entity \"#{entity.name}\", fields #{entity.fields.map {|f| f.name}.join(', ')}" if logger
      logger.info "Using json => #{entity.to_extract_fragment(pid, :pretty => false).to_json}" if logger

      if options[:verbose] || options[:json] || options[:debug] then
        puts "Entity #{entity.name.bright}" 
        puts "Config from #{load_config_files.join(', ')} and #{extract_config_file}"
      end

      puts JSON.pretty_generate(entity.to_extract_fragment(pid)) if options[:debug]

      if options[:json]
        # puts "load the file #{entity.file} to destination #{web_dav_file} and run the specified as the task"
        puts "======= Extract JSON start"
        puts entity.to_extract_fragment(pid, :pretty => false).to_json.color(:blue)
        puts "======= Extract JSON end"
        puts
      else
        entity.extract(pid, es_name)
      end
    end
  end
end

.generate_base(options) ⇒ Object



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
# File 'lib/commands.rb', line 293

def self.generate_base(options)
  entity = options[:entity]
  input_filename = options[:input]
  input_dir = options[:inputdir]
  output_filename = options[:output]
  base_dir = options[:basedir]

  fail "You need to specify input file name or input dir" if input_filename.nil? && input_dir.nil?

  if base_dir.nil?
    input_filenames = [input_filename]
    else
    input_filenames = Dir::glob("#{input_dir}/*.csv")
  end
  
  input_filenames.each do |input_filename|
    
    headers = nil
    FasterCSV.foreach(input_filename, :headers => true, :return_headers => true) do |row|
      if row.header_row?
        headers = row.fields
        break
      end
    end
    
    entity_name = entity || File.basename(input_filename, ".csv")
    load = Es::Load.new([
                        Es::Entity.new(entity_name, {
                                       :file => input_filename,
                                       :fields => headers.map do |field_name|
                                       Es::Field.new(field_name, "none")
                                       end
                                       })
                        ])
    
    config = JSON.pretty_generate(load.to_config)
    
    if input_dir && base_dir
      File.open(base_dir+"/gen_load_"+entity_name+".json", 'w') do |f|
        f.write config
      end
      elsif input_filename && output_filename
      File.open(output_filename, 'w') do |f|
        f.write config
      end
      else 
      puts config
    end
  end
end

.generate_extract(options) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/commands.rb', line 344

def self.generate_extract(options)
  base_dir = options[:basedir]
  fail "You need to specify base dir" if base_dir.nil?

  base_filenames = Dir::glob("#{base_dir}/gen_load_*.json")
  # build one giant load config
  base_entities = base_filenames.reduce([]) do |memo, filename|
    fail "File #{filename} cannot be found" unless File.exist?(filename)
    load_config = Es::Helpers.load_config(filename)
    load = Es::Load.parse(load_config)
    memo.concat(load.entities)
  end
  hyper_load = Es::Load.new(base_entities)
  entity_names = hyper_load.entities.map {|e| e.name}.uniq

  entity_names.each do |entity_name|
    entity = hyper_load.get_merged_entity_for(entity_name)
    
    File.open(base_dir+"/gen_extract_"+entity.name+".json", 'w') do |f|
      f.write JSON.pretty_generate(entity.to_extract_config)
    end
  end
end

.get_typesObject



22
23
24
# File 'lib/commands.rb', line 22

def self.get_types
  Es::Field::FIELD_TYPES
end

.init(options) ⇒ Object



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
# File 'lib/commands.rb', line 26

def self.init(options)
  pid             = options[:pid]
  es_name         = options[:es_name]
  base_dir        = options[:basedir]
  deleted         = options[:deleted]
  only            = options[:only]
  
  max_timestamp = 2147483647
  
  fail "Provide path to the loading configuration" if base_dir.nil?
  base_filenames = Dir::glob("#{base_dir}/gen_load*.json")
  
  base_entities = base_filenames.reduce([]) do |memo, filename|
    fail "File #{filename} cannot be found" unless File.exist?(filename)
    load_config = Es::Helpers.load_config(filename)
    load = Es::Load.parse(load_config)
    memo.concat(load.entities)
  end
  hyper_load = Es::Load.new(base_entities)
  entity_names = hyper_load.entities.map {|e| e.name}.uniq

  entity_names.each do |entity_name|
    next if only && entity_name != only
    entity = hyper_load.get_merged_entity_for(entity_name)  
    
    Tempfile.open(entity.name) do |tmp_file|
      header_row = []
      content_row = []
      entity.fields.each do |field|
        header_row << field.name
        content_row << max_timestamp if field.is_timestamp?
        content_row << 1 if field.is_recordid?
        content_row << "" if !field.is_recordid? && !field.is_timestamp?
      end
      if deleted
        header_row << "IsDeleted" << "DeletedAt"
        content_row << "" << ""
        entity.add_field(Es::Field.new('IsDeleted', 'attribute')) unless entity.has_field?('IsDeleted')
        entity.add_field(Es::Field.new('DeletedAt', 'time')) unless entity.has_field?('DeletedAt')
      end
      tmp_file.puts(header_row.join(","))
      tmp_file.puts(content_row.join(","))
      tmp_file.flush
      
      entity.file = tmp_file.path
      # create temp file, link it to entity
      web_dav_file = Es::Helpers.load_destination_dir(pid, entity) + '/' + Es::Helpers.destination_file(entity)

      if options[:verbose]
        puts "Entity #{entity.name}".bright
        puts "Will load from #{entity.file} to #{web_dav_file}"
        puts JSON::pretty_generate(entity.to_load_fragment(pid))
      end
      entity.load(pid, es_name)
      puts "Done" if options[:verbose]
    end
    if options[:verbose]
      puts "Truncating loaded dummy data for #{entity.name}. Using timestamp #{max_timestamp - 1}."
    end
    entity.truncate(pid, es_name, (max_timestamp - 1))
  end
end

.load(options) ⇒ Object



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/commands.rb', line 122

def self.load(options)
  pid       = options[:pid]
  es_name   = options[:es_name]
  filenames = options[:filenames]
  base_dir  = options[:basedir]
  pattern   = options[:pattern] || "*.json"
  only      = options[:only]
  logger    = options[:logger]

  if base_dir.nil?
    fail "Provide path to the loading configuration as a first argument" if filenames.empty?
  else
    # puts "would grab files like this #{"#{base_dir}/gen_json*.json"}"
    filenames = Dir::glob("#{base_dir}/#{pattern}")
  end

  # for each config file
  filenames.each do |filename|
    fail "File #{filename} cannot be found" unless File.exist?(filename)
    load_config_file = Es::Helpers.load_config(filename)
    load = Es::Load.parse(load_config_file)

    load.entities.each do |entity|
      next if only && entity.name != only
      next unless Es::Helpers.has_more_lines?(entity.file)

      logger.info "Loading entity \"#{entity.name}\", fields #{entity.fields.map {|f| f.name}.join(', ')}" if logger
      logger.info "Using json => #{entity.to_load_fragment(pid).to_json}" if logger

      web_dav_file = Es::Helpers.load_destination_dir(pid, entity) + '/' + Es::Helpers.destination_file(entity)
      if options[:verbose]
        puts "Entity #{entity.name}".bright
        puts "Configuration from #{filename}"
        puts "Will load from #{entity.file} to #{web_dav_file}"
        puts JSON::pretty_generate(entity.to_load_fragment(pid))
      end
      if options[:j]
        puts "Entity #{entity.name}".bright unless options[:verbose]
        puts "load the file #{entity.file} to destination #{web_dav_file} and run the specified as the task"
        puts "======= Load JSON start"
        puts entity.to_load_fragment(pid).to_json.color(:blue)
        puts "======= Load JSON end"
        puts
      else
        entity.load(pid, es_name)
        puts "Done" if options[:verbose]
      end
    end
  end
end

.load_column(options) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/commands.rb', line 368

def self.load_column(options)
  file = options[:input]
  name = options[:name]
  type = options[:type]
  entity = options[:entity]
  base_filename = options[:base]
  pid = options[:pid]
  es_name = options[:es_name]
  rid_name = options[:rid] || 'Id'

  fail "You need to specify column name" if name.nil?
  fail "You need to specify column type" if type.nil?
  fail "You need to specify entity name" if entity.nil?
  fail "You need to specify input file name" if file.nil?

  base_config_file = Es::Helpers.load_config(base_filename)
  base = Es::Load.parse(base_config_file)

  load = Es::Load.new([
    Es::Entity.new(entity, {
       :file => file,
       :fields => [
         Es::Field.new('Timestamp', 'timestamp'),
         Es::Field.new(rid_name, 'recordid'),
         Es::Field.new(name, type)
       ]
    })
  ])

  base.get_entity(entity).add_field(Es::Field.new(name, type))
  puts "Added field #{name}" if options[:verbose]
  base.to_config_file(base_filename)

  load.entities.first.load(pid, es_name)

end

.load_deleted(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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/commands.rb', line 173

def self.load_deleted(options)
  filenames = options[:filenames]
  base_dir  = options[:basedir]
  pattern   = options[:pattern] || "*.json"
  pid       = options[:pid]
  es_name   = options[:es_name]
  logger    = options[:logger]

  if base_dir.nil?
    fail "Provide path to the loading configuration as a first argument" if filenames.empty?
  else
    # puts "would grab files like this #{"#{base_dir}/gen_load*.json"}"
    filenames = Dir::glob("#{base_dir}/#{pattern}")
  end

  compatibility_mode = options[:compatibility] || false
  deleted_type = compatibility_mode ? "isDeleted" : "attribute"

  filenames.each do |load_config_file|
    load_config = Es::Helpers.load_config(load_config_file)
    load = Es::Load.parse(load_config)

    load.entities.each do |entity|
      source_dir = File.dirname(entity.file)
      deleted_filename = Es::Helpers.destination_file(entity, :deleted => true)
      deleted_source = "#{source_dir}/#{deleted_filename}"
      next unless File.exist? deleted_source
      has_more_lines = Es::Helpers.has_more_lines?(deleted_source)
      logger.info "Deleted records for entity #{entity.name} is not loaded since the file #{deleted_source} is empty." if logger && !has_more_lines
      next unless has_more_lines

      logger.info "Loading deleted records for entity #{entity.name} in with compatibility mode set to #{compatibility_mode}." if logger

      e = Es::Entity.create_deleted_entity(entity.name, {:compatibility_mode => compatibility_mode, :file => deleted_source})
      e.load(pid, es_name)

      if !compatibility_mode
        deleted_with_time = "#{source_dir}/#{deleted_filename}".gsub(/\.csv$/, '_del.csv')
        FasterCSV.open(deleted_with_time, 'w') do |csv|
          csv << ['Id', 'Timestamp', 'DeletedAt']
          FasterCSV.foreach("#{source_dir}/#{deleted_filename}", :headers => true, :return_headers => false) do |row|
            csv << row.values_at('Id', 'Timestamp', 'Timestamp')
          end
        end

        e1 = Es::Entity.new(entity.name, {
          :file   => deleted_with_time,
          :fields => [
            Es::Field.new('Id', 'recordid'),
            Es::Field.new('Timestamp', 'timestamp'),
            Es::Field.new('DeletedAt', 'time')
          ]
        })
        e1.load(pid, es_name)
      end
    end
  end
  
end

.truncate(options) ⇒ Object



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
# File 'lib/commands.rb', line 89

def self.truncate(options)
  pid             = options[:pid]
  es_name         = options[:es_name]
  entity_name     = options[:entity]
  timestamp       = options[:timestamp]
  filenames       = options[:load_filenames]
  basedir_pattern = options[:basedir_pattern] || "*.json"
  logger          = options[:logger]

  base_dir = options[:basedir]
  
  fail "You need to specify timestamp" if timestamp.nil?
  if base_dir.nil?
    # fail "You need to specify entity name" if entity_name.nil?
    fail "You need to specify base filename" if filenames.empty?
  else
    # puts "would grab files like this #{"#{base_dir}/gen_load*.json"}"
    filenames = Dir::glob("#{base_dir}/#{basedir_pattern}")
  end

  filenames.each do |base_filename|

    base_config_file = Es::Helpers.load_config(base_filename)
    base = Es::Load.parse(base_config_file)

    base.entities.each do |entity|
      next if !entity_name.nil? and entity_name != entity.name
      logger.info "truncating entity \"#{entity.name}\"} at timestamp #{timestamp} that is #{Time.at(timestamp).to_s}" if logger
      entity.truncate(pid, es_name, timestamp)
    end
  end
end