Skip to content

Commit

Permalink
🎱 Fields: Finished, Successful (#184)
Browse files Browse the repository at this point in the history
* Adds custom bool fields for Finished, Successful

* Simplifies js render logic
  • Loading branch information
mrharpo authored Oct 27, 2023
1 parent 154ff05 commit fb696c4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
26 changes: 25 additions & 1 deletion chowda/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from starlette.datastructures import FormData
from starlette.requests import Request
from starlette_admin._types import RequestAction
from starlette_admin.fields import BaseField, IntegerField, TextAreaField
from starlette_admin.fields import BaseField, BooleanField, IntegerField, TextAreaField

from chowda.models import MediaFile

Expand Down Expand Up @@ -161,3 +161,27 @@ class BatchUnstartedGuidsCount(IntegerField):

async def parse_obj(self, request: Request, obj: Any) -> Any:
return len(obj.unstarted_guids())


@dataclass
class FinishedField(BooleanField):
"""A field that displays a boolean value for the 'Finished' property.
True: Green check
False: Grey clock"""

display_template: str = 'displays/finished.html'
render_function_key: str = 'finished'


@dataclass
class SuccessfulField(BooleanField):
"""A field that displays a boolean value for the 'Successful' property.
True: Green check circle
False: Red X circle
None: Blank
"""

display_template: str = 'displays/successful.html'
render_function_key: str = 'successful'
3 changes: 2 additions & 1 deletion chowda/routers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ async def event(event: dict):
run = Run(f"{payload['flow_name']}/{payload['run_id']}")
row.finished = run.finished
row.finished_at = run.finished_at
row.successful = run.successful
if row.finished:
row.successful = run.successful
row.current_step = payload['step_name']
row.current_task = payload['task_id']
db.add(row)
Expand Down
15 changes: 15 additions & 0 deletions chowda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
BatchPercentSuccessful,
BatchUnstartedGuids,
BatchUnstartedGuidsCount,
FinishedField,
MediaFileCount,
MediaFilesGuidsField,
SonyCiAssetThumbnail,
SuccessfulField,
)
from chowda.models import MMIF, Batch, Collection, MediaFile
from chowda.utils import get_duplicates, validate_media_file_guids
Expand Down Expand Up @@ -471,6 +473,19 @@ def can_create(self, request: Request) -> bool:
class MetaflowRunView(AdminModelView):
form_include_pk: ClassVar[bool] = True

fields: ClassVar[list[Any]] = [
'id',
'pathspec',
'batch',
'mmif',
'media_file',
'created_at',
FinishedField('finished'),
SuccessfulField('successful'),
'current_step',
'current_task',
]


class MMIFView(ChowdaModelView):
label: ClassVar[str] = 'MMIFs'
Expand Down
27 changes: 27 additions & 0 deletions static/js/custom-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Object.assign(render, {
guid => `<a href="../media-file/detail/${guid}"> ${guid} </a>`
)
},

media_file_count: function render(data, type, full, meta, fieldOptions) {
// Render a count of media files
return data.length
Expand All @@ -21,4 +22,30 @@ Object.assign(render, {
? `<img src="${data.location}" style="max-height:150px;" loading="lazy">`
: null
},

finished: function render(data, type, full, meta, fieldOptions) {
if (data == null) return null_column();
data = Array.isArray(data) ? data : [data];
if (data.length == 0) return empty_column();
return `<div class="d-flex">${data
.map((d) =>
d === true
? `<div class="p-1"><span class="text-center text-info"><i class="fa-solid fa-check fa-lg"></i></span></div>`
: `<div class="p-1"><span class="text-center text-secondary"><i class="fa-solid fa-clock fa-lg"></i></span></div>`
)
.join("")}</div>`;
},

successful: function render(data, type, full, meta, fieldOptions) {
if (data == null) return null_column();
data = Array.isArray(data) ? data : [data];
if (data.length == 0) return empty_column();
return `<div class="d-flex">${data
.map((d) =>
d === true
? `<div class="p-1"><span class="text-center text-success"><i class="fa-solid fa-check-circle fa-lg"></i></span></div>`
: `<div class="p-1"><span class="text-center text-danger"><i class="fa-solid fa-circle-xmark fa-lg"></i></span></div>`
)
.join("")}</div>`;
},
})
9 changes: 9 additions & 0 deletions templates/displays/finished.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% if data %}
<span class="text-center text-info me-1">
<i class="fa-solid fa-check fa-lg"></i>
</span>
{% else %}
<span class="text-center text-secondary me-1">
<i class="fa-solid fa-clock fa-lg"></i>
</span>
{% endif %}
9 changes: 9 additions & 0 deletions templates/displays/successful.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% if data %}
<span class="text-center text-success me-1">
<i class="fa-solid fa-check-circle fa-lg"></i>
</span>
{% elif data != None %}
<span class="text-center text-danger me-1">
<i class="fa-solid fa-circle-xmark fa-lg"></i>
</span>
{% endif %}

0 comments on commit fb696c4

Please sign in to comment.