DynamoDBLog.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD019
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2019/10/9 11:50
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: AliPayObject.py
  12. @Contact: pzb3076@163.com
  13. """
  14. from django.views.generic.base import View
  15. from django.utils.decorators import method_decorator
  16. from django.views.decorators.csrf import csrf_exempt
  17. from Service.ModelService import ModelService
  18. from Model.models import User_Brand,Device_User
  19. from django.utils import timezone
  20. import traceback,time,json,urllib.request
  21. from Object.ResponseObject import ResponseObject
  22. from Object.TokenObject import TokenObject
  23. from Service.CommonService import CommonService
  24. from collections import Counter
  25. from Ansjer.config import DOMAIN_HOST,AWS_DynamoDB_REGION,AWS_DynamoDB_ACCESS_KEY,AWS_DynamoDB_SECRET_KEY
  26. from Object.RedisObject import RedisObject
  27. from Service.MiscellService import MiscellService
  28. import datetime, simplejson as json
  29. import time
  30. '''
  31. http://192.168.136.39:8000/dynamoDBLog/searchByAdmin?search_value=154390905041313800138000&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiY24iLCJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1NzMyNjc3OTR9.YWNEHKKrzFfEc4liz6N8ANQl8C1VfhhLv_MEOqf3yV4&page=1&line=12
  32. http://192.168.136.39:8000/dynamoDBLog/connector?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiY24iLCJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1NzMyNjc3OTR9.YWNEHKKrzFfEc4liz6N8ANQl8C1VfhhLv_MEOqf3yV4
  33. '''
  34. class DynamoDBLog(View):
  35. @method_decorator(csrf_exempt)
  36. def dispatch(self, *args, **kwargs):
  37. return super(DynamoDBLog, self).dispatch(*args, **kwargs)
  38. def get(self, request, *args, **kwargs):
  39. request.encoding = 'utf-8'
  40. operation = kwargs.get('operation')
  41. return self.validation(request.GET, request, operation)
  42. def post(self, request, *args, **kwargs):
  43. request.encoding = 'utf-8'
  44. operation = kwargs.get('operation')
  45. return self.validation(request.POST, request, operation)
  46. def validation(self, request_dict, request, operation):
  47. self.clientIP = CommonService.get_ip_address(request)
  48. response = ResponseObject()
  49. token = request_dict.get('token', None)
  50. tko = TokenObject(token)
  51. response.lang = tko.lang
  52. if tko.code == 0:
  53. userID = tko.userID
  54. if operation == 'searchByAdmin':
  55. return self.search_info(request_dict, userID, response)
  56. elif operation == 'connector':
  57. return self.connector_info(request_dict, userID, response)
  58. elif operation == 'daySearch':
  59. return self.get_day_search(request_dict, userID, response)
  60. else:
  61. return response.json(444)
  62. else:
  63. return response.json(tko.code)
  64. # 查询搜索表(可以通过用户名,和接口名称,ip地址搜索等)
  65. def search_info(self, request_dict, userID,response):
  66. search_value = request_dict.get('search_value',None)
  67. check_perm = ModelService.check_perm(userID=userID,permID=30)
  68. if check_perm is True:
  69. if search_value is None or search_value is '':
  70. return response.json(0, {'datas': {}, 'count': 0})
  71. else:
  72. if DOMAIN_HOST == 'www.dvema.com':
  73. user_brand = 'access_log'
  74. else:
  75. user_brand = 'test_access_log'
  76. # 查询
  77. user_ID = Device_User.objects.filter(username=search_value).values('userID', 'NickName')
  78. try:
  79. search_value = user_ID[0]['userID']
  80. except Exception:
  81. search_value = search_value
  82. items = MiscellService.get_item_log( user_brand ,search_value)
  83. for k, v in enumerate(items):
  84. user_ID = Device_User.objects.filter(userID=items[k]['userID']).values('username')
  85. try:
  86. items[k]['userID'] = user_ID[0]['username']
  87. except Exception:
  88. items[k]['userID'] = ''
  89. return response.json(0, {'datas': items, 'count': len(items)})
  90. else:
  91. return response.json(404)
  92. # 查询接口的访问量的对应数量
  93. def connector_info(self, request_dict, userID,response):
  94. check_perm = ModelService.check_perm(userID=userID,permID=30)
  95. if check_perm is True:
  96. if DOMAIN_HOST == 'www.dvema.com':
  97. user_brand = 'access_log'
  98. else:
  99. user_brand = 'test_access_log'
  100. table_value = MiscellService.item_get_brand(user_brand)
  101. send_jsons = []
  102. counts = 0
  103. for i in table_value:
  104. send_jsons.append(table_value[counts]['operation'])
  105. counts = counts + 1
  106. send_jsons = Counter(send_jsons)
  107. return response.json(0, {'datas': send_jsons,'counts':counts})
  108. else:
  109. return response.json(404)
  110. # 通过传过来日期天来搜索,访问量,并返回对应小时的访问数量
  111. def get_day_search(self, request_dict,userID, response):
  112. check_perm = ModelService.check_perm(userID=userID, permID=30)
  113. if DOMAIN_HOST == 'www.dvema.com':
  114. user_brand = 'access_log'
  115. else:
  116. user_brand = 'test_access_log'
  117. time_stamp = int(request_dict.get('timestamp', None))
  118. if check_perm is True:
  119. # 分割小时再去搜索
  120. times = datetime.datetime.fromtimestamp(time_stamp)
  121. time_dict = CommonService.getTimeDict(times)
  122. res = {}
  123. for k, v in time_dict.items():
  124. start_date = time_dict[k]
  125. end_date = time_dict[k] + datetime.timedelta(hours=1)
  126. start_date = int(start_date.timestamp())
  127. end_date = int(end_date.timestamp())
  128. count = MiscellService.get_item_time(user_brand,start_date, end_date)
  129. if count:
  130. res[k] = count
  131. else:
  132. res[k] = 0
  133. # res = MiscellService.get_item_date(user_brand,time_stamp)
  134. return response.json(0, {'count': res})
  135. else:
  136. return response.json(404)