MohanadAfiffy commited on
Commit
4c58d1a
·
verified ·
1 Parent(s): 0f3ffc1

Add analytics

Browse files
Files changed (2) hide show
  1. app.py +3 -2
  2. clients.py +73 -1
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import os
3
- from clients import CompanySpecificClient, UserSpecificClient,hooks,RengagmentEmail
4
 
5
  st.set_page_config(page_title="SalesIntel",layout="wide")
6
  st.html("styles.html")
@@ -67,4 +67,5 @@ if __name__ == "__main__":
67
  unsafe_allow_html=True,
68
  )
69
 
70
- main()
 
 
1
  import streamlit as st
2
  import os
3
+ from clients import CompanySpecificClient, UserSpecificClient,hooks,RengagmentEmail,display_analytics
4
 
5
  st.set_page_config(page_title="SalesIntel",layout="wide")
6
  st.html("styles.html")
 
67
  unsafe_allow_html=True,
68
  )
69
 
70
+ main()
71
+ display_analytics()
clients.py CHANGED
@@ -8,6 +8,7 @@ import os
8
  import streamlit as st
9
  import pandas as pd
10
  import requests
 
11
  cold_host = os.getenv("backend_cold")
12
  hook_host = os.getenv("hook_host") # Corrected here
13
  rengagment_host = os.getenv("rengagement_host")
@@ -350,4 +351,75 @@ def RengagmentEmail(email_receiver):
350
  else:
351
  st.error("Data transmission failed. Please verify that your file contains the required columns and try again. If the problem persists, please contact us.")
352
 
353
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  import streamlit as st
9
  import pandas as pd
10
  import requests
11
+ import datetime
12
  cold_host = os.getenv("backend_cold")
13
  hook_host = os.getenv("hook_host") # Corrected here
14
  rengagment_host = os.getenv("rengagement_host")
 
351
  else:
352
  st.error("Data transmission failed. Please verify that your file contains the required columns and try again. If the problem persists, please contact us.")
353
 
354
+ return None
355
+
356
+
357
+ def fetch_analytics_data(host, start_date=None, end_date=None):
358
+ """
359
+ Fetches analytics data from the specified host, processes it, and aggregates by week.
360
+
361
+ Parameters:
362
+ host (str): The host URL to query.
363
+ start_date (datetime): The start date for filtering (inclusive).
364
+ end_date (datetime): The end date for filtering (inclusive).
365
+
366
+ Returns:
367
+ pd.DataFrame: The processed and aggregated analytics data.
368
+ """
369
+ endpoint = f"{host}/analytics/"
370
+ response = requests.get(endpoint)
371
+
372
+ if response.status_code != 200:
373
+ st.error(f"Failed to fetch analytics data from {host}")
374
+ return pd.DataFrame()
375
+
376
+ data = response.json()
377
+
378
+ # Process the fetched data
379
+ records = []
380
+ for email_receiver, timestamps in data.items():
381
+ for timestamp, count in timestamps.items():
382
+ records.append({
383
+ 'email_receiver': email_receiver,
384
+ 'timestamp': pd.to_datetime(timestamp),
385
+ 'count': count
386
+ })
387
+
388
+ df = pd.DataFrame(records)
389
+
390
+ # Filter by date range if specified
391
+ if start_date:
392
+ df = df[df['timestamp'] >= pd.to_datetime(start_date)]
393
+ if end_date:
394
+ df = df[df['timestamp'] <= pd.to_datetime(end_date)]
395
+
396
+ # Aggregate by week
397
+ df['week'] = df['timestamp'].dt.to_period('W').apply(lambda r: r.start_time)
398
+ df = df.groupby(['email_receiver', 'week']).agg({'count': 'sum'}).reset_index()
399
+
400
+ return df
401
+
402
+ def display_analytics():
403
+ """
404
+ Displays the analytics data with a time filter.
405
+ """
406
+ cold_host = os.getenv("backend_cold")
407
+ rengagement_host = os.getenv("rengagement_host")
408
+ hook_host = os.getenv("hook_host")
409
+
410
+ # Time filter inputs
411
+ start_date = st.date_input("Start Date", value=pd.to_datetime("2024-10-01"))
412
+ end_date = st.date_input("End Date", value=pd.to_datetime("today"))
413
+
414
+ cold_df = fetch_analytics_data(cold_host, start_date, end_date)
415
+ rengagement_df = fetch_analytics_data(rengagement_host, start_date, end_date)
416
+ hook_df = fetch_analytics_data(hook_host, start_date, end_date)
417
+
418
+ st.html('<h4 class="hero-subtitle">Cold Emails</h4>')
419
+ st.dataframe(cold_df)
420
+
421
+ st.html('<h4 class="hero-subtitle">Re-engagement Emails</h4>')
422
+ st.dataframe(rengagement_df)
423
+
424
+ st.html('<h4 class="hero-subtitle">Re-engagement Hooks "Hook2Lead"</h4>')
425
+ st.dataframe(hook_df)