Skip to content

Commit

Permalink
Add endpoint to view status of batch import (internetarchive#9682)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrini authored and SivanC committed Aug 20, 2024
1 parent cba1360 commit 96b83bd
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 26 deletions.
101 changes: 75 additions & 26 deletions openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,81 @@ msgstr ""
msgid "Not queued because they are already present in the queue:"
msgstr ""

#: batch_import.html
msgid "View Batch Status"
msgstr ""

#: batch_import_view.html
msgid "Batch Import Status"
msgstr ""

#: batch_import_view.html
msgid "Batch ID:"
msgstr ""

#: batch_import_view.html
msgid "Batch Name:"
msgstr ""

#: batch_import_view.html
msgid "Submitter:"
msgstr ""

#: batch_import_view.html
msgid "Submit Time:"
msgstr ""

#: batch_import_view.html
msgid "Status Summary"
msgstr ""

#: batch_import_view.html
msgid "Import Items"
msgstr ""

#: admin/imports.html admin/imports_by_date.html batch_import_view.html
msgid "Added Time"
msgstr ""

#: batch_import_view.html
msgid "Import Time"
msgstr ""

#: account/import.html account/loans.html admin/imports.html
#: admin/imports_by_date.html admin/loans_table.html admin/menu.html
#: admin/sponsorship.html batch_import_view.html status.html
msgid "Status"
msgstr ""

#: account/import.html admin/imports.html admin/imports_by_date.html
#: batch_import_view.html
msgid "Error"
msgstr ""

#: batch_import_view.html
msgid "IA ID"
msgstr ""

#: batch_import_view.html
msgid "Data"
msgstr ""

#: admin/imports.html admin/imports_by_date.html batch_import_view.html
msgid "OL Key"
msgstr ""

#: RecentChangesAdmin.html batch_import_view.html
msgid "Comments"
msgstr ""

#: batch_import_view.html merge_request_table/table_header.html
msgid "Submitter"
msgstr ""

#: batch_import_view.html
msgid "Not yet imported"
msgstr ""

#: design.html
msgid "Design Pattern Library"
msgstr ""
Expand Down Expand Up @@ -606,12 +681,6 @@ msgstr ""
msgid "Name"
msgstr ""

#: account/import.html account/loans.html admin/imports.html
#: admin/imports_by_date.html admin/loans_table.html admin/menu.html
#: admin/sponsorship.html status.html
msgid "Status"
msgstr ""

#: SearchNavigation.html SubjectTags.html lib/nav_foot.html lib/nav_head.html
#: subjects.html subjects/notfound.html type/author/view.html
#: type/list/view_body.html work_search.html
Expand Down Expand Up @@ -1267,10 +1336,6 @@ msgstr ""
msgid "Reason"
msgstr ""

#: account/import.html admin/imports.html admin/imports_by_date.html
msgid "Error"
msgstr ""

#: account/import.html
msgid "No ISBN"
msgstr ""
Expand Down Expand Up @@ -2186,14 +2251,6 @@ msgstr ""
msgid "Identifier"
msgstr ""

#: admin/imports.html admin/imports_by_date.html
msgid "OL Key"
msgstr ""

#: admin/imports.html admin/imports_by_date.html
msgid "Added Time"
msgstr ""

#: admin/imports.html admin/imports_by_date.html
msgid "Imported Time"
msgstr ""
Expand Down Expand Up @@ -5734,10 +5791,6 @@ msgstr ""
msgid "Submitter ▾"
msgstr ""

#: merge_request_table/table_header.html
msgid "Submitter"
msgstr ""

#: merge_request_table/table_header.html
msgid "Filter submitters"
msgstr ""
Expand Down Expand Up @@ -7461,10 +7514,6 @@ msgstr ""
msgid "Path"
msgstr ""

#: RecentChangesAdmin.html
msgid "Comments"
msgstr ""

#: RecentChangesAdmin.html
msgid "view"
msgstr ""
Expand Down
50 changes: 50 additions & 0 deletions openlibrary/plugins/openlibrary/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from time import time
import math
import infogami
from openlibrary.core import db
from openlibrary.core.batch_imports import (
batch_import,
)
Expand Down Expand Up @@ -504,6 +505,55 @@ def POST(self):
return render_template("batch_import.html", batch_result=batch_result)


class BatchImportView(delegate.page):
path = r'/import/batch/(\d+)'

def GET(self, batch_id):
i = web.input(page=1, limit=10, sort='added_time asc')
page = int(i.page)
limit = int(i.limit)
sort = i.sort

valid_sort_fields = ['added_time', 'import_time', 'status']
sort_field, sort_order = sort.split()
if sort_field not in valid_sort_fields or sort_order not in ['asc', 'desc']:
sort_field = 'added_time'
sort_order = 'asc'

offset = (page - 1) * limit

batch = db.select('import_batch', where='id=$batch_id', vars=locals())[0]
total_rows = db.query(
'SELECT COUNT(*) AS count FROM import_item WHERE batch_id=$batch_id',
vars=locals(),
)[0].count

rows = db.select(
'import_item',
where='batch_id=$batch_id',
order=f'{sort_field} {sort_order}',
limit=limit,
offset=offset,
vars=locals(),
)

status_counts = db.query(
'SELECT status, COUNT(*) AS count FROM import_item WHERE batch_id=$batch_id GROUP BY status',
vars=locals(),
)

return render_template(
'batch_import_view.html',
batch=batch,
rows=rows,
total_rows=total_rows,
page=page,
limit=limit,
sort=sort,
status_counts=status_counts,
)


class isbn_lookup(delegate.page):
path = r'/(?:isbn|ISBN)/(.{10,})'

Expand Down
1 change: 1 addition & 0 deletions openlibrary/templates/batch_import.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ <h1>$_("Batch Imports")</h1>
<li>$item</li>
</ol>
</details>
<p><a href="/import/batch/$batch_result.batch.id">$_('View Batch Status')</a></p>
</div>
</div>
56 changes: 56 additions & 0 deletions openlibrary/templates/batch_import_view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
$def with (batch, rows, total_rows, page, limit, sort, status_counts)
$var title: $_("Batch Import Status")

<div id="contentHead">
<h1>$_("Batch Import Status")</h1>
</div>
<div id="contentBody" class="batchImportStatus">
<p>$_('Batch ID:') $batch.id</p>
<p>$_('Batch Name:') $batch.name</p>
<p>$_('Submitter:') $batch.submitter</p>
<p>$_('Submit Time:') $datestr(batch.submit_time)</p>

<h2>$_('Status Summary')</h2>
<ul>
$for status_count in status_counts:
<li>$status_count.status: $status_count.count</li>
</ul>

<h2>$_('Import Items')</h2>
<table class="changeHistory">
<thead>
<tr>
<th><a href="$changequery(page=None, sort='added_time asc')">$_('Added Time')</a></th>
<th><a href="$changequery(page=None, sort='import_time asc')">$_('Import Time')</a></th>
<th><a href="$changequery(page=None, sort='status asc')">$_('Status')</a></th>
<th>$_('Error')</th>
<th>$_('IA ID')</th>
<th>$_('Data')</th>
<th>$_('OL Key')</th>
<th>$_('Comments')</th>
<th>$_('Submitter')</th>
</tr>
</thead>
<tbody>
$for row in rows:
<tr>
<td>$datestr(row.added_time)</td>
<td>$(datestr(row.import_time) if row.import_time else _('Not yet imported'))</td>
<td>$row.status</td>
<td>$row.error</td>
<td>$row.ia_id</td>
<td>$row.data</td>
<td>
$if row.ol_key:
<a href="$row.ol_key">$row.ol_key</a>
$else:
$row.ol_key
</td>
<td>$row.comments</td>
<td>$row.submitter</td>
</tr>
</tbody>
</table>

$:macros.Pager(page, total_rows, limit)
</div>

0 comments on commit 96b83bd

Please sign in to comment.