Class: DatasetRecordsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration, ZipTricks::RailsStreaming
Defined in:
app/controllers/dataset_records_controller.rb

Constant Summary

Constants included from ProjectsHelper

ProjectsHelper::CLASSIFIER, ProjectsHelper::CLASSIFIER_ANNOTATION

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#cumulative_gb_per_year, #document_cumulative_gb_per_year, #document_gb_per_year, #gb_per_year, #image_cumulative_gb_per_year, #image_gb_per_year, #invalid_object, #project_classification, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form, #taxonworks_classification, #week_in_review_graphs

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#autocomplete_data_fieldsObject



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

def autocomplete_data_fields
  render json: {} and return if params[:field].blank? || params[:value].blank?

  import_dataset = ImportDataset.where(project_id: sessions_current_project_id).find(params[:import_dataset_id])

  values = import_dataset.core_records_fields.where(dataset_record: filtered_records)
    .at(params[:field].to_i).with_prefix_value(params[:value])
    .select(:value).distinct
    .page(params[:page]).per(params[:per] || 10)
    .map { |f| f.value }

  render json: values, status: :ok
end

#createObject

POST /dataset_records POST /dataset_records.json



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/dataset_records_controller.rb', line 28

def create
  @dataset_record = DatasetRecord.new(dataset_record_params)

  respond_to do |format|
    if @dataset_record.save
      format.html { redirect_to @dataset_record, notice: 'Dataset record was successfully created.' }
      format.json { render :show, status: :created, location: @dataset_record }
    else
      format.html { render :new }
      format.json { render json: @dataset_record.errors, status: :unprocessable_entity }
    end
  end
end

#dataset_record_paramsObject (private)

Only allow a list of trusted parameters through.



101
102
103
# File 'app/controllers/dataset_records_controller.rb', line 101

def dataset_record_params
  params.require(:dataset_record).permit(data_fields: {})
end

#destroyObject

DELETE /dataset_records/1 DELETE /dataset_records/1.json



83
84
85
86
87
88
89
# File 'app/controllers/dataset_records_controller.rb', line 83

def destroy
  @dataset_record.destroy
  respond_to do |format|
    format.html { redirect_to dataset_records_url, notice: 'Dataset record was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#filtered_recordsObject (private)



109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/dataset_records_controller.rb', line 109

def filtered_records
  dataset_records = import_dataset.core_records
  params[:filter]&.each do |key, value|
    dataset_records = dataset_records.where(
      id: import_dataset.core_records_fields.at(key.to_i).having_value(value).select(:dataset_record_id)
    )
  end

  params[:status].blank? ? dataset_records : dataset_records.where(status: params[:status])
end

#import_datasetObject (private)



105
106
107
# File 'app/controllers/dataset_records_controller.rb', line 105

def import_dataset
  ImportDataset.where(project_id: sessions_current_project_id).find(params[:import_dataset_id])
end

#indexObject

GET /dataset_records GET /dataset_records.json



10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/dataset_records_controller.rb', line 10

def index
  respond_to do |format|
    format.json do
      @import_dataset = import_dataset
      @dataset_records = filtered_records.order(id: :asc).page(params[:page]).per(params[:per] || 100) #.preload_fields
      @filters = params[:filter]
    end
    format.zip { render_zip }
  end
end

#render_zipObject (private)



120
121
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
# File 'app/controllers/dataset_records_controller.rb', line 120

def render_zip
  response.headers[Rack::ETAG] = SecureRandom.hex # Prevents Rack::ETAG from buffering the response

  filename = Zaru::sanitize!("#{import_dataset.description}_#{DateTime.now}.tsv").gsub(' ', '_').downcase
  headers = import_dataset.["core_headers"]

  response.headers["Content-Disposition"] = "attachment; filename=\"#{filename}.zip\""

  zip_tricks_stream do |zip|
    zip.write_deflated_file('filters.txt') do |sink|
      sink.write "Status: #{params[:status] || 'Any'}\n---\n"
      params[:filter].each {|k, v| sink.write("#{headers[k.to_i]}: #{v}\n") } if params[:filter]
    end

    zip.write_deflated_file(filename) do |sink|
      sink.write CSV.generate_line([
        'Status', 'error_data', *headers
      ], col_sep: "\t")

      filtered_records.find_each do |row|
        sink.write CSV.generate_line([
          row.status, row.&.dig('error_data', 'messages'), *row.data_fields
        ], col_sep: "\t")
      end
    end
  end
end

#set_dataset_recordObject (private)

Use callbacks to share common setup or constraints between actions.



93
94
95
96
97
98
# File 'app/controllers/dataset_records_controller.rb', line 93

def set_dataset_record
  @dataset_record = DatasetRecord.where(
    project_id: sessions_current_project_id,
    import_dataset_id: params[:import_dataset_id]
  ).find(params[:id])
end

#set_field_valueObject

PATCH/PUT /dataset_records/set_field_value



58
59
60
61
62
63
64
65
# File 'app/controllers/dataset_records_controller.rb', line 58

def set_field_value
  filtered_records.find_each(batch_size: 10000) do |record| #.preload_fields
    record.set_data_field(Integer(params[:field]), params[:value])
    record.save!
  end unless params[:filter].blank?

  render json: true, status: :ok
end

#showObject

GET /dataset_records/1 GET /dataset_records/1.json



23
24
# File 'app/controllers/dataset_records_controller.rb', line 23

def show
end

#updateObject

PATCH/PUT /dataset_records/1 PATCH/PUT /dataset_records/1.json



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/dataset_records_controller.rb', line 44

def update
  respond_to do |format|

    JSON.parse(params[:data_fields]).each { |index, value| @dataset_record.set_data_field(index.to_i, value) }

    if @dataset_record.save
      format.json { render :show, status: :ok }
    else
      format.json { render json: @dataset_record.errors, status: :unprocessable_entity }
    end
  end
end