瀏覽代碼

Paging query and background export excel

liqudong 3 年之前
父節點
當前提交
61fad5683a
共有 4 個文件被更改,包括 150 次插入4 次删除
  1. 1 1
      config.json
  2. 14 0
      event_storage.py
  3. 45 3
      gateway.py
  4. 90 0
      hard_disk_storage.py

+ 1 - 1
config.json

@@ -3,7 +3,7 @@
     "ip": "127.0.0.1",
     "username": "root",
     "password": "sea12345",
-    "dataBaseName": "datacollection"
+    "dataBaseName": "shucai"
   },
   "memoryDatabase": {
     "ip": "127.0.0.1",

+ 14 - 0
event_storage.py

@@ -49,6 +49,20 @@ class EventStorage:
     def get_command_info(self, station_name):
         return self.hardDiskStorage.get_command_info(station_name)
 
+    # 历史查询接口(new)
+    def get_total_count_and_first_id(self, select_info):
+        data = self.hardDiskStorage.get_total_count_and_first_id(select_info)
+        return data
+
+    def get_item_by_id_offset(self, select_info):
+        data = self.hardDiskStorage.get_item_by_id_offset(select_info)
+        return data
+
+    # 数据导出接口
+    def quary_table_data(self, select_info):
+        data = self.hardDiskStorage.quary_table_data(select_info)
+        return data
+
 
 class Networkerror(RuntimeError):
     def __init__(self, arg):

+ 45 - 3
gateway.py

@@ -1,14 +1,13 @@
 import asyncio
 import datetime
 import json
+import sys
 import threading
 import time
 
 from sanic import Sanic
-from sanic.response import text, json
 from sanic_cors import CORS, cross_origin
 from sanic import response
-from multiprocessing import Process
 
 # device import
 from event_storage import EventStorage
@@ -52,6 +51,49 @@ async def read_table_data(request):
     return response.text(data_json)
 
 
+# 历史数据库分页查询接口--------------------------------------------\\
+@app.post('/getTotalNumber')
+async def get_total_number(request):
+    dict = request.json
+    data_list = gateway_storage.get_total_count_and_first_id(dict)
+    return response.json(data_list)
+
+
+@app.post("/getItem")
+def get_one_page_content(request):
+    dict = request.json
+    data_list = gateway_storage.get_item_by_id_offset(dict)
+    data_json = Utility.data_encoder(data_list)
+    return response.text(data_json)
+
+
+# 历史数据库分页查询接口--------------------------------------------//
+
+# 历史数据库分页查询接口--------------------------------------------\\
+@app.post('/quary')
+async def quary_table_data(request):
+    dict = request.json
+    res = gateway_storage.quary_table_data(dict)
+    if not res:
+        return response.text("查询参数错误")
+    return response.json({"filename": res})
+
+
+@app.route("/download")
+async def test(request):
+    filename = request.args.get("filename")
+    if sys.platform == 'win32':
+        filepath = './' + filename
+    elif sys.platform == 'linux':
+        filepath = filename
+    return await response.file_stream(
+        filepath,
+        chunk_size=1024,
+        filename=filename
+    )
+
+
+# 历史数据库分页查询接口--------------------------------------------//
 @app.route('/readPointInfo', methods=['POST'])
 async def read_point_info(request):
     data_list = gateway_storage.get_point_info(None)
@@ -123,6 +165,6 @@ if __name__ == "__main__":
     # app.add_task(overrun_alarm(app, alarm))
     # app.add_task(displacement_alarm(app, alarm))
     # app.add_task(notify_server_started_after_five_seconds())  # 气象仪降雨量每日清零:一号打开,二号关闭,三号关闭
-    app.run(host="0.0.0.0", port=8001)
+    app.run(host="0.0.0.0", port=8000, debug=True)
 # pyinstaller -F -p C:\Users\wenge\AppData\Local\Programs\Python\Python38\Lib\site-packages  gateway.spec
 # pyinstaller -F -p D:\DevTools\Python38\Lib\site-packages  gateway.spec

+ 90 - 0
hard_disk_storage.py

@@ -1,3 +1,7 @@
+import datetime
+import json
+
+import openpyxl
 import pymysql
 import traceback
 import time
@@ -89,6 +93,92 @@ class HardDiskStorage():
             print(traceback.format_exc())
             return None
 
+    # 历史查询接口(new)--------------------------------------------\\
+    def get_total_count_and_first_id(self, search_info):
+        table_name = "table_" + search_info['deviceName']
+        time_begin = search_info['timeBegin']
+        time_end = search_info['timeEnd']
+        sql = "select count(*) from %s where times >= '%s' and times <= '%s';" % (table_name, time_begin, time_end)
+        sql_1 = "select id from %s where times >= '%s' limit 1;" % (table_name, time_begin)
+        try:
+            self._reConn()
+            self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
+            self.cursor.execute(sql)
+            count = self.cursor.fetchall()
+            self.cursor.execute(sql_1)
+            first_id = self.cursor.fetchall()
+            if isinstance(first_id, tuple):
+                first_id = list(first_id)
+            result = count + first_id
+            return result
+        except:
+            print(traceback.format_exc())
+            return None
+
+    def get_item_by_id_offset(self, search_info):
+        table_name = "table_" + search_info['deviceName']
+        point_list = search_info['pointList']
+        id_offset = search_info['idOffset']
+        quantity = search_info['quantity']
+        sql = "select times, %s from %s where id  >= %s limit %s" % (','.join(point_list), table_name, id_offset, quantity)
+        try:
+            self._reConn()
+            self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
+            self.cursor.execute(sql)
+            results = self.cursor.fetchall()
+            self.cursor.close()
+            return results
+        except:
+            print(traceback.format_exc())
+            return None
+
+    # 历史查询接口(new)--------------------------------------------//
+
+    # 数据导出接口------------------------------------------------\\
+    def quary_table_data(self, search_info):
+        table_name = "table_" + search_info['deviceName']
+        time_begin = search_info['timeBegin']
+        time_end = search_info['timeEnd']
+        point_list = search_info['pointList']
+        point_list_1 = str([i[1:] for i in point_list])[1:-1]
+
+        sql = "select times, %s from %s where times >= '%s' and times <= '%s';" % (','.join(point_list), table_name, time_begin, time_end)
+        sql1 = "select io_point_name from data_point_tbl where serial_number in ( %s );" % point_list_1
+
+        try:
+            self._reConn()
+            self.cursor = self.conn.cursor()
+            self.cursor.execute(sql)
+            res = self.cursor.fetchall()
+            self.cursor.execute(sql1)
+            res1 = self.cursor.fetchall()
+            title = [item[0] for item in res1]
+            title.insert(0, '日期')
+            self.cursor.close()
+        except:
+            print(traceback.format_exc())
+            return None
+        book = openpyxl.Workbook()
+        sheet = book.create_sheet(index=0)
+        # 循环将表头写入到sheet页
+        for i in range(len(title)):
+            sheet.cell(1, i + 1).value = title[i]
+        # 写数据
+        for row in range(0, len(res)):
+            for col in range(0, len(res[row])):
+                cell_val = res[row][col]
+                if isinstance(cell_val, datetime.datetime):
+                    times = cell_val.strftime("%Y-%m-%d %H:%M:%S")
+                    sheet.cell(row + 2, col + 1).value = times
+                else:
+                    sheet.cell(row + 2, col + 1).value = cell_val
+        file_path = (table_name + '.xlsx')
+        savepath = file_path
+        book.save(savepath)
+        return savepath
+
+    # 数据导出接口------------------------------------------------//
+
     def get_connectors(self):
         sql = "SELECT * FROM station_info_tbl WHERE status = 1"
         try: