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)
)
active_worklogs = worklog_query.all()
).all()
active_worklogs = [log for log in worklog_query if not log.complete]
active_count = len(active_worklogs)
active_worklog_headers = {
@ -28,7 +26,7 @@ def index():
)
results = inventory_query.all()
data = [{
'id': item.id,
'condition': item.condition
@ -49,15 +47,39 @@ def index():
# Convert pandas/numpy int64s to plain old Python ints
pivot = pivot.astype(int)
labels = list(pivot.index)
data = [int(x) for x in pivot.values]
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}
@ -76,8 +98,8 @@ def index():
})
return render_template(
"index.html",
active_count=active_count,
"index.html",
active_count=active_count,
active_worklog_headers=active_worklog_headers,
active_worklog_rows=active_worklog_rows,
labels=labels,

View file

@ -14,7 +14,7 @@
<div class="card-body">
<h5 class="card-title">Active Worklogs</h5>
<h6 class="card-subtitle mb-2 text-body-secondary">
You have {{ active_count }} active worklogs.
You have {{ active_count }} active worklogs.
{% set ids %}
{ids: [{% for row in active_worklog_rows %}{{ row['id'] }}, {% endfor %}]}
{% endset %}
@ -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 %}