From 2140a85929564d0104bc364cd4e306f75c7dc77d Mon Sep 17 00:00:00 2001
From: lixin <lixinb@uniontech.com>
Date: Thu, 9 Nov 2023 16:35:55 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=BB=E6=9C=BA=E4=BF=A1?=
 =?UTF-8?q?=E6=81=AF=E5=B1=95=E7=A4=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 index.py        | 13 +++++++++++
 views/server.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/index.py b/index.py
index 2cb89aa..0c64990 100644
--- a/index.py
+++ b/index.py
@@ -27,6 +27,7 @@ mods = {
         'system_migration': migration.system_migration,
         'migration_details': migration.migration_details,
         'import_host_info': server.import_host_info,
+        'host_info_display': server.host_info_display,
         }
 
 @app.route('/import_host_info', methods=['GET', 'POST'])
@@ -39,6 +40,18 @@ def import_host_info():
     if mod:
         return Response(mod, content_type='application/json')
 
+
+@app.route('/host_info_display', methods=['GET', 'POST'])
+def host_info_display():
+    """
+    显示主机信息
+    :return:
+    """
+    mod = check_methods()
+    if mod:
+        return Response(mod, content_type='application/json')
+
+
 def check_methods():
     if request.method == 'POST':
         data = request.get_data()
diff --git a/views/server.py b/views/server.py
index cf07d2f..d2ca719 100644
--- a/views/server.py
+++ b/views/server.py
@@ -38,7 +38,6 @@ def import_host_info(data):
     return data_json
 
 
-
 def get_agent_id(agent_ip):
     """
     获取agent_id
@@ -77,4 +76,60 @@ def create_task_stream(agent_ip):
     create_agent_task_sql = "insert into agent_task(agent_id,agent_ip,task_id,task_status,task_progress," \
                             "task_CreateTime,task_Updatetime,task_stream_id,task_data) values " \
                             "(%s, %s, %s, %s, %s, %s, %s, %s, %s);"
-    DBHelper().insert(create_agent_task_sql, values)
\ No newline at end of file
+    DBHelper().insert(create_agent_task_sql, values)
+
+
+def pagebreak(data, page, size):
+    """
+    页面数据分页
+    :param data:
+    :return:
+    """
+    page_start = (page - 1) * size
+    page_end = page * size
+    result = data[page_start:page_end]
+
+    return result
+
+def host_info_display(data):
+    """
+    显示主机信息
+    agent_ip,hostname,agent_online_status,agent_os,agent_arch,
+    agent_history_faild_reason,task_CreateTime,task_status
+    :return:
+    """
+    page = json.loads(data).get('page')
+    size = json.loads(data).get('size')
+    sql = "select agent_ip,hostname,agent_online_status,agent_os,agent_arch," \
+          "agent_history_faild_reason from agent_info;"
+    data = DBHelper().execute(sql).fetchall()
+    data = list(data)
+    for i in range(0, len(data)):
+        data[i] = list(data[i])
+        agent_task = "select task_CreateTime,task_data from agent_task where agent_ip = '%s';" % data[i][0]
+        get_agent_task = DBHelper().execute(agent_task).fetchall()
+        get_agent_task = list(get_agent_task)
+
+        if not get_agent_task:
+            data[i] += ["", ""]
+        else:
+            task_CreateTime = get_agent_task[0][0].strftime('%Y-%-m-%d %H:%M:%S')
+            task_status = get_agent_task[0][1]
+            data[i].append(task_CreateTime)
+            data[i].append(task_status)
+
+    res = {}
+    res['num'] = len(data)
+    info_list = []
+    info_dict_keys_list = ['agent_ip', 'hostname', 'agent_online_status', 'agent_os', 'agent_arch',
+                           'failure_reasons', 'task_CreateTime', 'task_status']
+    for i in data:
+        info_list.append(dict(zip(info_dict_keys_list, i)))
+
+    page_list = pagebreak(info_list, page, size)
+    res['info'] = page_list
+    res['page'] = page
+    res['size'] = size
+
+    json_res = json.dumps(res)
+    return json_res
\ No newline at end of file
-- 
2.20.1

