class GoodData::Schedule

Constants

SCHEDULE_TEMPLATE

Attributes

dirty[R]
json[R]

Public Class Methods

[](id, opts = { :client => GoodData.connection, :project => GoodData.project }) click to toggle source

Looks for schedule @param id [String] URL, ID of schedule or :all @return [GoodData::Schedule|Array<GoodData::Schedule>] List of schedules

# File lib/gooddata/models/schedule.rb, line 31
def [](id, opts = { :client => GoodData.connection, :project => GoodData.project })
  c, project = GoodData.get_client_and_project(opts)

  if id == :all
    GoodData::Schedule.all(opts)
  else
    if id =~ %r{\/gdc\/projects\/[a-zA-Z\d]+\/schedules\/?[a-zA-Z\d]*}
      url = id
      tmp = c.get url
      return c.create(GoodData::Schedule, tmp)
    end

    tmp = c.get "/gdc/projects/#{project.pid}/schedules/#{id}"
    c.create(GoodData::Schedule, tmp, project: project)
  end
end
all(opts = { :client => GoodData.connection, :project => GoodData.project }) click to toggle source

Returns list of all schedules for active project @return [Array<GoodData::Schedule>] List of schedules

# File lib/gooddata/models/schedule.rb, line 50
def all(opts = { :client => GoodData.connection, :project => GoodData.project })
  c, project = GoodData.get_client_and_project(opts)

  tmp = c.get "/gdc/projects/#{project.pid}/schedules"
  tmp['schedules']['items'].map { |schedule| c.create(GoodData::Schedule, schedule, project: project) }
end
create(process_id, trigger, executable, options = {}) click to toggle source

Creates new schedules from parameters passed

@param process_id [String] Process ID @param trigger [String|GoodData::Schedule] Trigger of schedule. Can be cron string or reference to another schedule. @param executable [String] Execution executable @param options [Hash] Optional options @return [GoodData::Schedule] New GoodData::Schedule instance

# File lib/gooddata/models/schedule.rb, line 64
def create(process_id, trigger, executable, options = {})
  c, project = GoodData.get_client_and_project(options)

  fail 'Process ID has to be provided' if process_id.blank?

  process = Process[process_id, project: project, client: c]
  is_dataload_process = process.type == :dataload

  if is_dataload_process
    dataload_datasets = options[:dataload_datasets] || options['GDC_DATALOAD_DATASETS']
    dataload_datasets = '[]' unless dataload_datasets

    de_synchronize_all = options[:de_synchronize_all] || options['GDC_DE_SYNCHRONIZE_ALL']
  else
    lcm_component = process.type == :lcm
    add_component = process.add_v2_component?
    executable_missing = !lcm_component && !add_component && executable.blank?
    fail 'Executable has to be provided' if executable_missing
  end

  schedule = c.create(GoodData::Schedule, GoodData::Helpers.stringify_keys(GoodData::Helpers.deep_dup(SCHEDULE_TEMPLATE)), client: c, project: project)

  params = { 'PROCESS_ID' => process_id }
  if is_dataload_process
    params['GDC_DATALOAD_DATASETS'] = dataload_datasets
    params['GDC_DE_SYNCHRONIZE_ALL'] = de_synchronize_all if de_synchronize_all
  else
    params['EXECUTABLE'] = executable
  end

  default_opts = {
    :type => 'MSETL',
    :timezone => 'UTC',
    :state => 'ENABLED',
    :params => params,
    # :reschedule => nil
  }

  schedule.name = options[:name]
  schedule.set_trigger(trigger)
  schedule.trigger_execution_status = options[:trigger_execution_status]
  schedule.params = default_opts[:params].merge(options[:params] || {})
  schedule.hidden_params = options[:hidden_params] || {}
  schedule.timezone = options[:timezone] || default_opts[:timezone]
  schedule.state = options[:state] || default_opts[:state]
  schedule.schedule_type = options[:type] || default_opts[:type]
  schedule.reschedule = options[:reschedule] if options[:reschedule]
  schedule
end
new(json) click to toggle source

Initializes object from raw json

@param json [Object] Raw JSON @return [GoodData::Schedule] New GoodData::Schedule instance

Calls superclass method
# File lib/gooddata/models/schedule.rb, line 119
def initialize(json)
  json = GoodData::Helpers.stringify_keys(json)
  super
  @json = json
  self.params = GoodData::Helpers.decode_params(json['schedule']['params'] || {})
  self.hidden_params = GoodData::Helpers.decode_params(json['schedule']['hiddenParams'] || {})
end

Public Instance Methods

