-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require "csv" | ||
|
||
# ExportTableauJob | ||
class ExportTableauJob < ApplicationJob | ||
queue_as :priority | ||
|
||
def perform(current_user) | ||
logger.debug("\n\n Background Job: ♞") | ||
logger.debug("User: #{current_user.inspect}") | ||
logger.debug("Export Service: Tableau Export") | ||
logger.debug("\n\n") | ||
|
||
# Test broadcast | ||
ActionCable.server.broadcast("export_channel", {data: "Hello from Export Tableau Job!"}) | ||
|
||
# Send progress | ||
file_content = ExportTableauService.call | ||
|
||
# Write into tempfile | ||
@tempfile = Tempfile.new(["export-#{Time.zone.today}", ".csv"]).tap do |file| | ||
CSV.open(file, "wb") do |csv| | ||
file_content.each do |row| | ||
csv << row | ||
end | ||
end | ||
end | ||
|
||
# Create notification | ||
# Message: "Download Type|Row Count|Button Label" | ||
notification = ExportNotification.with(message: "CSV (Tableau)|#{ActionController::Base.helpers.number_with_delimiter(file_content.size - 1)} rows|CSV") | ||
|
||
# Deliver notification | ||
notification.deliver(current_user) | ||
|
||
# Attach CSV file (can only attach after persisted) | ||
notification.record.file.attach(io: @tempfile, filename: "geomg-export-#{Time.zone.today}.csv", | ||
content_type: "text/csv") | ||
|
||
# Update UI | ||
ActionCable.server.broadcast("export_channel", { | ||
data: "Notification ready!", | ||
actions: [ | ||
{ | ||
method: "RefreshNotifications", | ||
payload: current_user.notifications.unread.count | ||
} | ||
] | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
# ExportTableauService | ||
class ExportTableauService | ||
def self.call | ||
ActionCable.server.broadcast("export_channel", {progress: 0}) | ||
|
||
total = Document.count | ||
count = 0 | ||
slice_count = 1000 | ||
csv_file = [] | ||
|
||
Rails.logger.debug { "\n\nExportTableauService: #{total}\n\n" } | ||
|
||
csv_file << [ | ||
"Title", | ||
"Provider", | ||
"Resource Class", | ||
"Resource Type", | ||
"Index Year", | ||
"Spatial Coverage", | ||
"B1G Image", | ||
"ID", | ||
"Download", | ||
"Language" | ||
] | ||
|
||
Document.in_batches do |slice| | ||
# Broadcast progress percentage | ||
count += slice_count | ||
progress = ((count.to_f / total) * 100).round | ||
progress = 100 if progress > 100 | ||
|
||
slice.each do |row| | ||
csv_file << [ | ||
row.title, | ||
row.schema_provider_s, | ||
row.gbl_resourceClass_sm&.join("|"), | ||
row.gbl_resourceType_sm&.join("|"), | ||
row.gbl_indexYear_im&.join("|"), | ||
row.dct_spatial_sm&.join("|"), | ||
row.b1g_image_ss, | ||
row.geomg_id_s, | ||
row.dct_references_s.find { |ref| ref.category == "download" }&.value, | ||
row.dct_language_sm&.join("|") | ||
] | ||
end | ||
|
||
ActionCable.server.broadcast("export_channel", {progress: progress}) | ||
end | ||
|
||
csv_file | ||
end | ||
end |