|
@@ -11,11 +11,12 @@ import datetime
|
|
import time
|
|
import time
|
|
|
|
|
|
from django.db import connection, connections, transaction
|
|
from django.db import connection, connections, transaction
|
|
-from django.db.models import Q
|
|
|
|
|
|
+from django.db.models import Q, Sum, Count
|
|
from django.views import View
|
|
from django.views import View
|
|
|
|
|
|
from Model.models import Device_User, Device_Info, UidSetModel, UID_Bucket, Unused_Uid_Meal, Order_Model, StsCrdModel, \
|
|
from Model.models import Device_User, Device_Info, UidSetModel, UID_Bucket, Unused_Uid_Meal, Order_Model, StsCrdModel, \
|
|
- VodHlsModel, ExperienceContextModel, AiService, VodHlsSummary
|
|
|
|
|
|
+ VodHlsModel, ExperienceContextModel, AiService, VodHlsSummary, VideoPlaybackTimeModel, DeviceUserSummary, \
|
|
|
|
+ CountryModel
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.utils import LocalDateTimeUtil
|
|
from Object.utils import LocalDateTimeUtil
|
|
from Service.CommonService import CommonService
|
|
from Service.CommonService import CommonService
|
|
@@ -104,10 +105,9 @@ class CronDelDataView(View):
|
|
@staticmethod
|
|
@staticmethod
|
|
def delVodHls(response):
|
|
def delVodHls(response):
|
|
nowTime = int(time.time())
|
|
nowTime = int(time.time())
|
|
- # cursor = connection.cursor()
|
|
|
|
try:
|
|
try:
|
|
with transaction.atomic():
|
|
with transaction.atomic():
|
|
- month_ago_time = nowTime - 1 * 30 * 24 * 60 * 60
|
|
|
|
|
|
+ month_ago_time = nowTime - 30 * 24 * 60 * 60 # 删除1个月前的数据
|
|
vod_hls_qs = VodHlsModel.objects.filter(endTime__lte=month_ago_time)
|
|
vod_hls_qs = VodHlsModel.objects.filter(endTime__lte=month_ago_time)
|
|
for vod_hls in vod_hls_qs:
|
|
for vod_hls in vod_hls_qs:
|
|
end_time = vod_hls.endTime
|
|
end_time = vod_hls.endTime
|
|
@@ -115,21 +115,17 @@ class CronDelDataView(View):
|
|
this_month_start = datetime.datetime(end_time_str.year, end_time_str.month, 1)
|
|
this_month_start = datetime.datetime(end_time_str.year, end_time_str.month, 1)
|
|
this_month_start_stamp = CommonService.str_to_timestamp(
|
|
this_month_start_stamp = CommonService.str_to_timestamp(
|
|
this_month_start.strftime('%Y-%m-%d %H:%M:%S'))
|
|
this_month_start.strftime('%Y-%m-%d %H:%M:%S'))
|
|
- vod_business_qs = VodHlsSummary.objects.filter(time=this_month_start_stamp, uid=vod_hls.uid)
|
|
|
|
- if vod_business_qs.exists():
|
|
|
|
- vod_business = vod_business_qs.first()
|
|
|
|
- vod_business.sec = vod_business.sec + vod_hls.sec
|
|
|
|
- vod_business.upload_frequency = vod_business.upload_frequency + 1
|
|
|
|
- vod_business.save()
|
|
|
|
|
|
+ vod_hls_summary_qs = VodHlsSummary.objects.filter(time=this_month_start_stamp,
|
|
|
|
+ uid=vod_hls.uid)
|
|
|
|
+ if vod_hls_summary_qs.exists():
|
|
|
|
+ vod_hls_summary = vod_hls_summary_qs.first()
|
|
|
|
+ vod_hls_summary.upload_duration = vod_hls_summary.upload_duration + vod_hls.sec
|
|
|
|
+ vod_hls_summary.upload_frequency = vod_hls_summary.upload_frequency + 1
|
|
|
|
+ vod_hls_summary.save()
|
|
else:
|
|
else:
|
|
- VodHlsSummary.objects.create(time=this_month_start_stamp, uid=vod_hls.uid, sec=vod_hls.sec)
|
|
|
|
|
|
+ VodHlsSummary.objects.create(time=this_month_start_stamp, uid=vod_hls.uid,
|
|
|
|
+ upload_duration=vod_hls.sec, upload_frequency=1)
|
|
vod_hls.delete()
|
|
vod_hls.delete()
|
|
- # # 删除1个月前的数据
|
|
|
|
- # sql = "DELETE FROM `vod_hls` WHERE endTime<={} LIMIT 50000".format(
|
|
|
|
- # nowTime - 1 * 30 * 24 * 60 * 60)
|
|
|
|
- # cursor.execute(sql)
|
|
|
|
- # 关闭游标
|
|
|
|
- # cursor.close()
|
|
|
|
return response.json(0)
|
|
return response.json(0)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, repr(e))
|
|
return response.json(500, repr(e))
|
|
@@ -294,3 +290,129 @@ class CronUpdateDataView(View):
|
|
except Exception:
|
|
except Exception:
|
|
continue
|
|
continue
|
|
return response.json(0)
|
|
return response.json(0)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class CronCollectDataView(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 == 'collectPlayBack': # 定时保存云存视频回放
|
|
|
|
+ return self.collect_play_back(response)
|
|
|
|
+ if operation == 'collectDeviceUser': # 定时保存云存视频回放
|
|
|
|
+ return self.collect_device_user(response)
|
|
|
|
+ else:
|
|
|
|
+ return response.json(404)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def collect_play_back(response):
|
|
|
|
+ try:
|
|
|
|
+ end_time = int(time.time())
|
|
|
|
+ start_time = end_time - 24 * 60 * 60 # 每天执行一次
|
|
|
|
+ today = datetime.datetime.today()
|
|
|
|
+ this_month_str = datetime.datetime(today.year, today.month, 1)
|
|
|
|
+ this_month_stamp = CommonService.str_to_timestamp(this_month_str.strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
|
+ video_play_back_time_qs = VideoPlaybackTimeModel.objects.filter(startTime__gte=start_time,
|
|
|
|
+ startTime__lt=end_time,
|
|
|
|
+ playMode='cloud').values('uid').annotate(
|
|
|
|
+ play_duration=Sum('duration'), play_frequency=Count('uid'))
|
|
|
|
+ with transaction.atomic():
|
|
|
|
+ for item in video_play_back_time_qs:
|
|
|
|
+ vod_hls_summary_qs = VodHlsSummary.objects.filter(uid=item['uid'], time=this_month_stamp)
|
|
|
|
+ if vod_hls_summary_qs.exists():
|
|
|
|
+ vod_hls_summary = vod_hls_summary_qs.first()
|
|
|
|
+ vod_hls_summary.play_duration += item['play_duration']
|
|
|
|
+ vod_hls_summary.play_frequency += 1
|
|
|
|
+ vod_hls_summary.save()
|
|
|
|
+ else:
|
|
|
|
+ VodHlsSummary.objects.create(uid=item['uid'], time=this_month_stamp,
|
|
|
|
+ play_duration=item['play_duration'], play_frequency=1)
|
|
|
|
+
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def collect_device_user(response):
|
|
|
|
+ try:
|
|
|
|
+ today = datetime.datetime.today()
|
|
|
|
+ start_time = datetime.datetime(today.year, today.month, today.day)
|
|
|
|
+ end_time = start_time + datetime.timedelta(days=1)
|
|
|
|
+ increase_user_qs = Device_User.objects.filter(data_joined__gte=start_time, data_joined__lt=end_time).values(
|
|
|
|
+ 'data_joined',
|
|
|
|
+ 'region_country')
|
|
|
|
+ active_user_qs = Device_User.objects.filter(last_login__gte=start_time, last_login__lt=end_time).values(
|
|
|
|
+ 'last_login',
|
|
|
|
+ 'region_country')
|
|
|
|
+ start_time = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
|
+ country_qs = CountryModel.objects.all().values('id', 'region__name', 'country_name')
|
|
|
|
+ country_dict = {}
|
|
|
|
+ continent_dict = {}
|
|
|
|
+ for item in country_qs:
|
|
|
|
+ country_dict[item['id']] = item['country_name']
|
|
|
|
+ continent_dict[item['country_name']] = item['region__name']
|
|
|
|
+ with transaction.atomic():
|
|
|
|
+ for item in increase_user_qs:
|
|
|
|
+ device_user_summary_qs = DeviceUserSummary.objects.filter(time=start_time, query_type=0)
|
|
|
|
+ country_name = country_dict.get(item['region_country'], '未知国家')
|
|
|
|
+ continent_name = continent_dict.get(country_name, '未知大洲')
|
|
|
|
+ if device_user_summary_qs.exists():
|
|
|
|
+ device_user_summary = device_user_summary_qs.first()
|
|
|
|
+ country_temp_dict = eval(device_user_summary.country)
|
|
|
|
+ continent_temp_dict = eval(device_user_summary.continent)
|
|
|
|
+ if country_name in country_temp_dict:
|
|
|
|
+ country_temp_dict[country_name] += 1
|
|
|
|
+ else:
|
|
|
|
+ country_temp_dict[country_name] = 1
|
|
|
|
+ if continent_name in continent_temp_dict:
|
|
|
|
+ continent_temp_dict[continent_name] += 1
|
|
|
|
+ else:
|
|
|
|
+ continent_temp_dict[continent_name] = 1
|
|
|
|
+ device_user_summary.country = country_temp_dict
|
|
|
|
+ device_user_summary.continent = continent_temp_dict
|
|
|
|
+ device_user_summary.count += 1
|
|
|
|
+ device_user_summary.save()
|
|
|
|
+ else:
|
|
|
|
+ country_temp_dict = {country_name: 1}
|
|
|
|
+ continent_temp_dict = {continent_name: 1}
|
|
|
|
+ DeviceUserSummary.objects.create(time=start_time, count=1,
|
|
|
|
+ country=country_temp_dict,
|
|
|
|
+ continent=continent_temp_dict)
|
|
|
|
+ for item in active_user_qs:
|
|
|
|
+ device_user_summary_qs = DeviceUserSummary.objects.filter(time=start_time, query_type=1)
|
|
|
|
+ country_name = country_dict.get(item['region_country'], '未知国家')
|
|
|
|
+ continent_name = continent_dict.get(country_name, '未知大洲')
|
|
|
|
+ if device_user_summary_qs.exists():
|
|
|
|
+ device_user_summary = device_user_summary_qs.first()
|
|
|
|
+ country_temp_dict = eval(device_user_summary.country)
|
|
|
|
+ continent_temp_dict = eval(device_user_summary.continent)
|
|
|
|
+ if country_name in country_temp_dict:
|
|
|
|
+ country_temp_dict[country_name] += 1
|
|
|
|
+ else:
|
|
|
|
+ country_temp_dict[country_name] = 1
|
|
|
|
+ if continent_name in continent_temp_dict:
|
|
|
|
+ continent_temp_dict[continent_name] += 1
|
|
|
|
+ else:
|
|
|
|
+ continent_temp_dict[continent_name] = 1
|
|
|
|
+ device_user_summary.country = country_temp_dict
|
|
|
|
+ device_user_summary.continent = continent_temp_dict
|
|
|
|
+ device_user_summary.count += 1
|
|
|
|
+ device_user_summary.save()
|
|
|
|
+ else:
|
|
|
|
+ country_temp_dict = {country_name: 1}
|
|
|
|
+ continent_temp_dict = {continent_name: 1}
|
|
|
|
+ DeviceUserSummary.objects.create(time=start_time, query_type=1, count=1,
|
|
|
|
+ country=country_temp_dict,
|
|
|
|
+ continent=continent_temp_dict)
|
|
|
|
+
|
|
|
|
+ return response.json(0)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, repr(e))
|