Spaces:
Build error
Build error
Create analytics.py
Browse files- utils/analytics.py +73 -0
utils/analytics.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/analytics.py
|
| 2 |
+
|
| 3 |
+
def get_collection_analytics(conn: sqlite3.Connection) -> Dict:
|
| 4 |
+
"""Get analytics for collections and documents."""
|
| 5 |
+
analytics = {}
|
| 6 |
+
|
| 7 |
+
# Most used collections
|
| 8 |
+
cursor = conn.cursor()
|
| 9 |
+
cursor.execute("""
|
| 10 |
+
SELECT
|
| 11 |
+
c.name,
|
| 12 |
+
COUNT(DISTINCT cm.chat_id) as usage_count
|
| 13 |
+
FROM collections c
|
| 14 |
+
LEFT JOIN chats ch ON c.id = ch.collection_id
|
| 15 |
+
LEFT JOIN chat_messages cm ON ch.id = cm.chat_id
|
| 16 |
+
GROUP BY c.id
|
| 17 |
+
ORDER BY usage_count DESC
|
| 18 |
+
LIMIT 5
|
| 19 |
+
""")
|
| 20 |
+
analytics['top_collections'] = cursor.fetchall()
|
| 21 |
+
|
| 22 |
+
# Most viewed documents
|
| 23 |
+
cursor.execute("""
|
| 24 |
+
SELECT
|
| 25 |
+
d.name,
|
| 26 |
+
COUNT(q.id) as query_count
|
| 27 |
+
FROM documents d
|
| 28 |
+
LEFT JOIN queries q ON d.id = q.document_id
|
| 29 |
+
GROUP BY d.id
|
| 30 |
+
ORDER BY query_count DESC
|
| 31 |
+
LIMIT 5
|
| 32 |
+
""")
|
| 33 |
+
analytics['top_documents'] = cursor.fetchall()
|
| 34 |
+
|
| 35 |
+
# Recent questions
|
| 36 |
+
cursor.execute("""
|
| 37 |
+
SELECT DISTINCT query
|
| 38 |
+
FROM queries
|
| 39 |
+
ORDER BY query_date DESC
|
| 40 |
+
LIMIT 5
|
| 41 |
+
""")
|
| 42 |
+
analytics['recent_questions'] = cursor.fetchall()
|
| 43 |
+
|
| 44 |
+
return analytics
|
| 45 |
+
|
| 46 |
+
def display_analytics_dashboard():
|
| 47 |
+
"""Display analytics dashboard."""
|
| 48 |
+
st.markdown("### 📊 Analytics Dashboard")
|
| 49 |
+
|
| 50 |
+
analytics = get_collection_analytics(st.session_state.db_conn)
|
| 51 |
+
|
| 52 |
+
col1, col2, col3 = st.columns(3)
|
| 53 |
+
|
| 54 |
+
with col1:
|
| 55 |
+
st.markdown("#### Top Collections")
|
| 56 |
+
if analytics['top_collections']:
|
| 57 |
+
chart_data = pd.DataFrame(
|
| 58 |
+
analytics['top_collections'],
|
| 59 |
+
columns=['Collection', 'Usage']
|
| 60 |
+
)
|
| 61 |
+
st.bar_chart(chart_data.set_index('Collection'))
|
| 62 |
+
|
| 63 |
+
with col2:
|
| 64 |
+
st.markdown("#### Most Viewed Documents")
|
| 65 |
+
if analytics['top_documents']:
|
| 66 |
+
for doc in analytics['top_documents']:
|
| 67 |
+
st.metric(doc[0], f"{doc[1]} views")
|
| 68 |
+
|
| 69 |
+
with col3:
|
| 70 |
+
st.markdown("#### Recent Questions")
|
| 71 |
+
if analytics['recent_questions']:
|
| 72 |
+
for question in analytics['recent_questions']:
|
| 73 |
+
st.write(f"• {question[0]}")
|