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,