99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
from flask import Blueprint, render_template, request, jsonify, send_file, current_app
|
|
from app.utils.ceph_calculator import calculate_ceph_capacity
|
|
from app.utils.pdf_generator import generate_pdf_report
|
|
from io import BytesIO
|
|
import logging
|
|
import traceback
|
|
|
|
logger = logging.getLogger(__name__)
|
|
bp = Blueprint('main', __name__)
|
|
|
|
@bp.route('/', methods=['GET'])
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@bp.route('/calculate', methods=['POST'])
|
|
def calculate():
|
|
try:
|
|
data = request.json
|
|
if not data:
|
|
return jsonify({"error": "Keine Daten empfangen"}), 400
|
|
|
|
replication_type = data.get('replication_type') # 'replication' oder 'erasure_coding'
|
|
if not replication_type:
|
|
return jsonify({"error": "Replikationstyp fehlt"}), 400
|
|
|
|
replicas = int(data.get('replicas', 3)) # Standardwert: 3 Replikate
|
|
|
|
# Für Erasure Coding
|
|
k = int(data.get('k', 0)) # Datenchunks
|
|
m = int(data.get('m', 0)) # Codierungschunks
|
|
|
|
# Minimale Replikate für I/O-Operationen
|
|
min_size = int(data.get('min_size', 2)) # Standardwert: 2
|
|
|
|
# Nodes und OSDs
|
|
nodes = data.get('nodes', [])
|
|
if not nodes:
|
|
return jsonify({"error": "Keine Nodes definiert"}), 400
|
|
|
|
# Speichereinheit
|
|
storage_unit = data.get('storage_unit', 'GB')
|
|
|
|
# Berechnung durchführen
|
|
result = calculate_ceph_capacity(
|
|
replication_type=replication_type,
|
|
replicas=replicas,
|
|
k=k,
|
|
m=m,
|
|
nodes=nodes,
|
|
min_size=min_size,
|
|
storage_unit=storage_unit
|
|
)
|
|
|
|
return jsonify(result)
|
|
except ValueError as e:
|
|
logger.error(f"Validierungsfehler bei der Berechnung: {str(e)}")
|
|
return jsonify({"error": str(e)}), 400
|
|
except Exception as e:
|
|
logger.error(f"Fehler bei der Berechnung: {str(e)}\n{traceback.format_exc()}")
|
|
return jsonify({"error": "Ein unerwarteter Fehler ist aufgetreten"}), 500
|
|
|
|
@bp.route('/generate-pdf', methods=['POST'])
|
|
def generate_pdf():
|
|
try:
|
|
data = request.get_json()
|
|
if not data:
|
|
return jsonify({"error": "Keine Daten empfangen"}), 400
|
|
|
|
# Validiere die Daten
|
|
if not data.get('replication_type'):
|
|
return jsonify({"error": "Replikationstyp fehlt"}), 400
|
|
|
|
if not data.get('nodes') or not isinstance(data['nodes'], list):
|
|
return jsonify({"error": "Ungültige Node-Daten"}), 400
|
|
|
|
if not data.get('result') or not isinstance(data['result'], dict):
|
|
return jsonify({"error": "Ungültige Ergebnisdaten"}), 400
|
|
|
|
if not data['result'].get('raw_total'):
|
|
return jsonify({"error": "Keine Berechnungsergebnisse vorhanden"}), 400
|
|
|
|
# Generiere PDF
|
|
pdf_bytes = generate_pdf_report(data)
|
|
|
|
if not pdf_bytes:
|
|
return jsonify({"error": "PDF-Generierung fehlgeschlagen"}), 500
|
|
|
|
return send_file(
|
|
BytesIO(pdf_bytes),
|
|
mimetype='application/pdf',
|
|
as_attachment=True,
|
|
download_name='ceph-report.pdf'
|
|
)
|
|
except ValueError as e:
|
|
logger.error(f"Validierungsfehler bei der PDF-Generierung: {str(e)}")
|
|
return jsonify({"error": str(e)}), 400
|
|
except Exception as e:
|
|
logger.error(f"Fehler bei der PDF-Generierung: {str(e)}\n{traceback.format_exc()}")
|
|
return jsonify({"error": "Ein unerwarteter Fehler ist aufgetreten"}), 500 |