model class update total cal fields all model

This commit is contained in:
2026-02-25 15:27:59 +05:30
parent 163c7814ed
commit cf7d1636f9
8 changed files with 380 additions and 195 deletions

View File

@@ -6,7 +6,7 @@ class Config:
# secret key # secret key
SECRET_KEY = os.getenv("SECRET_KEY", "dev-secret-key") SECRET_KEY = os.getenv("SECRET_KEY", "dev-secret-key")
# Database varibles # Database variables
DB_DIALECT = os.getenv("DB_DIALECT") DB_DIALECT = os.getenv("DB_DIALECT")
DB_DRIVER = os.getenv("DB_DRIVER") DB_DRIVER = os.getenv("DB_DRIVER")
DB_USER = os.getenv("DB_USER") DB_USER = os.getenv("DB_USER")

View File

@@ -1,5 +1,6 @@
from app import db from app import db
from datetime import datetime from datetime import datetime
from sqlalchemy import event
class ManholeExcavation(db.Model): class ManholeExcavation(db.Model):
__tablename__ = "manhole_excavation" __tablename__ = "manhole_excavation"
@@ -65,3 +66,17 @@ class ManholeExcavation(db.Model):
def serialize(self): def serialize(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns} return {c.name: getattr(self, c.name) for c in self.__table__.columns}
# ==========================================
# AUTO CALCULATE GRAND TOTAL
# ==========================================
def calculate_trench_total(mapper, connection, target):
total = 0
for column in target.__table__.columns:
if column.name.endswith("_total"):
total += getattr(target, column.name) or 0
target.Total = total
event.listen(TrenchExcavation, "before_insert", calculate_trench_total)
event.listen(TrenchExcavation, "before_update", calculate_trench_total)

View File

@@ -1,5 +1,6 @@
from app import db from app import db
from datetime import datetime from datetime import datetime
from sqlalchemy import event
class ManholeExcavationClient(db.Model): class ManholeExcavationClient(db.Model):
__tablename__ = "mh_ex_client" __tablename__ = "mh_ex_client"
@@ -81,3 +82,18 @@ class ManholeExcavationClient(db.Model):
def serialize(self): def serialize(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns} return {c.name: getattr(self, c.name) for c in self.__table__.columns}
# ==========================================
# AUTO CALCULATE GRAND TOTAL
# ==========================================
def calculate_trench_total(mapper, connection, target):
total = 0
for column in target.__table__.columns:
if column.name.endswith("_total"):
total += getattr(target, column.name) or 0
target.Total = total
event.listen(TrenchExcavation, "before_insert", calculate_trench_total)
event.listen(TrenchExcavation, "before_update", calculate_trench_total)

View File

@@ -1,14 +1,11 @@
from app import db from app import db
from datetime import datetime from datetime import datetime
from sqlalchemy import event
class TrenchExcavationClient(db.Model): class TrenchExcavationClient(db.Model):
__tablename__ = "tr_ex_client" __tablename__ = "tr_ex_client"
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
# Foreign Key to Subcontractor table
# subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False)
# Relationship for easy access (subcontractor.subcontractor_name)
# subcontractor = db.relationship("Subcontractor", backref="tr_ex_records")
# Basic Fields # Basic Fields
RA_Bill_No=db.Column(db.String(500)) RA_Bill_No=db.Column(db.String(500))
@@ -86,3 +83,19 @@ class TrenchExcavationClient(db.Model):
def serialize(self): def serialize(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns} return {c.name: getattr(self, c.name) for c in self.__table__.columns}
# ==========================================
# AUTO CALCULATE GRAND TOTAL
# ==========================================
def calculate_trench_client_total(mapper, connection, target):
total = 0
for column in target.__table__.columns:
if column.name.endswith("_total"):
total += getattr(target, column.name) or 0
target.Total = total
event.listen(TrenchExcavationClient, "before_insert", calculate_trench_client_total)
event.listen(TrenchExcavationClient, "before_update", calculate_trench_client_total)

View File

