Class: GoodData::Process

Inherits:
Rest::Resource show all
Defined in:
lib/gooddata/models/process.rb

Instance Attribute Summary collapse

Attributes inherited from Rest::Object

#client, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rest::Object

client, default_client, #saved?

Methods included from Mixin::DataPropertyReader

#data_property_reader

Methods included from Mixin::DataPropertyWriter

#data_property_writer

Methods included from Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from Mixin::MetaGetter

#meta

Methods included from Mixin::RootKeyGetter

#root_key

Methods included from Mixin::ContentGetter

#content

Constructor Details

#initialize(data) ⇒ Process

Returns a new instance of Process.



251
252
253
# File 'lib/gooddata/models/process.rb', line 251

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly) Also known as: raw_data, json, to_hash

Returns the value of attribute data.



20
21
22
# File 'lib/gooddata/models/process.rb', line 20

def data
  @data
end

Class Method Details

.[](id, options = {}) ⇒ Object



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
# File 'lib/gooddata/models/process.rb', line 27

def [](id, options = {})
  project = options[:project]
  c = options[:client] || (project && project.client)

  if id == :all && project
    uri = "/gdc/projects/#{project.pid}/dataload/processes"
    data = c.get(uri)
    data['processes']['items'].map do |process_data|
      c.create(Process, process_data, project: project)
    end
  elsif id == :all
    uri = "/gdc/account/profile/#{c.user.obj_id}/dataload/processes"
    data = c.get(uri)
    pids = data['processes']['items'].map { |process_data| process_data['process']['links']['self'].match(%r{/gdc/projects/(\w*)/})[1] }.uniq
    projects_lookup = pids.pmap { |pid| c.projects(pid) }.reduce({}) do |a, e|
      a[e.pid] = e
      a
    end

    data['processes']['items'].map do |process_data|
      pid = process_data['process']['links']['self'].match(%r{/gdc/projects/(\w*)/})[1]
      c.create(Process, process_data, project: projects_lookup[pid])
    end
  else
    uri = "/gdc/projects/#{project.pid}/dataload/processes/#{id}"
    c.create(Process, c.get(uri), project: project)
  end
end

.allObject



56
57
58
# File 'lib/gooddata/models/process.rb', line 56

def all
  Process[:all]
end

.deploy(path, options = { :client => GoodData.client, :project => GoodData.project }) ⇒ Object

Deploy a new process or redeploy existing one.

Parameters:

  • path (String)

    Path to ZIP archive or to a directory containing files that should be ZIPed

  • options (Hash) (defaults to: { :client => GoodData.client, :project => GoodData.project })

    a customizable set of options

Options Hash (options):

  • :files_to_exclude (String)
  • :type (String) — default: 'GRAPH'

    Type of process - GRAPH or RUBY

  • :name (String)

    Readable name of the process

  • :process_id (String)

    ID of a process to be redeployed (do not set if you want to create a new process)

  • :verbose (Boolean) — default: false

    Switch on verbose mode for detailed logging



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
# File 'lib/gooddata/models/process.rb', line 93

def deploy(path, options = { :client => GoodData.client, :project => GoodData.project })
  return deploy_brick(path, options) if path.to_s.start_with?(APP_STORE_URL)

  return deploy_from_appstore(path.to_s, options) if (path.to_s =~ %r{\${.*}:(.*)\/(.*):\/}) == 0 # rubocop:disable Style/NumericPredicate

  client, project = GoodData.get_client_and_project(options)

  path = Pathname(path) || fail('Path is not specified')
  files_to_exclude = options[:files_to_exclude].nil? ? [] : options[:files_to_exclude].map { |pname| Pathname(pname) }
  process_id = options[:process_id]

  type = options[:type] || 'GRAPH'
  deploy_name = options[:name]
  fail ArgumentError, 'options[:name] can not be nil or empty!' if deploy_name.nil? || deploy_name.empty?

  verbose = options[:verbose] || false
  puts HighLine.color("Deploying #{path}", HighLine::BOLD) if verbose

  deployed_path = Process.upload_package(path, files_to_exclude, client: client, project: project)
  data = {
    :process => {
      :name => deploy_name,
      :path => "/uploads/#{File.basename(deployed_path)}",
      :type => type
    }
  }

  res = if process_id.nil?
          client.post("/gdc/projects/#{project.pid}/dataload/processes", data)
        else
          client.put("/gdc/projects/#{project.pid}/dataload/processes/#{process_id}", data)
        end

  process = client.create(Process, res, project: project)
  puts HighLine.color("Deploy DONE #{path}", HighLine::GREEN) if verbose
  process
