Skip to content

Commit

Permalink
Displays sync history in dashboard (#86)
Browse files Browse the repository at this point in the history
* Displays last 10 sync operations from IngestFlow runs.
* Handles error for when no IngestFlow runs are round.
* Moves some env var loading into chowda/__init__.py.

Closes #85.
  • Loading branch information
afred authored Jul 31, 2023
1 parent eef7414 commit 98f6642
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 530 deletions.
19 changes: 17 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,24 @@
"request": "launch",
"cwd": "${workspaceFolder}",
"module": "pytest",
"args": ["-v", "-n", "auto"],
"args": [
"-v",
"-n",
"auto"
],
"jinja": false,
"justMyCode": false
},
{
"name": "Metaflow: Run",
"type": "python",
"request": "launch",
"program": "${file}",
"args": [
"run"
],
"subProcess": true,
"console": "integratedTerminal"
}
]
}
}
13 changes: 13 additions & 0 deletions chowda/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
from ._version import __version__
from os import environ
from dotenv import find_dotenv, load_dotenv

__all__ = ['__version__']

if environ.get('METAFLOW_NAMESPACE'):
from metaflow import namespace

namespace(environ.get('METAFLOW_NAMESPACE'))


# Load dotenv file if present
CHOWDA_ENV = environ.get('CHOWDA_ENV', 'development')
dotenv_path = find_dotenv(filename=f'.env.{CHOWDA_ENV}')
load_dotenv(dotenv_path)
10 changes: 0 additions & 10 deletions chowda/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
from os import environ

from dotenv import find_dotenv, load_dotenv

CHOWDA_ENV = environ.get('CHOWDA_ENV', 'development')


dotenv_path = find_dotenv(filename=f'.env.{CHOWDA_ENV}')


load_dotenv(dotenv_path)

DB_USER = environ.get('DB_USER', 'postgres')
DB_PASSWORD = environ.get('DB_PASSWORD', 'postgres')
DB_HOST = environ.get('DB_HOST', 'localhost')
Expand Down
20 changes: 15 additions & 5 deletions chowda/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass
from datetime import datetime
from json import loads
from typing import Any, ClassVar, Dict

Expand All @@ -16,6 +15,9 @@
from chowda.db import engine
from chowda.models import MediaFile

from metaflow import Flow
from metaflow.exception import MetaflowNotFound


@dataclass
class MediaFilesGuidLinkField(BaseField):
Expand Down Expand Up @@ -136,14 +138,22 @@ class ClamsEventView(ModelView):


class DashboardView(CustomView):
def sony_ci_last_sync(self):
# TODO: replace with actual "last sync" time
return datetime.now()
def sync_history(self) -> Dict[str, Any]:
try:
return [
{'created_at': sync_run.created_at, 'successful': sync_run.successful}
for sync_run in list(Flow('IngestFlow'))
][:10]
except MetaflowNotFound:
return []

async def render(self, request: Request, templates: Jinja2Templates) -> Response:
return templates.TemplateResponse(
'dashboard.html',
{'request': request, 'sony_ci_last_sync': self.sony_ci_last_sync()},
{
'request': request,
'sync_history': self.sync_history(),
},
)


Expand Down
682 changes: 182 additions & 500 deletions pdm.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies = [
"authlib>=1.2.1",
"itsdangerous>=2.1.2",
"httpx>=0.24.1",
"rich>=13.5.1",
]
requires-python = '>=3.8.1,<4.0'
readme = 'README.md'
Expand Down
53 changes: 40 additions & 13 deletions templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,64 @@ <h1>Dashboard</h1>
<div class="row">
<div class="col-12">
<div class="card">
<form action="/sony_ci/sync" method="POST">
<form action="/api/sony_ci/sync" method="POST">
<div class="card-header">
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center">
<h3 class="card-title">Sony Ci</h3>
<h3 class="card-title">Sync History</h3>
</div>
</div>
</div>
<div class="card-body border-bottom py-3">
<div class="mb-3">
Last Sync was {{ sony_ci_last_sync.strftime('%a, %B %d at I:%m:%S %p') }}
</div>
</div>
<div class="card-footer text-black">
<div class="btn-list ms-auto justify-content-end">
<button type="submit" name="_add_another" class="btn">
<i class="fa-solid fa-rotate me-2">
</i>
{{ _("Full Sync")}}
</button>
{% if sync_history|length==0 %}
<h4>No sync history was found</h4>
{% else %}
<div class="table-responsive">
<table class="table table-vcenter">
<thead>
<tr>
<th>Date / Time</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for sync in sync_history %}
<tr>
<td>
{{ sync['created_at'].strftime('%Y-%m-%d %I:%M:%S %p') }}
</td>
<td>
{{ 'Success' if sync['successful'] else 'Failed'}}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</div>
</div>
</form>
</div>
<div class="card-footer text-black">
<div class="btn-list ms-auto justify-content-end">
<button type="submit" name="_add_another" class="btn">
<i class="fa-solid fa-rotate me-2">
</i>
{{ _("Sync Now")}}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
{% block head_css %}
{{ super() }}
{% endblock %}
{% block script %}
new DataTable('#sync_history');
{{ super() }}
{% endblock %}

0 comments on commit 98f6642

Please sign in to comment.