Commit 5ef4ee00 authored by xianyang's avatar xianyang

业务类型汇总添加缓存

parent 110d8850
import json
import math
import threading
import time
from concurrent.futures.thread import ThreadPoolExecutor
import pandas as pd
from sqlalchemy.orm import Session
from core.config.env import env
from core.config.env import env, red
from libs.business import TYPE_NAME, GUILD_NAME
from libs.db_link import LinkMysql
from libs.functions import get_now_timestamp, time_str_to_timestamp, \
......@@ -750,42 +751,48 @@ class HomePageDisplay(object):
def get_month_data(self):
acc_sql = "select unique_tag,uuid from fi_account"
guild_sql = "select uuid from guild"
count_sql = f"select count(id) as num from {self.date}"
with ThreadPoolExecutor(max_workers=2) as pool:
future1 = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, acc_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()
guild_data = future2.result()
count = future3.result()
account = [i['uuid'] for i in acc_data]
guild = [i['uuid'] for i in guild_data]
assets_cond = ['type in(0,1)']
if self.unique_tag == 'guild_account':
assets_cond.append(f" uuid in{tuple(guild)}")
elif self.unique_tag == 'user_account':
assets_cond.append(f" uuid not in{tuple(guild + account)} and amount_type in('consumable','withdrawable')")
elif self.unique_tag == 'knapsack_account':
assets_cond.append(f" amount_type='backpack'")
elif self.unique_tag == 'pledgeDeduction':
assets_cond.append(f" reference_type in('marginRecharge','pledgeDeduction')")
is_data = red.get('account_sum-' + self.unique_tag + '-' + str(self.date))
if not is_data:
acc_sql = "select unique_tag,uuid from fi_account"
guild_sql = "select uuid from guild"
count_sql = f"select count(id) as num from {self.date}"
with ThreadPoolExecutor(max_workers=2) as pool:
future1 = pool.submit(LinkMysql(env.DB_3YV2).query_mysql, acc_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()
guild_data = future2.result()
count = future3.result()
account = [i['uuid'] for i in acc_data]
guild = [i['uuid'] for i in guild_data]
assets_cond = ['type in(0,1)']
if self.unique_tag == 'guild_account':
assets_cond.append(f" uuid in{tuple(guild)}")
elif self.unique_tag == 'user_account':
assets_cond.append(f" uuid not in{tuple(guild + account)} and amount_type in('consumable','withdrawable')")
elif self.unique_tag == 'knapsack_account':
assets_cond.append(f" amount_type='backpack'")
elif self.unique_tag == 'pledgeDeduction':
assets_cond.append(f" reference_type in('marginRecharge','pledgeDeduction')")
else:
acc_uuid = [i['uuid'] for i in acc_data if i['unique_tag'] == self.unique_tag]
if not acc_uuid:
Logger(20).logger.info('没找到系统账户')
return [], 0, 0
assets_cond.append(f" uuid='{acc_uuid[0]}'")
ths_count = math.ceil(count[0]['num']/5000000)
ths = []
for x in range(ths_count):
assets_sql = f"select reference_type,type,sum(cast(amount as FLOAT(3))) as amount from {self.date} where {' and '.join(assets_cond)} GROUP BY reference_type,type LIMIT {x * 5000000},{(x+1) * 5000000}"
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()
redis_dict = {"my_data": self.bus_data}
red.set('account_sum-' + self.unique_tag + '-' + str(self.date), json.dumps(redis_dict), 3600)
else:
acc_uuid = [i['uuid'] for i in acc_data if i['unique_tag'] == self.unique_tag]
if not acc_uuid:
Logger(20).logger.info('没找到系统账户')
return [], 0, 0
assets_cond.append(f" uuid='{acc_uuid[0]}'")
ths_count = math.ceil(count[0]['num']/10000000)
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()
self.bus_data = json.loads(is_data)['my_data']
# 数据分类
income = []
outcome = []
......
......@@ -112,4 +112,4 @@ env = TestingEnv() # 开发环境
redis_data = env.Redis
pool = redis.ConnectionPool(host=redis_data.get("host"), port=redis_data.get("port"), password=redis_data.get("password"),
db=redis_data.get("redis_db"), decode_responses=True)
red = redis.StrictRedis(connection_pool=pool)
\ No newline at end of file
red = redis.StrictRedis(connection_pool=pool)
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