|
| 1 | +from flask import Flask, jsonify, request |
| 2 | + |
| 3 | +from vectordb_bench.backend.clients import DB |
| 4 | +from vectordb_bench.interface import benchmark_runner |
| 5 | +from vectordb_bench.models import ALL_TASK_STAGES, CaseConfig, TaskConfig, TaskStage |
| 6 | +from vectordb_bench.restful.format_res import format_results |
| 7 | + |
| 8 | +app = Flask(__name__) |
| 9 | + |
| 10 | + |
| 11 | +def res_wrapper(code: int = 0, message: str = "", data: any = None): # noqa: RUF013 |
| 12 | + return jsonify({"code": code, "message": message, "data": data}), 200 |
| 13 | + |
| 14 | + |
| 15 | +def success_res(data: any = None, message: str = "success"): # noqa: RUF013 |
| 16 | + return res_wrapper(code=0, message=message, data=data) |
| 17 | + |
| 18 | + |
| 19 | +def failed_res(data: any = None, message: str = "failed"): # noqa: RUF013 |
| 20 | + return res_wrapper(code=1, message=message, data=data) |
| 21 | + |
| 22 | + |
| 23 | +@app.route("/get_res", methods=["GET"]) |
| 24 | +def get_res(): |
| 25 | + """task label -> res""" |
| 26 | + task_label = request.args.get("task_label", "standard") |
| 27 | + all_results = benchmark_runner.get_results() |
| 28 | + res = format_results(all_results, task_label=task_label) |
| 29 | + |
| 30 | + return success_res(res) |
| 31 | + |
| 32 | + |
| 33 | +@app.route("/get_status", methods=["GET"]) |
| 34 | +def get_status(): |
| 35 | + "running 5/18, not running" |
| 36 | + is_running = benchmark_runner.has_running() |
| 37 | + tasks_count = benchmark_runner.get_tasks_count() |
| 38 | + if is_running: |
| 39 | + tasks_count = benchmark_runner.get_tasks_count() |
| 40 | + cur_task_idx = benchmark_runner.get_current_task_id() |
| 41 | + return success_res( |
| 42 | + data={ |
| 43 | + "is_running": is_running, |
| 44 | + "tasks_count": tasks_count, |
| 45 | + "cur_task_idx": cur_task_idx, |
| 46 | + } |
| 47 | + ) |
| 48 | + return success_res(data={"is_running": is_running}) |
| 49 | + |
| 50 | + |
| 51 | +@app.route("/stop", methods=["GET"]) |
| 52 | +def stop(): |
| 53 | + benchmark_runner.stop_running() |
| 54 | + return success_res(message="stopped") |
| 55 | + |
| 56 | + |
| 57 | +@app.route("/run", methods=["post"]) |
| 58 | +def run(): |
| 59 | + if benchmark_runner.has_running(): |
| 60 | + return failed_res(message="There are already running tasks.") |
| 61 | + data = request.get_json() |
| 62 | + task_label = data.get("task_label", "test") |
| 63 | + use_aliyun = data.get("use_aliyun", False) |
| 64 | + task_configs: list[TaskConfig] = [] |
| 65 | + try: |
| 66 | + tasks = data.get("tasks", []) |
| 67 | + if len(tasks) == 0: |
| 68 | + return failed_res(message="empty tasks") |
| 69 | + for task in tasks: |
| 70 | + db = DB(task["db"]) |
| 71 | + db_config = db.config_cls(**task["db_config"]) |
| 72 | + case_config = CaseConfig(**task["case_config"]) |
| 73 | + print(case_config) # noqa: T201 |
| 74 | + db_case_config = db.case_config_cls(index_type=task["db_case_config"].get("index", None))( |
| 75 | + **task["db_case_config"] |
| 76 | + ) |
| 77 | + stages = [TaskStage(stage) for stage in task.get("stages", ALL_TASK_STAGES)] |
| 78 | + print(stages) # noqa: T201 |
| 79 | + task_config = TaskConfig( |
| 80 | + db=db, |
| 81 | + db_config=db_config, |
| 82 | + case_config=case_config, |
| 83 | + db_case_config=db_case_config, |
| 84 | + stages=stages, |
| 85 | + ) |
| 86 | + task_configs.append(task_config) |
| 87 | + except Exception as e: |
| 88 | + return failed_res(message=f"invalid tasks: {e}") |
| 89 | + |
| 90 | + benchmark_runner.set_download_address(use_aliyun) |
| 91 | + benchmark_runner.run(task_configs, task_label) |
| 92 | + |
| 93 | + return success_res(message="start") |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + app.run(host="0.0.0.0", port=5000, debug=False) # noqa: S104 |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
0 commit comments