@@ -1,5 +1,6 @@
from app import db from app import db
from datetime import datetime from datetime import datetime
from sqlalchemy import event
class TrenchExcavation(db.Model): class TrenchExcavation(db.Model):
__tablename__ = "trench_excavation" __tablename__ = "trench_excavation"
@@ -108,3 +109,17 @@ class TrenchExcavation(db.Model):
+ safe(self.Hard_Rock_6_0_to_7_5) + safe(self.Hard_Rock_6_0_to_7_5)
), ),
} }
# ==========================================
# AUTO CALCULATE GRAND TOTAL
# ==========================================
def calculate_trench_total(mapper, connection, target):
total = 0
for column in target.__table__.columns:
if column.name.endswith("_total"):
total += getattr(target, column.name) or 0
target.Total = total
event.listen(TrenchExcavation, "before_insert", calculate_trench_total)
event.listen(TrenchExcavation, "before_update", calculate_trench_total)

View File

@@ -1,75 +1,156 @@
import matplotlib import matplotlib
matplotlib.use("Agg") matplotlib.use("Agg")
from flask import Blueprint, render_template, session, redirect, url_for from flask import Blueprint, render_template, session, redirect, url_for, jsonify
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import io import io
import base64 import base64
from app.utils.plot_utils import plot_to_base64 from app.utils.plot_utils import plot_to_base64
from app.services.dashboard_service import DashboardService 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
dashboard_bp = Blueprint("dashboard", __name__, url_prefix="/dashboard") 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 # import matplotlib
def histogram_chart(): # matplotlib.use("Agg")
daily_work = [5, 10, 15, 20, 20, 25, 30, 35, 40, 45, 50]
plt.figure() # from flask import Blueprint, render_template, session, redirect, url_for
plt.hist(daily_work, bins=5) # import matplotlib.pyplot as plt
plt.title("Daily Work Distribution") # import io
plt.xlabel("Work Units") # import base64
plt.ylabel("Frequency") # from app.utils.plot_utils import plot_to_base64
# from app.services.dashboard_service import DashboardService
return plot_to_base64(plt) # 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
# )
@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
# Dashboaed page
@dashboard_bp.route("/") @dashboard_bp.route("/")
def dashboard(): def dashboard():
if not session.get("user_id"): if not session.get("user_id"):
return redirect(url_for("auth.login")) return redirect(url_for("auth.login"))
return render_template("dashboard.html", title="Business Intelligence Dashboard")
return render_template(
"dashboard.html",
title="Dashboard",
bar_chart=bar_chart(),
pie_chart=pie_chart(),
histogram=histogram_chart()
)
# subcontractor dashboard # subcontractor dashboard
@dashboard_bp.route("/subcontractor_dashboard", methods=["GET", "POST"]) @dashboard_bp.route("/subcontractor_dashboard", methods=["GET", "POST"])

View File

