Add pandas dependency, enhance inventory summary visualization, and update table rendering

This commit is contained in:
Yaro Kasear 2025-06-16 15:45:54 -05:00
parent 7673b4e1b6
commit 25e67cce28
6 changed files with 86 additions and 6 deletions

View file

@ -6,6 +6,7 @@ from typing import Callable, Any, List
from . import db
from .utils import eager_load_user_relationships, eager_load_inventory_relationships, eager_load_room_relationships, eager_load_worklog_relationships, chunk_list
from datetime import datetime, timedelta
import pandas as pd
main = Blueprint('main', __name__)
@ -141,7 +142,42 @@ def index():
stale_pagination = make_paginated_data(worklog_query, stale_worklog_page, 3)
stale_count = len(worklog_query.all())
stale_worklog_headers = {k: v for k, v in worklog_headers.items() if k not in ['End Time', 'Quick Analysis?', 'Complete?', 'Follow Up?']}
stale_worklog_headers = {
k: v for k, v in worklog_headers.items()
if k not in ['End Time', 'Quick Analysis?', 'Complete?', 'Follow Up?']
}
inventory_query = eager_load_inventory_relationships(
db.session.query(Inventory)
)
results = inventory_query.all()
data = [{
'id': item.id,
'condition': item.condition
} for item in results]
df = pd.DataFrame(data)
# Count items per condition
expected_conditions = [
'Deployed', 'Disposed', 'Inoperable', 'Partially Inoperable',
'Removed', 'Unverified', 'Working'
]
pivot = df['condition'].value_counts().reindex(expected_conditions, fill_value=0)
# 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] # <- This is what fixes the JSON error
datasets = [{
'type': 'bar',
'x': labels,
'y': data,
'name': 'Inventory Conditions'
}]
return render_template(
"index.html",
@ -149,9 +185,15 @@ def index():
stale_pagination=stale_pagination,
stale_count=stale_count,
stale_worklog_headers=stale_worklog_headers,
stale_worklog_rows=[{"id": log.id, "cells": [fn(log) for fn in stale_worklog_headers.values()]} for log in stale_pagination['items']],
stale_worklog_rows=[{
"id": log.id,
"cells": [fn(log) for fn in stale_worklog_headers.values()]
} for log in stale_pagination['items']],
labels=labels,
datasets=datasets
)
def link(text, endpoint, **values):
return {"text": text, "url": url_for(endpoint, **values)}