Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.

Commit 0bd911c

Browse files
committed
add execute query given a dashboard,metric
1 parent 3a18f95 commit 0bd911c

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

src/data_neuron/chat_cmd/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ def process_chat_message(message, context_name, chat_history=None):
5252

5353
if not sql_query:
5454
return {
55-
"reason": db_result
55+
"error": db_result
5656
}
5757
response = {
58-
"original_query": message,
59-
"changed_query": changed_query,
58+
"original_question": message,
59+
"rephrased_question": changed_query,
6060
"sql_query": sql_query,
61-
"result": format_db_result(db_result)
61+
"data": format_db_result(db_result)
6262
}
6363

6464
return response

src/data_neuron/server.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .context_init_cmd.main import init_context
44
from .ask_cmd.main import query as ask_query
55
from .chat_cmd.main import process_chat_message
6+
from .db_operations.factory import DatabaseFactory
67
from .report_cmd.main import generate_report_html, list_dashboards, load_dashboard
78
import os
89

@@ -73,6 +74,49 @@ def get_dashboard(dashboard_id):
7374
return jsonify({"error": str(e)}), 500
7475

7576

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+
76120
def run_server(host='0.0.0.0', port=8084, debug=False):
77121
app.run(host=host, port=port, debug=debug)
78122

0 commit comments

Comments
 (0)