==(other) click to toggle source
# File lib/gooddata/models/schedule.rb, line 528
def ==(other)
  other.respond_to?(:uri) && other.uri == uri && other.respond_to?(:to_hash) && other.to_hash == to_hash
end
after() click to toggle source
# File lib/gooddata/models/schedule.rb, line 127
def after
  project.schedules(trigger_id) if trigger_id
end
after=(schedule) click to toggle source
# File lib/gooddata/models/schedule.rb, line 131
def after=(schedule)
  fail 'After trigger has to be a schedule object' unless schedule.is_a?(Schedule)
  json['schedule']['triggerScheduleId'] = schedule.obj_id
  @json['schedule']['cron'] = nil
  @dirty = true
end
cron() click to toggle source

Returns execution cron settings

@return [String] Cron settings

# File lib/gooddata/models/schedule.rb, line 260
def cron
  @json['schedule']['cron']
end
cron=(new_cron) click to toggle source

Assigns execution cron settings

@param new_cron [String] Cron settings to be set

# File lib/gooddata/models/schedule.rb, line 267
def cron=(new_cron)
  @json['schedule']['cron'] = new_cron
  @json['schedule']['triggerScheduleId'] = nil
  @dirty = true
end
delete() click to toggle source

Deletes schedule

# File lib/gooddata/models/schedule.rb, line 139
def delete
  saved? ? client.delete(uri) : nil
end
disable() click to toggle source

Disables the schedule.

@return [GoodData::Schedule]

# File lib/gooddata/models/schedule.rb, line 146
def disable
  @json['schedule']['state'] = 'DISABLED'
  @dirty = true
  self
end
disable!() click to toggle source

Disables and saves the schedule.

@return [Boolean]

# File lib/gooddata/models/schedule.rb, line 155
def disable!
  disable
  save
end
disabled?() click to toggle source

Is schedule disabled?

@return [Boolean]

# File lib/gooddata/models/schedule.rb, line 163
def disabled?
  state == 'DISABLED'
end
enable() click to toggle source

Enables the schedule

@return [GoodData::Schedule]

# File lib/gooddata/models/schedule.rb, line 177
def enable
  @json['schedule']['state'] = 'ENABLED'
  @dirty = true
  self
end
enable!() click to toggle source

Enables and saves the schedule

@return [GoodData::Schedule]

# File lib/gooddata/models/schedule.rb, line 186
def enable!
  enable
  save
end
enabled?() click to toggle source

Is schedule enabled?

@return [Boolean]

# File lib/gooddata/models/schedule.rb, line 170
def enabled?
  !disabled?
end
executable() click to toggle source

Returns execution executable

@return [String] Executable (graph) name

# File lib/gooddata/models/schedule.rb, line 310
def executable
  @json['schedule']['params']['EXECUTABLE']
end
executable=(new_executable) click to toggle source

Assigns execution executable

@param new_executable [String] Executable to be set.

# File lib/gooddata/models/schedule.rb, line 317
def executable=(new_executable)
  @json['schedule']['params']['EXECUTABLE'] = new_executable
  @dirty = true
end
execute(opts = {}) click to toggle source

Executes schedule

@param [Hash] opts execution options. @option opts [Boolean] :wait Wait for execution result @return [Object] Raw Response

# File lib/gooddata/models/schedule.rb, line 196
def execute(opts = {})
  return nil unless saved?
  opts = { :wait => true }.merge(opts)
  data = {
    :execution => {}
  }
  res = client.post(execution_url, data)
  execution = client.create(GoodData::Execution, res, client: client, project: project)

  return execution unless opts[:wait]
  execution.wait_for_result(opts)
end
execution_url() click to toggle source

Returns execution URL

@return [String] Executions URL

# File lib/gooddata/models/schedule.rb, line 212
def execution_url
  saved? ? @json['schedule']['links']['executions'] : nil
end
executions() click to toggle source

Returns enumerator of executions

@return [Array] Raw Executions JSON

# File lib/gooddata/models/schedule.rb, line 325
def executions
  return nil unless @json
  url = @json['schedule']['links']['executions']
  Enumerator.new do |y|
    loop do
      res = client.get url
      res['executions']['paging']['next']
      res['executions']['items'].each do |execution|
        y << client.create(Execution, execution, :project => project)
      end
      url = res['executions']['paging']['next']
      break unless url
    end
  end
end
hidden_params() click to toggle source

Returns hidden_params as Hash

@return [Hash] Hidden Parameters

# File lib/gooddata/models/schedule.rb, line 344
def hidden_params
  @json['schedule']['hiddenParams']
