Refactor index route and template to enhance worklog summaries and improve code readability

This commit is contained in:
Yaro Kasear 2025-07-21 09:23:58 -05:00
parent a4bfb9838c
commit fa37497e3a
2 changed files with 52 additions and 14 deletions

View file

@ -11,11 +11,9 @@ from ..utils.load import eager_load_worklog_relationships, eager_load_inventory_
def index():
worklog_query = eager_load_worklog_relationships(
db.session.query(WorkLog)
).filter(
(WorkLog.complete == False)
)
).all()
active_worklogs = worklog_query.all()
active_worklogs = [log for log in worklog_query if not log.complete]
active_count = len(active_worklogs)
active_worklog_headers = {
@ -51,13 +49,37 @@ def index():
labels = list(pivot.index)
data = [int(x) for x in pivot.values]
datasets = [{
datasets = {}
datasets['summary'] = [{
'type': 'pie',
'labels': labels,
'values': data,
'name': 'Inventory Conditions'
}]
users = set([log.contact for log in worklog_query if log.contact])
work_summary = {}
for user in sorted(users, key=lambda u: u.full_name):
work_summary[user.full_name] = {}
work_summary[user.full_name]['active_count'] = len([log for log in worklog_query if log.contact == user and not log.complete])
work_summary[user.full_name]['complete_count'] = len([log for log in worklog_query if log.contact == user and log.complete])
datasets['work_summary'] = [{
'type': 'bar',
'x': list(work_summary.keys()),
'y': [work_summary[user]['active_count'] for user in work_summary],
'name': 'Active Worklogs',
'marker': {'color': 'red'}
}, {
'type': 'bar',
'x': list(work_summary.keys()),
'y': [work_summary[user]['complete_count'] for user in work_summary],
'name': 'Completed Worklogs',
'marker': {'color': 'green'}
}]
active_worklog_rows = []
for log in active_worklogs:
# Create a dictionary of {column name: cell dict}

View file

@ -35,22 +35,38 @@
</div>
</div>
{% endif %}
{% if (datasets[0]['values'] | sum) > 0 %}
{% if (datasets['summary'][0]['values'] | sum) > 0 %}
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">Summary</h5>
<h5 class="card-title">Inventory Summary</h5>
<div id="summary"></div>
</div>
</div>
</div>
{% endif %}
</div>
<div class="row mt-2">
{% if (datasets['summary'][0]['values'] | sum) > 0 %}
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">Work Summary</h5>
<div id="work_summary"></div>
</div>
</div>
</div>
{% endif %}
</div>
</div>
{% endblock %}
{% block script %}
const data = {{ datasets|tojson }};
const data = {{ datasets['summary']|tojson }};
const layout = { title: 'Summary' };
const work_data = {{ datasets['work_summary']|tojson }};
const work_layout = { title: 'Work Summary', xaxis: { tickangle: -45 } };
Plotly.newPlot('summary', data, layout)
Plotly.newPlot('work_summary', work_data, work_layout);
{% endblock %}