Module: AnalyticsOps::Reports::CSVExport

Defined in:
lib/analytics_ops/reports/csv_export.rb,
sig/analytics_ops.rbs

Overview

Atomic, spreadsheet-safe streaming export for report pages.

Class Method Summary collapse

Class Method Details

.write(path, pages) ⇒ ExportResult

Parameters:

  • path (String)
  • pages (Enumerable[Result])

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/analytics_ops/reports/csv_export.rb', line 17

def write(path, pages)
  destination = validated_path(path)
  temporary = File.join(
    File.dirname(destination),
    ".#{File.basename(destination)}.#{Process.pid}.#{SecureRandom.hex(6)}.tmp"
  )
  headers = nil
  row_count = nil
  File.open(temporary, File::WRONLY | File::CREAT | File::EXCL, 0o600) do |file|
    headers, row_count = write_pages(file, pages)
    file.flush
    file.fsync
  end
  File.rename(temporary, destination)
  File.chmod(0o600, destination)
  ExportResult.new(path: destination, rows: row_count, headers:)
rescue AnalyticsOps::Error
  raise
rescue SystemCallError => error
  raise ConfigurationError, "Cannot write CSV export: #{Redaction.message(error.message)}"
ensure
  File.unlink(temporary) if temporary && File.exist?(temporary)
end