end
hidden_params=(new_hidden_params = {}) click to toggle source

Assigns hidden parameters

@param new_hidden_param [String] Hidden parameters to be set

# File lib/gooddata/models/schedule.rb, line 375
def hidden_params=(new_hidden_params = {})
  @json['schedule']['hiddenParams'] = GoodData::Helpers.stringify_values(new_hidden_params)
  @dirty = true
  self
end
name() click to toggle source
# File lib/gooddata/models/schedule.rb, line 501
def name
  json['schedule']['name']
end
name=(name) click to toggle source
# File lib/gooddata/models/schedule.rb, line 505
def name=(name)
  json['schedule']['name'] = name
  @dirty = true
  self
end
params() click to toggle source

Returns params as Hash

@return [Hash] Parameters

# File lib/gooddata/models/schedule.rb, line 384
def params
  @json['schedule']['params']
end
params=(new_params = {}) click to toggle source

Assigns execution parameters

@param params [String] Params to be set

# File lib/gooddata/models/schedule.rb, line 391
def params=(new_params = {})
  default_params = {
    'PROCESS_ID' => process_id,
    'EXECUTABLE' => executable
  }
  @json['schedule']['params'] = default_params.merge(GoodData::Helpers.stringify_values(new_params))
  @dirty = true
  self
end
process() click to toggle source

Returns execution process related to this schedule

@return [GoodData::Process] Process ID

# File lib/gooddata/models/schedule.rb, line 291
def process
  project.processes(process_id)
end
process_id() click to toggle source

Returns execution process ID

@return [String] Process ID

# File lib/gooddata/models/schedule.rb, line 298
def process_id
  @json['schedule']['params']['PROCESS_ID']
end
process_id=(new_project_id) click to toggle source
# File lib/gooddata/models/schedule.rb, line 302
def process_id=(new_project_id)
  @json['schedule']['params']['PROCESS_ID'] = new_project_id
  @dirty = true
end
reschedule() click to toggle source

Returns reschedule settings

@return [Integer] Reschedule settings

# File lib/gooddata/models/schedule.rb, line 276
def reschedule
  @json['schedule']['reschedule']
end
reschedule=(new_reschedule) click to toggle source

Assigns execution reschedule settings

@param new_reschedule [Integer] Reschedule settings to be set

# File lib/gooddata/models/schedule.rb, line 283
def reschedule=(new_reschedule)
  @json['schedule']['reschedule'] = new_reschedule
  @dirty = true
end
rewrite_deprecated_params() click to toggle source
# File lib/gooddata/models/schedule.rb, line 401
def rewrite_deprecated_params
  params['EXECUTABLE'] = params.delete('GRAPH') if params['GRAPH']
end
save() click to toggle source

Saves object if dirty

@return [Boolean] True if saved

# File lib/gooddata/models/schedule.rb, line 408
def save
  fail 'A timezone has to be provided' if timezone.blank?
  fail 'Schedule type has to be provided' if schedule_type.blank?
  rewrite_deprecated_params
  if @dirty
    if saved?
      res = client.put(uri, to_update_payload)
      @json = Schedule.new(res).json
    else
      res = client.post "/gdc/projects/#{project.pid}/schedules", to_update_payload
      fail 'Unable to create new schedule' if res.nil?
      new_obj_json = client.get res['schedule']['links']['self']
      @json = Schedule.new(new_obj_json).json
    end
    @dirty = false
  end
  self
end
schedule_type() click to toggle source
# File lib/gooddata/models/schedule.rb, line 449
def schedule_type
  json['schedule']['type']
end
schedule_type=(type) click to toggle source
# File lib/gooddata/models/schedule.rb, line 453
def schedule_type=(type)
  json['schedule']['type'] = type
  @dirty = true
  self
end
set_hidden_parameter(k, v) click to toggle source

Updates hidden params at key k with val v

@param k [String] key @param v [Object] value @return [GoodData::Schedule] Returns self

# File lib/gooddata/models/schedule.rb, line 443
def set_hidden_parameter(k, v)
  hidden_params[k] = v
  @dirty = true
  self
end
set_parameter(k, v) click to toggle source

Updates params at key k with val v

@param k [String] key @param v [Object] value @return [GoodData::Schedule] Returns self

# File lib/gooddata/models/schedule.rb, line 432
def set_parameter(k, v)
  params[k] = v
  @dirty = true
  self
