(session: SessionDep, current_user: CurrentUser, chat_record_id: int, chat_id: int, trans: Trans)
| 481 | @router.get("/record/{chat_record_id}/excel/export/{chat_id}", summary=f"{PLACEHOLDER_PREFIX}export_chart_data") |
| 482 | @system_log(LogConfig(operation_type=OperationType.EXPORT, module=OperationModules.CHAT, resource_id_expr="chat_id", )) |
| 483 | async def export_excel(session: SessionDep, current_user: CurrentUser, chat_record_id: int, chat_id: int, trans: Trans): |
| 484 | chat_record = session.get(ChatRecord, chat_record_id) |
| 485 | if not chat_record: |
| 486 | raise HTTPException( |
| 487 | status_code=500, |
| 488 | detail=f"ChatRecord with id {chat_record_id} not found" |
| 489 | ) |
| 490 | if chat_record.create_by != current_user.id: |
| 491 | raise HTTPException( |
| 492 | status_code=500, |
| 493 | detail=f"ChatRecord with id {chat_record_id} not Owned by the current user" |
| 494 | ) |
| 495 | is_predict_data = chat_record.predict_record_id is not None |
| 496 | |
| 497 | _origin_data = format_json_data(get_chat_chart_data(chat_record_id=chat_record_id, session=session)) |
| 498 | |
| 499 | _base_field = _origin_data.get('fields') |
| 500 | _data = _origin_data.get('data') |
| 501 | |
| 502 | if not _data: |
| 503 | raise HTTPException( |
| 504 | status_code=500, |
| 505 | detail=trans("i18n_excel_export.data_is_empty") |
| 506 | ) |
| 507 | |
| 508 | chart_info = get_chart_config(session, chat_record_id) |
| 509 | |
| 510 | _title = chart_info.get('title') if chart_info.get('title') else 'Excel' |
| 511 | |
| 512 | fields = [] |
| 513 | if chart_info.get('columns') and len(chart_info.get('columns')) > 0: |
| 514 | for column in chart_info.get('columns'): |
| 515 | fields.append(AxisObj(name=column.get('name'), value=column.get('value'))) |
| 516 | # 处理 axis |
| 517 | if axis := chart_info.get('axis'): |
| 518 | # 处理 x 轴 |
| 519 | if x_axis := axis.get('x'): |
| 520 | if 'name' in x_axis or 'value' in x_axis: |
| 521 | fields.append(AxisObj(name=x_axis.get('name'), value=x_axis.get('value'))) |
| 522 | |
| 523 | # 处理 y 轴 - 兼容数组和对象格式 |
| 524 | if y_axis := axis.get('y'): |
| 525 | if isinstance(y_axis, list): |
| 526 | for column in y_axis: |
| 527 | if 'name' in column or 'value' in column: |
| 528 | fields.append(AxisObj(name=column.get('name'), value=column.get('value'))) |
| 529 | elif isinstance(y_axis, dict) and ('name' in y_axis or 'value' in y_axis): |
| 530 | fields.append(AxisObj(name=y_axis.get('name'), value=y_axis.get('value'))) |
| 531 | |
| 532 | # 处理 series |
| 533 | if series := axis.get('series'): |
| 534 | if 'name' in series or 'value' in series: |
| 535 | fields.append(AxisObj(name=series.get('name'), value=series.get('value'))) |
| 536 | |
| 537 | _predict_data = [] |
| 538 | if is_predict_data: |
| 539 | _predict_data = format_json_list_data(get_chat_predict_data(chat_record_id=chat_record_id, session=session)) |
| 540 |
nothing calls this directly
no test coverage detected