Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,24 +5,27 @@ from oauth2client.service_account import ServiceAccountCredentials
|
|
5 |
from datetime import datetime, timedelta
|
6 |
|
7 |
# -------------------- AUTH --------------------
|
8 |
-
scope = [
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
client = gspread.authorize(creds)
|
11 |
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
|
12 |
|
13 |
# -------------------- UTILS --------------------
|
14 |
-
def normalize_columns(df):
|
15 |
-
df.columns = df.columns.str.strip().str.title()
|
16 |
return df
|
17 |
|
18 |
-
def load_sheet(sheet_name):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return df
|
24 |
-
except Exception as e:
|
25 |
-
return pd.DataFrame([{"Error": str(e)}])
|
26 |
|
27 |
def get_current_week_range():
|
28 |
today = datetime.now()
|
@@ -30,54 +33,70 @@ def get_current_week_range():
|
|
30 |
end = start + timedelta(days=6)
|
31 |
return start.date(), end.date()
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
start, end = get_current_week_range()
|
36 |
-
filtered = df[(df[
|
37 |
if rep:
|
38 |
-
filtered = filtered[filtered[
|
39 |
return filtered
|
40 |
|
41 |
-
def
|
|
|
|
|
|
|
42 |
try:
|
43 |
target = datetime(int(y), int(m), int(d)).date()
|
44 |
except:
|
45 |
return pd.DataFrame([{"Error": "Invalid date input"}])
|
46 |
-
df[
|
47 |
-
filtered = df[df[
|
48 |
if rep:
|
49 |
-
filtered = filtered[filtered[
|
50 |
return filtered
|
51 |
|
52 |
-
# --------------------
|
53 |
-
def
|
54 |
-
df = load_sheet("Calls")
|
55 |
-
if "Call Date" not in df.columns:
|
56 |
-
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
57 |
-
return filter_week(df, "Call Date", "Rep", rep)
|
58 |
-
|
59 |
-
def get_appointments(rep=None):
|
60 |
df = load_sheet("Appointments")
|
61 |
if "Appointment Date" not in df.columns:
|
62 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
def
|
66 |
-
|
67 |
-
if "
|
68 |
-
return
|
69 |
-
return
|
|
|
|
|
70 |
|
71 |
def search_appointments_by_date(y, m, d, rep):
|
72 |
df = load_sheet("Appointments")
|
73 |
if "Appointment Date" not in df.columns:
|
74 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
|
|
77 |
def get_leads_detail():
|
78 |
df = load_sheet("AllocatedLeads")
|
79 |
-
|
80 |
-
df = df.rename(columns={"Assigned Rep": "Assigned Rep", "Company Name": "Company Name"})
|
81 |
if "Assigned Rep" not in df.columns or "Company Name" not in df.columns:
|
82 |
return pd.DataFrame([{"Error": "Missing 'Assigned Rep' or 'Company Name' column"}])
|
83 |
return df
|
@@ -86,87 +105,99 @@ def get_leads_summary():
|
|
86 |
df = get_leads_detail()
|
87 |
if "Error" in df.columns:
|
88 |
return df
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
|
93 |
-
# -------------------- INSIGHTS
|
94 |
def compute_insights():
|
95 |
calls = get_calls()
|
96 |
-
|
97 |
leads = get_leads_detail()
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
top_leads = leads.groupby("Assigned Rep").size().idxmax() if "Assigned Rep" in leads.columns else "N/A"
|
102 |
|
103 |
-
|
104 |
-
{"Metric": "Most Calls This Week", "Rep":
|
105 |
-
{"Metric": "Most Appointments This Week", "Rep":
|
106 |
-
{"Metric": "Most Leads Allocated", "Rep":
|
107 |
])
|
108 |
-
return insights
|
109 |
|
110 |
# -------------------- DROPDOWN OPTIONS --------------------
|
111 |
def rep_options(sheet_name, rep_col):
|
112 |
df = load_sheet(sheet_name)
|
113 |
-
if rep_col in df.columns
|
114 |
-
return sorted(df[rep_col].dropna().unique().tolist())
|
115 |
-
return []
|
116 |
|
117 |
# -------------------- UI LAYOUT --------------------
|
118 |
with gr.Blocks(title="Graffiti Admin Dashboard") as app:
|
119 |
gr.Markdown("# 📆 Graffiti Admin Dashboard")
|
120 |
|
|
|
121 |
with gr.Tab("Calls Report"):
|
122 |
-
rep_calls = gr.Dropdown(
|
123 |
-
|
|
|
|
|
124 |
calls_table = gr.Dataframe()
|
125 |
calls_btn.click(fn=get_calls, inputs=rep_calls, outputs=calls_table)
|
126 |
|
127 |
gr.Markdown("### 🔍 Search Calls by Specific Date")
|
128 |
-
y1, m1, d1 = gr.Textbox(
|
129 |
-
rep1 = gr.Dropdown(
|
130 |
-
|
|
|
|
|
131 |
calls_date_table = gr.Dataframe()
|
132 |
-
calls_date_btn.click(fn=search_calls_by_date,
|
|
|
|
|
133 |
|
|
|
134 |
with gr.Tab("Appointments Report"):
|
135 |
-
rep_appt = gr.Dropdown(
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
142 |
inputs=rep_appt,
|
143 |
-
outputs=[
|
144 |
)
|
145 |
|
146 |
gr.Markdown("### 🔍 Search Appointments by Specific Date")
|
147 |
-
y2, m2, d2 = gr.Textbox(
|
148 |
-
rep2 = gr.Dropdown(
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
with gr.Tab("Appointed Leads"):
|
162 |
leads_btn = gr.Button("View Appointed Leads")
|
163 |
-
|
164 |
-
|
165 |
leads_btn.click(
|
166 |
fn=lambda: (get_leads_summary(), get_leads_detail()),
|
167 |
-
outputs=[
|
168 |
)
|
169 |
|
|
|
170 |
with gr.Tab("Insights"):
|
171 |
insights_btn = gr.Button("Generate Insights")
|
172 |
insights_tbl = gr.Dataframe()
|
|
|
5 |
from datetime import datetime, timedelta
|
6 |
|
7 |
# -------------------- AUTH --------------------
|
8 |
+
scope = [
|
9 |
+
"https://spreadsheets.google.com/feeds",
|
10 |
+
"https://www.googleapis.com/auth/drive"
|
11 |
+
]
|
12 |
+
creds = ServiceAccountCredentials.from_json_keyfile_name(
|
13 |
+
"deep-mile-461309-t8-0e90103411e0.json",
|
14 |
+
scope
|
15 |
+
)
|
16 |
client = gspread.authorize(creds)
|
17 |
sheet_url = "https://docs.google.com/spreadsheets/d/1if4KoVQvw5ZbhknfdZbzMkcTiPfsD6bz9V3a1th-bwQ"
|
18 |
|
19 |
# -------------------- UTILS --------------------
|
20 |
+
def normalize_columns(df: pd.DataFrame) -> pd.DataFrame:
|
21 |
+
df.columns = df.columns.str.strip().str.title()
|
22 |
return df
|
23 |
|
24 |
+
def load_sheet(sheet_name: str) -> pd.DataFrame:
|
25 |
+
records = client.open_by_url(sheet_url) \
|
26 |
+
.worksheet(sheet_name) \
|
27 |
+
.get_all_records()
|
28 |
+
return normalize_columns(pd.DataFrame(records))
|
|
|
|
|
|
|
29 |
|
30 |
def get_current_week_range():
|
31 |
today = datetime.now()
|
|
|
33 |
end = start + timedelta(days=6)
|
34 |
return start.date(), end.date()
|
35 |
|
36 |
+
# -------------------- CALLS --------------------
|
37 |
+
def get_calls(rep=None):
|
38 |
+
df = load_sheet("Calls")
|
39 |
+
if "Call Date" not in df.columns:
|
40 |
+
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
41 |
+
df["Call Date"] = pd.to_datetime(df["Call Date"], errors="coerce").dt.date
|
42 |
start, end = get_current_week_range()
|
43 |
+
filtered = df[(df["Call Date"] >= start) & (df["Call Date"] <= end)]
|
44 |
if rep:
|
45 |
+
filtered = filtered[filtered["Rep"] == rep]
|
46 |
return filtered
|
47 |
|
48 |
+
def search_calls_by_date(y, m, d, rep):
|
49 |
+
df = load_sheet("Calls")
|
50 |
+
if "Call Date" not in df.columns:
|
51 |
+
return pd.DataFrame([{"Error": "Missing 'Call Date' column"}])
|
52 |
try:
|
53 |
target = datetime(int(y), int(m), int(d)).date()
|
54 |
except:
|
55 |
return pd.DataFrame([{"Error": "Invalid date input"}])
|
56 |
+
df["Call Date"] = pd.to_datetime(df["Call Date"], errors="coerce").dt.date
|
57 |
+
filtered = df[df["Call Date"] == target]
|
58 |
if rep:
|
59 |
+
filtered = filtered[filtered["Rep"] == rep]
|
60 |
return filtered
|
61 |
|
62 |
+
# -------------------- APPOINTMENTS --------------------
|
63 |
+
def appointments_detail(rep=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
df = load_sheet("Appointments")
|
65 |
if "Appointment Date" not in df.columns:
|
66 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
67 |
+
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
68 |
+
start, end = get_current_week_range()
|
69 |
+
filtered = df[(df["Appointment Date"] >= start) & (df["Appointment Date"] <= end)]
|
70 |
+
if rep:
|
71 |
+
filtered = filtered[filtered["Rep"] == rep]
|
72 |
+
return filtered
|
73 |
|
74 |
+
def appointments_summary(rep=None):
|
75 |
+
det = appointments_detail(rep)
|
76 |
+
if "Error" in det.columns:
|
77 |
+
return det
|
78 |
+
return det.groupby("Rep") \
|
79 |
+
.size() \
|
80 |
+
.reset_index(name="Appointment Count")
|
81 |
|
82 |
def search_appointments_by_date(y, m, d, rep):
|
83 |
df = load_sheet("Appointments")
|
84 |
if "Appointment Date" not in df.columns:
|
85 |
return pd.DataFrame([{"Error": "Missing 'Appointment Date' column"}])
|
86 |
+
try:
|
87 |
+
target = datetime(int(y), int(m), int(d)).date()
|
88 |
+
except:
|
89 |
+
return pd.DataFrame([{"Error": "Invalid date input"}])
|
90 |
+
df["Appointment Date"] = pd.to_datetime(df["Appointment Date"], errors="coerce").dt.date
|
91 |
+
filtered = df[df["Appointment Date"] == target]
|
92 |
+
if rep:
|
93 |
+
filtered = filtered[filtered["Rep"] == rep]
|
94 |
+
return filtered
|
95 |
|
96 |
+
# -------------------- LEADS --------------------
|
97 |
def get_leads_detail():
|
98 |
df = load_sheet("AllocatedLeads")
|
99 |
+
df = df.rename(columns={"Assigned Rep": "Assigned Rep"})
|
|
|
100 |
if "Assigned Rep" not in df.columns or "Company Name" not in df.columns:
|
101 |
return pd.DataFrame([{"Error": "Missing 'Assigned Rep' or 'Company Name' column"}])
|
102 |
return df
|
|
|
105 |
df = get_leads_detail()
|
106 |
if "Error" in df.columns:
|
107 |
return df
|
108 |
+
return df.groupby("Assigned Rep") \
|
109 |
+
.size() \
|
110 |
+
.reset_index(name="Leads Count")
|
111 |
|
112 |
+
# -------------------- INSIGHTS --------------------
|
113 |
def compute_insights():
|
114 |
calls = get_calls()
|
115 |
+
appt = appointments_detail()
|
116 |
leads = get_leads_detail()
|
117 |
|
118 |
+
def top(df, col):
|
119 |
+
return df[col].value_counts().idxmax() if not df.empty else "N/A"
|
|
|
120 |
|
121 |
+
return pd.DataFrame([
|
122 |
+
{"Metric": "Most Calls This Week", "Rep": top(calls, "Rep")},
|
123 |
+
{"Metric": "Most Appointments This Week", "Rep": top(appt, "Rep")},
|
124 |
+
{"Metric": "Most Leads Allocated", "Rep": top(leads, "Assigned Rep")},
|
125 |
])
|
|
|
126 |
|
127 |
# -------------------- DROPDOWN OPTIONS --------------------
|
128 |
def rep_options(sheet_name, rep_col):
|
129 |
df = load_sheet(sheet_name)
|
130 |
+
return sorted(df[rep_col].dropna().unique().tolist()) if rep_col in df.columns else []
|
|
|
|
|
131 |
|
132 |
# -------------------- UI LAYOUT --------------------
|
133 |
with gr.Blocks(title="Graffiti Admin Dashboard") as app:
|
134 |
gr.Markdown("# 📆 Graffiti Admin Dashboard")
|
135 |
|
136 |
+
# Calls Report
|
137 |
with gr.Tab("Calls Report"):
|
138 |
+
rep_calls = gr.Dropdown("Optional Rep Filter",
|
139 |
+
choices=rep_options("Calls", "Rep"),
|
140 |
+
allow_custom_value=True)
|
141 |
+
calls_btn = gr.Button("Load Current Week Calls")
|
142 |
calls_table = gr.Dataframe()
|
143 |
calls_btn.click(fn=get_calls, inputs=rep_calls, outputs=calls_table)
|
144 |
|
145 |
gr.Markdown("### 🔍 Search Calls by Specific Date")
|
146 |
+
y1, m1, d1 = gr.Textbox("Year"), gr.Textbox("Month"), gr.Textbox("Day")
|
147 |
+
rep1 = gr.Dropdown("Optional Rep Filter",
|
148 |
+
choices=rep_options("Calls", "Rep"),
|
149 |
+
allow_custom_value=True)
|
150 |
+
calls_date_btn = gr.Button("Search Calls by Date")
|
151 |
calls_date_table = gr.Dataframe()
|
152 |
+
calls_date_btn.click(fn=search_calls_by_date,
|
153 |
+
inputs=[y1, m1, d1, rep1],
|
154 |
+
outputs=calls_date_table)
|
155 |
|
156 |
+
# Appointments Report
|
157 |
with gr.Tab("Appointments Report"):
|
158 |
+
rep_appt = gr.Dropdown("Optional Rep Filter",
|
159 |
+
choices=rep_options("Appointments", "Rep"),
|
160 |
+
allow_custom_value=True)
|
161 |
+
load_btn = gr.Button("Load Current Week Appointments")
|
162 |
+
appt_sum = gr.Dataframe(label="📊 Weekly Appointments Summary by Rep")
|
163 |
+
appt_det = gr.Dataframe(label="🔎 Detailed Appointments")
|
164 |
+
load_btn.click(
|
165 |
+
fn=lambda rep: (appointments_summary(rep), appointments_detail(rep)),
|
166 |
inputs=rep_appt,
|
167 |
+
outputs=[appt_sum, appt_det]
|
168 |
)
|
169 |
|
170 |
gr.Markdown("### 🔍 Search Appointments by Specific Date")
|
171 |
+
y2, m2, d2 = gr.Textbox("Year"), gr.Textbox("Month"), gr.Textbox("Day")
|
172 |
+
rep2 = gr.Dropdown("Optional Rep Filter",
|
173 |
+
choices=rep_options("Appointments", "Rep"),
|
174 |
+
allow_custom_value=True)
|
175 |
+
date_btn = gr.Button("Search Appointments by Date")
|
176 |
+
date_sum = gr.Dataframe(label="📊 Appointments Summary for Date by Rep")
|
177 |
+
date_det = gr.Dataframe(label="🔎 Detailed Appointments")
|
178 |
+
def by_date(y, m, d, rep):
|
179 |
+
df = search_appointments_by_date(y, m, d, rep)
|
180 |
+
if "Error" in df.columns:
|
181 |
+
return df, df
|
182 |
+
return (
|
183 |
+
df.groupby("Rep").size().reset_index(name="Appointment Count"),
|
184 |
+
df
|
185 |
+
)
|
186 |
+
date_btn.click(fn=by_date,
|
187 |
+
inputs=[y2, m2, d2, rep2],
|
188 |
+
outputs=[date_sum, date_det])
|
189 |
+
|
190 |
+
# Appointed Leads
|
191 |
with gr.Tab("Appointed Leads"):
|
192 |
leads_btn = gr.Button("View Appointed Leads")
|
193 |
+
leads_sum = gr.Dataframe(label="📊 Leads Count by Rep")
|
194 |
+
leads_det = gr.Dataframe(label="🔎 Detailed Leads")
|
195 |
leads_btn.click(
|
196 |
fn=lambda: (get_leads_summary(), get_leads_detail()),
|
197 |
+
outputs=[leads_sum, leads_det]
|
198 |
)
|
199 |
|
200 |
+
# Insights
|
201 |
with gr.Tab("Insights"):
|
202 |
insights_btn = gr.Button("Generate Insights")
|
203 |
insights_tbl = gr.Dataframe()
|