many changes
parent
117bae03e0
commit
6d040c0040
|
|
@ -0,0 +1,3 @@
|
||||||
|
apps/ticket_export/exports/RE2025.00012.1.docx
|
||||||
|
apps/ticket_export/exports/RE2025.00013.1.docx
|
||||||
|
apps/ticket_export/exports/RE2025.00014.1.docx
|
||||||
155
app.py
155
app.py
|
|
@ -1,5 +1,6 @@
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
from streamlit_option_menu import option_menu
|
from streamlit_option_menu import option_menu
|
||||||
|
from sqlalchemy import create_engine
|
||||||
import sites.services_reporting as sr
|
import sites.services_reporting as sr
|
||||||
import sites.userlist as us
|
import sites.userlist as us
|
||||||
import sites.server as s
|
import sites.server as s
|
||||||
|
|
@ -16,6 +17,8 @@ st.set_page_config(page_title="Reporting", layout="wide")
|
||||||
|
|
||||||
start_date = datetime.today().replace(day=1) - relativedelta(months=1)
|
start_date = datetime.today().replace(day=1) - relativedelta(months=1)
|
||||||
end_date = datetime.today().replace(day=1) - relativedelta(days=1)
|
end_date = datetime.today().replace(day=1) - relativedelta(days=1)
|
||||||
|
start_date_lastmonth = datetime.today().replace(day=1) - relativedelta(months=2)
|
||||||
|
end_date_lastmonth = datetime.today().replace(day=1) - relativedelta(months=1) - relativedelta(days=1)
|
||||||
|
|
||||||
# Datumsformatierung
|
# Datumsformatierung
|
||||||
start_date_format = start_date.strftime("%Y-%m-%d")
|
start_date_format = start_date.strftime("%Y-%m-%d")
|
||||||
|
|
@ -26,7 +29,7 @@ def load_css(file_name):
|
||||||
with open(file_name) as f:
|
with open(file_name) as f:
|
||||||
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
|
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
|
||||||
|
|
||||||
def get_customer_used_service():
|
def get_customer_used_service(end_date):
|
||||||
mydb = mysql.connector.connect(
|
mydb = mysql.connector.connect(
|
||||||
host=os.getenv("MYSQL_HOST"),
|
host=os.getenv("MYSQL_HOST"),
|
||||||
user=os.getenv("MYSQL_USER"),
|
user=os.getenv("MYSQL_USER"),
|
||||||
|
|
@ -36,89 +39,129 @@ def get_customer_used_service():
|
||||||
|
|
||||||
mycursor = mydb.cursor()
|
mycursor = mydb.cursor()
|
||||||
|
|
||||||
mycursor.execute(f"""SELECT c.companyname, c.customer_ID, cs.services_ID,s.name from Kunden.`customers.services` cs
|
mycursor.execute(f"""SELECT cs.companyname, cs.customer_ID, cs.services_ID,cs.name from Kunden.`daily.customer.services` cs WHERE add_date LIKE '%{end_date}%'""")
|
||||||
JOIN Kunden.company c ON c.customer_ID = cs.customer_ID
|
|
||||||
JOIN Kunden.services s ON s.service_ID = cs.services_ID
|
|
||||||
where 1=1 GROUP by c.companyname , c.customer_ID, cs.services_ID, s.name """)
|
|
||||||
|
|
||||||
myresult = mycursor.fetchall()
|
myresult = mycursor.fetchall()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
return myresult
|
return myresult
|
||||||
|
|
||||||
def load_user_service_list(service_id, customer_id, start_date, end_date):
|
def load_server_list(start_date, end_date):
|
||||||
mydb = mysql.connector.connect(
|
db_url = (
|
||||||
host=os.getenv("MYSQL_HOST"),
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
||||||
user=os.getenv("MYSQL_USER"),
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
||||||
password=os.getenv("MYSQL_PASSWORD"),
|
f"{os.getenv('MYSQL_DATABASE')}"
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
|
||||||
)
|
)
|
||||||
|
engine = create_engine(db_url)
|
||||||
|
query = f"""
|
||||||
|
SELECT server, cores, customer_id
|
||||||
|
FROM Kunden.`daily.spla.server` sr
|
||||||
|
WHERE sr.timestamp BETWEEN '{start_date}' AND '{end_date}'
|
||||||
|
"""
|
||||||
|
max_server_count = pd.read_sql_query(query, engine)
|
||||||
|
return max_server_count
|
||||||
|
|
||||||
|
|
||||||
|
def load_user_service_list(service_id, customer_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)
|
||||||
|
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT COUNT(*) AS max_count
|
SELECT COUNT(*) AS max_count
|
||||||
FROM (
|
FROM (
|
||||||
SELECT
|
SELECT username
|
||||||
u.user_id,
|
FROM Kunden.`daily.user.enabled` sr
|
||||||
u.username,
|
WHERE sr.customer_ID = {customer_id}
|
||||||
active_users.last_active_timestamp,
|
AND sr.services_ID = {service_id}
|
||||||
active_users.status
|
AND sr.timestamp BETWEEN '{start_date_format}' AND '{end_date_format}'
|
||||||
FROM Kunden.`users` u
|
) AS sub;
|
||||||
JOIN (
|
|
||||||
SELECT
|
|
||||||
us.user_id,
|
|
||||||
us.`timestamp` AS last_active_timestamp,
|
|
||||||
us.status
|
|
||||||
FROM Kunden.`users.services` us
|
|
||||||
JOIN (
|
|
||||||
SELECT
|
|
||||||
user_id,
|
|
||||||
MAX(`timestamp`) AS last_active_timestamp
|
|
||||||
FROM Kunden.`users.services`
|
|
||||||
WHERE `timestamp` <= '{end_date}'
|
|
||||||
AND customer_id = {customer_id}
|
|
||||||
AND service_ID = {service_id}
|
|
||||||
AND user_id NOT IN (
|
|
||||||
SELECT user_id
|
|
||||||
FROM Kunden.`users.status`
|
|
||||||
WHERE status = 0
|
|
||||||
AND `timestamp` < '{start_date}'
|
|
||||||
AND customer_id = {customer_id}
|
|
||||||
)
|
|
||||||
GROUP BY user_id
|
|
||||||
) max_timestamps
|
|
||||||
ON us.user_id = max_timestamps.user_id
|
|
||||||
AND us.`timestamp` = max_timestamps.last_active_timestamp
|
|
||||||
WHERE us.customer_id = {customer_id}
|
|
||||||
AND us.service_ID = {service_id}
|
|
||||||
AND us.status = 1
|
|
||||||
) active_users
|
|
||||||
ON u.user_id = active_users.user_id
|
|
||||||
) AS max_count;
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
max_user_count = pd.read_sql_query(query, mydb)
|
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
|
return max_user_count.iloc[0]['max_count'] if not max_user_count.empty else 0
|
||||||
|
|
||||||
|
def load_user_disabled(start_date, end_date,customer_id):
|
||||||
|
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 disabledate, username, service_name, customer_name, ticketnumber, comment
|
||||||
|
FROM Kunden.`daily.user.disabled` sud
|
||||||
|
WHERE sud.disabledate BETWEEN '{start_date}' AND '{end_date}' AND customer_id = '{customer_id}' AND services_id IN (100,101,116,120) ORDER BY service_name
|
||||||
|
"""
|
||||||
|
all = pd.read_sql_query(query, engine)
|
||||||
|
return all
|
||||||
|
|
||||||
def home():
|
def home():
|
||||||
st.title("Dashboard")
|
st.title("Dashboard")
|
||||||
edit_start_date = start_date.strftime("%d.%m.%Y")
|
edit_start_date = start_date.strftime("%d.%m.%Y")
|
||||||
edit_end_date = end_date.strftime("%d.%m.%Y")
|
edit_end_date = end_date.strftime("%d.%m.%Y")
|
||||||
|
edit_start_date_lastmonth = start_date_lastmonth.strftime("%d.%m.%Y")
|
||||||
|
edit_end_date_lastmonth = end_date_lastmonth.strftime("%d.%m.%Y")
|
||||||
st.subheader(f"Übersicht {edit_start_date} - {edit_end_date}")
|
st.subheader(f"Übersicht {edit_start_date} - {edit_end_date}")
|
||||||
previous_value = None
|
previous_value = None
|
||||||
columns = None
|
columns = None
|
||||||
|
|
||||||
c = 0
|
c = 0
|
||||||
for i in get_customer_used_service():
|
print(end_date)
|
||||||
|
for i in get_customer_used_service(end_date.strftime("%Y-%m-%d")):
|
||||||
|
print(i)
|
||||||
if previous_value != i[1]:
|
if previous_value != i[1]:
|
||||||
st.subheader(f"{i[0]}")
|
st.subheader(f"{i[0]}")
|
||||||
columns = st.columns(3)
|
columns = st.columns(4)
|
||||||
|
df = load_user_disabled(start_date_lastmonth, end_date_lastmonth, i[1])
|
||||||
|
|
||||||
|
if not df.empty:
|
||||||
|
st.text(f"Deaktivierte User {edit_start_date_lastmonth} - {edit_end_date_lastmonth}")
|
||||||
|
st.data_editor(df,use_container_width=True)
|
||||||
c = 0
|
c = 0
|
||||||
if not (load_user_service_list(i[2], i[1], start_date, end_date)):
|
|
||||||
|
active_users = load_user_service_list(i[2], i[1], start_date, end_date)
|
||||||
|
|
||||||
|
# Filter nach dem spezifischen Service und zähle die eindeutigen Benutzernamen
|
||||||
|
disabled_users_count = 0
|
||||||
|
if not df.empty and 'service_name' in df.columns and 'username' in df.columns:
|
||||||
|
# Annahme: i[3] enthält den Service-Namen, der mit der 'service_name'-Spalte übereinstimmt
|
||||||
|
service_filtered_df = df[df['service_name'] == i[3]]
|
||||||
|
disabled_users_count = service_filtered_df['username'].nunique()
|
||||||
|
|
||||||
|
if not active_users:
|
||||||
st.info(f"Kunde {i[0]} - Service {i[3]} - Not Data found!")
|
st.info(f"Kunde {i[0]} - Service {i[3]} - Not Data found!")
|
||||||
else:
|
else:
|
||||||
columns[c].metric(label=f"Aktive {i[3]} User", value=load_user_service_list(i[2], i[1], start_date, end_date))
|
columns[c].metric(
|
||||||
c +=1
|
label=f"Aktive {i[3]} User",
|
||||||
|
value=active_users,
|
||||||
|
delta=f"-{disabled_users_count}" if disabled_users_count > 0 else None,
|
||||||
|
delta_color="inverse"
|
||||||
|
)
|
||||||
|
c += 1
|
||||||
previous_value = i[1]
|
previous_value = i[1]
|
||||||
|
|
||||||
|
col1, = st.columns(1)
|
||||||
|
df = load_server_list(start_date,end_date)
|
||||||
|
grouped = df.groupby('server')['cores'].count()*8/2
|
||||||
|
grouped_series = df.groupby('server')['cores'].max()
|
||||||
|
grouped_str = grouped_series.to_string(header=False)
|
||||||
|
df['cores'] = pd.to_numeric(df['cores'], errors='coerce')
|
||||||
|
server_cores = df.groupby('server')['cores'].max()
|
||||||
|
count_cores = server_cores.count()
|
||||||
|
with col1:
|
||||||
|
st.header("CPU Liste")
|
||||||
|
st.text(
|
||||||
|
f"Anzahl der Cores:\n{grouped_str}\n "
|
||||||
|
)
|
||||||
|
st.text(f"Gesamte Anzahl der Cores: {count_cores}")
|
||||||
|
st.text(f"Berechung der Core-Pakete = Anzahl der Cores ({count_cores}) * Core-Pakete aus SPLA (8) / 2")
|
||||||
|
st.header(f"Gesamt : {str(grouped.sum()).split('.')[0]} Pakete")
|
||||||
|
|
||||||
|
# Ausgabe der Ergebnisse
|
||||||
|
|
||||||
# Navigation bar using streamlit-option-menu
|
# Navigation bar using streamlit-option-menu
|
||||||
with st.sidebar:
|
with st.sidebar:
|
||||||
selected_page = option_menu(
|
selected_page = option_menu(
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -24,7 +24,7 @@ def fetch_tickets_from_database():
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
|
|
||||||
# Tickets abrufen, inklusive customer_ID
|
# Tickets abrufen, inklusive customer_ID
|
||||||
cursor.execute("""
|
cursor.execute(f"""
|
||||||
SELECT t.`number`, t.title, t.createdate, t.`type`, t.customer_ID, tct.firstdate, t.time, s.price, t.service_ID, t.tags
|
SELECT t.`number`, t.title, t.createdate, t.`type`, t.customer_ID, tct.firstdate, t.time, s.price, t.service_ID, t.tags
|
||||||
FROM Kunden.tickets t
|
FROM Kunden.tickets t
|
||||||
JOIN Kunden.`tickets.customer.timerange` tct ON t.customer_ID = tct.customer_ID
|
JOIN Kunden.`tickets.customer.timerange` tct ON t.customer_ID = tct.customer_ID
|
||||||
|
|
@ -267,9 +267,9 @@ if __name__ == "__main__":
|
||||||
"housenumber": customer_data[2],
|
"housenumber": customer_data[2],
|
||||||
"postcode": customer_data[3],
|
"postcode": customer_data[3],
|
||||||
"city": customer_data[4],
|
"city": customer_data[4],
|
||||||
"customernumber": customer_data[5],
|
"cnumber": customer_data[5],
|
||||||
"year": datetime.now().year,
|
"year": datetime.now().year,
|
||||||
"ordernumber": "1",
|
"onumber": "1",
|
||||||
"startdate": startdate.strftime("%d.%m.%Y"),
|
"startdate": startdate.strftime("%d.%m.%Y"),
|
||||||
"enddate": enddate.strftime("%d.%m.%Y"),
|
"enddate": enddate.strftime("%d.%m.%Y"),
|
||||||
"today": datetime.now().strftime("%d.%m.%Y"),
|
"today": datetime.now().strftime("%d.%m.%Y"),
|
||||||
|
|
@ -284,6 +284,6 @@ if __name__ == "__main__":
|
||||||
"mwst_set": "0,00",
|
"mwst_set": "0,00",
|
||||||
"sum": "0,00",
|
"sum": "0,00",
|
||||||
}
|
}
|
||||||
output_path = f"apps/ticket_export/exports/RE2025.{customer_data[5]}.{data['ordernumber']}.docx"
|
output_path = f"apps/ticket_export/exports/RE2025.{customer_data[5]}.{data['onumber']}.docx"
|
||||||
fill_template('apps/ticket_export/template.docx', output_path, data, customer_tickets)
|
fill_template('apps/ticket_export/template.docx', output_path, data, customer_tickets)
|
||||||
print("True")
|
print("True")
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -16,14 +16,14 @@ def get_filtered_users(customer_id, service_id):
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
database=os.getenv("MYSQL_DATABASE")
|
||||||
)
|
)
|
||||||
|
|
||||||
query = f"""SELECT * FROM Kunden.`services.reporting` where DATE(reportingdate) = CURDATE() """
|
query = f"""SELECT * FROM Kunden.`daily.user.online` where DATE(timestamp) = CURDATE() """
|
||||||
|
|
||||||
|
|
||||||
if customer_id:
|
if customer_id:
|
||||||
query += f"AND customer_ID = {customer_id}"
|
query += f"AND customer_ID = {customer_id}"
|
||||||
if service_id:
|
if service_id:
|
||||||
query += f" AND service_ID = {service_id}"
|
query += f" AND services_id = {service_id}"
|
||||||
query += " ORDER BY reportingdate DESC"
|
query += " ORDER BY timestamp DESC"
|
||||||
users = pd.read_sql_query(query, mydb)
|
users = pd.read_sql_query(query, mydb)
|
||||||
print(query)
|
print(query)
|
||||||
mydb.close()
|
mydb.close()
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
from datetime import datetime, date
|
from datetime import datetime, date, timedelta
|
||||||
|
from sqlalchemy import create_engine
|
||||||
import os
|
import os
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import altair as alt
|
import altair as alt
|
||||||
|
|
@ -12,91 +13,71 @@ 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.
|
Fetches the user count data grouped by month within the specified date range.
|
||||||
"""
|
"""
|
||||||
mydb = mysql.connector.connect(
|
db_url = (
|
||||||
host=os.getenv("MYSQL_HOST"),
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
||||||
user=os.getenv("MYSQL_USER"),
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
||||||
password=os.getenv("MYSQL_PASSWORD"),
|
f"{os.getenv('MYSQL_DATABASE')}"
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
|
||||||
)
|
)
|
||||||
|
engine = create_engine(db_url)
|
||||||
|
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT DATE_FORMAT(sr.reportingdate, '%Y-%m') AS day,
|
SELECT DATE_FORMAT(add_date, '%Y-%m') AS day,
|
||||||
COUNT(DISTINCT sr.username) as count
|
count as count
|
||||||
FROM Kunden.`services.reporting` sr
|
FROM Kunden.`daily.users.count_by_services`
|
||||||
JOIN Kunden.services s ON sr.service_ID = s.service_ID
|
WHERE customer_ID = {customer_id}
|
||||||
WHERE sr.customer_ID = {customer_id}
|
AND services_ID = {service_id}
|
||||||
AND sr.service_ID = {service_id}
|
AND add_date BETWEEN '{start_date}' AND '{end_date}'
|
||||||
AND sr.username NOT LIKE '%admin%'
|
ORDER BY DATE_FORMAT(add_date, '%Y-%m');
|
||||||
AND sr.username NOT LIKE '%test%'
|
|
||||||
AND sr.reportingdate BETWEEN '{start_date}' AND '{end_date}'
|
|
||||||
GROUP BY DATE_FORMAT(sr.reportingdate, '%Y-%m')
|
|
||||||
ORDER BY DATE_FORMAT(sr.reportingdate, '%Y-%m');
|
|
||||||
"""
|
"""
|
||||||
|
service_reporting = pd.read_sql_query(query, engine)
|
||||||
service_reporting = pd.read_sql_query(query, mydb)
|
#engine.close()
|
||||||
mydb.close()
|
|
||||||
return service_reporting
|
return service_reporting
|
||||||
|
|
||||||
|
|
||||||
def get_user_online(customer_id,service_id,start_date,end_date):
|
def get_user_online(customer_id,service_id,start_date,end_date):
|
||||||
mydb = mysql.connector.connect(
|
db_url = (
|
||||||
host=os.getenv("MYSQL_HOST"),
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
||||||
user=os.getenv("MYSQL_USER"),
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
||||||
password=os.getenv("MYSQL_PASSWORD"),
|
f"{os.getenv('MYSQL_DATABASE')}"
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
|
||||||
)
|
)
|
||||||
|
engine = create_engine(db_url)
|
||||||
if service_id == 100:
|
if service_id == 100:
|
||||||
user_info = "u.primarymail as username"
|
user_info = "sr.primarymail"
|
||||||
else:
|
else:
|
||||||
user_info = "u.username"
|
user_info = "sr.username"
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT DATE_FORMAT(sr.reportingdate, '%Y-%m') AS day,
|
SELECT
|
||||||
sr.username
|
{user_info} as username
|
||||||
FROM Kunden.`services.reporting` sr
|
FROM Kunden.`daily.user.online` sr
|
||||||
JOIN Kunden.services s ON sr.service_ID = s.service_ID
|
|
||||||
WHERE sr.customer_ID = {customer_id}
|
WHERE sr.customer_ID = {customer_id}
|
||||||
AND sr.service_ID = {service_id}
|
AND sr.services_ID = {service_id}
|
||||||
AND sr.username NOT LIKE '%admin%'
|
AND sr.timestamp BETWEEN '{start_date}' AND '{end_date}'
|
||||||
AND sr.username NOT LIKE '%test%'
|
GROUP BY {user_info}
|
||||||
AND sr.reportingdate BETWEEN '{start_date}' AND '{end_date}'
|
ORDER BY {user_info};
|
||||||
GROUP BY day, sr.username
|
|
||||||
ORDER BY sr.username;
|
|
||||||
"""
|
"""
|
||||||
|
user_online = pd.read_sql_query(query, engine)
|
||||||
user_online = pd.read_sql_query(query, mydb)
|
|
||||||
user_online_count= user_online.shape[0]
|
user_online_count= user_online.shape[0]
|
||||||
mydb.close()
|
#mydb.close()
|
||||||
return user_online, user_online_count
|
return user_online, user_online_count
|
||||||
|
|
||||||
def get_max_user_count(customer_id, service_id, start_date, end_date):
|
def get_max_user_count(customer_id, service_id, start_date, end_date):
|
||||||
"""
|
"""
|
||||||
Fetches the maximum user count within the specified date range.
|
Fetches the maximum user count within the specified date range.
|
||||||
"""
|
"""
|
||||||
mydb = mysql.connector.connect(
|
db_url = (
|
||||||
host=os.getenv("MYSQL_HOST"),
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
||||||
user=os.getenv("MYSQL_USER"),
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
||||||
password=os.getenv("MYSQL_PASSWORD"),
|
f"{os.getenv('MYSQL_DATABASE')}"
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
|
||||||
)
|
)
|
||||||
|
engine = create_engine(db_url)
|
||||||
|
user_info = "sr.username"
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT MAX(user_counts.count) as max_count
|
SELECT MAX(username) as max_count
|
||||||
FROM (
|
FROM Kunden.`daily.user.enabled` WHERE customer_ID = {customer_id} AND services_ID = {service_id} AND timestamp BETWEEN '{start_date}' AND '{end_date}'
|
||||||
SELECT DATE_FORMAT(sr.reportingdate, '%Y-%m') AS day,
|
|
||||||
COUNT(DISTINCT sr.username) as count
|
|
||||||
FROM Kunden.`services.reporting` sr
|
|
||||||
JOIN Kunden.services s ON sr.service_ID = s.service_ID
|
|
||||||
WHERE sr.customer_ID = {customer_id}
|
|
||||||
AND sr.service_ID = {service_id}
|
|
||||||
AND sr.username NOT LIKE '%admin%'
|
|
||||||
AND sr.username NOT LIKE '%test%'
|
|
||||||
AND sr.reportingdate BETWEEN '{start_date}' AND '{end_date}'
|
|
||||||
GROUP BY DATE_FORMAT(sr.reportingdate, '%Y-%m')
|
|
||||||
) as user_counts;
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
max_user_count = pd.read_sql_query(query, mydb)
|
max_user_count = pd.read_sql_query(query, engine)
|
||||||
mydb.close()
|
#mydb.close()
|
||||||
return max_user_count.iloc[0]['max_count'] if not max_user_count.empty else 0
|
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):
|
def get_active_users(customer_id, service_id, start_date, end_date):
|
||||||
|
|
@ -104,104 +85,61 @@ def get_active_users(customer_id, service_id, start_date, end_date):
|
||||||
Fetch all active users for the given customer, service, and date range
|
Fetch all active users for the given customer, service, and date range
|
||||||
based on the most recent activity and status.
|
based on the most recent activity and status.
|
||||||
"""
|
"""
|
||||||
mydb = mysql.connector.connect(
|
db_url = (
|
||||||
host=os.getenv("MYSQL_HOST"),
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
||||||
user=os.getenv("MYSQL_USER"),
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
||||||
password=os.getenv("MYSQL_PASSWORD"),
|
f"{os.getenv('MYSQL_DATABASE')}"
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
|
||||||
)
|
)
|
||||||
|
engine = create_engine(db_url)
|
||||||
if service_id == 100:
|
if service_id == 100:
|
||||||
user_info = "u.primarymail as username"
|
user_info = "sr.primarymail"
|
||||||
else:
|
else:
|
||||||
user_info = "u.username"
|
user_info = "sr.username"
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT
|
SELECT
|
||||||
{user_info}
|
{user_info} as username
|
||||||
FROM Kunden.`users` u
|
FROM Kunden.`daily.user.enabled` sr
|
||||||
JOIN (
|
WHERE sr.customer_ID = {customer_id}
|
||||||
SELECT
|
AND sr.services_ID = {service_id}
|
||||||
us.user_id,
|
AND sr.timestamp = '{start_date}'
|
||||||
us.`timestamp` AS last_active_timestamp,
|
ORDER BY {user_info};
|
||||||
us.status
|
|
||||||
FROM Kunden.`users.services` us
|
|
||||||
JOIN (
|
|
||||||
SELECT
|
|
||||||
user_id,
|
|
||||||
MAX(`timestamp`) AS last_active_timestamp
|
|
||||||
FROM Kunden.`users.services`
|
|
||||||
WHERE `timestamp` <= '{end_date}'
|
|
||||||
AND customer_id = {customer_id}
|
|
||||||
AND service_ID = {service_id}
|
|
||||||
AND user_id NOT IN (
|
|
||||||
SELECT user_id
|
|
||||||
FROM Kunden.`users.status`
|
|
||||||
WHERE status = 0
|
|
||||||
AND `timestamp` < '{start_date}'
|
|
||||||
AND customer_id = {customer_id}
|
|
||||||
)
|
|
||||||
GROUP BY user_id
|
|
||||||
) max_timestamps ON us.user_id = max_timestamps.user_id
|
|
||||||
AND us.`timestamp` = max_timestamps.last_active_timestamp
|
|
||||||
WHERE us.customer_id = {customer_id}
|
|
||||||
AND us.service_ID = {service_id}
|
|
||||||
AND us.status = 1
|
|
||||||
) active_users ON u.user_id = active_users.user_id
|
|
||||||
GROUP BY u.user_id, u.username, active_users.last_active_timestamp, active_users.status ORDER by u.username ASC;
|
|
||||||
"""
|
"""
|
||||||
active_users = pd.read_sql_query(query, mydb)
|
active_users = pd.read_sql_query(query, engine)
|
||||||
user_active_count = active_users.shape[0]
|
user_active_count = active_users.shape[0]
|
||||||
mydb.close()
|
|
||||||
return active_users, user_active_count
|
return active_users, user_active_count
|
||||||
|
|
||||||
def get_user_not_online(customer_id,service_id,start_date,end_date):
|
def get_user_not_online(customer_id,service_id,start_date,end_date):
|
||||||
mydb = mysql.connector.connect(
|
db_url = (
|
||||||
host=os.getenv("MYSQL_HOST"),
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
||||||
user=os.getenv("MYSQL_USER"),
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
||||||
password=os.getenv("MYSQL_PASSWORD"),
|
f"{os.getenv('MYSQL_DATABASE')}"
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
|
||||||
)
|
)
|
||||||
|
engine = create_engine(db_url)
|
||||||
|
if service_id == 100:
|
||||||
|
user_info = "primarymail"
|
||||||
|
else:
|
||||||
|
user_info = "username"
|
||||||
query = f"""
|
query = f"""
|
||||||
SELECT u.username, ss.service_ID
|
SELECT
|
||||||
FROM Kunden.users u
|
{user_info} as username
|
||||||
JOIN Kunden.`users.services` ss ON ss.user_ID = u.user_ID
|
FROM Kunden.`daily.user.notonline` sr
|
||||||
JOIN (
|
WHERE sr.customer_ID = {customer_id}
|
||||||
SELECT user_id,
|
AND sr.services_ID = {service_id}
|
||||||
MAX(CASE WHEN status = 1 THEN timestamp END) AS latest_active_timestamp,
|
AND sr.timestamp BETWEEN '{start_date}' AND '{end_date}'
|
||||||
MAX(CASE WHEN status = 0 THEN timestamp END) AS latest_inactive_timestamp
|
GROUP BY {user_info}
|
||||||
FROM Kunden.`users.status`
|
ORDER BY {user_info};
|
||||||
GROUP BY user_id
|
|
||||||
) us ON u.user_ID = us.user_id
|
|
||||||
WHERE ss.service_ID = {service_id}
|
|
||||||
AND (
|
|
||||||
(us.latest_active_timestamp IS NOT NULL AND
|
|
||||||
(us.latest_inactive_timestamp IS NULL OR us.latest_active_timestamp > us.latest_inactive_timestamp))
|
|
||||||
OR us.latest_inactive_timestamp IS NULL
|
|
||||||
)
|
|
||||||
AND u.username NOT IN (
|
|
||||||
SELECT sr.username
|
|
||||||
FROM Kunden.`services.reporting` sr
|
|
||||||
WHERE sr.service_ID = {service_id}
|
|
||||||
AND sr.reportingdate BETWEEN '{start_date}' AND '{end_date}'
|
|
||||||
)
|
|
||||||
AND u.user_ID != 95
|
|
||||||
AND u.user_ID != 102
|
|
||||||
AND u.customer_ID = {customer_id}
|
|
||||||
AND u.username NOT LIKE "%ad-test-user%"
|
|
||||||
"""
|
"""
|
||||||
|
not_active_users = pd.read_sql_query(query, engine)
|
||||||
not_active_users = pd.read_sql_query(query, mydb)
|
|
||||||
user_not_online_count = not_active_users.shape[0]
|
user_not_online_count = not_active_users.shape[0]
|
||||||
mydb.close()
|
|
||||||
return not_active_users, user_not_online_count
|
return not_active_users, user_not_online_count
|
||||||
|
|
||||||
def get_initial_data():
|
def get_initial_data():
|
||||||
mydb = mysql.connector.connect(
|
db_url = (
|
||||||
host=os.getenv("MYSQL_HOST"),
|
f"mysql+mysqlconnector://{os.getenv('MYSQL_USER')}:"
|
||||||
user=os.getenv("MYSQL_USER"),
|
f"{os.getenv('MYSQL_PASSWORD')}@{os.getenv('MYSQL_HOST')}/"
|
||||||
password=os.getenv("MYSQL_PASSWORD"),
|
f"{os.getenv('MYSQL_DATABASE')}"
|
||||||
database=os.getenv("MYSQL_DATABASE")
|
|
||||||
)
|
)
|
||||||
|
engine = create_engine(db_url)
|
||||||
|
|
||||||
# Fetch unique service IDs and names
|
# Fetch unique service IDs and names
|
||||||
service_id_query = """
|
service_id_query = """
|
||||||
|
|
@ -209,7 +147,7 @@ def get_initial_data():
|
||||||
FROM Kunden.`services.reporting` sr
|
FROM Kunden.`services.reporting` sr
|
||||||
JOIN Kunden.services s ON sr.service_ID = s.service_ID
|
JOIN Kunden.services s ON sr.service_ID = s.service_ID
|
||||||
"""
|
"""
|
||||||
service_ids = pd.read_sql_query(service_id_query, mydb)
|
service_ids = pd.read_sql_query(service_id_query, engine)
|
||||||
|
|
||||||
# Fetch customer information
|
# Fetch customer information
|
||||||
customer_query = """
|
customer_query = """
|
||||||
|
|
@ -219,15 +157,14 @@ def get_initial_data():
|
||||||
JOIN Kunden.`services.reporting`sr ON sr.customer_ID = co.customer_ID
|
JOIN Kunden.`services.reporting`sr ON sr.customer_ID = co.customer_ID
|
||||||
GROUP BY c.customer_ID, c.customer, co.companyname;
|
GROUP BY c.customer_ID, c.customer, co.companyname;
|
||||||
"""
|
"""
|
||||||
customers = pd.read_sql_query(customer_query, mydb)
|
customers = pd.read_sql_query(customer_query, engine)
|
||||||
|
|
||||||
# Fetch date range
|
# Fetch date range
|
||||||
date_query = """
|
date_query = """
|
||||||
SELECT MIN(reportingdate) AS min_date, MAX(reportingdate) AS max_date
|
SELECT MIN(reportingdate) AS min_date, MAX(reportingdate) AS max_date
|
||||||
FROM Kunden.`services.reporting`
|
FROM Kunden.`services.reporting`
|
||||||
"""
|
"""
|
||||||
date_range = pd.read_sql_query(date_query, mydb)
|
date_range = pd.read_sql_query(date_query, engine)
|
||||||
mydb.close()
|
|
||||||
|
|
||||||
return service_ids, customers, date_range
|
return service_ids, customers, date_range
|
||||||
|
|
||||||
|
|
@ -261,16 +198,16 @@ def services_reporting():
|
||||||
min_date = initial_date_range['min_date'][0]
|
min_date = initial_date_range['min_date'][0]
|
||||||
max_date = initial_date_range['max_date'][0]
|
max_date = initial_date_range['max_date'][0]
|
||||||
|
|
||||||
# Date input for start and end date
|
min_date = (date.today().replace(day=1) - timedelta(days=1)).replace(day=1)
|
||||||
start_date = st.date_input('Start Date', min_date)
|
start_date = st.date_input('Start Date', min_date)
|
||||||
end_date = st.date_input('End Date', max_date)
|
end_date = st.date_input('End Date', initial_date_range['max_date'][0])
|
||||||
|
|
||||||
if st.button('Apply Filters'):
|
if st.button('Apply Filters'):
|
||||||
# Fetch filtered data from the database
|
# Fetch filtered data from the database
|
||||||
filtered_data = get_filtered_data(selected_customer_id, selected_service_id, start_date, end_date)
|
filtered_data = get_filtered_data(selected_customer_id, selected_service_id, start_date, end_date)
|
||||||
|
|
||||||
# Fetch max user count in the selected range
|
# Fetch max user count in the selected range
|
||||||
max_count = get_max_user_count(selected_customer_id, selected_service_id, start_date, end_date)
|
max_count = get_active_users(selected_customer_id, selected_service_id, start_date, end_date)
|
||||||
|
|
||||||
# Sort the data by day
|
# Sort the data by day
|
||||||
filtered_data = filtered_data.sort_values('day')
|
filtered_data = filtered_data.sort_values('day')
|
||||||
|
|
@ -297,31 +234,33 @@ def services_reporting():
|
||||||
|
|
||||||
# Combine bars and text into a single chart
|
# Combine bars and text into a single chart
|
||||||
chart = (bars + text).properties(
|
chart = (bars + text).properties(
|
||||||
title='Daily Service Usage with Highlighted Maximum'
|
title='User Enabled'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Fetch the data for users not online, online, and active users
|
# 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)
|
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)
|
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, 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
|
# Create three columns for each DataFrame
|
||||||
col1, col2, col3, col4, col5 = st.columns([2,2,2,2,2])
|
col1, col2, col3, col4, col5 = st.columns([2,2,2,2,2])
|
||||||
|
|
||||||
# Display each DataFrame in a separate column
|
# Display each DataFrame in a separate column
|
||||||
with col4:
|
# with col4:
|
||||||
st.subheader(f"{selected_service.split(' - ')[1]} - User not Online")
|
# st.subheader(f"{selected_service.split(' - ')[1]} - User not Online")
|
||||||
st.metric(label="1",label_visibility="hidden", value=max_count_user_not_online)
|
# st.metric(label="1",label_visibility="hidden", value=max_count_user_not_online)
|
||||||
st.data_editor(not_user_online['username'],use_container_width=True, hide_index=True)
|
# st.data_editor(not_user_online['username'],key="2",use_container_width=True, hide_index=True)
|
||||||
|
|
||||||
with col2:
|
with col2:
|
||||||
st.subheader(f"{selected_service.split(' - ')[1]} - User Online")
|
st.subheader(f"{selected_service.split(' - ')[1]} - Enabled Users")
|
||||||
st.metric(label="1",label_visibility="hidden", value=user_online_count)
|
st.metric(label="1",label_visibility="hidden", value=user_active_count)
|
||||||
st.data_editor(user_online['username'],use_container_width=True, hide_index=True)
|
st.data_editor(active_users_data, hide_index=True)
|
||||||
|
|
||||||
with col3:
|
with col3:
|
||||||
st.subheader(f"{selected_service.split(' - ')[1]} - Active Users")
|
st.subheader(f"{selected_service.split(' - ')[1]} - User Online")
|
||||||
st.metric(label="1",label_visibility="hidden", value=user_active_count)
|
st.metric(label="1",label_visibility="hidden", value=user_online_count)
|
||||||
st.data_editor(active_users_data['username'],key=active_users_data,use_container_width=True, hide_index=True)
|
st.data_editor(user_online,key=2,hide_index=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
st.altair_chart(chart, use_container_width=True)
|
st.altair_chart(chart, use_container_width=True)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue