dashboarded added
This commit is contained in:
@@ -1,82 +1,136 @@
|
||||
import matplotlib
|
||||
matplotlib.use("Agg")
|
||||
# import matplotlib
|
||||
# matplotlib.use("Agg")
|
||||
|
||||
from flask import Blueprint, render_template, session, redirect, url_for
|
||||
import matplotlib.pyplot as plt
|
||||
import io
|
||||
import base64
|
||||
# from flask import Blueprint, render_template, session, redirect, url_for
|
||||
# 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
|
||||
|
||||
# dashboard_bp = Blueprint("dashboard", __name__, url_prefix="/dashboard")
|
||||
|
||||
# # dashboard_bp = Blueprint("dashboard", __name__)
|
||||
|
||||
# # charts
|
||||
# # def plot_to_base64():
|
||||
# # img = io.BytesIO()
|
||||
# # plt.savefig(img, format="png", bbox_inches="tight")
|
||||
# # plt.close()
|
||||
# # img.seek(0)
|
||||
# # return base64.b64encode(img.getvalue()).decode()
|
||||
|
||||
# # bar chart
|
||||
# def bar_chart():
|
||||
# categories = ["Trench", "Manhole", "Pipe Laying", "Restoration"]
|
||||
# values = [120, 80, 150, 60]
|
||||
|
||||
# plt.figure()
|
||||
# plt.bar(categories, values)
|
||||
# plt.title("Work Category Report")
|
||||
# plt.xlabel("test Category")
|
||||
# plt.ylabel("test Quantity")
|
||||
|
||||
|
||||
# return plot_to_base64(plt)
|
||||
|
||||
# # Pie chart
|
||||
# def pie_chart():
|
||||
# labels = ["Completed", "In Progress", "Pending"]
|
||||
# sizes = [55, 20, 25]
|
||||
|
||||
# plt.figure()
|
||||
# plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=140)
|
||||
# plt.title("Project Status")
|
||||
|
||||
# return plot_to_base64(plt)
|
||||
|
||||
# # Histogram chart
|
||||
# def histogram_chart():
|
||||
# daily_work = [5, 10, 15, 20, 20, 25, 30, 35, 40, 45, 50]
|
||||
|
||||
# plt.figure()
|
||||
# plt.hist(daily_work, bins=5)
|
||||
# plt.title("Daily Work Distribution")
|
||||
# plt.xlabel("Work Units")
|
||||
# plt.ylabel("Frequency")
|
||||
|
||||
# return plot_to_base64(plt)
|
||||
|
||||
# # Dashboaed page
|
||||
# @dashboard_bp.route("/")
|
||||
# def dashboard():
|
||||
# if not session.get("user_id"):
|
||||
# return redirect(url_for("auth.login"))
|
||||
|
||||
# return render_template(
|
||||
# "dashboard.html",
|
||||
# title="Dashboard",
|
||||
# bar_chart=bar_chart(),
|
||||
# pie_chart=pie_chart(),
|
||||
# histogram=histogram_chart()
|
||||
# )
|
||||
|
||||
# # subcontractor dashboard
|
||||
# @dashboard_bp.route("/subcontractor_dashboard", methods=["GET", "POST"])
|
||||
# def subcontractor_dashboard():
|
||||
# if not session.get("user_id"):
|
||||
# return redirect(url_for("auth.login"))
|
||||
|
||||
# tr_dash = DashboardService().bar_chart_of_tr_ex
|
||||
|
||||
|
||||
# return render_template(
|
||||
# "subcontractor_dashboard.html",
|
||||
# title="Dashboard",
|
||||
# bar_chart=tr_dash
|
||||
# )
|
||||
|
||||
from flask import Blueprint, render_template, session, redirect, url_for, jsonify
|
||||
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
|
||||
|
||||
dashboard_bp = Blueprint("dashboard", __name__, url_prefix="/dashboard")
|
||||
|
||||
# charts
|
||||
def plot_to_base64():
|
||||
img = io.BytesIO()
|
||||
plt.savefig(img, format="png", bbox_inches="tight")
|
||||
plt.close()
|
||||
img.seek(0)
|
||||
return base64.b64encode(img.getvalue()).decode()
|
||||
@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()
|
||||
|
||||
# bar chart
|
||||
def bar_chart():
|
||||
categories = ["Trench", "Manhole", "Pipe Laying", "Restoration"]
|
||||
values = [120, 80, 150, 60]
|
||||
# 2. Location Distribution (Business reach)
|
||||
loc_results = db.session.query(
|
||||
TrenchExcavation.Location,
|
||||
func.count(TrenchExcavation.id)
|
||||
).group_by(TrenchExcavation.Location).all()
|
||||
|
||||
plt.figure()
|
||||
plt.bar(categories, values)
|
||||
plt.title("Work Category Report")
|
||||
plt.xlabel("Category")
|
||||
plt.ylabel("Count")
|
||||
# 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
|
||||
|
||||
return plot_to_base64()
|
||||
|
||||
# Pie chart
|
||||
def pie_chart():
|
||||
labels = ["Completed", "In Progress", "Pending"]
|
||||
sizes = [55, 20, 25]
|
||||
|
||||
plt.figure()
|
||||
plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=140)
|
||||
plt.title("Project Status")
|
||||
|
||||
return plot_to_base64()
|
||||
|
||||
# Histogram chart
|
||||
def histogram_chart():
|
||||
daily_work = [5, 10, 15, 20, 20, 25, 30, 35, 40, 45, 50]
|
||||
|
||||
plt.figure()
|
||||
plt.hist(daily_work, bins=5)
|
||||
plt.title("Daily Work Distribution")
|
||||
plt.xlabel("Work Units")
|
||||
plt.ylabel("Frequency")
|
||||
|
||||
return plot_to_base64()
|
||||
|
||||
# Dashboaed page
|
||||
@dashboard_bp.route("/")
|
||||
def dashboard():
|
||||
if not session.get("user_id"):
|
||||
return redirect(url_for("auth.login"))
|
||||
|
||||
return render_template(
|
||||
"dashboard.html",
|
||||
title="Dashboard",
|
||||
bar_chart=bar_chart(),
|
||||
pie_chart=pie_chart(),
|
||||
histogram=histogram_chart()
|
||||
)
|
||||
|
||||
# subcontractor dashboard
|
||||
@dashboard_bp.route("/subcontractor_dashboard")
|
||||
def subcontractor_dashboard():
|
||||
if not session.get("user_id"):
|
||||
return redirect(url_for("auth.login"))
|
||||
|
||||
return render_template(
|
||||
"subcontractor_dashboard.html",
|
||||
title="Dashboard",
|
||||
bar_chart=bar_chart(),
|
||||
pie_chart=pie_chart(),
|
||||
histogram=histogram_chart()
|
||||
)
|
||||
return render_template("dashboard.html", title="Business Intelligence Dashboard")
|
||||
Reference in New Issue
Block a user