end
set_trigger(trigger) click to toggle source
# File lib/gooddata/models/schedule.rb, line 511
def set_trigger(trigger) # rubocop:disable Style/AccessorMethodName
  if trigger.is_a?(String) && trigger =~ /[a-fA-Z0-9]{24}/
    self.trigger_id = trigger
  elsif trigger.is_a?(GoodData::Schedule)
    self.trigger_id = trigger.obj_id
  else
    self.cron = trigger
  end
end
state() click to toggle source

Returns execution state

@return [String] Execution state

# File lib/gooddata/models/schedule.rb, line 219
def state
  @json['schedule']['state']
end
state=(a_state) click to toggle source
# File lib/gooddata/models/schedule.rb, line 223
def state=(a_state)
  @json['schedule']['state'] = a_state
end
time_based?() click to toggle source
# File lib/gooddata/models/schedule.rb, line 459
def time_based?
  cron != nil
end
timezone() click to toggle source

Returns execution timezone

@return [String] Execution timezone

# File lib/gooddata/models/schedule.rb, line 230
def timezone
  @json['schedule']['timezone']
end
timezone=(new_timezone) click to toggle source

Assigns execution timezone

@param new_timezone [String] Timezone to be set

# File lib/gooddata/models/schedule.rb, line 237
def timezone=(new_timezone)
  @json['schedule']['timezone'] = new_timezone
  @dirty = true
end
to_hash() click to toggle source
# File lib/gooddata/models/schedule.rb, line 463
def to_hash
  {
    name: name,
    type: type,
    state: state,
    params: params,
    hidden_params: hidden_params,
    cron: cron,
    trigger_id: trigger_id,
    trigger_execution_status: trigger_execution_status,
    timezone: timezone,
    uri: uri,
    reschedule: reschedule,
    executable: executable,
    process_id: process_id
  }
end
to_update_payload() click to toggle source
# File lib/gooddata/models/schedule.rb, line 532
def to_update_payload
  res = {
    'schedule' => {
      'name' => name,
      'type' => type,
      'state' => state,
      'timezone' => timezone,
      'cron' => cron,
      'triggerScheduleId' => trigger_id,
      'params' => GoodData::Helpers.encode_public_params(params),
      'hiddenParams' => GoodData::Helpers.encode_hidden_params(hidden_params)
    }
  }
  res['schedule']['triggerExecutionStatus'] = trigger_execution_status if trigger_execution_status
  res['schedule']['reschedule'] = reschedule if reschedule

  res
end
trigger_execution_status() click to toggle source
# File lib/gooddata/models/schedule.rb, line 491
def trigger_execution_status
  json['schedule']['triggerExecutionStatus']
end
trigger_execution_status=(trigger_execution_status) click to toggle source
# File lib/gooddata/models/schedule.rb, line 495
def trigger_execution_status=(trigger_execution_status)
  json['schedule']['triggerExecutionStatus'] = trigger_execution_status
  @dirty = true
  self # rubocop:disable Lint/Void
end
trigger_id() click to toggle source
# File lib/gooddata/models/schedule.rb, line 481
def trigger_id
  json['schedule']['triggerScheduleId']
end
trigger_id=(a_trigger) click to toggle source
# File lib/gooddata/models/schedule.rb, line 485
def trigger_id=(a_trigger)
  json['schedule']['triggerScheduleId'] = a_trigger
  @dirty = true
  self
end
type() click to toggle source

Returns execution type

@return [String] Execution type

# File lib/gooddata/models/schedule.rb, line 245
def type
  @json['schedule']['type']
end
type=(new_type) click to toggle source

Assigns execution type

@param new_type [String] Execution type to be set

# File lib/gooddata/models/schedule.rb, line 252
def type=(new_type)
  @json['schedule']['type'] = new_type
  @dirty = true
end
update_hidden_params(params_to_merge) click to toggle source

Updates hidden params by merging the current params with new ones

@param params_to_merge [Hash] params @return [GoodData::Schedule] Returns self

# File lib/gooddata/models/schedule.rb, line 364
def update_hidden_params(params_to_merge)
  params_to_merge.each do |k, v|
    set_hidden_parameter(k, v)
  end
  @dirty = true
  self
end
update_params(params_to_merge) click to toggle source

Updates params by merging the current params with new ones

@param params_to_merge [Hash] params @return [GoodData::Schedule] Returns self

# File lib/gooddata/models/schedule.rb, line 352
def update_params(params_to_merge)
  params_to_merge.each do |k, v|
    set_parameter(k, v)
  end
  @dirty = true
  self
end
uri() click to toggle source

Returns URL

@return [String] Schedule URL

# File lib/gooddata/models/schedule.rb, line 524
def uri
  @json['schedule']['links']['self'] if @json && @json['schedule'] && @json['schedule']['links']
end