@@ -1,4 +1,5 @@
from flask import Blueprint, render_template, request, send_file, flash from flask import Blueprint, render_template, request, send_file, flash
from collections import defaultdict
import pandas as pd import pandas as pd
import io import io
@@ -65,16 +66,19 @@ def make_lookup(rows, key_field):
key_val = normalize_key(r.get(key_field)) key_val = normalize_key(r.get(key_field))
if location and key_val: if location and key_val:
lookup[(location, key_val)] = r lookup.setdefault((location, key_val), []).append(r)
return lookup return lookup
# COMPARISON BUILDER # COMPARISON BUILDER
def build_comparison(client_rows, contractor_rows, key_field): def build_comparison(client_rows, contractor_rows, key_field):
contractor_lookup = make_lookup(contractor_rows, key_field) contractor_lookup = make_lookup(contractor_rows, key_field)
output = [] output = []
used_index = defaultdict(int) # 🔥 THIS FIXES YOUR ISSUE
for c in client_rows: for c in client_rows:
client_location = normalize_key(c.get("Location")) client_location = normalize_key(c.get("Location"))
client_key = normalize_key(c.get(key_field)) client_key = normalize_key(c.get(key_field))
@@ -82,46 +86,54 @@ def build_comparison(client_rows, contractor_rows, key_field):
if not client_location or not client_key: if not client_location or not client_key:
continue continue
s = contractor_lookup.get((client_location, client_key)) subs = contractor_lookup.get((client_location, client_key))
if not s: if not subs:
continue continue
idx = used_index[(client_location, client_key)]
# ❗ If subcontractor rows are exhausted, skip
if idx >= len(subs):
continue
s = subs[idx] # ✅ take NEXT subcontractor row
used_index[(client_location, client_key)] += 1
# ---- totals ----
client_total = sum( client_total = sum(
float(v or 0) float(v or 0)
for k, v in c.items() for k, v in c.items()
if k.endswith("_total") or D_RANGE_PATTERN.match(k) or PIPE_MM_PATTERN.match(k) if k.endswith("_total")
or D_RANGE_PATTERN.match(k)
or PIPE_MM_PATTERN.match(k)
) )
sub_total = sum( sub_total = sum(
float(v or 0) float(v or 0)
for k, v in s.items() for k, v in s.items()
if k.endswith("_total") or D_RANGE_PATTERN.match(k) or PIPE_MM_PATTERN.match(k) if k.endswith("_total")
or D_RANGE_PATTERN.match(k)
or PIPE_MM_PATTERN.match(k)
) )
diff = client_total - sub_total
row = { row = {
"Location": client_location, "Location": client_location,
key_field.replace("_", " "): client_key key_field.replace("_", " "): client_key
} }
# CLIENT DATA
for k, v in c.items(): for k, v in c.items():
if k in ["id", "created_at"]: if k not in ["id", "created_at"]:
continue row[f"Client-{k}"] = v
row[f"Client-{k}"] = v
row["Client-Total"] = round(client_total, 2) row["Client-Total"] = round(client_total, 2)
row[" "] = "" row[" "] = ""
# SUBCONTRACTOR DATA
for k, v in s.items(): for k, v in s.items():
if k in ["id", "created_at", "subcontractor_id"]: if k not in ["id", "created_at", "subcontractor_id"]:
continue row[f"Subcontractor-{k}"] = v
row[f"Subcontractor-{k}"] = v
row["Subcontractor-Total"] = round(sub_total, 2) row["Subcontractor-Total"] = round(sub_total, 2)
row["Diff"] = round(diff, 2) row["Diff"] = round(client_total - sub_total, 2)
output.append(row) output.append(row)
@@ -130,6 +142,8 @@ def build_comparison(client_rows, contractor_rows, key_field):
return df return df
# EXCEL SHEET WRITER # EXCEL SHEET WRITER
def write_sheet(writer, df, sheet_name, subcontractor_name): def write_sheet(writer, df, sheet_name, subcontractor_name):
workbook = writer.book workbook = writer.book
@@ -220,8 +234,8 @@ def comparison_report():
with pd.ExcelWriter(output, engine="xlsxwriter") as writer: with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
write_sheet(writer, df_tr, "Tr.Ex", subcontractor.subcontractor_name) write_sheet(writer, df_tr, "Tr.Ex", subcontractor.subcontractor_name)
write_sheet(writer, df_mh, "Mh.Ex", subcontractor.subcontractor_name) write_sheet(writer, df_mh, "Mh.Ex", subcontractor.subcontractor_name)
write_sheet(writer, df_dc, "MH & DC", subcontractor.subcontractor_name) # write_sheet(writer, df_dc, "MH & DC", subcontractor.subcontractor_name)
write_sheet(writer, df_lay, "Laying", subcontractor.subcontractor_name) # write_sheet(writer, df_lay, "Laying", subcontractor.subcontractor_name)
output.seek(0) output.seek(0)
return send_file( return send_file(

View File

@@ -1,87 +1,118 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
<div class="container-fluid px-2 px-md-4"> <div class="container-fluid px-2 px-md-4">
<h4 class="mb-3 text-center text-md-start">Comparison dSoftware Solapur (UGD) - Live Dashboard</h4>
<h4 class="mb-3 text-center text-md-start">Comparison Software Solapur(UGD) </h4>
<!-- Summary Cards -->
<div class="row g-3 mb-4"> <div class="row g-3 mb-4">
<!-- Total Work -->
<div class="col-12 col-md-4"> <div class="col-12 col-md-4">
<div class="card text-white bg-primary shadow h-100"> <div class="card text-white bg-primary shadow h-100">
<div class="card-body text-center text-md-start"> <div class="card-body text-center text-md-start">
<h6>Test Total Work</h6> <h6>Trenching Units</h6>
<h3 class="fw-bold">30%</h3> <h3 class="fw-bold" id="card-trench">0</h3>
</div> </div>
</div> </div>
</div> </div>
<!-- Completed -->
<div class="col-12 col-md-4"> <div class="col-12 col-md-4">
<div class="card text-white bg-success shadow h-100"> <div class="card text-white bg-success shadow h-100">
<div class="card-body text-center text-md-start"> <div class="card-body text-center text-md-start">
<h6>test Completed</h6> <h6>Manhole Units</h6>
<h3 class="fw-bold">35%</h3> <h3 class="fw-bold" id="card-manhole">0</h3>
</div> </div>
</div> </div>
</div> </div>
<!-- Pending -->
<div class="col-12 col-md-4"> <div class="col-12 col-md-4">
<div class="card text-dark bg-warning shadow h-100"> <div class="card text-dark bg-warning shadow h-100">
<div class="card-body text-center text-md-start"> <div class="card-body text-center text-md-start">
<h6>Pending</h6> <h6>Laying Units</h6>
<h3 class="fw-bold">35%</h3> <h3 class="fw-bold" id="card-laying">0</h3>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<!-- Charts -->
<div class="row g-3"> <div class="row g-3">
<!-- Bar Chart -->
<div class="col-12 col-md-6"> <div class="col-12 col-md-6">
<div class="card shadow-sm h-100"> <div class="card shadow-sm h-100">
<div class="card-header bg-dark text-white text-center text-md-start"> <div class="card-header bg-dark text-white">Live Category Bar Chart</div>
Work Category Bar Chart <div class="card-body">
</div> <canvas id="liveBarChart" style="max-height:300px;"></canvas>
<div class="card-body text-center">
<img src="data:image/png;base64,{{ bar_chart }}" class="img-fluid" style="max-height:300px;">
</div> </div>
</div> </div>
</div> </div>
<!-- Pie Chart -->
<div class="col-12 col-md-6"> <div class="col-12 col-md-6">
<div class="card shadow-sm h-100"> <div class="card shadow-sm h-100">
<div class="card-header bg-dark text-white text-center text-md-start"> <div class="card-header bg-dark text-white">Location Distribution Pie Chart</div>
Project Status Pie Chart <div class="card-body">
</div> <canvas id="livePieChart" style="max-height:300px;"></canvas>
<div class="card-body text-center">
<img src="data:image/png;base64,{{ pie_chart }}" class="img-fluid" style="max-height:300px;">
</div> </div>
</div> </div>
</div> </div>
<!-- Histogram -->
<div class="col-12">
<div class="card shadow-sm">
<div class="card-header bg-dark text-white text-center text-md-start">
Daily Work Histogram
</div>
<div class="card-body text-center">
<img src="data:image/png;base64,{{ histogram }}" class="img-fluid" style="max-height:350px;">
</div>
</div>
</div>
</div> </div>
</div> </div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
// 2. Initialize the Bar Chart
const barCtx = document.getElementById('liveBarChart').getContext('2d');
let liveBarChart = new Chart(barCtx, {
type: 'bar',
data: {
labels: ['Trenching', 'Manholes', 'Laying'],
datasets: [{
label: 'Units Completed',
data: [0, 0, 0],
backgroundColor: ['#0d6efd', '#198754', '#ffc107']
}]
},
options: { responsive: true, maintainAspectRatio: false }
});
// 3. Initialize the Pie Chart
const pieCtx = document.getElementById('livePieChart').getContext('2d');
let livePieChart = new Chart(pieCtx, {
type: 'pie',
data: {
labels: [], // Will be filled from SQL
datasets: [{
data: [],
backgroundColor: ['#0d6efd', '#198754', '#ffc107', '#6f42c1', '#fd7e14']
}]
},
options: { responsive: true, maintainAspectRatio: false }
});
// 4. Function to Fetch Live Data from your Python API
function fetchLiveData() {
fetch('/dashboard/api/live-stats') // This matches the route we created in the "Kitchen"
.then(response => response.json())
.then(data => {
// Update the Summary Cards
document.getElementById('card-trench').innerText = data.summary.trench;
document.getElementById('card-manhole').innerText = data.summary.manhole;
document.getElementById('card-laying').innerText = data.summary.laying;
// Update Bar Chart
liveBarChart.data.datasets[0].data = [
data.summary.trench,
data.summary.manhole,
data.summary.laying
];
liveBarChart.update();
// Update Pie Chart (Location stats)
livePieChart.data.labels = Object.keys(data.locations);
livePieChart.data.datasets[0].data = Object.values(data.locations);
livePieChart.update();
})
.catch(err => console.error("Error fetching live data:", err));
}
// 5. Check for updates every 10 seconds (Real-time effect)
setInterval(fetchLiveData, 10000);
fetchLiveData(); // Load immediately on page open
</script>
{% endblock %} {% endblock %}