Bläddra i källkod

整合定时删除访问接口数据和定时删除推送数据接口

locky 3 år sedan
förälder
incheckning
a8e46d974a
3 ändrade filer med 57 tillägg och 35 borttagningar
  1. 4 2
      Ansjer/urls.py
  2. 53 16
      Controller/Cron/CronDelDataController.py
  3. 0 17
      Controller/EquipmentInfo.py

+ 4 - 2
Ansjer/urls.py

@@ -167,8 +167,7 @@ urlpatterns = [
     url(r'^deviceShadow/update$', ShadowController.update_device_shadow),
     url(r'^deviceShadow/generateUTK$', ShadowController.generate_utk),
     url(r'^test/notify/push$', TestDetectController.NotificationView.as_view()),
-    url(r'^eq/del$', EquipmentInfo.deleteExpireEquipmentInfo),
-    url(r'^eq/log/del$', CronDelDataController.deleteExpireAccessLog),
+
     # 新增
     url(r'^cloudVod/(?P<operation>.*)$', CloudVod.CloudVodView.as_view()),
     url(r'^meal/(?P<operation>.*)$', MealManage.MealView.as_view()),
@@ -394,6 +393,9 @@ urlpatterns = [
     url(r'surveys/(?P<operation>.*)', SurveysManageController.SurveysView.as_view()),
     # 后台界面接口 -----------------------------------------------------
 
+    # 定时删除任务接口
+    url(r'^cron/(?P<operation>.*)', CronDelDataController.CronDelDataView.as_view()),
+
     re_path('(?P<path>.*)', LogManager.errorPath),
 
 ]

+ 53 - 16
Controller/Cron/CronDelDataController.py

@@ -7,24 +7,61 @@
 # @Email   : zhangdongming@asj6.wecom.work
 # @File    : CronDelDataController.py
 # @Software: PyCharm
-from django.db import connection
+import time
+
+from django.db import connection, connections
+from django.views import View
 
 from Object.ResponseObject import ResponseObject
 from Object.utils import LocalDateTimeUtil
 
 
-def deleteExpireAccessLog(request):
-    response = ResponseObject()
-    try:
-        print(request)
-        cursor = connection.cursor()
-        # 删除一个月前的数据
-        last_month = LocalDateTimeUtil.get_last_month()
-        sql = 'DELETE FROM access_log WHERE time < %s limit %s'
-        cursor.execute(sql, [last_month, 10000])
-        # 关闭游标
-        cursor.close()
-        connection.close()
-        return response.json(0)
-    except Exception as e:
-        return response.json(500, repr(e))
+class CronDelDataView(View):
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        return self.validation(request.GET, request, operation)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        return self.validation(request.POST, request, operation)
+
+    def validation(self, request_dict, request, operation):
+        response = ResponseObject()
+        if operation == 'access-log/del':   # 定时删除访问接口数据
+            return self.del_access_log(response)
+        elif operation == 'push-info/del':  # 定时删除推送数据
+            return self.del_push_info(response)
+        else:
+            return response.json(404)
+
+    @staticmethod
+    def del_access_log(response):
+        try:
+            cursor = connection.cursor()
+            # 删除一个月前的数据
+            last_month = LocalDateTimeUtil.get_last_month()
+            sql = 'DELETE FROM access_log WHERE time < %s limit %s'
+            cursor.execute(sql, [last_month, 10000])
+            # 关闭游标
+            cursor.close()
+            connection.close()
+            return response.json(0)
+        except Exception as e:
+            return response.json(500, repr(e))
+
+    @staticmethod
+    def del_push_info(response):
+        nowTime = int(time.time())
+        cursor = connections['mysql02'].cursor()
+        try:
+            for i in range(5):
+                # 删除7天前的数据
+                sql = "DELETE FROM `equipment_info` WHERE addTime<={} LIMIT 10000".format(nowTime - 3600 * 24 * 7)
+                cursor.execute(sql)
+            # 关闭游标
+            cursor.close()
+            return response.json(0)
+        except Exception as e:
+            return response.json(500, repr(e))

+ 0 - 17
Controller/EquipmentInfo.py

@@ -290,23 +290,6 @@ class EquipmentInfo(View):
 use information_schema;
 select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='Ansjer81';
 '''
-# 定时删除推送数据
-def deleteExpireEquipmentInfo(request):
-    response = ResponseObject()
-    import time
-    nowTime = int(time.time())
-    cursor = connections['mysql02'].cursor()
-    try:
-        for i in range(5):
-            # 删除7天前的数据
-            sql = "DELETE FROM `equipment_info` WHERE addTime<={} LIMIT 10000".format(nowTime - 3600 * 24 * 7)
-            cursor.execute(sql)
-        # 关闭游标
-        cursor.close()
-        return response.json(0)
-    except Exception as e:
-        return response.json(500, repr(e))
-
 
 # 按季度删除访问日志
 def deleteExpireEquipmentInfoById(request):