Skip to content

Commit

Permalink
Merge pull request #221 from curationexperts/turbo
Browse files Browse the repository at this point in the history
Display live import updates
  • Loading branch information
mark-dce authored May 23, 2024
2 parents b59e7e6 + 1bcd3e3 commit 5ad56fd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
13 changes: 11 additions & 2 deletions app/jobs/import_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ def initialize(ingest)

def <<(json)
@statuses << json
# use update_column for fast updates - bypass validations & callbacks becuse we're only incremeting a counter
@ingest.update_column(:processed, processed) # rubocop:disable Rails/SkipsModelValidations
broadcast_status
end

def processed
Expand All @@ -110,6 +109,7 @@ def final_status
def save
@ingest.update(status: final_status, processed: processed, error_count: errored)
attach_report
broadcast_status
end

# Assemble artifacts into a pretty JSON file and attach to the ingest record
Expand All @@ -120,6 +120,8 @@ def attach_report
filename: "import#{@ingest.id}.json",
content_type: 'application/json'
)
# Update browser clients watching the import (using Turbo Streams)
@ingest.broadcast_replace_to 'ingests', partial: 'admin/ingests/report', target: "report_ingest_#{@ingest.id}"
end

def context
Expand All @@ -135,6 +137,13 @@ def context
}
end

def broadcast_status
# Update the processed item count in the database and broadcast to any watching clients
@ingest.update_column(:processed, processed) # rubocop:disable Rails/SkipsModelValidations
# Update browser clients watching the import (using Turbo Streams)
@ingest.broadcast_replace_to 'ingests', partial: 'admin/ingests/status', target: "status_ingest_#{@ingest.id}"
end

def record_error(err, doc = nil, index = nil)
ImportJob.logger.error err
@errors << { id: "manifest_record_#{index || 'none'}", error_class: err.class,
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/ingests/_report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="<%= dom_id(ingest, :report) -%>">
<%= link_to ingest.report.filename, rails_blob_path(ingest.report, disposition: "attachment") if ingest.report.attached? %>
</div>
3 changes: 3 additions & 0 deletions app/views/admin/ingests/_status.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="<%= dom_id(ingest, :status) -%>">
<%= progress_bar(ingest.processed, ingest.size, ingest.status, ingest.error_count) %>
</div>
18 changes: 4 additions & 14 deletions app/views/admin/ingests/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%= turbo_stream_from 'ingests' -%>

<div id="ingests">
<h1>Ingests</h1>
<%= link_to "Submit new job", new_ingest_path, id: 'new_ingest', class: 'btn btn-primary' %>
Expand All @@ -17,8 +19,8 @@
<td class='batch_id'><%= ingest.id %> <%= link_to 'view', url_for(ingest) %> </td>
<td class='owner'><%= ingest.user.display_name %></td>
<td class='manifest'><%= link_to ingest.manifest.filename, rails_blob_path(ingest.manifest, disposition: "attachment") %></td>
<td class='report'><%= link_to ingest.report.filename, rails_blob_path(ingest.report, disposition: "attachment") if ingest.report.attached? %></td>
<td class='status'><%= progress_bar(ingest.processed, ingest.size, ingest.status, ingest.error_count) %></td>
<td class='report'><%= render 'report', ingest: ingest %></td>
<td class='status'><%= render 'status', ingest: ingest %></td>
</tr>
<% end %></tbody>
<tfoot>
Expand All @@ -32,15 +34,3 @@
</tfoot>
</table>
</div>

<!--Hack to get job processing updates-->
<!--TODO: Use Turbo streams for status badge updates-->
<% if @ingests.map(&:status).include?('processing') %>
<script>
function autoRefresh() {
window.location = window.location.href;
}
setInterval('autoRefresh()', 2000);
</script>
<% end %>

0 comments on commit 5ad56fd

Please sign in to comment.