end

.deploy_brick(path, options = { :client => GoodData.client, :project => GoodData.project }) ⇒ Object



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
# File 'lib/gooddata/models/process.rb', line 131

def deploy_brick(path, options = { :client => GoodData.client, :project => GoodData.project })
  client, project = GoodData.get_client_and_project(options)

  brick_uri_parts = URI(path).path.split('/')
  ref = brick_uri_parts[4]
  brick_name = brick_uri_parts.last
  brick_path = brick_uri_parts[5..-1].join('/')

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      `git clone #{APP_STORE_URL}`
    end

    Dir.chdir(File.join(dir, 'app_store')) do
      if ref
        `git checkout #{ref}`

        fail 'Wrong branch or tag specified!' if $CHILD_STATUS.to_i.nonzero?
      end

      opts = {
        :client => client,
        :project => project,
        :name => brick_name,
        :type => 'RUBY'
      }

      full_brick_path = File.join(dir, 'app_store', brick_path)

      unless File.exist?(full_brick_path)
        fail "Invalid brick name specified - '#{brick_name}'"
      end

      return deploy(full_brick_path, opts)
    end
  end
end

.deploy_from_appstore(path, options = { :client => GoodData.client, :project => GoodData.project }) ⇒ Object



169
170
171
172
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
# File 'lib/gooddata/models/process.rb', line 169

def deploy_from_appstore(path, options = { :client => GoodData.client, :project => GoodData.project })
  client, project = GoodData.get_client_and_project(options)

  deploy_name = options[:name]
  fail ArgumentError, 'options[:name] can not be nil or empty!' if deploy_name.nil? || deploy_name.empty?

  verbose = options[:verbose] || false
  puts HighLine.color("Deploying #{path}", HighLine::BOLD) if verbose

  process_id = options[:process_id]

  data = {
    process: {
      name: deploy_name,
      path: path,
      type: 'RUBY'
    }
  }

  res =
    if process_id.nil?
      client.post("/gdc/projects/#{project.pid}/dataload/processes", data)

    else
      client.put("/gdc/projects/#{project.pid}/dataload/processes/#{process_id}", data)

    end

  if res.keys.first == 'asyncTask'
    res = JSON.parse(client.poll_on_code(res['asyncTask']['links']['poll'], options.merge(process: false)))
  end

  process = client.create(Process, res, project: project)
  puts HighLine.color("Deploy DONE #{path}", HighLine::GREEN) if verbose
  process
end

.upload_package(path, files_to_exclude, opts = { :client => GoodData.connection }) ⇒ Object



80
81
82
83
# File 'lib/gooddata/models/process.rb', line 80

def upload_package(path, files_to_exclude, opts = { :client => GoodData.connection })
  GoodData.get_client_and_project(opts)
  zip_and_upload(path, files_to_exclude, opts)
end

