Module: WorkflowRESTHelpers

Defined in:
lib/rbbt/rest/workflow/jobs.rb,
lib/rbbt/rest/workflow/locate.rb,
lib/rbbt/rest/workflow/render.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#workflow_resourcesObject

Returns the value of attribute workflow_resources.



2
3
4
# File 'lib/rbbt/rest/workflow/locate.rb', line 2

def workflow_resources
  @workflow_resources
end

Class Method Details

.workflow_resourcesObject



4
5
6
# File 'lib/rbbt/rest/workflow/locate.rb', line 4

def self.workflow_resources
  @workflow_resources ||= [Rbbt.share.views.find(:lib)]
end

Instance Method Details

#clean_job(workflow, job) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/rbbt/rest/workflow/jobs.rb', line 173

def clean_job(workflow, job)
  job.clean

  if ajax
    halt 200
  else
    redirect to(File.join("/", workflow.to_s, job.task_name.to_s))
  end
end

#complete_input_set(workflow, task, inputs) ⇒ Object



23
24
25
# File 'lib/rbbt/rest/workflow/jobs.rb', line 23

def complete_input_set(workflow, task, inputs)
  inputs.keys.sort === workflow.task_info(task.to_sym)[:inputs].sort
end

#consume_task_parameters(workflow, task, params = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rbbt/rest/workflow/jobs.rb', line 6

def consume_task_parameters(workflow, task, params = nil)
  task_inputs = workflow.task_info(task.to_sym)[:inputs]

  task_parameters = {}
  task_inputs.each do |input|

    input_val = consume_parameter(input, params)
    task_parameters[input] = input_val unless input_val.nil?

    # Param files
    input_val = consume_parameter(input.to_s + '__param_file', params)
    task_parameters[input.to_s + '__param_file'] = input_val unless input_val.nil?
  end

  task_parameters
end

#execution_type(workflow, task) ⇒ Object



41
42
43
44
45
# File 'lib/rbbt/rest/workflow/jobs.rb', line 41

def execution_type(workflow, task)
  export = type_of_export(workflow, task)
  return export if export == :exec or cache_type.nil?
  return cache_type if cache_type
end

#issue_job(workflow, task, jobname = nil, params = {}) ⇒ Object



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/rbbt/rest/workflow/jobs.rb', line 140

def issue_job(workflow, task, jobname = nil, params = {})
  inputs = prepare_job_inputs(workflow, task, params)
  job = workflow.job(task, jobname, inputs)

  job.clean if update == :clean

  execution_type = execution_type(workflow, task)
  case execution_type
  when :exec
    show_exec_result job.exec, workflow, task
  when :synchronous, :sync
    job.clean if update == :reload
    begin
      job.run(false) unless File.exists? job.info_file
      job_url = to(File.join("/", workflow.to_s, task, job.name)) 
      job_url += "?_format=#{@format}" if @format
      halt 200, job.name if format === :jobname
      redirect job_url
    rescue
      halt 500, $!.message
    end
  when :asynchronous, :async, nil
    job.clean if update == :reload
    job.fork unless File.exists? job.info_file
    job_url = to(File.join("/", workflow.to_s, task, job.name)) 
    job_url += "?_format=#{@format}" if @format
    halt 200, job.name if format === :jobname
    redirect job_url
  else
    raise "Unsupported execution_type: #{ execution_type }"
  end
end

#locate_workflow_template(template, workflow = nil, task = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbbt/rest/workflow/locate.rb', line 27

def locate_workflow_template(template, workflow = nil, task = nil)

  if workflow
    path = locate_workflow_template_from_resource(workflow.libdir.www.views.find, template, workflow, task)
    return path if path and path.exists?
  end

  workflow_resources.each do |resource|
    path = locate_workflow_template_from_resource(resource, template, workflow, task)
    return path if path and path.exists?
  end

  raise "Template not found: [#{ template }, #{workflow}, #{ task }]"
end

#locate_workflow_template_from_resource(resource, template, workflow = nil, task = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rbbt/rest/workflow/locate.rb', line 12

def locate_workflow_template_from_resource(resource, template, workflow = nil, task = nil)
  template += '.haml' unless template =~ /.+\..+/

  paths = []
  paths << resource[workflow][task][template] if task and workflow
  paths << resource[workflow][template] if workflow
  paths << resource[template]

  paths.each do |path|
    return path.find if path.exists?
  end 

  nil
end

#prepare_job_inputs(workflow, task, params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbbt/rest/workflow/jobs.rb', line 47

def prepare_job_inputs(workflow, task, params)
  inputs = workflow.task_info(task)[:inputs]
  input_types = workflow.task_info(task)[:input_types]

  task_inputs = {}
  inputs.each do |input|
    value = consume_parameter(input, params)
    param_file = consume_parameter(input.to_s + '__param_file', params)
    next if value.nil? and param_file.nil?
    type = input_types[input]

    fixed_value = fix_input(type, value, param_file)
    task_inputs[input] = fixed_value
  end

  task_inputs
