Spaces:
Running
Running
Update analytics_fetch_and_rendering.py
Browse files
analytics_fetch_and_rendering.py
CHANGED
@@ -265,6 +265,52 @@ def plot_interaction_metrics(data):
|
|
265 |
plt.tight_layout(rect=[0, 0, 1, 0.96]) # Leave space for suptitle
|
266 |
return fig
|
267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
|
269 |
def fetch_and_render_analytics(client_id, token):
|
270 |
loading = gr.update(value="<p>Loading follower count...</p>", visible=True)
|
@@ -279,6 +325,8 @@ def fetch_and_render_analytics(client_id, token):
|
|
279 |
posts, org_name, sentiments = fetch_posts_and_stats(client_id, token, count=30)
|
280 |
engagement_data = compute_monthly_avg_engagement_rate(posts)
|
281 |
interaction_data = compute_post_interaction_metrics(posts)
|
|
|
|
|
282 |
|
283 |
|
284 |
count_html = f"""
|
@@ -289,7 +337,7 @@ def fetch_and_render_analytics(client_id, token):
|
|
289 |
<p style='font-size:0.9em; color:#777;'>(As of latest data)</p>
|
290 |
</div>
|
291 |
"""
|
292 |
-
return gr.update(value=count_html, visible=True), gr.update(value=plot_follower_gains(gains), visible=True), gr.update(value=plot_growth_rate(gains, count), visible=True), gr.update(value=plot_avg_engagement_rate(engagement_data), visible=True), gr.update(value=plot_interaction_metrics(interaction_data), visible=True)
|
293 |
|
294 |
except Exception as e:
|
295 |
error = display_error("Analytics load failed.", e).get('value', "<p style='color:red;'>Error loading data.</p>")
|
|
|
265 |
plt.tight_layout(rect=[0, 0, 1, 0.96]) # Leave space for suptitle
|
266 |
return fig
|
267 |
|
268 |
+
from collections import defaultdict
|
269 |
+
import matplotlib.pyplot as plt
|
270 |
+
|
271 |
+
def compute_eb_content_ratio(posts):
|
272 |
+
if not posts:
|
273 |
+
return []
|
274 |
+
|
275 |
+
monthly_counts = defaultdict(lambda: {"eb_count": 0, "total": 0})
|
276 |
+
|
277 |
+
for post in posts:
|
278 |
+
try:
|
279 |
+
month = post["when"][:7] # YYYY-MM
|
280 |
+
category = post.get("category", "None")
|
281 |
+
monthly_counts[month]["total"] += 1
|
282 |
+
if category and category.strip() != "None":
|
283 |
+
monthly_counts[month]["eb_count"] += 1
|
284 |
+
except Exception:
|
285 |
+
continue
|
286 |
+
|
287 |
+
results = []
|
288 |
+
for month in sorted(monthly_counts.keys()):
|
289 |
+
data = monthly_counts[month]
|
290 |
+
ratio = (data["eb_count"] / data["total"]) * 100 if data["total"] else 0
|
291 |
+
results.append({"month": month, "eb_ratio": round(ratio, 2)})
|
292 |
+
|
293 |
+
return results
|
294 |
+
|
295 |
+
def plot_eb_content_ratio(data):
|
296 |
+
if not data:
|
297 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
298 |
+
ax.text(0.5, 0.5, 'No EB content data.', ha='center', va='center', transform=ax.transAxes)
|
299 |
+
ax.set_title('EB Content Ratio (%)')
|
300 |
+
ax.set_xticks([]); ax.set_yticks([])
|
301 |
+
return fig
|
302 |
+
|
303 |
+
months = [d["month"] for d in data]
|
304 |
+
ratios = [d["eb_ratio"] for d in data]
|
305 |
+
|
306 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
307 |
+
ax.plot(months, ratios, label="EB Content Ratio (%)", marker="o", color="#2ca02c")
|
308 |
+
ax.set(title="Monthly EB Content Ratio (%)", xlabel="Month", ylabel="EB Content %")
|
309 |
+
ax.tick_params(axis='x', rotation=45)
|
310 |
+
ax.legend()
|
311 |
+
plt.tight_layout()
|
312 |
+
return fig
|
313 |
+
|
314 |
|
315 |
def fetch_and_render_analytics(client_id, token):
|
316 |
loading = gr.update(value="<p>Loading follower count...</p>", visible=True)
|
|
|
325 |
posts, org_name, sentiments = fetch_posts_and_stats(client_id, token, count=30)
|
326 |
engagement_data = compute_monthly_avg_engagement_rate(posts)
|
327 |
interaction_data = compute_post_interaction_metrics(posts)
|
328 |
+
eb_data = compute_eb_content_ratio(posts)
|
329 |
+
|
330 |
|
331 |
|
332 |
count_html = f"""
|
|
|
337 |
<p style='font-size:0.9em; color:#777;'>(As of latest data)</p>
|
338 |
</div>
|
339 |
"""
|
340 |
+
return gr.update(value=count_html, visible=True), gr.update(value=plot_follower_gains(gains), visible=True), gr.update(value=plot_growth_rate(gains, count), visible=True), gr.update(value=plot_avg_engagement_rate(engagement_data), visible=True), gr.update(value=plot_interaction_metrics(interaction_data), visible=True), gr.update(value=plot_eb_content_ratio(eb_data)
|
341 |
|
342 |
except Exception as e:
|
343 |
error = display_error("Analytics load failed.", e).get('value', "<p style='color:red;'>Error loading data.</p>")
|