.with_deploy(dir, options = {}, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gooddata/models/process.rb', line 60

def with_deploy(dir, options = {}, &block)
  _client, project = GoodData.get_client_and_project(options)

  GoodData.with_project(project) do
    params = options[:params].nil? ? [] : [options[:params]]
    if block
      begin
        res = GoodData::Process.deploy(dir, options.merge(:files_to_exclude => params))
        block.call(res)
      rescue => e
        puts e.inspect
      ensure
        res.delete if res
      end
    else
      GoodData::Process.deploy(dir, options.merge(:files_to_exclude => params))
    end
  end
end

Instance Method Details

#create_notification_rule(opts = {}) ⇒ Object



362
363
364
# File 'lib/gooddata/models/process.rb', line 362

def create_notification_rule(opts = {})
  NotificationRule.create(opts.merge(project: project, process: self, client: client))
end

#create_schedule(cron, executable, options = {}) ⇒ Object



328
329
330
# File 'lib/gooddata/models/process.rb', line 328

def create_schedule(cron, executable, options = {})
  project.create_schedule(process_id, cron, executable, options.merge(client: client, project: project))
end

#deleteObject



255
256
257
# File 'lib/gooddata/models/process.rb', line 255

def delete
  client.delete(uri)
end

#deploy(path, options = {}) ⇒ Object

Redeploy existing process.

Parameters:

  • path (String)

    Path to ZIP archive or to a directory containing files that should be ZIPed

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :files_to_exclude (String)
  • :process_id (String) — default: 'nobody'

    From address

  • :type (String) — default: 'GRAPH'

    Type of process - GRAPH or RUBY

  • :name (String)

    Readable name of the process

  • :verbose (Boolean) — default: false

    Switch on verbose mode for detailed logging



267
268
269
# File 'lib/gooddata/models/process.rb', line 267

def deploy(path, options = {})
  Process.deploy(path, { client: client, process_id: process_id, :project => project, :name => name, :type => type }.merge(options))
end

#downloadIO

Downloads the process from S3 in a zipped form.

Returns:

  • (IO)

    The stream of data that represents a zipped deployed process.



274
275
276
277
278
# File 'lib/gooddata/models/process.rb', line 274

def download
  link = links['source']
  client.connection.refresh_token
  client.get(link, process: false) { |_, _, result| RestClient.get(result.to_hash['location'].first) }
end

#executablesObject



316
317
318
# File 'lib/gooddata/models/process.rb', line 316

def executables
  process['executables']
end

#execute(executable, options = {}) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/gooddata/models/process.rb', line 332

def execute(executable, options = {})
  result = start_execution(executable, options)
  begin
    client.poll_on_code(result['executionTask']['links']['poll'], options)
  rescue RestClient::RequestFailed => e
    raise(e)
  ensure
    result = client.get(result['executionTask']['links']['detail'])
    if result['executionDetail']['status'] == 'ERROR'
      fail "Runing process failed. You can look at a log here #{result['executionDetail']['logFileName']}"
    end
  end
  client.create(GoodData::ExecutionDetail, result, client: client, project: project)
end


308
309
310
# File 'lib/gooddata/models/process.rb', line 308

def executions_link
  links['executions']
end

#graphsObject



312
313
314
# File 'lib/gooddata/models/process.rb', line 312

def graphs
  process['graphs']
end


296
297
298
# File 'lib/gooddata/models/process.rb', line 296

def link
  links['self']
end


292
293
294
# File 'lib/gooddata/models/process.rb', line 292

def links
  process['links']
end

#nameObject



284
285
286
# File 'lib/gooddata/models/process.rb', line 284

def name
  process['name']
end

#notification_rulesObject



358
359
360
# File 'lib/gooddata/models/process.rb', line 358

def notification_rules
  NotificationRule.all(project: project, process: self, client: client)
end

#obj_idObject Also known as: process_id



302
303
304
# File 'lib/gooddata/models/process.rb', line 302

def obj_id
  uri.split('/').last
end

#pathObject



320
321
322
# File 'lib/gooddata/models/process.rb', line 320

def path
  process['path']
end

#processObject



280
281
282
# File 'lib/gooddata/models/process.rb', line 280

def process
  data['process']
end

#schedulesObject



324
325
326
# File 'lib/gooddata/models/process.rb', line 324

def schedules
  project.schedules.select { |schedule| schedule.process_id == obj_id }
end

#start_execution(executable, options = {}) ⇒ Object



347
348
349
350
351
352
353
354
355
356
# File 'lib/gooddata/models/process.rb', line 347

def start_execution(executable, options = {})
  params = options[:params] || {}
  hidden_params = options[:hidden_params] || {}
  client.post(executions_link,
              :execution => {
                :graph => executable.to_s,
                :params => GoodData::Helpers.encode_public_params(params),
                :hiddenParams => GoodData::Helpers.encode_hidden_params(hidden_params)
              })
end

#typeObject



288
289
290
# File 'lib/gooddata/models/process.rb', line 288

def type
  process['type'].downcase.to_sym
end