Skip to content

Commit c31f43a

Browse files
committed
修复算子导入脚本
1 parent 3dacf55 commit c31f43a

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

tools/utils/load_operators.sh

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,29 @@ read -r -d '' PY_SCRIPT << 'EOF'
110110
from pathlib import Path
111111
import json, sys, yaml, base64
112112
113-
operator_sql = 'INSERT IGNORE INTO t_operator (id, name, description, version, inputs, outputs, runtime, settings, file_name, is_star) VALUES '
114-
category_sql = 'INSERT IGNORE INTO t_operator_category_relation(category_id, operator_id) VALUES '
113+
operator_sql = 'INSERT INTO t_operator (id, name, description, version, inputs, outputs, runtime, settings, file_name, is_star, metrics, file_size) VALUES '
114+
category_sql = 'INSERT INTO t_operator_category_relation(category_id, operator_id) VALUES '
115+
release_sql = 'INSERT INTO t_operator_release (id, version, release_date, changelog) VALUES '
116+
117+
# Modal类型映射 (与前端分类ID对应)
115118
modal_map = {
116119
'text': 'd8a5df7a-52a9-42c2-83c4-01062e60f597',
117120
'image': 'de36b61c-9e8a-4422-8c31-d30585c7100f',
118121
'audio': '42dd9392-73e4-458c-81ff-41751ada47b5',
119122
'video': 'a233d584-73c8-4188-ad5d-8f7c8dda9c27',
120123
'multimodal': '4d7dbd77-0a92-44f3-9056-2cd62d4a71e4'
121124
}
122-
language_map = {
123-
'python': '9eda9d5d-072b-499b-916c-797a0a8750e1',
124-
'java': 'b5bfc548-8ef6-417c-b8a6-a4197c078249'
125+
126+
function_map = {
127+
'cleaning': '8c09476a-a922-418f-a908-733f8a0de521',
128+
'annotation': 'cfa9d8e2-5b5f-4f1e-9f12-1234567890ab'
125129
}
126130
131+
# 系统预置分类ID
132+
PREDEFINED_ID = 'ec2cdd17-8b93-4a81-88c4-ac9e98d10757'
133+
VENDOR_CATEGORY_ID = 'f00eaa3e-96c1-4de4-96cd-9848ef5429ec'
134+
PYTHON_CATEGORY_ID = '9eda9d5d-072b-499b-916c-797a0a8750e1'
135+
127136
base_path = Path('/opt/runtime/datamate/ops/user')
128137
for metadata_file in base_path.rglob('metadata.yml'):
129138
try:
@@ -140,25 +149,66 @@ for metadata_file in base_path.rglob('metadata.yml'):
140149
141150
runtime = f"'{json.dumps(data.get('runtime'), ensure_ascii=False)}'" if 'runtime' in data else 'null'
142151
settings = f"'{json.dumps(data.get('settings'), ensure_ascii=False)}'" if 'settings' in data else 'null'
143-
file_name = Path(metadata_file).parent.name
152+
metrics = f"'{json.dumps(data.get('metrics', []), ensure_ascii=False)}'" if 'metrics' in data else 'null'
153+
154+
# 计算文件大小
155+
root_dir = Path(metadata_file).parent
156+
file_size = sum(f.stat().st_size for f in root_dir.rglob('*') if f.is_file())
157+
158+
file_name = root_dir.name
159+
160+
# 构建operator插入SQL
161+
operator_sql += "('{}', '{}', '{}', '{}', '{}', '{}', {}, {}, '{}', 'false', {}, {}),".format(
162+
id, name, desc, version, inputs, outputs, runtime, settings, file_name, metrics, file_size
163+
)
164+
165+
# 功能类型分类(支持多个types)
166+
types = data.get('types', [])
167+
has_type = False
168+
if isinstance(types, list):
169+
for func_type in types:
170+
func_category_id = function_map.get(func_type)
171+
if func_category_id:
172+
has_type = True
173+
category_sql += f"('{func_category_id}', '{id}'),"
174+
if not has_type:
175+
category_sql += f"('{function_map.get('cleaning')}', '{id}'),"
176+
177+
# 系统预置分类
178+
category_sql += f"('{PREDEFINED_ID}', '{id}'),"
179+
category_sql += f"('{VENDOR_CATEGORY_ID}', '{id}'),"
180+
category_sql += f"('{PYTHON_CATEGORY_ID}', '{id}'),"
181+
category_sql += f"('{modal_map.get(modal, modal_map.get('text'))}', '{id}'),"
182+
183+
# Release信息
184+
releases = data.get('release', [])
185+
if isinstance(releases, list) and releases:
186+
release_sql += f"('{id}', '{version}', NOW(), '{json.dumps(releases, ensure_ascii=False)}'),"
144187
145-
operator_sql += "('{}', '{}', '{}', '{}', '{}', '{}', {}, {}, '{}', 'false'),".format(id, name, desc, version, inputs, outputs, runtime, settings, file_name)
146-
category_sql += "('{}', '{}'),".format(modal_map.get(modal, 'd8a5df7a-52a9-42c2-83c4-01062e60f597'), id)
147-
category_sql += "('{}', '{}'),".format(language_map.get(language, '9eda9d5d-072b-499b-916c-797a0a8750e1'), id)
148-
category_sql += "('ec2cdd17-8b93-4a81-88c4-ac9e98d10757', '{}'),".format(id)
149-
category_sql += "('f00eaa3e-96c1-4de4-96cd-9848ef5429ec', '{}'),".format(id)
150188
except Exception as e:
151189
print(f'ERROR: {e}', file=sys.stderr)
152190
sys.exit(1)
153-
print(operator_sql[:-1] + ';\n' + category_sql[:-1] + ';')
191+
192+
# 输出SQL
193+
full_sql = ''
194+
if operator_sql != 'INSERT INTO t_operator (id, name, description, version, inputs, outputs, runtime, settings, file_name, is_star, metrics, file_size) VALUES ':
195+
full_sql += operator_sql[:-1] + ' ON CONFLICT DO NOTHING;\n'
196+
197+
if category_sql != 'INSERT INTO t_operator_category_relation(category_id, operator_id) VALUES ':
198+
full_sql += category_sql[:-1] + ' ON CONFLICT DO NOTHING;\n'
199+
200+
if release_sql != 'INSERT INTO t_operator_release (id, version, release_date, changelog) VALUES ':
201+
full_sql += release_sql[:-1] + ' ON CONFLICT DO NOTHING;\n'
202+
203+
print(full_sql)
154204
EOF
155205

156206
B64_CODE=$(python3 -c "import base64, sys; print(base64.b64encode(sys.stdin.read().encode('utf-8')).decode('utf-8'))" <<< "$PY_SCRIPT")
157207
FULL_SQL=$(kubectl exec -i "$HEAD_POD_NAME" -n "$NAMESPACE" -c ray-head -- /bin/sh -c "echo '$B64_CODE' | base64 -d | python3 -")
158208

159209
log_info "插入数据库..."
160210
DATABASE_POD_NAME=$(kubectl get pod -n "$NAMESPACE" -l app.kubernetes.io/name=datamate-database -o jsonpath='{.items[*].metadata.name}')
161-
kubectl exec -i "$DATABASE_POD_NAME" -n "$NAMESPACE" -c database -- sh -c 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot datamate' << EOF
211+
kubectl exec -i "$DATABASE_POD_NAME" -n "$NAMESPACE" -c database -- sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -U "$POSTGRES_USER" -d datamate' << EOF
162212
$FULL_SQL
163213
EOF
164214

0 commit comments

Comments
 (0)