DeviceDataController.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : UserDataController.py
  4. @Time : 2022/8/16 10:44
  5. @Author : peng
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import datetime
  10. import requests
  11. from django.db.models import Q, Sum
  12. from django.views.generic.base import View
  13. from Model.models import CountryModel, UidSetModel, DeviceInfoSummary
  14. from Service.CommonService import CommonService
  15. # 设备数据
  16. class DeviceDataView(View):
  17. def get(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.GET, request, operation)
  21. def post(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. operation = kwargs.get('operation')
  24. return self.validation(request.POST, request, operation)
  25. def validation(self, request_dict, request, operation):
  26. token_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
  27. if token_code != 0:
  28. return response.json(token_code)
  29. if operation == 'type': # 统计设备类型
  30. return self.type_statistics(response)
  31. if operation == 'regional': # 设备地区分布
  32. return self.regional_statistics(response)
  33. if operation == 'addDevice': # 查询设备增长数据(数据有些许差异)
  34. return self.add_device(request_dict, response)
  35. if operation == 'active': # 设备活跃数据
  36. return self.device_active(request_dict, response)
  37. if operation == 'global/regional': # 全球设备分布
  38. return self.global_regional(request, request_dict, response)
  39. if operation == 'global/type': # 全球设备类型
  40. return self.golbal_type(request, request_dict, response)
  41. if operation == 'global/active': # 全球设备活跃分布
  42. return self.golbal_active(request, request_dict, response)
  43. if operation == 'global/addDevice': # 全球新增设备数据
  44. return self.golbal_add_device(request, request_dict, response)
  45. if operation == 'ip/country': #
  46. return self.ip_country(response)
  47. else:
  48. return response.json(414)
  49. @staticmethod
  50. def ip_country(response):
  51. uid_set_qs = UidSetModel.objects.filter(~Q(ip='') & Q(tb_country=0)).values('ip')
  52. for uid_set in uid_set_qs:
  53. ip = uid_set['ip']
  54. ipInfo = CommonService.getIpIpInfo(ip, 'CN')
  55. country_qs = CountryModel.objects.filter(country_code=ipInfo['country_code']).values('id')
  56. if country_qs.exists():
  57. country = country_qs[0]['id']
  58. uid_set_qs.filter(ip=ip).update(tb_country=country)
  59. return response.json(0)
  60. @classmethod
  61. def golbal_add_device(cls, request, request_dict, response):
  62. """
  63. 全球新增设备数据
  64. @param request:请求
  65. @param request_dict:请求参数
  66. @param response: 响应对象
  67. """
  68. url_list = CommonService.get_domain_name()
  69. try:
  70. headers = {
  71. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  72. }
  73. device_list = []
  74. device_count = 0
  75. type_list = []
  76. type_count = 0
  77. region_list = []
  78. region_count = 0
  79. order_list = []
  80. order_count = 0
  81. for url in url_list:
  82. url = url + request.path.replace('global/', '')
  83. res = requests.get(url=url, params=request_dict, headers=headers)
  84. result = res.json()
  85. if result['result_code'] == 0:
  86. for item in result['result']['addDevice']:
  87. flag = 0
  88. for each in device_list:
  89. if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
  90. each['count'] += item['count']
  91. device_count += item['count']
  92. flag = 1
  93. break
  94. if flag == 0:
  95. device_list.append(item)
  96. device_count += item['count']
  97. for item in device_list:
  98. if device_count != 0:
  99. item['rate'] = round(item['count'] / device_count * 100, 2)
  100. else:
  101. break
  102. for item in result['result']['region']:
  103. flag = 0
  104. for each in region_list:
  105. if item['countryName'] == each['countryName']:
  106. each['count'] += item['count']
  107. region_count += item['count']
  108. flag = 1
  109. break
  110. if flag == 0:
  111. region_list.append(item)
  112. region_count += item['count']
  113. for item in region_list:
  114. if region_count != 0:
  115. item['rate'] = round(item['count'] / region_count * 100, 2)
  116. else:
  117. break
  118. for item in result['result']['type']:
  119. flag = 0
  120. for each in type_list:
  121. if item['type'] == each['type']:
  122. each['count'] += item['count']
  123. type_count += item['count']
  124. flag = 1
  125. break
  126. if flag == 0:
  127. type_list.append(item)
  128. type_count += item['count']
  129. for item in type_list:
  130. if type_count != 0:
  131. item['rate'] = round(item['count'] / type_count * 100, 2)
  132. else:
  133. break
  134. for item in result['result']['version']:
  135. flag = 0
  136. for each in order_list:
  137. if item['type'] == each['type']:
  138. each['count'] += item['count']
  139. order_count += item['count']
  140. flag = 1
  141. break
  142. if flag == 0:
  143. order_list.append(item)
  144. order_count += item['count']
  145. for item in order_list:
  146. if order_count != 0:
  147. item['rate'] = round(item['count'] / order_count * 100, 2)
  148. else:
  149. break
  150. else:
  151. return response.json(result['result_code'])
  152. res = {
  153. 'device': device_list,
  154. 'type': type_list,
  155. 'region': CommonService.list_sort(region_list),
  156. 'version': order_list
  157. }
  158. return response.json(0, res)
  159. except Exception as e:
  160. print(e)
  161. return response.json(500, repr(e))
  162. @classmethod
  163. def golbal_active(cls, request, request_dict, response):
  164. """
  165. 全球设备活跃分布
  166. @param request:请求
  167. @param request_dict:请求参数
  168. @param response: 响应对象
  169. """
  170. url_list = CommonService.get_domain_name()
  171. try:
  172. headers = {
  173. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  174. }
  175. type_list = []
  176. type_count = 0
  177. region_list = []
  178. region_count = 0
  179. for url in url_list:
  180. url = url + request.path.replace('global/', '')
  181. res = requests.get(url=url, params=request_dict, headers=headers)
  182. result = res.json()
  183. if result['result_code'] == 0:
  184. for item in result['result']['vodHls']:
  185. flag = 0
  186. for each in type_list:
  187. if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
  188. each['count'] += item['count']
  189. type_count += item['count']
  190. flag = 1
  191. break
  192. if flag == 0:
  193. type_list.append(item)
  194. type_count += item['count']
  195. for item in type_list:
  196. if type_count != 0:
  197. item['rate'] = round(item['count'] / type_count * 100, 2)
  198. else:
  199. break
  200. for item in result['result']['region']:
  201. flag = 0
  202. for each in region_list:
  203. if item['countryName'] == each['countryName']:
  204. each['count'] += item['count']
  205. region_count += item['count']
  206. flag = 1
  207. break
  208. if flag == 0:
  209. region_list.append(item)
  210. region_count += item['count']
  211. for item in region_list:
  212. if region_count != 0:
  213. item['rate'] = round(item['count'] / region_count * 100, 2)
  214. else:
  215. break
  216. else:
  217. return response.json(result['result_code'])
  218. res = {
  219. 'device': type_list,
  220. 'region': CommonService.list_sort(region_list)
  221. }
  222. return response.json(0, res)
  223. except Exception as e:
  224. print(e)
  225. return response.json(500, repr(e))
  226. @classmethod
  227. def golbal_type(cls, request, request_dict, response):
  228. """
  229. 全球设备类型分布
  230. @param request:请求
  231. @param request_dict:请求参数
  232. @param response: 响应对象
  233. """
  234. url_list = CommonService.get_domain_name()
  235. try:
  236. headers = {
  237. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  238. }
  239. type_list = []
  240. type_count = 0
  241. for url in url_list:
  242. url = url + request.path.replace('global/', '')
  243. res = requests.get(url=url, params=request_dict, headers=headers)
  244. result = res.json()
  245. if result['result_code'] == 0:
  246. for item in result['result']['type']:
  247. flag = 0
  248. for each in type_list:
  249. if item['type'] == each['type']:
  250. each['count'] += item['count']
  251. type_count += item['count']
  252. flag = 1
  253. break
  254. if flag == 0:
  255. type_list.append(item)
  256. type_count += item['count']
  257. for item in type_list:
  258. item['rate'] = round(item['count'] / type_count * 100, 2)
  259. else:
  260. return response.json(result['result_code'])
  261. res = {
  262. 'type': CommonService.list_sort(type_list)
  263. }
  264. return response.json(0, res)
  265. except Exception as e:
  266. print(e)
  267. return response.json(500)
  268. @classmethod
  269. def global_regional(cls, request, request_dict, response):
  270. """
  271. 全球设备分布
  272. @param request:请求
  273. @param request_dict:请求参数
  274. @param response:响应对象
  275. @return:
  276. """
  277. url_list = CommonService.get_domain_name()
  278. try:
  279. headers = {
  280. 'Authorization': request.META.get('HTTP_AUTHORIZATION')
  281. }
  282. device_list = []
  283. device_count = 0
  284. region_list = []
  285. region_count = 0
  286. for url in url_list:
  287. url = url + request.path.replace('global/', '')
  288. res = requests.get(url=url, params=request_dict, headers=headers)
  289. result = res.json()
  290. if result['result_code'] == 0:
  291. # 处理地区
  292. for item in result['result']['countries']:
  293. flag = 0
  294. for each in device_list:
  295. if each['countryName'] == item['countryName']:
  296. each['count'] += int(item['count'])
  297. device_count += int(item['count'])
  298. flag = 1
  299. break
  300. if flag == 0:
  301. device_list.append(item)
  302. device_count += int(item['count'])
  303. for item in device_list:
  304. rate = round(item['count'] / device_count * 100, 2)
  305. item['rate'] = rate
  306. for item in result['result']['continent']:
  307. flag = 0
  308. for each in region_list:
  309. if each['continentName'] == item['continentName']:
  310. each['count'] += item['count']
  311. region_count += item['count']
  312. flag = 1
  313. break
  314. if flag == 0:
  315. region_list.append(item)
  316. region_count += item['count']
  317. for item in region_list:
  318. item['rate'] = round(item['count'] / region_count * 100, 2)
  319. else:
  320. return response.json(result['result_code'])
  321. res = {
  322. 'countries': CommonService.list_sort(device_list[:20]),
  323. 'continent': region_list
  324. }
  325. return response.json(0, res)
  326. except Exception as e:
  327. return response.json(500, repr(e))
  328. @classmethod
  329. def device_active(cls, request_dict, response):
  330. """
  331. 设备活跃数据
  332. @param request_dict:请求参数
  333. @request_dict starTime:开始时间
  334. @request_dict endTime:结束时间
  335. @param response:响应对象
  336. """
  337. start_time = request_dict.get('startTime', None) # 时间戳
  338. end_time = request_dict.get('endTime', None)
  339. unit_time = request_dict.get('timeUnit', None)
  340. if not all([start_time, end_time, unit_time]):
  341. return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
  342. s_time = datetime.datetime.fromtimestamp(int(start_time))
  343. e_time = datetime.datetime.fromtimestamp(int(end_time))
  344. time_list = CommonService.cutting_time(s_time, e_time, unit_time)
  345. try:
  346. device_info_summary_qs = DeviceInfoSummary.objects.filter(
  347. time__range=(start_time, end_time), query_type=1).values('country', 'count')
  348. res = {}
  349. if not device_info_summary_qs.exists():
  350. return response.json(0, res)
  351. count_all = device_info_summary_qs.aggregate(total=Sum('count'))['total']
  352. video_list = []
  353. region_list = []
  354. for item in time_list:
  355. deivce_type_qs = device_info_summary_qs.filter(time__range=(item[0], item[1])).values('count')
  356. if deivce_type_qs.exists():
  357. count = deivce_type_qs.aggregate(total=Sum('count'))['total']
  358. else:
  359. count = deivce_type_qs.count()
  360. vod_dict = {
  361. 'count': count,
  362. 'rate': round(count / count_all * 100, 2),
  363. 'startTime': item[0],
  364. 'endTime': item[1]
  365. }
  366. video_list.append(vod_dict)
  367. res['vodHls'] = video_list
  368. for type_country in device_info_summary_qs:
  369. country_temp_dict = eval(type_country['country'])
  370. for k, v in country_temp_dict.items():
  371. flag = 0
  372. for each in region_list:
  373. if k == each['countryName']:
  374. each['count'] += v
  375. flag = 1
  376. break
  377. if flag == 0:
  378. region_list.append({
  379. 'countryName': k,
  380. 'count': v
  381. })
  382. for item in region_list:
  383. item['rate'] = round(item['count'] / count_all * 100, 2) if count_all else 0
  384. res['region'] = region_list
  385. return response.json(0, res)
  386. except Exception as e:
  387. print(e)
  388. return response.json(500)
  389. @classmethod
  390. def add_device(cls, request_dict, response):
  391. """
  392. 查询设备增长数据
  393. @param request_dict:请求参数
  394. @request_dict starTime:开始时间
  395. @request_dict endTime:结束时间
  396. @param response:响应对象
  397. """
  398. start_time = request_dict.get('startTime', None) # 时间戳
  399. end_time = request_dict.get('endTime', None)
  400. unit_time = request_dict.get('timeUnit', None)
  401. if not all([start_time, end_time, unit_time]):
  402. return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
  403. try:
  404. uid_set_qs = DeviceInfoSummary.objects.filter(time__range=(start_time, end_time), query_type=0)
  405. res = {}
  406. if not uid_set_qs.exists():
  407. return response.json(0, res)
  408. count_all = uid_set_qs.aggregate(total=Sum('count'))['total']
  409. # 统计该时间段的设备数量(已去重)
  410. info_list = []
  411. region_list = []
  412. type_list = []
  413. version_list = []
  414. start_time = datetime.datetime.fromtimestamp(int(start_time))
  415. end_time = datetime.datetime.fromtimestamp(int(end_time))
  416. time_list = CommonService.cutting_time(start_time, end_time, unit_time)
  417. for item in time_list:
  418. device_uid_qs = uid_set_qs.filter(time__range=(item[0], item[1]), query_type=0)
  419. if device_uid_qs.exists():
  420. count = device_uid_qs.aggregate(total=Sum('count'))['total']
  421. else:
  422. count = device_uid_qs.count()
  423. rate = round(count / count_all * 100, 2)
  424. info_dict = {
  425. 'startTime': item[0],
  426. 'endTime': item[1],
  427. 'count': count,
  428. 'rate': rate
  429. }
  430. info_list.append(info_dict)
  431. res['addDevice'] = info_list
  432. # 统计地区设备数量
  433. device_info_country_qs = uid_set_qs.values('country')
  434. for item in device_info_country_qs:
  435. country_temp_dict = eval(item['country'])
  436. for x, y in country_temp_dict.items():
  437. flag = 0
  438. for each in region_list:
  439. if x == each['countryName']:
  440. each['count'] += y
  441. flag = 1
  442. break
  443. if flag == 0:
  444. region_list.append({
  445. 'countryName': x,
  446. 'count': y
  447. })
  448. for item in region_list:
  449. item['rate'] = round(item['count'] / count_all * 100, 2) if count_all else 0
  450. res['region'] = CommonService.list_sort(region_list)
  451. # 统计设备类型数量
  452. device_type_qs = uid_set_qs.values('device_type')
  453. for item in device_type_qs:
  454. country_temp_dict = eval(item['device_type'])
  455. for x, y in country_temp_dict.items():
  456. flag = 0
  457. for each in type_list:
  458. if x == each['type']:
  459. each['count'] += y
  460. flag = 1
  461. break
  462. if flag == 0:
  463. type_list.append({
  464. 'type': x,
  465. 'count': y
  466. })
  467. for item in type_list:
  468. item['rate'] = round(item['count'] / count_all * 100, 2) if count_all else 0
  469. res['type'] = CommonService.list_sort(type_list)
  470. # 云存版本数量
  471. cloud_type_qs = uid_set_qs.values('vod_service')
  472. for cloud_type in cloud_type_qs:
  473. country_temp_dict = eval(cloud_type['vod_service'])
  474. for x, y in country_temp_dict.items():
  475. flag = 0
  476. for each in version_list:
  477. if x == each['type']:
  478. each['count'] += y
  479. flag = 1
  480. break
  481. if flag == 0:
  482. version_list.append({
  483. 'type': x,
  484. 'count': y
  485. })
  486. for item in version_list:
  487. item['rate'] = round(item['count'] / count_all * 100, 2) if count_all else 0
  488. res['version'] = CommonService.list_sort(version_list)
  489. return response.json(0, res)
  490. except Exception as e:
  491. print(e)
  492. return response.json(500, repr(e))
  493. @classmethod
  494. def regional_statistics(cls, response):
  495. """
  496. 统计地区设备数量
  497. @param response:响应对象
  498. """
  499. all_device_qs = DeviceInfoSummary.objects.filter(query_type=0).values('continent', 'count', 'country')
  500. if not all_device_qs.exists():
  501. return response.json(173)
  502. country_count = all_device_qs.aggregate(total=Sum('count'))['total']
  503. res = {}
  504. try:
  505. continent_list = []
  506. region_list = []
  507. for item in all_device_qs:
  508. country_temp_dict = eval(item['country'])
  509. for x, y in country_temp_dict.items():
  510. flag = 0
  511. for each in region_list:
  512. if x == each['countryName']:
  513. each['count'] += y
  514. flag = 1
  515. break
  516. if flag == 0:
  517. region_list.append({
  518. 'countryName': x,
  519. 'count': y
  520. })
  521. for item in region_list:
  522. item['rate'] = round(item['count'] / country_count * 100, 2) if country_count else 0
  523. for item in all_device_qs:
  524. continent_temp_dict = eval(item['continent'])
  525. for x, y in continent_temp_dict.items():
  526. flag = 0
  527. for each in continent_list:
  528. if x == each['continentName']:
  529. each['count'] += y
  530. flag = 1
  531. break
  532. if flag == 0:
  533. continent_list.append({
  534. 'continentName': x,
  535. 'count': y
  536. })
  537. for item in continent_list:
  538. item['rate'] = round(item['count'] / country_count * 100, 2) if country_count else 0
  539. res['countries'] = CommonService.list_sort(region_list)
  540. res['continent'] = CommonService.list_sort(continent_list)
  541. return response.json(0, res)
  542. except Exception as e:
  543. print(e)
  544. return response.json(500)
  545. @classmethod
  546. def type_statistics(cls, response):
  547. """
  548. 统计设备类型
  549. @param response:响应对象
  550. @return:
  551. """
  552. all_device_qs = DeviceInfoSummary.objects.filter(query_type=0).values('device_type', 'count')
  553. if not all_device_qs.exists():
  554. return response.json(173)
  555. country_count = all_device_qs.aggregate(total=Sum('count'))['total']
  556. res = {}
  557. try:
  558. device_type_list = []
  559. for item in all_device_qs:
  560. country_temp_dict = eval(item['device_type'])
  561. for t, c in country_temp_dict.items():
  562. flag = 0
  563. for each in device_type_list:
  564. if t == each['type']:
  565. each['count'] += c
  566. flag = 1
  567. break
  568. if flag == 0:
  569. device_type_list.append({
  570. 'type': t,
  571. 'count': c
  572. })
  573. for item in device_type_list:
  574. item['rate'] = round(item['count'] / country_count * 100, 2) if country_count else 0
  575. res['type'] = CommonService.list_sort(device_type_list)
  576. return response.json(0, res)
  577. except Exception as e:
  578. print(e)
  579. return response.json(500)