Class: DatasetsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/datasets_controller.rb

Overview

Display, modify, delete, and analyze datasets belonging to a given user

This controller is responsible for the handling of the datasets which belong to a given user. It displays the user's list of datasets, and handles the starting and management of the user's background analysis tasks.

See Also:

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#devise_parameter_sanitizer, #ensure_trailing_slash, #set_locale, #set_timezone, #trailing_slash?

Instance Method Details

- (undefined) add

Add a single document to a dataset

Returns:

  • (undefined)


101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/datasets_controller.rb', line 101

def add
  # This isn't a member action, so that it can be called easily from
  # a form.  Get the id from :dataset_id, not :id.
  @dataset = current_user.datasets.find(params[:dataset_id])
  fail ActiveRecord::RecordNotFound unless @dataset
  @document = Document.find(params[:shasum])
  fail ActiveRecord::RecordNotFound unless @document

  # No reason for this to be a delayed job, just do the create
  @dataset.entries.create({ shasum: params[:shasum] })
  redirect_to dataset_path(@dataset)
end

- (undefined) create

Create a new dataset in the database

Returns:

  • (undefined)


68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/datasets_controller.rb', line 68

def create
  Resque.enqueue(Jobs::CreateDataset,
                 user_id: current_user.to_param,
                 name: dataset_params[:name],
                 q: params[:q],
                 fq: params[:fq],
                 defType: params[:defType])

  redirect_to datasets_path, notice: I18n.t('datasets.create.building')
end

- (undefined) dataset_list

Show the list of datasets for this user

This list needs to be updated live, as the datasets are being created in the background. This action is to be called via AJAX.

Returns:

  • (undefined)


26
27
28
29
# File 'app/controllers/datasets_controller.rb', line 26

def dataset_list
  @datasets = current_user.datasets
  render layout: false
end

- (ActionController::Parameters) dataset_params (private)

Whitelist acceptable dataset parameters

Returns:

  • (ActionController::Parameters)

    acceptable parameters for mass-assignment



245
246
247
# File 'app/controllers/datasets_controller.rb', line 245

def dataset_params
  params.require(:dataset).permit(:name)
end

- (undefined) delete

Show a confirmation box for deleting a dataset

Returns:

  • (undefined)


59
60
61
62
63
# File 'app/controllers/datasets_controller.rb', line 59

def delete
  @dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless @dataset
  render layout: 'dialog'
end

- (undefined) destroy

Delete a dataset from the database

Returns:

  • (undefined)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/datasets_controller.rb', line 82

def destroy
  @dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless @dataset

  if params[:cancel]
    redirect_to @dataset
    return
  end

  Resque.enqueue(Jobs::DestroyDataset,
                 user_id: current_user.to_param,
                 dataset_id: params[:id])

  redirect_to datasets_path
end

- (undefined) index

Show all of the current user's datasets

Returns:

  • (undefined)


17
# File 'app/controllers/datasets_controller.rb', line 17

def index; end

- (undefined) new

Show the form for creating a new dataset

Returns:

  • (undefined)


51
52
53
54
# File 'app/controllers/datasets_controller.rb', line 51

def new
  @dataset = current_user.datasets.build
  render layout: 'dialog'
end

- (undefined) render_job_view(klass, view, format = 'html') (private)

Render a job view, given the class name and view name

Parameters:

  • klass (Class)

    the job class

  • view (String)

    the view to render

Returns:

  • (undefined)


229
230
231
232
233
234
235
236
237
238
239
# File 'app/controllers/datasets_controller.rb', line 229

def render_job_view(klass, view, format = 'html')
  # Find the partial
  klass.view_paths.each do |p|
    path = File.join(p, "#{view}.#{format}.haml")
    if File.exist? path
      return render file: path
    end
  end

  fail "Cannot find job view #{view} for class #{klass}"
end

- (undefined) show

Show information about the requested dataset

This action also includes links for users to perform various analysis tasks on the dataset.

Returns:

  • (undefined)


38
39
40
41
42
43
44
45
46
# File 'app/controllers/datasets_controller.rb', line 38

def show
  @dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless @dataset

  if params[:clear_failed] && @dataset.analysis_tasks.failed.count > 0
    @dataset.analysis_tasks.failed.destroy_all
    flash[:notice] = t('datasets.show.deleted')
  end
end

- (undefined) task_destroy

Delete an analysis task

This action deletes a given analysis task and its associated files.

Returns:

  • (undefined)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/datasets_controller.rb', line 187

def task_destroy
  dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless dataset

  if params[:cancel]
    redirect_to dataset
    return
  end

  task = dataset.analysis_tasks.find(params[:task_id])
  fail ActiveRecord::RecordNotFound unless task

  task.destroy
  redirect_to dataset_path
end

- (undefined) task_download

Download a file from an analysis task

This method sends a user a result file from an analysis task. It requires a dataset ID and a task ID.

Returns:

  • (undefined)


210
211
212
213
214
215
216
217
218
219
220
# File 'app/controllers/datasets_controller.rb', line 210

def task_download
  dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless dataset
  task = dataset.analysis_tasks.find(params[:task_id])
  fail ActiveRecord::RecordNotFound unless task
  fail ActiveRecord::RecordNotFound unless task.result_file_size

  send_data(task.result.file_contents(:original),
            filename: task.result_file_name,
            type: task.result_content_type)
end

- (undefined) task_list

Show the list of analysis tasks for this dataset

This list needs to be updated live, as the tasks are running in the background, so we split this off to a small AJAX action.

Returns:

  • (undefined)


121
122
123
124
125
126
# File 'app/controllers/datasets_controller.rb', line 121

def task_list
  @dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless @dataset

  render layout: false
end

- (undefined) task_start

Start an analysis task for this dataset

This method dynamically determines the appropriate background job to start and starts it. It requires a dataset ID.

Returns:

  • (undefined)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/datasets_controller.rb', line 135

def task_start
  dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless dataset
  klass = AnalysisTask.job_class(params[:class])

  # Create an analysis task
  task = dataset.analysis_tasks.create(name: params[:class],
                                       job_type: params[:class])

  # Put the job parameters together out of the job hash
  job_params = {}
  job_params = params[:job_params].to_hash if params[:job_params]
  job_params.symbolize_keys!
  job_params[:user_id] = current_user.to_param
  job_params[:dataset_id] = dataset.to_param
  job_params[:task_id] = task.to_param

  # Enqueue the job
  Resque.enqueue(klass, job_params)
  redirect_to dataset_path(dataset)
end

- (undefined) task_view

Show a view from an analysis task

Background jobs are packaged with some of their own views. This controller action renders one of those views directly.

Returns:

  • (undefined)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/controllers/datasets_controller.rb', line 164

def task_view
  @dataset = current_user.datasets.find(params[:id])
  fail ActiveRecord::RecordNotFound unless @dataset
  fail ActiveRecord::RecordNotFound unless params[:view]

  if params[:class]
    klass = AnalysisTask.job_class(params[:class])
  else
    @task = @dataset.analysis_tasks.find(params[:task_id])
    fail ActiveRecord::RecordNotFound unless @task

    klass = @task.job_class
  end

  render_job_view(klass, params[:view], params[:format] || 'html')
end