(self, session: Session, res: str)
| 1021 | return sql |
| 1022 | |
| 1023 | def check_save_chart(self, session: Session, res: str) -> Dict[str, Any]: |
| 1024 | |
| 1025 | json_str = extract_nested_json(res) |
| 1026 | if json_str is None: |
| 1027 | raise SingleMessageError(orjson.dumps({'message': 'Cannot parse chart config from answer', |
| 1028 | 'traceback': "Cannot parse chart config from answer:\n" + res}).decode()) |
| 1029 | data: dict |
| 1030 | |
| 1031 | chart: Dict[str, Any] = {} |
| 1032 | message = '' |
| 1033 | error = False |
| 1034 | |
| 1035 | try: |
| 1036 | data = orjson.loads(json_str) |
| 1037 | if data['type'] and data['type'] != 'error': |
| 1038 | # todo type check |
| 1039 | chart = data |
| 1040 | if chart.get('columns'): |
| 1041 | for v in chart.get('columns'): |
| 1042 | v['value'] = v.get('value').lower() |
| 1043 | if chart.get('axis'): |
| 1044 | if chart.get('axis').get('x'): |
| 1045 | chart.get('axis').get('x')['value'] = chart.get('axis').get('x').get('value').lower() |
| 1046 | y_axis = chart.get('axis').get('y') |
| 1047 | if y_axis: |
| 1048 | if isinstance(y_axis, list): |
| 1049 | # 数组格式: y: [{name, value}, ...] |
| 1050 | for item in y_axis: |
| 1051 | if item.get('value'): |
| 1052 | item['value'] = item['value'].lower() |
| 1053 | elif isinstance(y_axis, dict) and y_axis.get('value'): |
| 1054 | # 旧格式: y: {name, value} |
| 1055 | y_axis['value'] = y_axis['value'].lower() |
| 1056 | if chart.get('axis').get('series'): |
| 1057 | chart.get('axis').get('series')['value'] = chart.get('axis').get('series').get('value').lower() |
| 1058 | if chart.get('axis') and chart['axis'].get('multi-quota'): |
| 1059 | multi_quota = chart['axis']['multi-quota'] |
| 1060 | if multi_quota.get('value'): |
| 1061 | if isinstance(multi_quota['value'], list): |
| 1062 | # 将数组中的每个值转换为小写 |
| 1063 | multi_quota['value'] = [v.lower() if v else v for v in multi_quota['value']] |
| 1064 | elif isinstance(multi_quota['value'], str): |
| 1065 | # 如果是字符串,也转换为小写 |
| 1066 | multi_quota['value'] = multi_quota['value'].lower() |
| 1067 | elif data['type'] == 'error': |
| 1068 | message = data['reason'] |
| 1069 | error = True |
| 1070 | else: |
| 1071 | raise Exception('Chart is empty') |
| 1072 | except Exception: |
| 1073 | error = True |
| 1074 | message = orjson.dumps({'message': 'Cannot parse chart config from answer', |
| 1075 | 'traceback': "Cannot parse chart config from answer:\n" + res}).decode() |
| 1076 | |
| 1077 | if error: |
| 1078 | raise SingleMessageError(message) |
| 1079 | |
| 1080 | save_chart(session=session, chart=orjson.dumps(chart).decode(), record_id=self.record.id) |
no test coverage detected