114 lines
3.4 KiB
Python
114 lines
3.4 KiB
Python
import matplotlib
|
|
matplotlib.use("Agg")
|
|
|
|
from flask import Blueprint, render_template, session, redirect, url_for, jsonify, request
|
|
import matplotlib.pyplot as plt
|
|
import io
|
|
import base64
|
|
from app.utils.plot_utils import plot_to_base64
|
|
from app.services.dashboard_service import DashboardService
|
|
from sqlalchemy import func
|
|
from app import db
|
|
from app.models.trench_excavation_model import TrenchExcavation
|
|
from app.models.manhole_excavation_model import ManholeExcavation
|
|
from app.models.laying_model import Laying
|
|
|
|
from app.models.subcontractor_model import Subcontractor
|
|
|
|
dashboard_bp = Blueprint("dashboard", __name__, url_prefix="/dashboard")
|
|
|
|
|
|
@dashboard_bp.route("/")
|
|
def dashboard():
|
|
if not session.get("user_id"):
|
|
return redirect(url_for("auth.login"))
|
|
return render_template("dashboard.html", title="Business Intelligence Dashboard")
|
|
|
|
|
|
@dashboard_bp.route("/api/live-stats")
|
|
def live_stats():
|
|
try:
|
|
# 1. Overall Volume
|
|
t_count = TrenchExcavation.query.count()
|
|
m_count = ManholeExcavation.query.count()
|
|
l_count = Laying.query.count()
|
|
|
|
# 2. Location Distribution (Business reach)
|
|
loc_results = db.session.query(
|
|
TrenchExcavation.Location,
|
|
func.count(TrenchExcavation.id)
|
|
).group_by(TrenchExcavation.Location).all()
|
|
|
|
# 3. Work Timeline (Business productivity trend)
|
|
# Assuming your models have a 'created_at' field
|
|
timeline_results = db.session.query(
|
|
func.date(TrenchExcavation.created_at),
|
|
func.count(TrenchExcavation.id)
|
|
).group_by(func.date(TrenchExcavation.created_at)).order_by(func.date(TrenchExcavation.created_at)).all()
|
|
|
|
return jsonify({
|
|
"summary": {
|
|
"trench": t_count,
|
|
"manhole": m_count,
|
|
"laying": l_count,
|
|
"total": t_count + m_count + l_count
|
|
},
|
|
"locations": {row[0]: row[1] for row in loc_results if row[0]},
|
|
"timeline": {str(row[0]): row[1] for row in timeline_results}
|
|
})
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
# subcontractor dashboard
|
|
@dashboard_bp.route("/subcontractor_dashboard")
|
|
def subcontractor_dashboard():
|
|
|
|
if not session.get("user_id"):
|
|
return redirect(url_for("auth.login"))
|
|
|
|
subcontractors = Subcontractor.query.all()
|
|
|
|
return render_template(
|
|
"subcontractor_dashboard.html",
|
|
subcontractors=subcontractors
|
|
)
|
|
|
|
|
|
# API FOR CHART DATA
|
|
@dashboard_bp.route("/api/subcontractor-chart")
|
|
def subcontractor_chart():
|
|
|
|
subcontractor_id = request.args.get("subcontractor")
|
|
category = request.args.get("category")
|
|
ra_bill = request.args.get("ra_bill")
|
|
|
|
query = db.session.query(
|
|
TrenchExcavation.excavation_category,
|
|
func.sum(TrenchExcavation.Total)
|
|
)
|
|
|
|
if subcontractor_id:
|
|
query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id)
|
|
|
|
if category:
|
|
query = query.filter(TrenchExcavation.subcontractor_id == subcontractor_id)
|
|
|
|
if ra_bill:
|
|
query = query.filter(TrenchExcavation.RA_Bill_No == ra_bill)
|
|
|
|
results = query.group_by(TrenchExcavation.excavation_category).all()
|
|
|
|
labels = []
|
|
values = []
|
|
|
|
for r in results:
|
|
labels.append(r[0])
|
|
values.append(float(r[1] or 0))
|
|
|
|
return jsonify({
|
|
"labels": labels,
|
|
"values": values
|
|
}) |