Commit 110d8850 authored by xianyang's avatar xianyang

优化业务类型汇总查询,线程版

parent 153461ec
import math
import threading import threading
import time import time
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
...@@ -737,18 +738,28 @@ class HomePageDisplay(object): ...@@ -737,18 +738,28 @@ class HomePageDisplay(object):
self.unique_tag = unique_tag self.unique_tag = unique_tag
self.account = [] self.account = []
self.guild = [] self.guild = []
self.bus_data = []
self.income = 0 self.income = 0
self.outcome = 0 self.outcome = 0
def th_task(self, sql):
res_ads = LinkMysql(env.DB_HISTORY).query_mysql(sql)
if res_ads:
self.bus_data += res_ads
def get_month_data(self): def get_month_data(self):
acc_sql = "select unique_tag,uuid from fi_account" acc_sql = "select unique_tag,uuid from fi_account"
guild_sql = "select uuid from guild" guild_sql = "select uuid from guild"
count_sql = f"select count(id) as num from {self.date}"
with ThreadPoolExecutor(max_workers=2) as pool: with ThreadPoolExecutor(max_workers=2) as pool:
future1 = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, acc_sql) future1 = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, acc_sql)
future2 = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, guild_sql) future2 = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, guild_sql)
future3 = pool.submit(LinkMysql(env.DB_HISTORY).query_mysql, count_sql)
acc_data = future1.result() acc_data = future1.result()
guild_data = future2.result() guild_data = future2.result()
count = future3.result()
account = [i['uuid'] for i in acc_data] account = [i['uuid'] for i in acc_data]
guild = [i['uuid'] for i in guild_data] guild = [i['uuid'] for i in guild_data]
assets_cond = ['type in(0,1)'] assets_cond = ['type in(0,1)']
...@@ -766,12 +777,19 @@ class HomePageDisplay(object): ...@@ -766,12 +777,19 @@ class HomePageDisplay(object):
Logger(20).logger.info('没找到系统账户') Logger(20).logger.info('没找到系统账户')
return [], 0, 0 return [], 0, 0
assets_cond.append(f" uuid='{acc_uuid[0]}'") assets_cond.append(f" uuid='{acc_uuid[0]}'")
assets_sql = f"select reference_type,type,sum(amount) as amount from {self.date} where {' and '.join(assets_cond)} GROUP BY reference_type,type" ths_count = math.ceil(count[0]['num']/10000000)
total_data = LinkMysql(env.DB_HISTORY).query_mysql(assets_sql) ths = []
for x in range(ths_count):
assets_sql = f"select reference_type,type,sum(amount) as amount from {self.date} where {' and '.join(assets_cond)} GROUP BY reference_type,type LIMIT {x * 10000000},{(x+1) * 10000000}"
ths.append(threading.Thread(target=self.th_task, args=[assets_sql]))
for y in range(ths_count):
ths[y].start()
for z in range(ths_count):
ths[z].join()
# 数据分类 # 数据分类
income = [] income = []
outcome = [] outcome = []
for i in total_data: for i in self.bus_data:
op = {} op = {}
if TYPE_NAME.get(i['reference_type']): if TYPE_NAME.get(i['reference_type']):
op['name'] = TYPE_NAME.get(i['reference_type']) op['name'] = TYPE_NAME.get(i['reference_type'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment