add Ticket Export as APP
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
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):
|
||||
mydb = mysql.connector.connect(
|
||||
host=os.getenv("MYSQL_HOST"),
|
||||
user=os.getenv("MYSQL_USER"),
|
||||
password=os.getenv("MYSQL_PASSWORD"),
|
||||
database=os.getenv("MYSQL_DATABASE")
|
||||
)
|
||||
|
||||
query = f"""SELECT * FROM Kunden.`services.reporting` where DATE(reportingdate) = CURDATE() """
|
||||
|
||||
|
||||
if customer_id:
|
||||
query += f"AND customer_ID = {customer_id}"
|
||||
if service_id:
|
||||
query += f" AND service_ID = {service_id}"
|
||||
query += " ORDER BY reportingdate DESC"
|
||||
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 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 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)
|
||||
|
||||
# Display the filtered data
|
||||
if not filtered_data.empty:
|
||||
st.dataframe(filtered_data)
|
||||
else:
|
||||
st.write("No data available for the selected filters.")
|
||||
@@ -31,7 +31,6 @@ def get_filtered_server(customer_id, service_id, service_status):
|
||||
query += f" AND s.status = {service_status}"
|
||||
|
||||
users = pd.read_sql_query(query, mydb)
|
||||
print(query)
|
||||
mydb.close()
|
||||
return users
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
import streamlit as st
|
||||
import pandas as pd
|
||||
import subprocess
|
||||
import mysql.connector
|
||||
from datetime import datetime
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
total_time_unit = []
|
||||
|
||||
def get_filtered_users(customer_id):
|
||||
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 number,title,createdate,time FROM Kunden.tickets
|
||||
WHERE 1=1
|
||||
"""
|
||||
if customer_id:
|
||||
query += f"AND customer_ID = {customer_id} order by createdate"
|
||||
|
||||
users = pd.read_sql_query(query, mydb)
|
||||
mydb.close()
|
||||
|
||||
for column in users.select_dtypes(include=['datetime64[ns]', 'datetime64[ns, UTC]']).columns:
|
||||
users[column] = users[column].dt.strftime('%d.%m.%Y')
|
||||
|
||||
users.iloc[:, 3] = users.iloc[:, 3].str.split('.').str[0]
|
||||
users['time'] = users['time'].astype(int)
|
||||
|
||||
base_url = "https://ticket.stines.de/#ticket/zoom/number/" # Replace with your actual base URL
|
||||
users['Link'] = users['number'].apply(lambda x: f'<a href="{base_url}{x}" target="_blank">{x}</a>')
|
||||
|
||||
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 run_script_and_list_documents():
|
||||
output_area = st.empty() # Platzhalter für die Ausgabe
|
||||
document_paths = [] # Liste der erstellten Dokumente
|
||||
|
||||
# Verzeichnis, in dem das Skript ausgeführt werden soll
|
||||
working_directory = "apps/ticket_export/exports"
|
||||
|
||||
# Führe das Skript in dem angegebenen Verzeichnis aus
|
||||
result = subprocess.run(
|
||||
["python3", "apps/ticket_export/main.py"],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
# Suche nach den erstellten Dokumenten im Arbeitsverzeichnis
|
||||
for filename in os.listdir("apps/ticket_export/exports"):
|
||||
if filename.endswith(".docx"):
|
||||
full_path = os.path.join(working_directory, filename)
|
||||
document_paths.append(full_path)
|
||||
|
||||
# Zeige die Liste der erstellten Dokumente an und biete sie zum Download an
|
||||
if document_paths:
|
||||
st.write("Erstellte Dokumente:")
|
||||
for doc_path in document_paths:
|
||||
with open(doc_path, "rb") as file:
|
||||
st.download_button(
|
||||
label=f"Download {os.path.basename(doc_path)}",
|
||||
data=file,
|
||||
file_name=os.path.basename(doc_path),
|
||||
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
)
|
||||
else:
|
||||
st.write("Keine Dokumente gefunden.")
|
||||
|
||||
def ticket_filter():
|
||||
st.title("Ticket Filter :mag_right:")
|
||||
# Get initial data for widgets
|
||||
initial_service_ids, customers = get_initial_data()
|
||||
|
||||
# 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])
|
||||
|
||||
if st.button("Rechnung erstellen"):
|
||||
st.write("Das Skript wird ausgeführt...")
|
||||
run_script_and_list_documents()
|
||||
|
||||
# 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)
|
||||
if not filtered_data.empty:
|
||||
# st.dataframe(filtered_data,hide_index=True)
|
||||
st.markdown(filtered_data.to_html(escape=False), unsafe_allow_html=True)
|
||||
# Convert DataFrame to CSV
|
||||
csv = filtered_data.drop(columns=['Link']).to_csv(index=False)
|
||||
|
||||
st.write(f"Total Time Unit: {filtered_data['time'].sum()}")
|
||||
|
||||
# Create a download button with a custom file name
|
||||
st.download_button(
|
||||
label="Download CSV",
|
||||
data=csv,
|
||||
file_name=f"filtered_data_{selected_customer}.csv", # Custom file name
|
||||
mime='text/csv',
|
||||
)
|
||||
|
||||
else:
|
||||
st.write("No data available for the selected filters.")
|
||||
+6
-4
@@ -18,7 +18,7 @@ def get_filtered_users(customer_id, service_id, service_status):
|
||||
|
||||
# Prepare the base query
|
||||
query = f"""
|
||||
SELECT us.user_id, u.username, s.service_ID, uss.status, uss.timestamp
|
||||
SELECT us.user_id, u.username, s.service_ID, uss.status, uss.timestamp, c.companyname
|
||||
FROM Kunden.`users.status` us
|
||||
JOIN (
|
||||
SELECT user_id, MAX(timestamp) AS latest_timestamp
|
||||
@@ -28,14 +28,16 @@ def get_filtered_users(customer_id, service_id, service_status):
|
||||
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}
|
||||
JOIN Kunden.company c ON c.customer_ID = us.customer_ID
|
||||
WHERE 1=1
|
||||
"""
|
||||
|
||||
if customer_id:
|
||||
query += f"AND us.customer_ID = {customer_id}"
|
||||
if service_id:
|
||||
query += f" AND s.service_ID = {service_id}"
|
||||
if service_status:
|
||||
query += f" AND uss.status = {service_status}"
|
||||
|
||||
query += " ORDER BY uss.status DESC"
|
||||
users = pd.read_sql_query(query, mydb)
|
||||
mydb.close()
|
||||
return users
|
||||
|
||||
Reference in New Issue
Block a user