优化代码结构,增加空影厅数据的打印

main
rogersun 2 weeks ago
parent a9d2cdb4cb
commit 86ec8db612
  1. 3
      ai/tasks.py
  2. 93
      ai/views.py

@ -1,10 +1,11 @@
from celery import shared_task from celery import shared_task
from ai.utils.show_process import show_main_process from ai.utils.show_process import show_main_process
from ai.utils.basic_func import clear_lock_func
@shared_task @shared_task
def ai_show_general(): def ai_show_general():
clear_lock_func()
show_main_process() show_main_process()
print("task success") print("task success")
return True return True

@ -2,17 +2,9 @@ from django.http import JsonResponse
from django.db.models import Q from django.db.models import Q
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from ai.models import * from ai.models import *
import datetime
import json
import pandas as pd
import csv
from io import StringIO
from ai.utils.show_database import GetData
from ai.utils.show_process import show_main_process from ai.utils.show_process import show_main_process
from django_redis import get_redis_connection from ai.utils.show_func import get_cinema_show_result_func
from ai.utils.basic_func import *
# Create your views here.
# 手动触发ai排片 # 手动触发ai排片
@ -42,55 +34,10 @@ def get_cinema_show_result(request):
return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False})
def get_cinema_show_result_func(_zz_code, _show_date):
cinema = TestCinema.objects.filter(zz_code=_zz_code).first()
start = datetime.datetime.strftime(datetime.datetime.strptime(_show_date, '%Y-%m-%d') + datetime.timedelta(hours=6),
'%Y-%m-%d %H:%M:%S')
end = datetime.datetime.strftime(
datetime.datetime.strptime(_show_date, '%Y-%m-%d') + datetime.timedelta(hours=29, minutes=59, seconds=59),
'%Y-%m-%d %H:%M:%S')
show = ''
income = ''
if cinema:
print(cinema)
data = GetData(cinema)
show = data.get_show_data(start, end)
income = data.get_total_income(start, end)
print(show, income)
show_db = AiShow.objects.filter(Q(is_ai_show=False) & Q(zz_code=_zz_code) & Q(show_date=_show_date)).order_by(
'-id').first()
if show_db:
print('show_db', show_db)
show_db.show = show
show_db.income = income
show_db.save()
else:
try:
AiShow.objects.create(
cinema=cinema.name,
zz_code=cinema.zz_code,
show_date=_show_date,
is_ai_show=False,
show=show,
sales=income,
prompt='',
result='',
message='',
take_times=0,
take_tokens='0'
)
except Exception as e:
print(e)
return show, income
# 清除redis锁 # 清除redis锁
@csrf_exempt @csrf_exempt
def clear_lock(request): def clear_lock(request):
redis_conn = get_redis_connection() clear_lock_func()
redis_key = f'ai_show{datetime.date.today().strftime("%Y%m%d")}'
if redis_conn.exists(redis_key):
redis_conn.delete(redis_key)
result_dict = { result_dict = {
'status': 'success', 'status': 'success',
'message': '完成', 'message': '完成',
@ -159,13 +106,16 @@ def report(request):
['影厅别名', '影片别名', '语言', '开始时间', '结束时间', '片长', '场间', '', '影厅别名', '影片别名', '语言', ['影厅别名', '影片别名', '语言', '开始时间', '结束时间', '片长', '场间', '', '影厅别名', '影片别名', '语言',
'开始时间', '结束时间', '片长', '场间']] '开始时间', '结束时间', '片长', '场间']]
for v in data.values(): for v in data.values():
if len(v['ai']) == 0 and len(v['real']) == 0:
output_list.append([v['hall_name'], show_date, '', '', '', '', '', '', '', '', '', '', '', '', '', ''])
output_csv.append([v['hall_name'], '', '', '', '', '', '', '', v['hall_name'], '', '', '', '', '', ''])
if len(v['ai']) >= len(v['real']): if len(v['ai']) >= len(v['real']):
for i in range(len(v['ai'])): for i in range(len(v['ai'])):
if i < len(v['real']): if i < len(v['real']):
a = v['ai'][i] a = v['ai'][i]
r = v['real'][i] r = v['real'][i]
output_list.append( output_list.append(
[v['hall_name'], a['show_date'], a['movie_name'], a['movie_id'], a['language'], a['start'], [v['hall_name'], show_date, a['movie_name'], a['movie_id'], a['language'], a['start'],
a['end'], a['length'], a['duration'], r['movie_name'], r['movie_id'], r['language'], a['end'], a['length'], a['duration'], r['movie_name'], r['movie_id'], r['language'],
r['start'], r['end'], r['length'], r['duration']] r['start'], r['end'], r['length'], r['duration']]
) )
@ -177,7 +127,7 @@ def report(request):
else: else:
a = v['ai'][i] a = v['ai'][i]
output_list.append( output_list.append(
[v['hall_name'], a['show_date'], a['movie_name'], a['movie_id'], a['language'], a['start'], [v['hall_name'], show_date, a['movie_name'], a['movie_id'], a['language'], a['start'],
a['end'], a['length'], a['duration'], '', '', '', '', '', '', ''] a['end'], a['length'], a['duration'], '', '', '', '', '', '', '']
) )
output_csv.append( output_csv.append(
@ -190,7 +140,7 @@ def report(request):
a = v['ai'][i] a = v['ai'][i]
r = v['real'][i] r = v['real'][i]
output_list.append( output_list.append(
[v['hall_name'], r['show_date'], a['movie_name'], a['movie_id'], a['language'], a['start'], [v['hall_name'], show_date, a['movie_name'], a['movie_id'], a['language'], a['start'],
a['end'], a['length'], a['duration'], r['movie_name'], r['movie_id'], r['language'], a['end'], a['length'], a['duration'], r['movie_name'], r['movie_id'], r['language'],
r['start'], r['end'], r['length'], r['duration']] r['start'], r['end'], r['length'], r['duration']]
) )
@ -202,7 +152,7 @@ def report(request):
else: else:
r = v['real'][i] r = v['real'][i]
output_list.append( output_list.append(
[v['hall_name'], r['show_date'], '', '', '', '', '', '', '', r['movie_name'], r['movie_id'], [v['hall_name'], show_date, '', '', '', '', '', '', '', r['movie_name'], r['movie_id'],
r['language'], r['start'], r['end'], r['length'], r['duration']] r['language'], r['start'], r['end'], r['length'], r['duration']]
) )
output_csv.append( output_csv.append(
@ -227,26 +177,3 @@ def report(request):
} }
print(result_dict) print(result_dict)
return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False}) return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False})
# 将csv转化成字典
def scv_to_obj(csv_data):
f = StringIO(csv_data)
reader = csv.DictReader(f)
return list(reader)
def list_to_csv(rows, include_header=True):
"""
将嵌套列表转换为 CSV 字符串
:param rows: 输入数据格式为 [['col1','col2'], ['val1','val2'], ...]
:param include_header: 若为 True rows[0] 作为表头否则全部当作数据行
"""
output = StringIO()
writer = csv.writer(output, lineterminator='\r\n')
if include_header and rows:
writer.writerow(rows[0])
writer.writerows(rows[1:])
else:
writer.writerows(rows)
return output.getvalue()

Loading…
Cancel
Save