end

#show_exec_result(result, workflow, task) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rbbt/rest/workflow/jobs.rb', line 71

def show_exec_result(result, workflow, task)
  case format
  when :html
    show_result_html result, workflow, task, nil
  when :json
    content_type "application/json"
    halt 200, result.to_json
  when :tsv
    content_type "text/tab-separated-values"
    halt 200, result.to_s
  when :literal, :raw
    content_type "text/plain"
    halt 200, result.to_s
  when :binary
    content_type "application/octet-stream"
    halt 200, result.to_s
  else
    raise "Unsupported format: #{ format }"
  end
end

#show_result(job, workflow, task) ⇒ Object



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rbbt/rest/workflow/jobs.rb', line 92

def show_result(job, workflow, task)
  case format.to_sym
  when :html
    show_result_html job.load, workflow, task, job.name, job
  when :table
    halt 200, tsv2html(job.path, :url => "/" << [workflow.to_s, task, job.name] * "/")
  when :entities
    tsv = tsv_process(load_tsv(job.path).first)
    list = tsv.values.flatten
    if not AnnotatedArray === list and Annotated === list.first
      list.first.annotate list 
      list.extend AnnotatedArray
    end
    type = list.annotation_types.last
    list_id = "TMP #{type} in #{ [workflow.to_s, task, job.name] * " - " }"
    Entity::List.save_list(type.to_s, list_id, list, user)
    redirect to(Entity::REST.entity_list_url(list_id, type))
  when :map
    tsv = tsv_process(load_tsv(job.path).first)
    type = tsv.keys.annotation_types.last
    column = tsv.fields.first
    map_id = "MAP #{type}-#{column} in #{ [workflow.to_s, task, job.name] * " - " }"
    Entity::Map.save_map(type.to_s, column, map_id, tsv, user)
    redirect to(Entity::REST.entity_map_url(map_id, type, column))
  when :json
    content_type "application/json"
    halt 200, job.load.to_json
  when :tsv
    content_type "text/tab-separated-values"
    job.path ? send_file(job.path) : halt(200, job.load.to_s)
  when :literal, :raw
    content_type "text/plain"
    job.path ? send_file(job.path) : halt(200, job.load.to_s)
  when :binary
    content_type "application/octet-stream"
    job.path ? send_file(job.path) : halt(200, job.load.to_s)
  when :excel
    require 'rbbt/tsv/excel'
    data = nil
    excel_file = TmpFile.tmp_file
    result = job.load
    result.excel(excel_file, :name => @excel_use_name,:sort_by => @excel_sort_by, :sort_by_cast => @excel_sort_by_cast)
    send_file excel_file, :type => 'application/vnd.ms-excel', :filename => job.clean_name + '.xls'
  else
    raise "Unsupported format: #{ format }"
  end
end

#show_result_html(result, workflow, task, jobname = nil, job = nil) ⇒ Object



65
66
67
68
69
# File 'lib/rbbt/rest/workflow/jobs.rb', line 65

def show_result_html(result, workflow, task, jobname = nil, job = nil)
  result_type = workflow.task_info(task)[:result_type]
  result_description = workflow.task_info(task)[:result_description]
  workflow_render('job_result', workflow, task, :result => result, :type => result_type, :description => result_description, :jobname => jobname, :job => job)
end

#type_of_export(workflow, task) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbbt/rest/workflow/jobs.rb', line 27

def type_of_export(workflow, task)
  task = task.to_sym
  case
  when workflow.exec_exports.include?(task)
    :exec
  when workflow.synchronous_exports.include?(task)
    :synchronous
  when workflow.asynchronous_exports.include?(task)
    :asynchronous
  else
    raise "No known export type for #{ workflow } #{ task }"
  end
end

#workflow_partial(template, workflow = nil, task = nil, params = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbbt/rest/workflow/render.rb', line 21

def workflow_partial(template, workflow = nil, task = nil, params = {})
  workflow = consume_parameter(:workflow, params) if workflow.nil?
  task     = consume_parameter(:task, params) if workflow.nil?

  template_file = locate_workflow_template(template, workflow, task)

  locals = params.dup
  locals[:workflow] = workflow if workflow
  locals[:task]     = task if task

  render_partial(template_file, locals)
end

#workflow_render(template, workflow = nil, task = nil, params = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rbbt/rest/workflow/render.rb', line 6

def workflow_render(template, workflow = nil, task = nil, params = {})
  workflow = consume_parameter(:workflow, params) if workflow.nil?
  task     = consume_parameter(:task, params) if workflow.nil?

  template_file = locate_workflow_template(template, workflow, task)

  locals = params.dup
  locals[:workflow] = workflow if workflow
  locals[:task]     = task if task

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file)
end