diff --git a/website/consts.py b/website/consts.py index 3c485d6..e155b83 100644 --- a/website/consts.py +++ b/website/consts.py @@ -26,4 +26,13 @@ model_type_machine = 1002 model_type_map = { model_type_classic: u"经典算法", model_type_machine: u"机器学习", +} + +# 1000/all/默认, 1001/在线,1002/离线,1003/运行中,1004/故障 +device_status_map = { + 1000: u"全部", + 1001: u"在线", + 1002: u"离线", + 1003: u"运行中", + 1004: u"故障", } \ No newline at end of file diff --git a/website/db/enterprise_device/enterprise_device.py b/website/db/enterprise_device/enterprise_device.py index 18c9ff3..163ffaa 100644 --- a/website/db/enterprise_device/enterprise_device.py +++ b/website/db/enterprise_device/enterprise_device.py @@ -60,6 +60,7 @@ class EnterpriseDevice(Base): device_model = Column(String) param = Column(String) comment = Column(String) + status = Column(Integer) delete = Column("del", Integer, default=0) create_time = Column(DateTime, default=func.now()) update_time = Column(DateTime, default=func.now()) @@ -293,4 +294,40 @@ class EnterpriseDeviceRepository(object): except Exception as e: logging.error("Failed to get all device count, error: {}".format(e)) - return 0 \ No newline at end of file + return 0 + + def list_entity_devices(self, entity_id: int, pageNo: int, pageSize: int, classification: str, status: int) -> dict: + with get_session() as session: + try: + session_count = ( + session.query(EnterpriseDevice) + .filter( + EnterpriseDevice.entity_id == entity_id, + EnterpriseDevice.delete != 1, + ) + ) + if classification: + session_count.filter(EnterpriseDevice.classification == classification) + if status: + session_count.filter(EnterpriseDevice.status == status) + count = session_count.count() + + session_device = ( + session.query(EnterpriseDevice) + .filter( + EnterpriseDevice.entity_id == entity_id, + EnterpriseDevice.delete != 1, + ) + ) + if classification: + session_device.filter(EnterpriseDevice.classification == classification) + if status: + session_device.filter(EnterpriseDevice.status == status) + + devices = session_device.order_by(EnterpriseDevice.id.desc()).limit(pageSize).offset((pageNo - 1) * pageSize).all() + + except Exception as e: + logging.error("Failed to list devices") + raise e + + return {"count": count, "devices": devices} \ No newline at end of file diff --git a/website/handlers/alg_model/handler.py b/website/handlers/alg_model/handler.py index 2d6a9b5..429f0fe 100644 --- a/website/handlers/alg_model/handler.py +++ b/website/handlers/alg_model/handler.py @@ -36,6 +36,7 @@ class ClassificationAddHandler(APIHandler): conn.execute( text("""insert into model_classification (name, create_time) values (:name, NOW())"""), {"name": name} ) + conn.commit() self.finish() @@ -159,15 +160,15 @@ class AddHandler(APIHandler): raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数缺失") with self.app_mysql.connect() as conn: - sql = text("select id from model_classification where id=:id", {"id": classification}) - cur = conn.execute(sql) + sql = text("select id from model_classification where id=:id") + cur = conn.execute(sql, {"id": classification}) if not cur.fetchone(): raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "分类不存在") conn.execute( text( - """insert into model (name, model_type, classification, comment, create_time) - values (:name, :model_type, :classification, :comment, NOW())"""), + """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} ) @@ -221,9 +222,13 @@ class InfoHandler(APIHandler): result = {} with self.app_mysql.connect() as conn: cur = conn.execute( - text( - """select m.name, mc.name as classification_name, m.comment, m.update_time - from model m, model_classification mc where m.id=:id and m.classification=c.id"""), + 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} ) @@ -233,6 +238,8 @@ class InfoHandler(APIHandler): data = { "name": result["name"], + "model_type": result["model_type"], + "classification_id": result["classification_id"], "classification_name": result["classification_name"], "comment": result["comment"], "update_time": str(result["update_time"]) @@ -395,9 +402,9 @@ class VersionListHandler(APIHandler): """ select mv.id as version_id, mv.version, mv.model_file, mv.update_time, mv.is_default, f.filepath, f.filesize from model_version mv - left join files f on mv.model_file=f.md5 + left join files f on mv.model_file=f.md5_str where mv.model_id=:mid and mv.del=0 - order by mv.version_id desc limit :offset, :limit + order by mv.id desc limit :offset, :limit """ ), {"mid": model_id, "offset": (pageNo - 1) * pageSize, "limit": pageSize} diff --git a/website/handlers/enterprise_device/handler.py b/website/handlers/enterprise_device/handler.py index 3793949..309994d 100644 --- a/website/handlers/enterprise_device/handler.py +++ b/website/handlers/enterprise_device/handler.py @@ -1,13 +1,16 @@ # -*- coding: utf-8 -*- import logging - +import random from sqlalchemy import text from website import db_mysql, errors from website.db.enterprise_device import enterprise_device as DB_Device -from website.db.device_classification import device_classification as DB_DeviceClassification +from website.db.device_classification import ( + device_classification as DB_DeviceClassification, +) from website.handler import APIHandler, authenticated from website.util import shortuuid +from website import consts class DeviceClassificationAddHandler(APIHandler): @@ -99,10 +102,13 @@ class DeviceClassificationDeleteHandler(APIHandler): with self.app_mysql.connect() as conn: cur = conn.execute( - text(""" + text( + """ select d.id from enterprise_device d, device_classification c where d.classification=c.suid and c.id=:id and c.del=0 - """), {"id": did} + """ + ), + {"id": did}, ) rows = cur.fetchall() if rows: @@ -156,9 +162,7 @@ class DeviceAddHandler(APIHandler): db_classification = DB_DeviceClassification.DeviceClassificationReporitory() row = db_classification.get_row_by_suid(classification) if not row: - raise errors.HTTPAPIError( - errors.ERROR_BAD_REQUEST, "设备分类不存在" - ) + raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "设备分类不存在") device_data = { "entity_id": entity_id, @@ -330,3 +334,86 @@ class DeviceInfoHandler(APIHandler): device = db_device.get_device(device_id=device_id) logging.info(device) self.finish(device) + + +class StatusListHandler(APIHandler): + """ + - 描述:设备状态列表 + - 请求方式:post + - 请求参数: + > - entity_id, int, 企业id + > - classification_suid, string, 分类id + > - status, int, 状态,1000/all/默认, 1001/在线,1002/离线,1003/运行中,1004/故障 + > - pageNo + > - pageSize + - 返回值:无 + ``` + { + "count": 123, + "data": [ + { + "id": 123, + "name": "xxx", + "status": 1001, + "cpu": 123, + "mem": 123, + "storage": 123, + "gpu": 123, + }, + ... + ] + } + ``` + """ + + @authenticated + def post(self): + entity_id = self.get_int_argument("entity_id") + classification = self.get_escaped_argument("classification", "") + status = self.get_int_argument("status", 1000) + pageNo = self.get_int_argument("pageNo", 1) + pageSize = self.get_int_argument("pageSize", 20) + + if not entity_id: + raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "企业节点不能为空") + if status not in consts.device_status_map: + raise errors.HTTPAPIError( + errors.ERROR_BAD_REQUEST, "状态参数错误") + + db_device = DB_Device.EnterpriseDeviceRepository() + res = db_device.list_entity_devices(entity_id=entity_id, pageNo=pageNo, pageSize=pageSize, + classification=classification, status=status) + + count = res["count"] + devices = res["devices"] + data = [] + for item in devices: + data.append({ + "id": item.id, + "name": item.name, + "status": item.status, + "cpu": random.randint(20, 30), + "mem": random.randint(20, 30), + "storage": random.randint(20, 30), + "gpu": random.randint(20, 30), + }) + + self.finish({"count": count, "data": data}) + + +class StatusInfoHandler(APIHandler): + """ """ + + @authenticated + def post(self): + + self.finish() + + +class StatusLogHandler(APIHandler): + """ """ + + @authenticated + def post(self): + + self.finish() diff --git a/website/handlers/enterprise_device/url.py b/website/handlers/enterprise_device/url.py index e3c68b6..8ce8fbb 100644 --- a/website/handlers/enterprise_device/url.py +++ b/website/handlers/enterprise_device/url.py @@ -16,5 +16,9 @@ handlers = [ ("/enterprise/entity/nodes/device/list", handler.DeviceListHandler), ("/enterprise/entity/nodes/device/list/simple", handler.DeviceListSimpleHandler), ("/enterprise/entity/nodes/device/info", handler.DeviceInfoHandler), + + ("/enterprise/device/status/list", handler.StatusListHandler), + ("/enterprise/device/status/info", handler.StatusInfoHandler), + ("/enterprise/device/status/log", handler.StatusLogHandler), ] page_handlers = [] diff --git a/website/handlers/file/handler.py b/website/handlers/file/handler.py index 073e001..d6600c5 100644 --- a/website/handlers/file/handler.py +++ b/website/handlers/file/handler.py @@ -50,7 +50,7 @@ class UploadHandler(APIHandler): if not os.path.exists(filepath): for meta in file_metas: # filename = meta['filename'] - async with open(filepath, 'wb') as f: + async with aiofiles.open(filepath, 'wb') as f: await f.write(meta['body']) sql_insert = text(