99 lines
4.4 KiB
Python
99 lines
4.4 KiB
Python
import requests
|
|
import json
|
|
from datetime import datetime, timedelta
|
|
from dotenv import load_dotenv
|
|
import os
|
|
|
|
# Lade die Umgebungsvariablen aus der .env Datei
|
|
load_dotenv()
|
|
|
|
# Hole die Werte aus der .env Datei
|
|
zammad_url = os.getenv('ZAMMAD_URL')
|
|
zammad_api_token = os.getenv('ZAMMAD_API_TOKEN')
|
|
|
|
api_url = os.getenv('API_SERVER')
|
|
api_token = os.getenv('API_TOKEN')
|
|
|
|
headers_zammad = {"Authorization": f"Token token={zammad_api_token}"}
|
|
headers_api = {"access_token": f"{api_token}", "Content-Type": "application/json"}
|
|
|
|
# API Anfrage an Zammad für Organisationen
|
|
response_org = requests.get(f"{zammad_url}/organizations", headers=headers_zammad)
|
|
# API Anfrage an Zammad für Benutzer
|
|
response_user = requests.get(f"{zammad_url}/users", headers=headers_zammad)
|
|
|
|
if response_org.status_code == 200 and response_user.status_code == 200:
|
|
organizations = response_org.json()
|
|
users = response_user.json()
|
|
|
|
# Erstelle ein Dictionary, das die Organisationen nach ID speichert
|
|
org_dict = {org['id']: org['domain'] for org in organizations}
|
|
# Erstelle ein Dictionary, das die Benutzer nach ID speichert
|
|
user_dict = {user['id']: user for user in users}
|
|
|
|
# Berechne den ersten und letzten Tag des letzten Monats
|
|
today = datetime.now()
|
|
first_day_last_month = today.replace(day=1) - timedelta(days=1)
|
|
first_day_last_month = first_day_last_month.replace(day=1)
|
|
last_day_last_month = today.replace(day=1) - timedelta(days=1)
|
|
|
|
# Formatierung der Daten für den Vergleich
|
|
start_date_str = first_day_last_month.strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
end_date_str = last_day_last_month.strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
|
|
# API Anfrage an Zammad für Tickets mit Pagination und Sortierung
|
|
params = {
|
|
'query': f"last_close_at:[{start_date_str} TO {end_date_str}]",
|
|
'sort_by': 'last_close_at',
|
|
'order': 'desc',
|
|
'per_page': 100,
|
|
'page': 1
|
|
}
|
|
response_tickets = requests.get(f"{zammad_url}/tickets/search", headers=headers_zammad, params=params)
|
|
|
|
if response_tickets.status_code == 200:
|
|
response_data = response_tickets.json()
|
|
|
|
# Extrahiere die Ticket-IDs und die zugehörigen Ticket-Daten aus assets
|
|
ticket_ids = response_data.get('tickets', [])
|
|
ticket_assets = response_data.get('assets', {}).get('Ticket', {})
|
|
|
|
# Filtere die Tickets, extrahiere die benötigten Felder und verknüpfe mit Organisationen und Benutzern
|
|
filtered_tickets = [
|
|
{
|
|
'number': ticket_data.get('number'),
|
|
'title': ticket_data.get('title'),
|
|
'create_date': datetime.strptime(ticket_data.get('created_at'), '%Y-%m-%dT%H:%M:%S.%fZ').strftime('%Y-%m-%d %H:%M:%S') if ticket_data.get('created_at') else None,
|
|
'closed_date': datetime.strptime(ticket_data.get('last_close_at'), '%Y-%m-%dT%H:%M:%S.%fZ').strftime('%Y-%m-%d %H:%M:%S') if ticket_data.get('last_close_at') else None,
|
|
'organization_domain': org_dict.get(ticket_data.get('organization_id')),
|
|
'time_unit': ticket_data.get('time_unit') if ticket_data.get('time_unit') is not None else "0",
|
|
'created_by': user_dict.get(ticket_data.get('created_by_id'), {}).get('login')
|
|
}
|
|
for ticket_id in ticket_ids
|
|
if (ticket_data := ticket_assets.get(str(ticket_id)))
|
|
]
|
|
|
|
# Ausgabe der gefilterten Tickets als JSON-String
|
|
json_output = json.dumps(filtered_tickets, indent=4, ensure_ascii=False)
|
|
|
|
|
|
for tickets in filtered_tickets:
|
|
# POST-Anfrage an den anderen API-Server
|
|
print(tickets)
|
|
response_post = requests.post(f"{api_url}/post/tickets", headers=headers_api, json=tickets)
|
|
|
|
# Überprüfen der Antwort des API-Servers
|
|
if response_post.status_code == 200 or response_post.status_code == 201:
|
|
print("Data successfully posted.")
|
|
else:
|
|
print(f"Failed to post data. Status code: {response_post.status_code}")
|
|
print(f"Response content: {response_post.content.decode('utf-8')}")
|
|
|
|
else:
|
|
print(f"Failed to retrieve tickets. Status code: {response_tickets.status_code}")
|
|
else:
|
|
if response_org.status_code != 200:
|
|
print(f"Failed to retrieve organizations. Status code: {response_org.status_code}")
|
|
if response_user.status_code != 200:
|
|
print(f"Failed to retrieve users. Status code: {response_user.status_code}")
|