Move dashboard + PC reports off Machine onto the asset model
- Dashboard counts assets by type/status (Asset/AssetType/AssetStatus); adds a printer count to the totals. - Software-compliance and pc-relationships reports query Computer / ComputerInstalledApp / assetrelationships instead of Machine. Removes the dashboard + reports read dependency on the Machine model. (The software-compliance report has a separate pre-existing bug: Application has no isrequired field, so it errors before the query runs - unrelated to this change.) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -4,74 +4,79 @@ from flask import Blueprint, current_app
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models import Machine, MachineType, MachineStatus
|
||||
from shopdb.core.models import Asset, AssetType, AssetStatus
|
||||
from shopdb.utils.responses import success_response
|
||||
|
||||
dashboard_bp = Blueprint('dashboard', __name__)
|
||||
|
||||
# Map asset type name -> dashboard category label
|
||||
_TYPE_CATEGORY = {
|
||||
'equipment': 'Equipment',
|
||||
'computer': 'PC',
|
||||
'printer': 'Printer',
|
||||
'network_device': 'Network',
|
||||
}
|
||||
|
||||
|
||||
def _count_by_type(assettype):
|
||||
return db.session.query(Asset).join(AssetType).filter(
|
||||
Asset.isactive == True,
|
||||
AssetType.assettype == assettype
|
||||
).count()
|
||||
|
||||
|
||||
@dashboard_bp.route('/summary', methods=['GET'])
|
||||
@dashboard_bp.route('', methods=['GET'])
|
||||
@jwt_required(optional=True)
|
||||
def get_dashboard():
|
||||
"""Get dashboard summary data."""
|
||||
# Count machines by category
|
||||
equipment_count = db.session.query(Machine).join(MachineType).filter(
|
||||
Machine.isactive == True,
|
||||
MachineType.category == 'Equipment'
|
||||
).count()
|
||||
|
||||
pc_count = db.session.query(Machine).join(MachineType).filter(
|
||||
Machine.isactive == True,
|
||||
MachineType.category == 'PC'
|
||||
).count()
|
||||
|
||||
network_count = db.session.query(Machine).join(MachineType).filter(
|
||||
Machine.isactive == True,
|
||||
MachineType.category == 'Network'
|
||||
).count()
|
||||
"""Get dashboard summary data (asset-based)."""
|
||||
equipment_count = _count_by_type('equipment')
|
||||
pc_count = _count_by_type('computer')
|
||||
network_count = _count_by_type('network_device')
|
||||
printer_count = _count_by_type('printer')
|
||||
total = equipment_count + pc_count + network_count + printer_count
|
||||
|
||||
# Count by status
|
||||
status_counts = db.session.query(
|
||||
MachineStatus.status,
|
||||
db.func.count(Machine.machineid)
|
||||
AssetStatus.status,
|
||||
db.func.count(Asset.assetid)
|
||||
).outerjoin(
|
||||
Machine,
|
||||
db.and_(Machine.statusid == MachineStatus.statusid, Machine.isactive == True)
|
||||
).group_by(MachineStatus.status).all()
|
||||
|
||||
# Recent machines
|
||||
recent_machines = Machine.query.filter_by(isactive=True).order_by(
|
||||
Machine.createddate.desc()
|
||||
).limit(10).all()
|
||||
|
||||
# Build status dict
|
||||
Asset,
|
||||
db.and_(Asset.statusid == AssetStatus.statusid, Asset.isactive == True)
|
||||
).group_by(AssetStatus.status).all()
|
||||
status_dict = {status: count for status, count in status_counts}
|
||||
|
||||
# Recent assets
|
||||
recent = Asset.query.filter_by(isactive=True).order_by(
|
||||
Asset.createddate.desc()
|
||||
).limit(10).all()
|
||||
|
||||
return success_response({
|
||||
# Fields expected by frontend
|
||||
'totalmachines': equipment_count + pc_count + network_count,
|
||||
'totalmachines': total,
|
||||
'totalequipment': equipment_count,
|
||||
'totalpc': pc_count,
|
||||
'totalnetwork': network_count,
|
||||
'totalprinter': printer_count,
|
||||
'activemachines': status_dict.get('In Use', 0),
|
||||
'inrepair': status_dict.get('In Repair', 0),
|
||||
# Also include structured data
|
||||
# Structured data
|
||||
'counts': {
|
||||
'equipment': equipment_count,
|
||||
'pcs': pc_count,
|
||||
'networkdevices': network_count,
|
||||
'total': equipment_count + pc_count + network_count
|
||||
'printers': printer_count,
|
||||
'total': total
|
||||
},
|
||||
'bystatus': status_dict,
|
||||
'recent': [
|
||||
{
|
||||
'machineid': m.machineid,
|
||||
'machinenumber': m.machinenumber,
|
||||
'machinetype': m.machinetype.machinetype if m.machinetype else None,
|
||||
'createddate': m.createddate.isoformat() + 'Z' if m.createddate else None
|
||||
'assetid': a.assetid,
|
||||
'assetnumber': a.assetnumber,
|
||||
'assettype': a.assettype.assettype if a.assettype else None,
|
||||
'createddate': a.createddate.isoformat() + 'Z' if a.createddate else None
|
||||
}
|
||||
for m in recent_machines
|
||||
for a in recent
|
||||
]
|
||||
})
|
||||
|
||||
@@ -79,23 +84,23 @@ def get_dashboard():
|
||||
@dashboard_bp.route('/stats', methods=['GET'])
|
||||
@jwt_required(optional=True)
|
||||
def get_stats():
|
||||
"""Get detailed statistics."""
|
||||
# Machine type breakdown
|
||||
"""Get detailed statistics by asset type."""
|
||||
type_counts = db.session.query(
|
||||
MachineType.machinetype,
|
||||
MachineType.category,
|
||||
db.func.count(Machine.machineid)
|
||||
AssetType.assettype,
|
||||
db.func.count(Asset.assetid)
|
||||
).outerjoin(
|
||||
Machine,
|
||||
db.and_(Machine.machinetypeid == MachineType.machinetypeid, Machine.isactive == True)
|
||||
).filter(MachineType.isactive == True).group_by(
|
||||
MachineType.machinetypeid
|
||||
).all()
|
||||
Asset,
|
||||
db.and_(Asset.assettypeid == AssetType.assettypeid, Asset.isactive == True)
|
||||
).group_by(AssetType.assettypeid).all()
|
||||
|
||||
return success_response({
|
||||
'bytype': [
|
||||
{'type': t, 'category': c, 'count': count}
|
||||
for t, c, count in type_counts
|
||||
{
|
||||
'type': t,
|
||||
'category': _TYPE_CATEGORY.get(t, t),
|
||||
'count': count
|
||||
}
|
||||
for t, count in type_counts
|
||||
]
|
||||
})
|
||||
|
||||
@@ -142,7 +147,6 @@ def get_navigation():
|
||||
def health_check():
|
||||
"""Health check endpoint (no auth required)."""
|
||||
try:
|
||||
# Test database connection
|
||||
db.session.execute(db.text('SELECT 1'))
|
||||
db_status = 'healthy'
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user