Coverage for mindsdb / api / http / namespaces / util.py: 40%
85 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1import os
2import tempfile
3from pathlib import Path
5import psutil
6from flask import request
7from flask_restx import Resource
8from flask import current_app as ca
10from mindsdb.metrics.metrics import api_endpoint_metrics
11from mindsdb.api.http.namespaces.configs.util import ns_conf
12from mindsdb.api.http.gui import update_static
13from mindsdb.utilities.fs import clean_unlinked_process_marks
14from mindsdb.api.http.utils import http_error
17def get_active_tasks():
18 response = {
19 'learn': False,
20 'predict': False,
21 'analyse': False
22 }
24 if os.name != 'posix':
25 return response
27 for process_type in response:
28 processes_dir = Path(tempfile.gettempdir()).joinpath(f'mindsdb/processes/{process_type}/')
29 if not processes_dir.is_dir():
30 continue
31 clean_unlinked_process_marks()
32 process_marks = [x.name for x in processes_dir.iterdir()]
33 if len(process_marks) > 0:
34 response[process_type] = True
36 return response
39@ns_conf.route('/ping')
40class Ping(Resource):
41 @ns_conf.doc('get_ping')
42 @api_endpoint_metrics('GET', '/util/ping')
43 def get(self):
44 '''Checks server avaliable'''
45 return {'status': 'ok'}
48@ns_conf.route('/ping/ml_task_queue')
49class PingMLTaskQueue(Resource):
50 @ns_conf.doc('get_ping_ml_task_queue')
51 @api_endpoint_metrics('GET', '/util/ping/ml_task_queue')
52 def get(self):
53 '''Check if ML tasks queue process is alive'''
54 processes_dir = Path(tempfile.gettempdir()).joinpath('mindsdb/processes/internal/')
55 if processes_dir.is_dir():
56 ml_tasks_queue_mark = next((x for x in processes_dir.iterdir() if x.name.endswith('ml_task_consumer')), None)
57 if ml_tasks_queue_mark is not None:
58 try:
59 pid = int(ml_tasks_queue_mark.name.split('-')[0])
60 process = psutil.Process(pid)
61 if process.status() in (psutil.STATUS_ZOMBIE, psutil.STATUS_DEAD):
62 raise psutil.NoSuchProcess(pid)
63 return '', 200
64 except Exception:
65 return '', 404
66 return '', 404
69@ns_conf.route('/readiness')
70class ReadinessProbe(Resource):
71 @ns_conf.doc('get_ready')
72 @api_endpoint_metrics('GET', '/util/readiness')
73 def get(self):
74 '''Checks server is ready for work'''
76 tasks = get_active_tasks()
77 for key in tasks:
78 if tasks[key] is True:
79 return http_error(503, 'not ready', 'not ready')
81 return '', 200
84@ns_conf.route('/ping_native')
85class PingNative(Resource):
86 @ns_conf.doc('get_ping_native')
87 @api_endpoint_metrics('GET', '/util/ping_native')
88 def get(self):
89 ''' Checks server use native for learn or analyse.
90 Will return right result only on Linux.
91 '''
92 return get_active_tasks()
95@ns_conf.route('/validate_json_ai')
96class ValidateJsonAI(Resource):
97 @api_endpoint_metrics('POST', '/util/validate_json_ai')
98 def post(self):
99 json_ai = request.json.get('json_ai')
100 if json_ai is None:
101 return 'Please provide json_ai', 400
102 try:
103 lw_handler = ca.integration_controller.get_ml_handler('lightwood')
104 code = lw_handler.code_from_json_ai(json_ai)
105 except Exception as e:
106 return {'error': str(e)}
107 return {'code': code}
110@ns_conf.route('/update-gui')
111class UpdateGui(Resource):
112 @ns_conf.doc('get_update_gui')
113 @api_endpoint_metrics('GET', '/util/update-gui')
114 def get(self):
115 update_static()
116 return '', 200