|
|
|
@ -9,13 +9,13 @@ from website import db_mysql
|
|
|
|
|
from website import errors
|
|
|
|
|
from website import settings
|
|
|
|
|
from website.handler import APIHandler, authenticated
|
|
|
|
|
from website.util import md5
|
|
|
|
|
from website.util import md5, shortuuid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ClassificationAddHandler(APIHandler):
|
|
|
|
|
"""
|
|
|
|
|
添加模型分类
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@authenticated
|
|
|
|
@ -28,13 +28,19 @@ class ClassificationAddHandler(APIHandler):
|
|
|
|
|
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "分类名称过长")
|
|
|
|
|
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
cur = conn.execute(text("select id from model_classification where name=:name"), {"name": name})
|
|
|
|
|
cur = conn.execute(
|
|
|
|
|
text("select id from model_classification where name=:name"),
|
|
|
|
|
{"name": name},
|
|
|
|
|
)
|
|
|
|
|
row = db_mysql.to_json(cur)
|
|
|
|
|
if row:
|
|
|
|
|
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "分类名称重复")
|
|
|
|
|
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("""insert into model_classification (name, create_time) values (:name, NOW())"""), {"name": name}
|
|
|
|
|
text(
|
|
|
|
|
"""insert into model_classification (name, create_time) values (:name, NOW())"""
|
|
|
|
|
),
|
|
|
|
|
{"name": name},
|
|
|
|
|
)
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
@ -53,8 +59,10 @@ class ClassificationEditHandler(APIHandler):
|
|
|
|
|
if not classification_id or not name:
|
|
|
|
|
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数缺失")
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
conn.execute(text("""update model_classification set name=:name where id=:id"""),
|
|
|
|
|
{"name": name, "id": classification_id})
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("""update model_classification set name=:name where id=:id"""),
|
|
|
|
|
{"name": name, "id": classification_id},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
@ -87,7 +95,10 @@ class ClassificationDeleteHandler(APIHandler):
|
|
|
|
|
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数缺失")
|
|
|
|
|
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
conn.execute(text("""DELETE FROM model_classification WHERE id=:id"""), {"id": classification_id})
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("""DELETE FROM model_classification WHERE id=:id"""),
|
|
|
|
|
{"id": classification_id},
|
|
|
|
|
)
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
|
self.finish()
|
|
|
|
@ -106,8 +117,10 @@ class ListHandler(APIHandler):
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
sql = "select m.id, m.name, m.model_type, m.default_version, mc.name classification_name, m.update_time " \
|
|
|
|
|
"from model m left join model_classification mc on m.classification=mc.id where m.del=0 "
|
|
|
|
|
sql = (
|
|
|
|
|
"select m.id, m.name, m.model_type, m.default_version, mc.name classification_name, m.update_time "
|
|
|
|
|
"from model m left join model_classification mc on m.classification=mc.id where m.del=0 "
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
param = {}
|
|
|
|
|
|
|
|
|
@ -132,18 +145,31 @@ class ListHandler(APIHandler):
|
|
|
|
|
|
|
|
|
|
data = []
|
|
|
|
|
for item in result:
|
|
|
|
|
data.append({
|
|
|
|
|
"id": item["id"],
|
|
|
|
|
"name": item["name"],
|
|
|
|
|
"model_type": consts.model_type_map[item["model_type"]],
|
|
|
|
|
"classification_name": item["classification_name"],
|
|
|
|
|
"default_version": item["default_version"],
|
|
|
|
|
"update_time": str(item["update_time"])
|
|
|
|
|
})
|
|
|
|
|
data.append(
|
|
|
|
|
{
|
|
|
|
|
"id": item["id"],
|
|
|
|
|
"name": item["name"],
|
|
|
|
|
"model_type": consts.model_type_map[item["model_type"]],
|
|
|
|
|
"classification_name": item["classification_name"],
|
|
|
|
|
"default_version": item["default_version"],
|
|
|
|
|
"update_time": str(item["update_time"]),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.finish({"data": data, "count": count})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ListSimpleHandler(APIHandler):
|
|
|
|
|
@authenticated
|
|
|
|
|
def post(self):
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
sql = "select id, name from model where del=0"
|
|
|
|
|
cur = conn.execute(text(sql))
|
|
|
|
|
res = db_mysql.to_json_list(cur)
|
|
|
|
|
|
|
|
|
|
self.finish({"result": res})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AddHandler(APIHandler):
|
|
|
|
|
"""
|
|
|
|
|
添加模型
|
|
|
|
@ -152,7 +178,9 @@ class AddHandler(APIHandler):
|
|
|
|
|
@authenticated
|
|
|
|
|
def post(self):
|
|
|
|
|
name = self.get_escaped_argument("name", "")
|
|
|
|
|
model_type = self.get_int_argument("model_type", consts.model_type_machine) # 1001/经典算法,1002/深度学习
|
|
|
|
|
model_type = self.get_int_argument(
|
|
|
|
|
"model_type", consts.model_type_machine
|
|
|
|
|
) # 1001/经典算法,1002/深度学习
|
|
|
|
|
classification = self.get_int_argument("classification")
|
|
|
|
|
comment = self.get_escaped_argument("comment", "")
|
|
|
|
|
|
|
|
|
@ -167,9 +195,16 @@ class AddHandler(APIHandler):
|
|
|
|
|
|
|
|
|
|
conn.execute(
|
|
|
|
|
text(
|
|
|
|
|
"""insert into model (name, model_type, classification, comment, create_time, update_time)
|
|
|
|
|
values (:name, :model_type, :classification, :comment, NOW(), NOW())"""),
|
|
|
|
|
{"name": name, "model_type": model_type, "classification": classification, "comment": comment}
|
|
|
|
|
"""insert into model (suid, name, model_type, classification, comment, create_time, update_time)
|
|
|
|
|
values (:suid, :name, :model_type, :classification, :comment, NOW(), NOW())"""
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
"suid": shortuuid.ShortUUID().random(10),
|
|
|
|
|
"name": name,
|
|
|
|
|
"model_type": model_type,
|
|
|
|
|
"classification": classification,
|
|
|
|
|
"comment": comment,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
@ -186,7 +221,9 @@ class EditHandler(APIHandler):
|
|
|
|
|
def post(self):
|
|
|
|
|
mid = self.get_int_argument("id")
|
|
|
|
|
name = self.get_escaped_argument("name", "")
|
|
|
|
|
model_type = self.get_int_argument("model_type", consts.model_type_machine) # 1001/经典算法,1002/深度学习
|
|
|
|
|
model_type = self.get_int_argument(
|
|
|
|
|
"model_type", consts.model_type_machine
|
|
|
|
|
) # 1001/经典算法,1002/深度学习
|
|
|
|
|
classification = self.get_int_argument("classification")
|
|
|
|
|
comment = self.get_escaped_argument("comment", "")
|
|
|
|
|
|
|
|
|
@ -199,9 +236,15 @@ class EditHandler(APIHandler):
|
|
|
|
|
"""update model
|
|
|
|
|
set name=:name, model_type=:model_type, classification=:classification, comment=:comment,
|
|
|
|
|
update_time=NOW()
|
|
|
|
|
where id=:id"""),
|
|
|
|
|
{"name": name, "model_type": model_type, "classification": classification, "comment": comment,
|
|
|
|
|
"id": mid}
|
|
|
|
|
where id=:id"""
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
"name": name,
|
|
|
|
|
"model_type": model_type,
|
|
|
|
|
"classification": classification,
|
|
|
|
|
"comment": comment,
|
|
|
|
|
"id": mid,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
@ -222,14 +265,16 @@ class InfoHandler(APIHandler):
|
|
|
|
|
result = {}
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
cur = conn.execute(
|
|
|
|
|
text("""
|
|
|
|
|
select
|
|
|
|
|
m.name, m.model_type, m.comment, m.update_time,
|
|
|
|
|
mc.id as classification_id, mc.name as classification_name
|
|
|
|
|
from model m, model_classification mc
|
|
|
|
|
where m.id=:id and m.classification=mc.id
|
|
|
|
|
"""),
|
|
|
|
|
{"id": mid}
|
|
|
|
|
text(
|
|
|
|
|
"""
|
|
|
|
|
select
|
|
|
|
|
m.name, m.model_type, m.comment, m.update_time,
|
|
|
|
|
mc.id as classification_id, mc.name as classification_name
|
|
|
|
|
from model m, model_classification mc
|
|
|
|
|
where m.id=:id and m.classification=mc.id
|
|
|
|
|
"""
|
|
|
|
|
),
|
|
|
|
|
{"id": mid},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = db_mysql.to_json(cur)
|
|
|
|
@ -242,7 +287,7 @@ class InfoHandler(APIHandler):
|
|
|
|
|
"classification_id": result["classification_id"],
|
|
|
|
|
"classification_name": result["classification_name"],
|
|
|
|
|
"comment": result["comment"],
|
|
|
|
|
"update_time": str(result["update_time"])
|
|
|
|
|
"update_time": str(result["update_time"]),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.finish(data)
|
|
|
|
@ -306,9 +351,15 @@ class VersionAddHandler(APIHandler):
|
|
|
|
|
(model_id, version, comment,model_file, config_file, config_str, create_time, update_time)
|
|
|
|
|
values (:model_id, :version, :comment, :model_file, :config_file, :config_str, NOW(), NOW())"""
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
{"model_id": mid, "version": version, "comment": comment, "model_file": model_file,
|
|
|
|
|
"config_file": config_file, "config_str": config_str})
|
|
|
|
|
{
|
|
|
|
|
"model_id": mid,
|
|
|
|
|
"version": version,
|
|
|
|
|
"comment": comment,
|
|
|
|
|
"model_file": model_file,
|
|
|
|
|
"config_file": config_file,
|
|
|
|
|
"config_str": config_str,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
conn.commit()
|
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
|
@ -349,9 +400,16 @@ class VersionEditHandler(APIHandler):
|
|
|
|
|
text(
|
|
|
|
|
"update model_version "
|
|
|
|
|
"set version=:version, comment=:comment, model_file=:model_file, config_file=:config_file, "
|
|
|
|
|
"config_str=:config_str, update_time=NOW() where id=:id"),
|
|
|
|
|
{"version": version, "comment": comment, "model_file": model_file, "config_file": config_file,
|
|
|
|
|
"config_str": config_str, "id": version_id}
|
|
|
|
|
"config_str=:config_str, update_time=NOW() where id=:id"
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
"version": version,
|
|
|
|
|
"comment": comment,
|
|
|
|
|
"model_file": model_file,
|
|
|
|
|
"config_file": config_file,
|
|
|
|
|
"config_str": config_str,
|
|
|
|
|
"id": version_id,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
|
@ -367,7 +425,7 @@ class VersionListHandler(APIHandler):
|
|
|
|
|
> - pageSize, int
|
|
|
|
|
- 返回值:
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
"count": 123,
|
|
|
|
|
"data": [
|
|
|
|
|
{
|
|
|
|
@ -407,10 +465,10 @@ class VersionListHandler(APIHandler):
|
|
|
|
|
order by mv.id desc limit :offset, :limit
|
|
|
|
|
"""
|
|
|
|
|
),
|
|
|
|
|
{"mid": model_id, "offset": (pageNo - 1) * pageSize, "limit": pageSize}
|
|
|
|
|
{"mid": model_id, "offset": (pageNo - 1) * pageSize, "limit": pageSize},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = db_mysql.to_json(cur)
|
|
|
|
|
result = db_mysql.to_json_list(cur)
|
|
|
|
|
|
|
|
|
|
# 获取记录数量
|
|
|
|
|
count = conn.execute(
|
|
|
|
@ -418,21 +476,23 @@ class VersionListHandler(APIHandler):
|
|
|
|
|
"""
|
|
|
|
|
select count(*)
|
|
|
|
|
from model_version mv
|
|
|
|
|
where mv.del=0
|
|
|
|
|
where mv.del=0 and model_id=:mid
|
|
|
|
|
"""
|
|
|
|
|
),
|
|
|
|
|
{"mid": model_id}
|
|
|
|
|
{"mid": model_id},
|
|
|
|
|
).scalar()
|
|
|
|
|
|
|
|
|
|
for item in result:
|
|
|
|
|
data.append({
|
|
|
|
|
"version_id": item["version_id"], # 版本id
|
|
|
|
|
"version": item["version"], # 版本号
|
|
|
|
|
"path": item["filepath"], # 文件路径
|
|
|
|
|
"size": item["filesize"],
|
|
|
|
|
"update_time": str(item["update_time"]),
|
|
|
|
|
"is_default": item["is_default"] # 是否默认,1/默认,0/非默认
|
|
|
|
|
})
|
|
|
|
|
data.append(
|
|
|
|
|
{
|
|
|
|
|
"version_id": item["version_id"], # 版本id
|
|
|
|
|
"version": item["version"], # 版本号
|
|
|
|
|
"path": item["filepath"], # 文件路径
|
|
|
|
|
"size": item["filesize"],
|
|
|
|
|
"update_time": str(item["update_time"]),
|
|
|
|
|
"is_default": item["is_default"], # 是否默认,1/默认,0/非默认
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.finish({"count": count, "data": data})
|
|
|
|
|
|
|
|
|
@ -475,15 +535,16 @@ class VersionInfoHandler(APIHandler):
|
|
|
|
|
"config_file_name": "",
|
|
|
|
|
"config_file_size": 0,
|
|
|
|
|
"config_file_md5": "",
|
|
|
|
|
"config_str": ""
|
|
|
|
|
"config_str": "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
cur = conn.execute(
|
|
|
|
|
text(
|
|
|
|
|
"""select m.name as model_name, mv.version, mv.comment, mv.model_file, mv.config_file, mv.config_str
|
|
|
|
|
from model_version mv, model m where mv.id=:id and mv.model_id=m.id"""),
|
|
|
|
|
{"id": version_id}
|
|
|
|
|
from model_version mv, model m where mv.id=:id and mv.model_id=m.id"""
|
|
|
|
|
),
|
|
|
|
|
{"id": version_id},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = db_mysql.to_json(cur)
|
|
|
|
@ -502,21 +563,31 @@ class VersionInfoHandler(APIHandler):
|
|
|
|
|
# 获取文件信息
|
|
|
|
|
if model_file:
|
|
|
|
|
cur_model_file = conn.execute(
|
|
|
|
|
text("select filename, filesize from files where md5_str=:md5_str"), {"md5_str": model_file}
|
|
|
|
|
text("select filename, filesize from files where md5_str=:md5_str"),
|
|
|
|
|
{"md5_str": model_file},
|
|
|
|
|
)
|
|
|
|
|
model_file_info = db_mysql.to_json(cur_model_file)
|
|
|
|
|
response["model_file_name"] = model_file_info["filename"]
|
|
|
|
|
response["model_file_size"] = model_file_info["filesize"]
|
|
|
|
|
response["model_file_name"] = (
|
|
|
|
|
model_file_info["filename"] if model_file_info else ""
|
|
|
|
|
)
|
|
|
|
|
response["model_file_size"] = (
|
|
|
|
|
model_file_info["filesize"] if model_file_info else 0
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if config_file:
|
|
|
|
|
cur_config_file = conn.execute(
|
|
|
|
|
text("select filename, filesize from files where md5_str=:md5_str"), {"md5_str": config_file}
|
|
|
|
|
text("select filename, filesize from files where md5_str=:md5_str"),
|
|
|
|
|
{"md5_str": config_file},
|
|
|
|
|
)
|
|
|
|
|
config_file_info = db_mysql.to_json(cur_config_file)
|
|
|
|
|
response["config_file_name"] = config_file_info["filename"]
|
|
|
|
|
response["config_file_size"] = config_file_info["filesize"]
|
|
|
|
|
response["config_file_name"] = (
|
|
|
|
|
config_file_info["filename"] if config_file_info else ""
|
|
|
|
|
)
|
|
|
|
|
response["config_file_size"] = (
|
|
|
|
|
config_file_info["filesize"] if config_file_info else 0
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response["config_str"] = result["config_str"]
|
|
|
|
|
response["config_str"] = self.unescape_string(result["config_str"])
|
|
|
|
|
|
|
|
|
|
self.finish(response)
|
|
|
|
|
|
|
|
|
@ -543,11 +614,13 @@ class VersionSetDefaultHandler(APIHandler):
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("update model_version set is_default=0 where model_id=:model_id"),
|
|
|
|
|
{"model_id": model_id})
|
|
|
|
|
{"model_id": model_id},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("update model_version set is_default=1 where id=:id"),
|
|
|
|
|
{"id": version_id})
|
|
|
|
|
{"id": version_id},
|
|
|
|
|
)
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
|
self.finish()
|
|
|
|
@ -573,8 +646,12 @@ class VersionDeleteHandler(APIHandler):
|
|
|
|
|
row = {}
|
|
|
|
|
# 获取模型对应的model_file, config_file,使用model_file, config_file删除对应的存储文件
|
|
|
|
|
with self.app_mysql.connect() as conn:
|
|
|
|
|
cur = conn.execute(text("select model_id, model_file, config_file from model_version where id=:id"),
|
|
|
|
|
{"id": version_id})
|
|
|
|
|
cur = conn.execute(
|
|
|
|
|
text(
|
|
|
|
|
"select model_id, model_file, config_file from model_version where id=:id"
|
|
|
|
|
),
|
|
|
|
|
{"id": version_id},
|
|
|
|
|
)
|
|
|
|
|
row = db_mysql.to_json(cur)
|
|
|
|
|
|
|
|
|
|
if not row:
|
|
|
|
@ -585,20 +662,29 @@ class VersionDeleteHandler(APIHandler):
|
|
|
|
|
|
|
|
|
|
# 清空模型默认版本
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("update model set default_version='' where id=:id"), {"id": model_id}
|
|
|
|
|
text("update model set default_version='' where id=:id"),
|
|
|
|
|
{"id": model_id},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 删除文件
|
|
|
|
|
try:
|
|
|
|
|
conn.execute(text("delete from files where md5_str=:md5_str"), {"md5_str": model_file})
|
|
|
|
|
conn.execute(text("delete from files where md5_str=:md5_str"), {"md5_str": config_file})
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("delete from files where md5_str=:md5_str"),
|
|
|
|
|
{"md5_str": model_file},
|
|
|
|
|
)
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("delete from files where md5_str=:md5_str"),
|
|
|
|
|
{"md5_str": config_file},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
os.remove(settings.file_upload_dir + "model/" + model_file)
|
|
|
|
|
os.remove(settings.file_upload_dir + "model/" + config_file)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.info(e)
|
|
|
|
|
|
|
|
|
|
conn.execute(text("delete from model_version where id=:id"), {"id": version_id})
|
|
|
|
|
conn.execute(
|
|
|
|
|
text("delete from model_version where id=:id"), {"id": version_id}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
|