from flask import Flask, jsonify
import requests

app = Flask(__name__)

EXTERNAL_APIS = {
    "nodes": "http://master.argus.com/api/v1/nodes",
    "alerts": "http://alertmanager.argus.com/api/v2/alerts"
}


def fetch_external(api_name):
    """通用的外部接口请求函数"""
    try:
        url = EXTERNAL_APIS[api_name]
        response = requests.get(url, timeout=5)
        response.raise_for_status()
        return {"status": "success", "data": response.json()}
    except requests.exceptions.RequestException as e:
        return {"status": "error", "message": str(e)}


@app.route("/api/v1/web/health", methods=["GET"])
def get_health():
    return jsonify(fetch_external("health"))


@app.route("/api/v1/web/nodes", methods=["GET"])
def get_nodes():
    return jsonify(fetch_external("nodes"))


@app.route("/api/v1/web/alerts", methods=["GET"])
def get_alerts():
    return jsonify(fetch_external("alerts"))


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)
