Module: Export::ProjectData::Tsv

Defined in:
lib/export/project_data/tsv.rb

Overview

!! !! Only exports ApplicationEnumeration.project_data_classes !! See the .sql version for code that would handle Community data !!

Class Method Summary collapse

Class Method Details

.copy_table(klass, zipfile, query) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/export/project_data/tsv.rb', line 22

def self.copy_table(klass, zipfile, query)
  conn = get_connection

  Tempfile.create do |table_file|
    query = "COPY ( #{query.to_sql} ) TO STDOUT WITH (FORMAT CSV, DELIMITER E'\t', HEADER, ENCODING 'UTF8')"

    conn.copy_data(query) do
      while row = conn.get_copy_data
        table_file.write(row.force_encoding('UTF-8'))
      end
    end

    table_file.flush
    table_file.rewind
    filename = Zaru::sanitize!("#{klass.name}_#{DateTime.now}.tsv")

    # ?! Does not queue the reads until the zipfile loop closes,
    # so tempfiles may dispappear before they can be read
    #
    # zipfile.add(filename, table_file.path )

    zipfile.get_output_stream(filename) { |f| f.write table_file.read }
  end
end

.download(target_project) ⇒ Object

TODO - DRY with generic params passing

Returns:

  • Download a completely built Download, but unsaved



50
51
52
53
54
55
56
# File 'lib/export/project_data/tsv.rb', line 50

def self.download(target_project)
  file_path = export(target_project)

  d = stub_download(target_project)
  d.source_file_path = file_path
  d
end

.download_async(target_project) ⇒ Object

Returns Download.

Returns:

  • Download



59
60
61
62
63
# File 'lib/export/project_data/tsv.rb', line 59

def self.download_async(target_project)
  d = stub_download(target_project)
  DownloadProjectTsvJob.perform_later(target_project, d)
  d
end

.export(project) ⇒ Object

Returns path to the zipped data in /tmp.

Returns:

  • path to the zipped data in /tmp



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/export/project_data/tsv.rb', line 9

def self.export(project)
  zip_file_path = "/tmp/_#{SecureRandom.hex(8)}_project_tsv.zip"

  Zip::File.open(zip_file_path, Zip::File::CREATE) do |zipfile|
    ApplicationEnumeration.project_data_classes.each do |k|
      q = k.where(project_id: project.to_param)
      next if !q.any?
      copy_table(k, zipfile, q)
    end
  end
  zip_file_path
end

.get_connectionObject

TODO: move to ::Export module



78
79
80
# File 'lib/export/project_data/tsv.rb', line 78

def self.get_connection
  ActiveRecord::Base.connection.raw_connection
end

.stub_download(target_project) ⇒ Object

Returns Download everything except source_file_path.

Returns:

  • Download everything except source_file_path



67
68
69
70
71
72
73
74
75
# File 'lib/export/project_data/tsv.rb', line 67

def self.stub_download(target_project)
  Download::ProjectDump::Tsv.create!(
     name: "#{target_project.name} export on #{Time.current}.",
     description: 'A zip file containing TSV dump of project-specific data.',
     filename: Zaru::sanitize!("#{target_project.name}_tsv.zip").gsub(' ', '_').downcase,
     expires: 2.days.from_now,
     is_public: false
   )
end