|
3 | 3 | from .context_init_cmd.main import init_context |
4 | 4 | from .ask_cmd.main import query as ask_query |
5 | 5 | from .chat_cmd.main import process_chat_message |
| 6 | +from .db_operations.factory import DatabaseFactory |
6 | 7 | from .report_cmd.main import generate_report_html, list_dashboards, load_dashboard |
7 | 8 | import os |
8 | 9 |
|
@@ -73,6 +74,49 @@ def get_dashboard(dashboard_id): |
73 | 74 | return jsonify({"error": str(e)}), 500 |
74 | 75 |
|
75 | 76 |
|
| 77 | +@app.route('/execute-metric', methods=['POST']) |
| 78 | +def execute_metric(): |
| 79 | + try: |
| 80 | + data = request.json |
| 81 | + dashboard_id = data.get('dashboard_id') |
| 82 | + metric_name = data.get('metric_name') |
| 83 | + parameters = data.get('parameters', {}) |
| 84 | + |
| 85 | + if not dashboard_id or not metric_name: |
| 86 | + return jsonify({"error": "dashboard_id and metric_name are required"}), 400 |
| 87 | + |
| 88 | + dashboard = load_dashboard(dashboard_id) |
| 89 | + if not dashboard: |
| 90 | + return jsonify({"error": "Dashboard not found"}), 404 |
| 91 | + |
| 92 | + metric = next( |
| 93 | + (m for m in dashboard['metrics'] if m['name'] == metric_name), None) |
| 94 | + if not metric: |
| 95 | + return jsonify({"error": "Metric not found"}), 404 |
| 96 | + |
| 97 | + sql_query = metric['sql_query'] |
| 98 | + |
| 99 | + # Apply parameters to the SQL query |
| 100 | + for key, value in parameters.items(): |
| 101 | + placeholder = f":{key}" |
| 102 | + sql_query = sql_query.replace(placeholder, str(value)) |
| 103 | + |
| 104 | + db = DatabaseFactory.get_database() |
| 105 | + result = db.execute_query_with_column_names(sql_query) |
| 106 | + |
| 107 | + if isinstance(result, tuple) and len(result) == 2: |
| 108 | + data, columns = result |
| 109 | + return jsonify({ |
| 110 | + "columns": columns, |
| 111 | + "data": data |
| 112 | + }) |
| 113 | + else: |
| 114 | + return jsonify({"error": "Unexpected result format"}), 500 |
| 115 | + |
| 116 | + except Exception as e: |
| 117 | + return jsonify({"error": str(e)}), 500 |
| 118 | + |
| 119 | + |
76 | 120 | def run_server(host='0.0.0.0', port=8084, debug=False): |
77 | 121 | app.run(host=host, port=port, debug=debug) |
78 | 122 |
|
|
0 commit comments