Compare commits
5 Commits
pankaj-dev
...
de8e68aff2
| Author | SHA1 | Date | |
|---|---|---|---|
| de8e68aff2 | |||
| 2ef8a9aff9 | |||
| 20a5d01f88 | |||
| 1dceb640bd | |||
| 359847958d |
2
.env
2
.env
@@ -20,7 +20,7 @@ DB_HOST=127.0.0.1
|
|||||||
DB_PORT=3306
|
DB_PORT=3306
|
||||||
DB_NAME=comparisondb
|
DB_NAME=comparisondb
|
||||||
DB_USER=root
|
DB_USER=root
|
||||||
DB_PASSWORD=root
|
DB_PASSWORD=admin
|
||||||
|
|
||||||
# DATABASE_URL=mysql+pymysql://root:root@localhost/comparisondb
|
# DATABASE_URL=mysql+pymysql://root:root@localhost/comparisondb
|
||||||
|
|
||||||
|
|||||||
@@ -1,87 +1,136 @@
|
|||||||
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
|
||||||
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
|
||||||
|
|
||||||
|
# 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")
|
dashboard_bp = Blueprint("dashboard", __name__, url_prefix="/dashboard")
|
||||||
|
|
||||||
# dashboard_bp = Blueprint("dashboard", __name__)
|
@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()
|
||||||
|
|
||||||
# charts
|
# 2. Location Distribution (Business reach)
|
||||||
# def plot_to_base64():
|
loc_results = db.session.query(
|
||||||
# img = io.BytesIO()
|
TrenchExcavation.Location,
|
||||||
# plt.savefig(img, format="png", bbox_inches="tight")
|
func.count(TrenchExcavation.id)
|
||||||
# plt.close()
|
).group_by(TrenchExcavation.Location).all()
|
||||||
# img.seek(0)
|
|
||||||
# return base64.b64encode(img.getvalue()).decode()
|
|
||||||
|
|
||||||
# bar chart
|
# 3. Work Timeline (Business productivity trend)
|
||||||
def bar_chart():
|
# Assuming your models have a 'created_at' field
|
||||||
categories = ["Trench", "Manhole", "Pipe Laying", "Restoration"]
|
timeline_results = db.session.query(
|
||||||
values = [120, 80, 150, 60]
|
func.date(TrenchExcavation.created_at),
|
||||||
|
func.count(TrenchExcavation.id)
|
||||||
|
).group_by(func.date(TrenchExcavation.created_at)).order_by(func.date(TrenchExcavation.created_at)).all()
|
||||||
|
|
||||||
plt.figure()
|
return jsonify({
|
||||||
plt.bar(categories, values)
|
"summary": {
|
||||||
plt.title("Work Category Report")
|
"trench": t_count,
|
||||||
plt.xlabel("test Category")
|
"manhole": m_count,
|
||||||
plt.ylabel("test Quantity")
|
"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(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("/")
|
@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
|
|
||||||
@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
|
|
||||||
)
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
from flask import Blueprint, render_template, request, send_file, flash
|
from flask import Blueprint, render_template, request, send_file, flash
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import io
|
import io
|
||||||
|
import re
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from app.models.subcontractor_model import Subcontractor
|
from app.models.subcontractor_model import Subcontractor
|
||||||
from app.models.trench_excavation_model import TrenchExcavation
|
from app.models.trench_excavation_model import TrenchExcavation
|
||||||
@@ -14,26 +16,20 @@ from app.models.mh_dc_client_model import ManholeDomesticChamberClient
|
|||||||
from app.models.laying_client_model import LayingClient
|
from app.models.laying_client_model import LayingClient
|
||||||
|
|
||||||
from app.utils.helpers import login_required
|
from app.utils.helpers import login_required
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
|
generate_report_bp = Blueprint("generate_report", __name__, url_prefix="/report")
|
||||||
|
|
||||||
|
# --- REGEX PATTERNS FOR TOTALING ---
|
||||||
# sum field of pipe laying (pipe_150_mm)
|
|
||||||
PIPE_MM_PATTERN = re.compile(r"^pipe_\d+_mm$")
|
PIPE_MM_PATTERN = re.compile(r"^pipe_\d+_mm$")
|
||||||
# sum fields of MH dc (d_0_to_0_75)
|
D_RANGE_PATTERN = re.compile(r"^d_\d+(?:_\d+)?_to_\d+(?:_\d+)?$")
|
||||||
D_RANGE_PATTERN = re.compile( r"^d_\d+(?:_\d+)?_to_\d+(?:_\d+)?$")
|
|
||||||
|
|
||||||
|
# --- UTILITIES ---
|
||||||
|
|
||||||
# NORMALIZER
|
|
||||||
def normalize_key(value):
|
def normalize_key(value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return ""
|
||||||
return str(value).strip().upper()
|
return str(value).strip().upper()
|
||||||
|
|
||||||
|
|
||||||
# HEADER FORMATTER
|
|
||||||
def format_header(header):
|
def format_header(header):
|
||||||
if "-" in header:
|
if "-" in header:
|
||||||
prefix, rest = header.split("-", 1)
|
prefix, rest = header.split("-", 1)
|
||||||
@@ -44,7 +40,6 @@ def format_header(header):
|
|||||||
parts = rest.split("_")
|
parts = rest.split("_")
|
||||||
result = []
|
result = []
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
while i < len(parts):
|
while i < len(parts):
|
||||||
if i + 1 < len(parts) and parts[i].isdigit() and parts[i + 1].isdigit():
|
if i + 1 < len(parts) and parts[i].isdigit() and parts[i + 1].isdigit():
|
||||||
result.append(f"{parts[i]}.{parts[i + 1]}")
|
result.append(f"{parts[i]}.{parts[i + 1]}")
|
||||||
@@ -56,122 +51,125 @@ def format_header(header):
|
|||||||
final_text = " ".join(result)
|
final_text = " ".join(result)
|
||||||
return f"{prefix}-{final_text}" if prefix else final_text
|
return f"{prefix}-{final_text}" if prefix else final_text
|
||||||
|
|
||||||
|
|
||||||
# LOOKUP CREATOR
|
|
||||||
def make_lookup(rows, key_field):
|
def make_lookup(rows, key_field):
|
||||||
lookup = {}
|
"""Creates a mapping of (Location, Key) to a list of records."""
|
||||||
|
lookup = defaultdict(list)
|
||||||
for r in rows:
|
for r in rows:
|
||||||
location = normalize_key(r.get("Location"))
|
# Check both capitalized and lowercase keys for robustness
|
||||||
key_val = normalize_key(r.get(key_field))
|
loc = normalize_key(r.get("Location") or r.get("location"))
|
||||||
|
key = normalize_key(r.get(key_field) or r.get(key_field.lower()))
|
||||||
if location and key_val:
|
if loc and key:
|
||||||
lookup[(location, key_val)] = r
|
lookup[(loc, key)].append(r)
|
||||||
|
|
||||||
return lookup
|
return lookup
|
||||||
|
|
||||||
|
def calculate_row_total(row_dict):
|
||||||
|
"""Calculates total based on _total suffix or regex patterns."""
|
||||||
|
return sum(
|
||||||
|
float(v or 0) for k, v in row_dict.items()
|
||||||
|
if k.endswith("_total") or D_RANGE_PATTERN.match(k) or PIPE_MM_PATTERN.match(k)
|
||||||
|
)
|
||||||
|
|
||||||
|
# --- CORE COMPARISON LOGIC ---
|
||||||
|
|
||||||
|
|
||||||
# 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)
|
# 1. Create Lookup for Subcontractors
|
||||||
|
contractor_lookup = {}
|
||||||
|
for r in contractor_rows:
|
||||||
|
loc = normalize_key(r.get("Location") or r.get("location"))
|
||||||
|
key = normalize_key(r.get(key_field) or r.get(key_field.lower()))
|
||||||
|
if loc and key:
|
||||||
|
contractor_lookup[(loc, key)] = r
|
||||||
|
|
||||||
output = []
|
output = []
|
||||||
|
|
||||||
|
# 2. Iterate through Client rows
|
||||||
for c in client_rows:
|
for c in client_rows:
|
||||||
client_location = normalize_key(c.get("Location"))
|
loc_raw = c.get("Location") or c.get("location")
|
||||||
client_key = normalize_key(c.get(key_field))
|
key_raw = c.get(key_field) or c.get(key_field.lower())
|
||||||
|
|
||||||
|
loc_norm = normalize_key(loc_raw)
|
||||||
|
key_norm = normalize_key(key_raw)
|
||||||
|
|
||||||
if not client_location or not client_key:
|
# Match check
|
||||||
continue
|
s = contractor_lookup.get((loc_norm, key_norm))
|
||||||
|
|
||||||
|
# We only include the row if there is a match (Inner Join)
|
||||||
|
if s:
|
||||||
|
client_total = calculate_row_total(c)
|
||||||
|
sub_total = calculate_row_total(s)
|
||||||
|
|
||||||
s = contractor_lookup.get((client_location, client_key))
|
row = {
|
||||||
if not s:
|
"Location": loc_raw,
|
||||||
continue
|
key_field.replace("_", " "): key_raw
|
||||||
|
}
|
||||||
|
|
||||||
client_total = sum(
|
# Add Client Data
|
||||||
float(v or 0)
|
for k, v in c.items():
|
||||||
for k, v in c.items()
|
if k in ["id", "created_at"]: continue
|
||||||
if k.endswith("_total") or D_RANGE_PATTERN.match(k) or PIPE_MM_PATTERN.match(k)
|
row[f"Client-{k}"] = v
|
||||||
)
|
row["Client-Total"] = round(client_total, 2)
|
||||||
|
|
||||||
sub_total = sum(
|
row[" "] = "" # Spacer
|
||||||
float(v or 0)
|
|
||||||
for k, v in s.items()
|
|
||||||
if k.endswith("_total") or D_RANGE_PATTERN.match(k) or PIPE_MM_PATTERN.match(k)
|
|
||||||
)
|
|
||||||
|
|
||||||
diff = client_total - sub_total
|
# Add Subcontractor Data (Aligned on same row)
|
||||||
|
for k, v in s.items():
|
||||||
|
if k in ["id", "created_at", "subcontractor_id"]: continue
|
||||||
|
row[f"Subcontractor-{k}"] = v
|
||||||
|
|
||||||
|
row["Subcontractor-Total"] = round(sub_total, 2)
|
||||||
|
row["Diff"] = round(client_total - sub_total, 2)
|
||||||
|
|
||||||
|
output.append(row)
|
||||||
|
|
||||||
row = {
|
# 3. Handle the "Empty/Blank" scenario using pd.concat
|
||||||
"Location": client_location,
|
if not output:
|
||||||
key_field.replace("_", " "): client_key
|
# Create a basic dataframe with a message so the Excel file isn't empty/corrupt
|
||||||
}
|
return pd.DataFrame([{"Location": "N/A", "Message": "No matching data found"}])
|
||||||
|
|
||||||
# CLIENT DATA
|
|
||||||
for k, v in c.items():
|
|
||||||
if k in ["id", "created_at"]:
|
|
||||||
continue
|
|
||||||
row[f"Client-{k}"] = v
|
|
||||||
|
|
||||||
row["Client-Total"] = round(client_total, 2)
|
|
||||||
row[" "] = ""
|
|
||||||
|
|
||||||
# SUBCONTRACTOR DATA
|
|
||||||
for k, v in s.items():
|
|
||||||
if k in ["id", "created_at", "subcontractor_id"]:
|
|
||||||
continue
|
|
||||||
row[f"Subcontractor-{k}"] = v
|
|
||||||
|
|
||||||
row["Subcontractor-Total"] = round(sub_total, 2)
|
|
||||||
row["Diff"] = round(diff, 2)
|
|
||||||
|
|
||||||
output.append(row)
|
|
||||||
|
|
||||||
df = pd.DataFrame(output)
|
df = pd.DataFrame(output)
|
||||||
df.columns = [format_header(col) for col in df.columns]
|
df.columns = [format_header(col) for col in df.columns]
|
||||||
return df
|
return df
|
||||||
|
|
||||||
|
# --- EXCEL WRITER ---
|
||||||
|
|
||||||
# EXCEL SHEET WRITER
|
|
||||||
def write_sheet(writer, df, sheet_name, subcontractor_name):
|
def write_sheet(writer, df, sheet_name, subcontractor_name):
|
||||||
|
if df.empty:
|
||||||
|
return
|
||||||
|
|
||||||
workbook = writer.book
|
workbook = writer.book
|
||||||
df.to_excel(writer, sheet_name=sheet_name, index=False, startrow=3)
|
df.to_excel(writer, sheet_name=sheet_name, index=False, startrow=3)
|
||||||
ws = writer.sheets[sheet_name]
|
ws = writer.sheets[sheet_name]
|
||||||
|
|
||||||
|
# Formats
|
||||||
title_fmt = workbook.add_format({"bold": True, "font_size": 14})
|
title_fmt = workbook.add_format({"bold": True, "font_size": 14})
|
||||||
client_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#B6DAED"})
|
client_header_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#B6DAED", "align": "center"})
|
||||||
sub_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#F3A081"})
|
sub_header_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#F3A081", "align": "center"})
|
||||||
total_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#F7D261"})
|
total_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#F7D261", "align": "center"})
|
||||||
diff_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#82DD49"})
|
diff_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#82DD49", "align": "center"})
|
||||||
default_header_fmt = workbook.add_format({"bold": True,"border": 1,"bg_color": "#E7E6E6","align": "center","valign": "vcenter"})
|
default_header_fmt = workbook.add_format({"bold": True, "border": 1, "bg_color": "#E7E6E6", "align": "center"})
|
||||||
|
|
||||||
|
|
||||||
ws.merge_range(
|
|
||||||
0, 0, 0, len(df.columns) - 1,
|
|
||||||
"CLIENT vs SUBCONTRACTOR",
|
|
||||||
title_fmt
|
|
||||||
)
|
|
||||||
ws.merge_range(
|
|
||||||
1, 0, 1, len(df.columns) - 1,
|
|
||||||
f"Subcontractor Name - {subcontractor_name}",
|
|
||||||
title_fmt
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# Header Titles
|
||||||
|
ws.merge_range(0, 0, 0, len(df.columns) - 1, "CLIENT vs SUBCONTRACTOR COMPARISON", title_fmt)
|
||||||
|
ws.merge_range(1, 0, 1, len(df.columns) - 1, f"Subcontractor: {subcontractor_name}", title_fmt)
|
||||||
|
|
||||||
for col_num, col_name in enumerate(df.columns):
|
for col_num, col_name in enumerate(df.columns):
|
||||||
if col_name.startswith("Client-"):
|
if col_name.startswith("Client-"):
|
||||||
ws.write(3, col_num, col_name, client_fmt)
|
fmt = client_header_fmt
|
||||||
elif col_name.startswith("Subcontractor-"):
|
elif col_name.startswith("Subcontractor-"):
|
||||||
ws.write(3, col_num, col_name, sub_fmt)
|
fmt = sub_header_fmt
|
||||||
elif col_name.endswith("_total") or col_name.endswith("_total") :
|
elif "Total" in col_name:
|
||||||
ws.write(3, col_num, col_name, total_fmt)
|
fmt = total_fmt
|
||||||
elif col_name == "Diff":
|
elif col_name == "Diff":
|
||||||
ws.write(3, col_num, col_name, diff_fmt)
|
fmt = diff_fmt
|
||||||
else:
|
else:
|
||||||
ws.write(3, col_num, col_name, default_header_fmt)
|
fmt = default_header_fmt
|
||||||
|
|
||||||
|
ws.write(3, col_num, col_name, fmt)
|
||||||
|
ws.set_column(col_num, col_num, 18)
|
||||||
|
|
||||||
ws.set_column(col_num, col_num, 20)
|
# --- ROUTES ---
|
||||||
|
|
||||||
|
|
||||||
# REPORT ROUTE
|
|
||||||
@generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
|
@generate_report_bp.route("/comparison_report", methods=["GET", "POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def comparison_report():
|
def comparison_report():
|
||||||
@@ -180,48 +178,29 @@ def comparison_report():
|
|||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
subcontractor_id = request.form.get("subcontractor_id")
|
subcontractor_id = request.form.get("subcontractor_id")
|
||||||
if not subcontractor_id:
|
if not subcontractor_id:
|
||||||
flash("Please select subcontractor", "danger")
|
flash("Please select a subcontractor", "danger")
|
||||||
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
return render_template("generate_comparison_report.html", subcontractors=subcontractors)
|
||||||
|
|
||||||
subcontractor = Subcontractor.query.get_or_404(subcontractor_id)
|
subcontractor = Subcontractor.query.get_or_404(subcontractor_id)
|
||||||
|
|
||||||
# -------- DATA --------
|
# Build Dataframes for each section
|
||||||
tr_client = [r.serialize() for r in TrenchExcavationClient.query.all()]
|
sections = [
|
||||||
tr_sub = [r.serialize() for r in TrenchExcavation.query.filter_by(
|
(TrenchExcavationClient, TrenchExcavation, "Tr.Ex"),
|
||||||
subcontractor_id=subcontractor_id
|
(ManholeExcavationClient, ManholeExcavation, "Mh.Ex"),
|
||||||
).all()]
|
(ManholeDomesticChamberClient, ManholeDomesticChamber, "MH & DC"),
|
||||||
df_tr = build_comparison(tr_client, tr_sub, "MH_NO")
|
(LayingClient, Laying, "Laying")
|
||||||
|
]
|
||||||
|
|
||||||
mh_client = [r.serialize() for r in ManholeExcavationClient.query.all()]
|
|
||||||
mh_sub = [r.serialize() for r in ManholeExcavation.query.filter_by(
|
|
||||||
subcontractor_id=subcontractor_id
|
|
||||||
).all()]
|
|
||||||
df_mh = build_comparison(mh_client, mh_sub, "MH_NO")
|
|
||||||
|
|
||||||
dc_client = [r.serialize() for r in ManholeDomesticChamberClient.query.all()]
|
|
||||||
dc_sub = [r.serialize() for r in ManholeDomesticChamber.query.filter_by(
|
|
||||||
subcontractor_id=subcontractor_id
|
|
||||||
).all()]
|
|
||||||
df_dc = build_comparison(dc_client, dc_sub, "MH_NO")
|
|
||||||
# df_dc = build_comparison_mh_dc(dc_client, dc_sub, "MH_NO")
|
|
||||||
|
|
||||||
lay_client = [r.serialize() for r in LayingClient.query.all()]
|
|
||||||
lay_sub = [r.serialize() for r in Laying.query.filter_by(
|
|
||||||
subcontractor_id=subcontractor_id
|
|
||||||
).all()]
|
|
||||||
df_lay = build_comparison(lay_client, lay_sub, "MH_NO")
|
|
||||||
# df_lay = build_comparison_laying(lay_client, lay_sub, "MH_NO")
|
|
||||||
|
|
||||||
|
|
||||||
# -------- EXCEL --------
|
|
||||||
output = io.BytesIO()
|
output = io.BytesIO()
|
||||||
filename = f"{subcontractor.subcontractor_name}_Comparison_Report.xlsx"
|
filename = f"{subcontractor.subcontractor_name}_Comparison_Report.xlsx"
|
||||||
|
|
||||||
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)
|
for client_model, sub_model, sheet_name in sections:
|
||||||
write_sheet(writer, df_mh, "Mh.Ex", subcontractor.subcontractor_name)
|
c_data = [r.serialize() for r in client_model.query.all()]
|
||||||
write_sheet(writer, df_dc, "MH & DC", subcontractor.subcontractor_name)
|
s_data = [r.serialize() for r in sub_model.query.filter_by(subcontractor_id=subcontractor_id).all()]
|
||||||
write_sheet(writer, df_lay, "Laying", subcontractor.subcontractor_name)
|
|
||||||
|
df = build_comparison(c_data, s_data, "MH_NO")
|
||||||
|
write_sheet(writer, df, sheet_name, subcontractor.subcontractor_name)
|
||||||
|
|
||||||
output.seek(0)
|
output.seek(0)
|
||||||
return send_file(
|
return send_file(
|
||||||
@@ -231,107 +210,4 @@ def comparison_report():
|
|||||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
)
|
)
|
||||||
|
|
||||||
return render_template("generate_comparison_report.html",subcontractors=subcontractors)
|
return render_template("generate_comparison_report.html", subcontractors=subcontractors)
|
||||||
|
|
||||||
|
|
||||||
# def build_comparison_mh_dc(client_rows, contractor_rows, key_field):
|
|
||||||
# contractor_lookup = make_lookup(contractor_rows, key_field)
|
|
||||||
# mh_dc_fields = ManholeDomesticChamberClient.sum_mh_dc_fields()
|
|
||||||
|
|
||||||
# output = []
|
|
||||||
|
|
||||||
# for c in client_rows:
|
|
||||||
# loc = normalize_key(c.get("Location"))
|
|
||||||
# key = normalize_key(c.get(key_field))
|
|
||||||
# if not loc or not key:
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# s = contractor_lookup.get((loc, key))
|
|
||||||
# if not s:
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# client_total = sum(float(c.get(f, 0) or 0) for f in mh_dc_fields)
|
|
||||||
# sub_total = sum(float(s.get(f, 0) or 0) for f in mh_dc_fields)
|
|
||||||
|
|
||||||
# row = {
|
|
||||||
# "Location": loc,
|
|
||||||
# key_field.replace("_", " "): key
|
|
||||||
# }
|
|
||||||
|
|
||||||
# # CLIENT – ALL FIELDS
|
|
||||||
# for k, v in c.items():
|
|
||||||
# if k in ["id", "created_at"]:
|
|
||||||
# continue
|
|
||||||
# row[f"Client-{k}"] = v
|
|
||||||
|
|
||||||
# row["Client-Total"] = round(client_total, 2)
|
|
||||||
# row[" "] = ""
|
|
||||||
|
|
||||||
# # SUBCONTRACTOR – ALL FIELDS
|
|
||||||
# for k, v in s.items():
|
|
||||||
# if k in ["id", "created_at", "subcontractor_id"]:
|
|
||||||
# continue
|
|
||||||
# row[f"Subcontractor-{k}"] = v
|
|
||||||
|
|
||||||
# row["Subcontractor-Total"] = round(sub_total, 2)
|
|
||||||
# row["Diff"] = round(client_total - sub_total, 2)
|
|
||||||
|
|
||||||
# output.append(row)
|
|
||||||
|
|
||||||
# df = pd.DataFrame(output)
|
|
||||||
# df.columns = [format_header(col) for col in df.columns]
|
|
||||||
# return df
|
|
||||||
|
|
||||||
|
|
||||||
# def build_comparison_laying(client_rows, contractor_rows, key_field):
|
|
||||||
# contractor_lookup = make_lookup(contractor_rows, key_field)
|
|
||||||
# laying_fields = Laying.sum_laying_fields()
|
|
||||||
|
|
||||||
# output = []
|
|
||||||
|
|
||||||
# for c in client_rows:
|
|
||||||
# loc = normalize_key(c.get("Location"))
|
|
||||||
# key = normalize_key(c.get(key_field))
|
|
||||||
# if not loc or not key:
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# s = contractor_lookup.get((loc, key))
|
|
||||||
# if not s:
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# client_total = sum(float(c.get(f, 0) or 0) for f in laying_fields)
|
|
||||||
# sub_total = sum(float(s.get(f, 0) or 0) for f in laying_fields)
|
|
||||||
|
|
||||||
# print("--------------",key,"----------")
|
|
||||||
# print("sum -client_total ",client_total)
|
|
||||||
# print("sum -sub_total ",sub_total)
|
|
||||||
# print("Diff ---- ",client_total - sub_total)
|
|
||||||
# print("------------------------")
|
|
||||||
# row = {
|
|
||||||
# "Location": loc,
|
|
||||||
# key_field.replace("_", " "): key
|
|
||||||
# }
|
|
||||||
|
|
||||||
# # CLIENT – ALL FIELDS
|
|
||||||
# for k, v in c.items():
|
|
||||||
# if k in ["id", "created_at"]:
|
|
||||||
# continue
|
|
||||||
# row[f"Client-{k}"] = v
|
|
||||||
|
|
||||||
# row["Client-Total"] = round(client_total, 2)
|
|
||||||
# row[" "] = ""
|
|
||||||
|
|
||||||
# # SUBCONTRACTOR – ALL FIELDS
|
|
||||||
# for k, v in s.items():
|
|
||||||
# if k in ["id", "created_at", "subcontractor_id"]:
|
|
||||||
# continue
|
|
||||||
# row[f"Subcontractor-{k}"] = v
|
|
||||||
|
|
||||||
# row["Subcontractor-Total"] = round(sub_total, 2)
|
|
||||||
# row["Diff"] = round(client_total - sub_total, 2)
|
|
||||||
|
|
||||||
# output.append(row)
|
|
||||||
|
|
||||||
# df = pd.DataFrame(output)
|
|
||||||
# df.columns = [format_header(col) for col in df.columns]
|
|
||||||
# return df
|
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
<!-- Dashboard -->
|
<!-- Dashboard -->
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="/dashboard">
|
<a class="nav-link" href="/dashboard">
|
||||||
<i class="bi bi-speedometer2 me-1"></i> Dashboard
|
<i class="bi bi-speedometer2 me-1"></i> Dashboard - Anish
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
|
|||||||
@@ -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 Software 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 %}
|
||||||
Reference in New Issue
Block a user