add Server page

main
sebastian.serfling 2024-08-15 12:36:53 +02:00
parent 28a95748a3
commit 47100302ee
3 changed files with 234 additions and 2 deletions

View File

@ -1,10 +1,11 @@
import streamlit as st
import sites.services_reporting as sr
import sites.userlist as us
import sites.server as s
# Page Settings
st.set_page_config(page_title="Reporting")
# Load custom CSS
def load_css(file_name):
with open(file_name) as f:
@ -26,9 +27,17 @@ if st.sidebar.button('Home'):
st.session_state.page = 'Home'
if st.sidebar.button('Services Reporting'):
st.session_state.page = 'Services Reporting'
if st.sidebar.button('User'):
st.session_state.page = 'User'
if st.sidebar.button('Server'):
st.session_state.page = 'Server'
# Page display logic
if st.session_state.page == 'Home':
home()
elif st.session_state.page == 'Services Reporting':
sr.services_reporting()
elif st.session_state.page == 'User':
us.user_filter()
elif st.session_state.page == 'Server':
s.server_filter()

109
sites/server.py Normal file
View File

@ -0,0 +1,109 @@
import streamlit as st
import pandas as pd
import mysql.connector
from datetime import datetime
import os
from dotenv import load_dotenv
load_dotenv()
def get_filtered_server(customer_id, service_id, service_status):
mydb = mysql.connector.connect(
host=os.getenv("MYSQL_HOST"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
database=os.getenv("MYSQL_DATABASE")
)
# Prepare the base query
query = f"""
select s.hostname,s.privat_ipaddress,s.public_ipaddress, s.ram, s.createdate, s.disabledate,s.customer_ID,s.server_ID,hc.name,hc.core
from Kunden.server s
join Kunden.`hardware.cpu` hc ON hc.cpu_ID = s.cpu_ID
WHERE 1=1
"""
if customer_id:
query += f"AND s.customer_ID = {customer_id}"
if service_id:
query += f" AND s.service_ID = {service_id}"
if service_status:
query += f" AND s.status = {service_status}"
users = pd.read_sql_query(query, mydb)
print(query)
mydb.close()
return users
def get_initial_data():
mydb = mysql.connector.connect(
host=os.getenv("MYSQL_HOST"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
database=os.getenv("MYSQL_DATABASE")
)
# Fetch unique service IDs and names
service_id_query = """
SELECT DISTINCT s.service_ID, s.name
FROM Kunden.services s
"""
service_ids = pd.read_sql_query(service_id_query, mydb)
# Fetch customer information
customer_query = """
SELECT DISTINCT c.customer_ID, c.customer, co.companyname
FROM Kunden.company co
JOIN Kunden.customers c ON co.customer_ID = c.customer_ID
"""
customers = pd.read_sql_query(customer_query, mydb)
mydb.close()
return service_ids, customers
def server_filter():
st.title("Server Filter :mag_right:")
# Get initial data for widgets
initial_service_ids, customers = get_initial_data()
# Combine service_ID and name for display
service_options = initial_service_ids.apply(lambda row: f"{row['service_ID']} - {row['name']}", axis=1)
# Add selection widget for customer ID
selected_customer = st.selectbox(
'Select Customer',
["All"] + customers.apply(lambda row: f"{row['customer_ID']} - {row['companyname']} - {row['customer']}",
axis=1).tolist()
)
# Extract customer_ID from selected option
selected_customer_id = None if selected_customer == "All" else int(selected_customer.split(' - ')[0])
# Add selection widget for service ID
selected_service = st.selectbox(
'Select Service',
["All"] + service_options.tolist()
)
# Extract service_ID from selected option
selected_service_id = None if selected_service == "All" else int(selected_service.split(' - ')[0])
# Add selection widget for service status
selected_status = st.selectbox(
'Select Service Status',
["All", "1 - Active", "0 - Inactive"]
)
# Extract status from selected option
service_status = None if selected_status == "All" else int(selected_status.split(' - ')[0])
# Add a button to apply filters
if st.button('Apply Filters'):
# Fetch filtered data from the database
filtered_data = get_filtered_server(selected_customer_id, selected_service_id, service_status)
# Display the filtered data
if not filtered_data.empty:
st.dataframe(filtered_data)
else:
st.write("No data available for the selected filters.")

114
sites/userlist.py Normal file
View File

@ -0,0 +1,114 @@
import streamlit as st
import pandas as pd
import mysql.connector
from datetime import datetime
import os
from dotenv import load_dotenv
load_dotenv()
def get_filtered_users(customer_id, service_id, service_status):
mydb = mysql.connector.connect(
host=os.getenv("MYSQL_HOST"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
database=os.getenv("MYSQL_DATABASE")
)
# Prepare the base query
query = f"""
SELECT us.user_id, u.username, s.service_ID, uss.status, uss.timestamp
FROM Kunden.`users.status` us
JOIN (
SELECT user_id, MAX(timestamp) AS latest_timestamp
FROM Kunden.`users.status`
GROUP BY user_id
) latest ON us.user_id = latest.user_id AND us.timestamp = latest.latest_timestamp
JOIN Kunden.users u ON u.user_ID = us.user_id
JOIN Kunden.`users.services` uss ON us.user_id = uss.user_id
JOIN Kunden.services s ON uss.service_ID = s.service_ID
WHERE us.customer_ID = {customer_id} and us.status = {service_status}
"""
if service_id:
query += f" AND s.service_ID = {service_id}"
if service_status:
query += f" AND uss.status = {service_status}"
users = pd.read_sql_query(query, mydb)
mydb.close()
return users
def get_initial_data():
mydb = mysql.connector.connect(
host=os.getenv("MYSQL_HOST"),
user=os.getenv("MYSQL_USER"),
password=os.getenv("MYSQL_PASSWORD"),
database=os.getenv("MYSQL_DATABASE")
)
# Fetch unique service IDs and names
service_id_query = """
SELECT DISTINCT s.service_ID, s.name
FROM Kunden.services s
"""
service_ids = pd.read_sql_query(service_id_query, mydb)
# Fetch customer information
customer_query = """
SELECT DISTINCT c.customer_ID, c.customer, co.companyname
FROM Kunden.company co
JOIN Kunden.customers c ON co.customer_ID = c.customer_ID
"""
customers = pd.read_sql_query(customer_query, mydb)
mydb.close()
return service_ids, customers
def user_filter():
st.title("User Filter :mag_right:")
# Get initial data for widgets
initial_service_ids, customers = get_initial_data()
# Combine service_ID and name for display
service_options = initial_service_ids.apply(lambda row: f"{row['service_ID']} - {row['name']}", axis=1)
# Add selection widget for customer ID
selected_customer = st.selectbox(
'Select Customer',
["All"] + customers.apply(lambda row: f"{row['customer_ID']} - {row['companyname']} - {row['customer']}",
axis=1).tolist()
)
# Extract customer_ID from selected option
selected_customer_id = None if selected_customer == "All" else int(selected_customer.split(' - ')[0])
# Add selection widget for service ID
selected_service = st.selectbox(
'Select Service',
["All"] + service_options.tolist()
)
# Extract service_ID from selected option
selected_service_id = None if selected_service == "All" else int(selected_service.split(' - ')[0])
# Add selection widget for service status
selected_status = st.selectbox(
'Select Service Status',
["All", "1 - Active", "0 - Inactive"]
)
# Extract status from selected option
service_status = None if selected_status == "All" else int(selected_status.split(' - ')[0])
# Add a button to apply filters
if st.button('Apply Filters'):
# Fetch filtered data from the database
filtered_data = get_filtered_users(selected_customer_id, selected_service_id, service_status)
# Display the filtered data
if not filtered_data.empty:
st.dataframe(filtered_data)
else:
st.write("No data available for the selected filters.")