268 lines
9.9 KiB
Python
268 lines
9.9 KiB
Python
import streamlit as st
|
|
import pandas as pd
|
|
import mysql.connector
|
|
from datetime import datetime, date, timedelta
|
|
from sqlalchemy import create_engine
|
|
import os
|
|
from dotenv import load_dotenv
|
|
import altair as alt
|
|
|
|
load_dotenv()
|
|
|
|
def get_filtered_data(customer_id, service_id, start_date, end_date):
|
|
"""
|
|
Fetches the user count data grouped by month within the specified date range.
|
|
"""
|
|
db_url = (
|
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
|
f"{os.getenv('MYSQL_DATABASE')}"
|
|
)
|
|
engine = create_engine(db_url)
|
|
|
|
query = f"""
|
|
SELECT DATE_FORMAT(add_date, '%Y-%m') AS day,
|
|
count as count
|
|
FROM Kunden.`daily.users.count_by_services`
|
|
WHERE customer_ID = {customer_id}
|
|
AND services_ID = {service_id}
|
|
AND add_date BETWEEN '{start_date}' AND '{end_date}'
|
|
ORDER BY DATE_FORMAT(add_date, '%Y-%m');
|
|
"""
|
|
service_reporting = pd.read_sql_query(query, engine)
|
|
#engine.close()
|
|
return service_reporting
|
|
|
|
|
|
def get_user_online(customer_id,service_id,start_date,end_date):
|
|
db_url = (
|
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
|
f"{os.getenv('MYSQL_DATABASE')}"
|
|
)
|
|
engine = create_engine(db_url)
|
|
if service_id == 100:
|
|
user_info = "sr.primarymail"
|
|
else:
|
|
user_info = "sr.username"
|
|
query = f"""
|
|
SELECT
|
|
{user_info} as username
|
|
FROM Kunden.`daily.user.online` sr
|
|
WHERE sr.customer_ID = {customer_id}
|
|
AND sr.services_ID = {service_id}
|
|
AND sr.timestamp BETWEEN '{start_date}' AND '{end_date}'
|
|
GROUP BY {user_info}
|
|
ORDER BY {user_info};
|
|
"""
|
|
user_online = pd.read_sql_query(query, engine)
|
|
user_online_count= user_online.shape[0]
|
|
#mydb.close()
|
|
return user_online, user_online_count
|
|
|
|
def get_max_user_count(customer_id, service_id, start_date, end_date):
|
|
"""
|
|
Fetches the maximum user count within the specified date range.
|
|
"""
|
|
db_url = (
|
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
|
f"{os.getenv('MYSQL_DATABASE')}"
|
|
)
|
|
engine = create_engine(db_url)
|
|
user_info = "sr.username"
|
|
query = f"""
|
|
SELECT MAX(username) as max_count
|
|
FROM Kunden.`daily.user.enabled` WHERE customer_ID = {customer_id} AND services_ID = {service_id} AND timestamp BETWEEN '{start_date}' AND '{end_date}'
|
|
"""
|
|
|
|
max_user_count = pd.read_sql_query(query, engine)
|
|
#mydb.close()
|
|
return max_user_count.iloc[0]['max_count'] if not max_user_count.empty else 0
|
|
|
|
def get_active_users(customer_id, service_id, start_date, end_date):
|
|
"""
|
|
Fetch all active users for the given customer, service, and date range
|
|
based on the most recent activity and status.
|
|
"""
|
|
db_url = (
|
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
|
f"{os.getenv('MYSQL_DATABASE')}"
|
|
)
|
|
engine = create_engine(db_url)
|
|
if service_id == 100:
|
|
user_info = "sr.primarymail"
|
|
else:
|
|
user_info = "sr.username"
|
|
query = f"""
|
|
SELECT
|
|
{user_info} as username
|
|
FROM Kunden.`daily.user.enabled` sr
|
|
WHERE sr.customer_ID = {customer_id}
|
|
AND sr.services_ID = {service_id}
|
|
AND sr.timestamp = '{start_date}'
|
|
ORDER BY {user_info};
|
|
"""
|
|
active_users = pd.read_sql_query(query, engine)
|
|
user_active_count = active_users.shape[0]
|
|
return active_users, user_active_count
|
|
|
|
def get_user_not_online(customer_id,service_id,start_date,end_date):
|
|
db_url = (
|
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
|
f"{os.getenv('MYSQL_DATABASE')}"
|
|
)
|
|
engine = create_engine(db_url)
|
|
if service_id == 100:
|
|
user_info = "primarymail"
|
|
else:
|
|
user_info = "username"
|
|
query = f"""
|
|
SELECT
|
|
{user_info} as username
|
|
FROM Kunden.`daily.user.notonline` sr
|
|
WHERE sr.customer_ID = {customer_id}
|
|
AND sr.services_ID = {service_id}
|
|
AND sr.timestamp BETWEEN '{start_date}' AND '{end_date}'
|
|
GROUP BY {user_info}
|
|
ORDER BY {user_info};
|
|
"""
|
|
not_active_users = pd.read_sql_query(query, engine)
|
|
user_not_online_count = not_active_users.shape[0]
|
|
return not_active_users, user_not_online_count
|
|
|
|
def get_initial_data():
|
|
db_url = (
|
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
|
f"{os.getenv('MYSQL_DATABASE')}"
|
|
)
|
|
engine = create_engine(db_url)
|
|
|
|
# Fetch unique service IDs and names
|
|
service_id_query = """
|
|
SELECT DISTINCT s.service_ID, s.name
|
|
FROM Kunden.`services.reporting` sr
|
|
JOIN Kunden.services s ON sr.service_ID = s.service_ID
|
|
"""
|
|
service_ids = pd.read_sql_query(service_id_query, engine)
|
|
|
|
# Fetch customer information
|
|
customer_query = """
|
|
SELECT c.customer_ID, c.customer, co.companyname
|
|
FROM Kunden.company co
|
|
JOIN Kunden.customers c ON co.customer_ID = c.customer_ID
|
|
JOIN Kunden.`services.reporting`sr ON sr.customer_ID = co.customer_ID
|
|
GROUP BY c.customer_ID, c.customer, co.companyname;
|
|
"""
|
|
customers = pd.read_sql_query(customer_query, engine)
|
|
|
|
# Fetch date range
|
|
date_query = """
|
|
SELECT MIN(reportingdate) AS min_date, MAX(reportingdate) AS max_date
|
|
FROM Kunden.`services.reporting`
|
|
"""
|
|
date_range = pd.read_sql_query(date_query, engine)
|
|
|
|
return service_ids, customers, date_range
|
|
|
|
def services_reporting():
|
|
st.title("Reporting :mag_right:")
|
|
|
|
# Get initial data for widgets
|
|
initial_service_ids, customers, initial_date_range = get_initial_data()
|
|
service_options = initial_service_ids.apply(lambda row: f"{row['service_ID']} - {row['name']}", axis=1)
|
|
|
|
# Selection widget for customer ID
|
|
customer_dict = {f"{row['companyname']} - {row['customer']}": row['customer_ID'] for _, row in customers.iterrows()}
|
|
|
|
# Selectbox with only the customer name and company displayed
|
|
selected_customer = st.selectbox(
|
|
'Select Customer',
|
|
list(customer_dict.keys()) # Display only companyname and customer
|
|
)
|
|
|
|
# Get the corresponding customer ID
|
|
selected_customer_id = customer_dict[selected_customer]
|
|
|
|
# Selection widget for service ID
|
|
selected_service = st.selectbox(
|
|
'Select Service',
|
|
service_options.tolist()
|
|
)
|
|
selected_service_id = int(selected_service.split(' - ')[0])
|
|
|
|
# Convert date range to datetime objects
|
|
min_date = initial_date_range['min_date'][0]
|
|
max_date = initial_date_range['max_date'][0]
|
|
|
|
min_date = (date.today().replace(day=1) - timedelta(days=1)).replace(day=1)
|
|
start_date = st.date_input('Start Date', min_date)
|
|
end_date = st.date_input('End Date', initial_date_range['max_date'][0])
|
|
|
|
if st.button('Apply Filters'):
|
|
# Fetch filtered data from the database
|
|
filtered_data = get_filtered_data(selected_customer_id, selected_service_id, start_date, end_date)
|
|
|
|
# Fetch max user count in the selected range
|
|
max_count = get_active_users(selected_customer_id, selected_service_id, start_date, end_date)
|
|
|
|
# Sort the data by day
|
|
filtered_data = filtered_data.sort_values('day')
|
|
|
|
if not filtered_data.empty:
|
|
# Highlight the max value in the chart
|
|
filtered_data['color'] = filtered_data['count'].apply(lambda x: 'red' if x == max_count else 'steelblue')
|
|
|
|
# Create an Altair bar chart
|
|
bars = alt.Chart(filtered_data).mark_bar().encode(
|
|
x='day:O',
|
|
y='count:Q',
|
|
color=alt.Color('color:N', scale=None, legend=None)
|
|
)
|
|
|
|
# Add text labels to bars
|
|
text = bars.mark_text(
|
|
align='center',
|
|
baseline='middle',
|
|
dy=-10
|
|
).encode(
|
|
text='count:Q'
|
|
)
|
|
|
|
# Combine bars and text into a single chart
|
|
chart = (bars + text).properties(
|
|
title='User Enabled'
|
|
)
|
|
|
|
# Fetch the data for users not online, online, and active users
|
|
not_user_online, max_count_user_not_online = get_user_not_online(selected_customer_id, selected_service_id, start_date, end_date)
|
|
user_online, user_online_count = get_user_online(selected_customer_id, selected_service_id, start_date, end_date)
|
|
active_users_data, user_active_count = get_active_users(selected_customer_id, selected_service_id, min_date, end_date)
|
|
# Create three columns for each DataFrame
|
|
col1, col2, col3, col4, col5 = st.columns([2,2,2,2,2])
|
|
|
|
# Display each DataFrame in a separate column
|
|
# with col4:
|
|
# st.subheader(f"{selected_service.split(' - ')[1]} - User not Online")
|
|
# st.metric(label="1",label_visibility="hidden", value=max_count_user_not_online)
|
|
# st.data_editor(not_user_online['username'],key="2",use_container_width=True, hide_index=True)
|
|
|
|
with col2:
|
|
st.subheader(f"{selected_service.split(' - ')[1]} - Enabled Users")
|
|
st.metric(label="1",label_visibility="hidden", value=user_active_count)
|
|
st.data_editor(active_users_data, hide_index=True)
|
|
|
|
with col3:
|
|
st.subheader(f"{selected_service.split(' - ')[1]} - User Online")
|
|
st.metric(label="1",label_visibility="hidden", value=user_online_count)
|
|
st.data_editor(user_online,key=2,hide_index=True)
|
|
|
|
|
|
|
|
st.altair_chart(chart, use_container_width=True)
|
|
else:
|
|
st.write("No data available for the selected filters.")
|