PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/tasks/bi_user_statistic/user_game_records.py

https://bitbucket.org/fanhaipeng/bi-backend
Python | 907 lines | 893 code | 9 blank | 5 comment | 1 complexity | 2ba332f4297fc1c20ba4c4dde4206be5 MD5 | raw file
  1. from itertools import groupby
  2. from arrow import Arrow
  3. from operator import itemgetter
  4. from sqlalchemy import text, and_
  5. from sqlalchemy.sql.expression import bindparam
  6. from app.extensions import db
  7. from app.models.bi import BIUserStatisticSharding
  8. from app.tasks import with_db_context
  9. from app.utils import generate_sql_date, get_timezone_offset_of_from_Beijing_to_EST
  10. timezone_offset_of_from_Beijing_to_EST = get_timezone_offset_of_from_Beijing_to_EST()
  11. def process_bi_user_statistic_game_records(target):
  12. today, someday, index_time, timezone_offset = generate_sql_date(target)
  13. someday_arrow = Arrow.strptime(someday, '%Y-%m-%d')
  14. start_time = someday_arrow.replace(hours=timezone_offset_of_from_Beijing_to_EST).format('YYYY-MM-DD HH:mm:ss')
  15. end_time = someday_arrow.replace(days=1, hours=timezone_offset_of_from_Beijing_to_EST).format('YYYY-MM-DD HH:mm:ss')
  16. # ring_game 的抽水, 分两部分
  17. # 对于beginner层次的从tj_cgz_flow_userpaninfo的scharges获取,根据每局的大盲值 ,抽取服务费
  18. # 对于其他层次的从tj_super_game_scharges_record获取根据底池大小,抽取服务费
  19. # 其他优秀牌局这个概念适用于ring_game
  20. #
  21. def collection_user_ring_game_rake(connection, transaction):
  22. if target == 'lifetime':
  23. return connection.execute(text("""
  24. SELECT stats_date username,
  25. Sum(rake) AS rake
  26. FROM (SELECT TO_CHAR(CAST(CAST(time_update AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd') AS
  27. stats_date,
  28. rolename
  29. AS username,
  30. Sum(pay_num)
  31. AS rake
  32. FROM ogdzweb.tj_super_game_scharges_record
  33. GROUP BY TO_CHAR(CAST(CAST(time_update AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd') ,
  34. rolename
  35. UNION
  36. SELECT TO_CHAR(CAST(CAST(cgz.time_update AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  37. AS stats_date,
  38. cgz.username AS username,
  39. Sum(cgz.scharges) AS rake
  40. FROM ogdzweb.tj_cgz_flow_userpaninfo cgz
  41. GROUP BY cgz.username,
  42. TO_CHAR(CAST(CAST(cgz.time_update AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  43. ) a
  44. GROUP BY stats_date,
  45. a.username
  46. """), timezone_offset=timezone_offset)
  47. else:
  48. return connection.execute(text("""
  49. SELECT a.username AS username,
  50. SUM(a.rake) AS rake
  51. FROM (SELECT rolename AS username,
  52. SUM(pay_num) AS rake
  53. FROM ogdzweb.tj_super_game_scharges_record
  54. WHERE time_update <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  55. AND time_update >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  56. GROUP BY rolename
  57. UNION
  58. SELECT cgz.username AS username,
  59. SUM(cgz.scharges) AS rake
  60. FROM ogdzweb.tj_cgz_flow_userpaninfo cgz
  61. WHERE time_update <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  62. AND time_update >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  63. GROUP BY cgz.username) a
  64. GROUP BY a.username
  65. """), end_time=end_time, start_time=start_time)
  66. ring_game_rake_records = with_db_context(db, collection_user_ring_game_rake, bind='orig_wpt_ods')
  67. if target == 'lifetime':
  68. rows = [{'_stats_date': row['stats_date'], '_username': row['username'], 'rake': row['rake']} for row in
  69. ring_game_rake_records]
  70. else:
  71. rows = [{'_stats_date': someday, '_username': row['username'], 'rake': row['rake']} for
  72. row in ring_game_rake_records]
  73. if rows:
  74. def sync_collection_user_ring_game_rake(connection, transaction, stats_date, group_rows):
  75. someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  76. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  77. someday_bi_user_statistic.__table__.c.username == bindparam('_username'))
  78. values = {'ring_rake': bindparam('rake')}
  79. try:
  80. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  81. list(group_rows))
  82. except:
  83. print(
  84. 'process bi_user_statistic_{} about ring game rake records related transaction.rollback()'.format(
  85. str(stats_date)))
  86. transaction.rollback()
  87. raise
  88. else:
  89. transaction.commit()
  90. print('process bi_user_statistic_{} about ring game rake related transaction.commit()'.format(
  91. str(stats_date)))
  92. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  93. for stats_date, group_rows in rows_group_by_states:
  94. with_db_context(db, sync_collection_user_ring_game_rake, stats_date=str(stats_date), group_rows=group_rows,
  95. bind='bi_user_statistic')
  96. def collection_user_ring_game_hands(connection, transaction):
  97. if target == 'lifetime':
  98. return connection.execute(text("""
  99. SELECT TO_CHAR(CAST(CAST(cgz.time_update AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  100. AS stats_date,
  101. cgz.username AS username,
  102. Count(DISTINCT cgz.pan_id) AS hands
  103. FROM ogdzweb.tj_cgz_flow_userpaninfo cgz
  104. GROUP BY TO_CHAR(CAST(CAST(cgz.time_update AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd'),
  105. cgz.username;
  106. """), timezone_offset=timezone_offset)
  107. else:
  108. return connection.execute(text("""
  109. SELECT cgz.username AS username,
  110. COUNT(DISTINCT cgz.pan_id) AS hands
  111. FROM ogdzweb.tj_cgz_flow_userpaninfo cgz
  112. WHERE time_update <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  113. AND time_update >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  114. GROUP BY cgz.username
  115. """), end_time=end_time, start_time=start_time)
  116. ring_game_hands_records = with_db_context(db, collection_user_ring_game_hands, bind='orig_wpt_ods')
  117. if target == 'lifetime':
  118. rows = [{'_stats_date': row['stats_date'], '_username': row['username'], 'hands': row['hands']} for row in
  119. ring_game_hands_records]
  120. else:
  121. rows = [{'_stats_date': someday, '_username': row['username'], 'hands': row['hands']} for
  122. row in ring_game_hands_records]
  123. if rows:
  124. def sync_collection_user_ring_game_hands(connection, transaction, stats_date, group_rows):
  125. someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  126. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  127. someday_bi_user_statistic.__table__.c.username == bindparam('_username'))
  128. values = {'ring_hands': bindparam('hands')}
  129. try:
  130. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  131. list(group_rows))
  132. except:
  133. print(
  134. 'process bi_user_statistic_{} about ring game hands records related transaction.rollback()'.format(
  135. str(stats_date)))
  136. transaction.rollback()
  137. raise
  138. else:
  139. transaction.commit()
  140. print('process bi_user_statistic_{} about ring game hands related transaction.commit()'.format(
  141. str(stats_date)))
  142. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  143. for stats_date, group_rows in rows_group_by_states:
  144. with_db_context(db, sync_collection_user_ring_game_hands, stats_date=str(stats_date), group_rows=group_rows,
  145. bind='bi_user_statistic')
  146. def collection_sng_and_mtt_buyins_and_rake(connection, transaction, game_type_id):
  147. if target == 'lifetime':
  148. return connection.execute(text("""
  149. SELECT usersign.username
  150. username,
  151. TO_CHAR(CAST(CAST(usersign.time AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  152. AS stats_date,
  153. Sum(CASE
  154. WHEN usersign.type = 2 THEN usersign.sign_totals
  155. WHEN usersign.type = 1 THEN usersign.sign_totals * -1
  156. ELSE 0
  157. END)
  158. AS buy_ins,
  159. Sum(CASE
  160. WHEN usersign.type = 2 THEN usersign.tax_totals
  161. WHEN usersign.type = 1 THEN usersign.tax_totals * -1
  162. ELSE 0
  163. END)
  164. AS rake
  165. FROM ogdzweb.tj_flow_usersign usersign
  166. INNER JOIN ogdzweb.tj_matchinfo matchinfo
  167. ON usersign.matchid = matchinfo.matchid
  168. WHERE matchinfo.type = 1
  169. GROUP BY usersign.username,
  170. TO_CHAR(CAST(CAST(usersign.time AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  171. """), timezone_offset=timezone_offset, game_type_id=game_type_id)
  172. else:
  173. return connection.execute(text("""
  174. SELECT usersign.username username,
  175. SUM(CASE
  176. WHEN usersign.type = 2 THEN usersign.sign_totals
  177. WHEN usersign.type = 1 THEN usersign.sign_totals * -1
  178. ELSE 0
  179. END) AS buy_ins,
  180. SUM(CASE
  181. WHEN usersign.type = 2 THEN usersign.tax_totals
  182. WHEN usersign.type = 1 THEN usersign.tax_totals * -1
  183. ELSE 0
  184. END) AS rake
  185. FROM ogdzweb.tj_flow_usersign usersign
  186. INNER JOIN ogdzweb.tj_matchinfo matchinfo
  187. ON usersign.matchid = matchinfo.matchid
  188. WHERE matchinfo.type = 1
  189. AND usersign.time <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  190. AND usersign.time >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  191. GROUP BY usersign.username
  192. """), end_time=end_time, start_time=start_time, game_type_id=game_type_id)
  193. def collection_winnings_records(connection, transaction, game_type_id):
  194. if target == 'lifetime':
  195. return connection.execute(text("""
  196. SELECT userreward.username AS username,
  197. TO_CHAR(CAST(CAST(userreward.time AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  198. AS stats_date,
  199. Sum(CASE
  200. WHEN userreward.rewardtype = 3 THEN userreward.totals
  201. ELSE 0
  202. END) AS winnings
  203. FROM ogdzweb.tj_flow_userreward userreward
  204. INNER JOIN ogdzweb.tj_matchinfo matchinfo
  205. ON matchinfo.matchid = userreward.matchid
  206. WHERE matchinfo.type = :game_type_id
  207. GROUP BY userreward.username,
  208. TO_CHAR(CAST(CAST(userreward.time AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  209. """), timezone_offset=timezone_offset, game_type_id=game_type_id)
  210. else:
  211. return connection.execute(text(""" SELECT userreward.username AS username,
  212. SUM(CASE
  213. WHEN userreward.rewardtype = 3 THEN userreward.totals
  214. ELSE 0
  215. END) AS winnings
  216. FROM ogdzweb.tj_flow_userreward userreward
  217. INNER JOIN ogdzweb.tj_matchinfo matchinfo
  218. ON matchinfo.matchid = userreward.matchid
  219. WHERE matchinfo.type = :game_type_id
  220. AND userreward.time <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  221. AND userreward.time >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  222. GROUP BY userreward.username
  223. """), end_time=end_time, start_time=start_time, game_type_id=game_type_id)
  224. if target == 'lifetime':
  225. result_proxy_for_buy_ins_and_rake = []
  226. result_proxy_for_winnings = []
  227. for game_type_id, game_type in [(1, 'sng'), (2, 'mtt')]:
  228. buy_ins_and_rake_records = with_db_context(db, collection_sng_and_mtt_buyins_and_rake,
  229. game_type_id=game_type_id, bind='orig_wpt_ods')
  230. buy_ins_and_rake_records_rows = [
  231. {'_stats_date': row['stats_date'], '_username': row['username'], 'rake': row['rake'],
  232. 'buy_ins': row['buy_ins']} for row in buy_ins_and_rake_records]
  233. buy_ins_and_rake_records_rows_dict = dict([(game_type, buy_ins_and_rake_records_rows)])
  234. result_proxy_for_buy_ins_and_rake.append(buy_ins_and_rake_records_rows_dict)
  235. winnings_records = with_db_context(db, collection_winnings_records, game_type_id=game_type_id,
  236. bind='orig_wpt_ods')
  237. winnings_records_rows = [
  238. {'_stats_date': row['stats_date'], '_username': row['username'], 'winnings': row['winnings']} for row in
  239. winnings_records]
  240. winnings_records_rows_dict = dict([(game_type, winnings_records_rows)])
  241. result_proxy_for_winnings.append(winnings_records_rows_dict)
  242. else:
  243. result_proxy_for_buy_ins_and_rake = []
  244. result_proxy_for_winnings = []
  245. for game_type_id, game_type in [(1, 'sng'), (2, 'mtt')]:
  246. buy_ins_and_rake_records = with_db_context(db, collection_sng_and_mtt_buyins_and_rake,
  247. game_type_id=game_type_id, bind='orig_wpt_ods')
  248. buy_ins_and_rake_records_rows = [
  249. {'_stats_date': someday, '_username': row['username'], 'rake': row['rake'], 'buy_ins': row['buy_ins']}
  250. for row in buy_ins_and_rake_records]
  251. buy_ins_and_rake_records_rows_dict = dict([(game_type, buy_ins_and_rake_records_rows)])
  252. result_proxy_for_buy_ins_and_rake.append(buy_ins_and_rake_records_rows_dict)
  253. winnings_records = with_db_context(db, collection_winnings_records, game_type_id=game_type_id,
  254. bind='orig_wpt_ods')
  255. winnings_records_rows = [{'_stats_date': someday, '_username': row['username'], 'winnings': row['winnings']}
  256. for row in winnings_records]
  257. winnings_records_rows_dict = dict([(game_type, winnings_records_rows)])
  258. result_proxy_for_winnings.append(winnings_records_rows_dict)
  259. for game_buy_ins_and_rake_records_rows_dict in result_proxy_for_buy_ins_and_rake:
  260. for game_type, rows in game_buy_ins_and_rake_records_rows_dict.items():
  261. if rows:
  262. def sync_collection_game_buy_ins_and_rake_records(connection, transaction, stats_date, group_rows):
  263. someday_bi_user_statistic = BIUserStatisticSharding.model(stats_date)
  264. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  265. someday_bi_user_statistic.__table__.c.username == bindparam('_username'))
  266. values = {'{}_gold_buyins'.format(game_type): bindparam('buy_ins'),
  267. '{}_rake'.format(game_type): bindparam('rake')}
  268. try:
  269. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  270. list(group_rows))
  271. except:
  272. print(
  273. 'process bi_user_statistic_{} about {} buy_ins and rake related transaction.rollback()'.format(
  274. str(stats_date), game_type))
  275. transaction.rollback()
  276. raise
  277. else:
  278. transaction.commit()
  279. print(
  280. 'process bi_user_statistic_{} about {} buy_ins and rake related transaction.commit()'.format(
  281. str(stats_date), game_type))
  282. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  283. for stats_date, group_rows in rows_group_by_states:
  284. with_db_context(db, sync_collection_game_buy_ins_and_rake_records, stats_date=str(stats_date),
  285. group_rows=group_rows, bind='bi_user_statistic')
  286. for game_winnings_records_rows_dict in result_proxy_for_winnings:
  287. for game_type, rows in game_winnings_records_rows_dict.items():
  288. if rows:
  289. def sync_collection_game_winnings_records(connection, transaction, stats_date, group_rows):
  290. someday_bi_user_statistic = BIUserStatisticSharding.model(stats_date)
  291. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  292. someday_bi_user_statistic.__table__.c.username == bindparam('_username'))
  293. values = {'{}_gold_winnings'.format(game_type): bindparam('winnings')}
  294. try:
  295. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  296. list(group_rows))
  297. except:
  298. print('process bi_user_statistic_{} about {} winnings related transaction.rollback()'.format(
  299. str(stats_date), game_type))
  300. transaction.rollback()
  301. raise
  302. else:
  303. transaction.commit()
  304. print('process bi_user_statistic_{} about {} winnings related transaction.commit()'.format(
  305. str(stats_date), game_type))
  306. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  307. for stats_date, group_rows in rows_group_by_states:
  308. with_db_context(db, sync_collection_game_winnings_records, stats_date=str(stats_date),
  309. group_rows=group_rows, bind='bi_user_statistic')
  310. def collection_user_sng_entries(connection, transaction):
  311. if target == 'lifetime':
  312. return connection.execute(text("""
  313. SELECT TO_CHAR(CAST(CAST(m.time_begin AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  314. AS stats_date,
  315. u.username
  316. username,
  317. Sum(CASE
  318. WHEN u.type = 1 THEN 1
  319. WHEN u.type = 2 THEN -1
  320. ELSE 0
  321. END)
  322. AS entries
  323. FROM ogdzweb.tj_flow_usersign u
  324. LEFT JOIN ogdzweb.tj_matchinfo m
  325. ON u.matchid = m.matchid
  326. WHERE m.type = 2
  327. GROUP BY TO_CHAR(CAST(CAST(m.time_begin AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd') ,
  328. username;
  329. """), timezone_offset=timezone_offset)
  330. else:
  331. return connection.execute(text("""
  332. SELECT SUM(CASE
  333. WHEN u.type = 1 THEN 1
  334. WHEN u.type = 2 THEN -1
  335. ELSE 0
  336. END) AS entries,
  337. u.username username
  338. FROM OGDZWEB.tj_flow_usersign u
  339. LEFT JOIN OGDZWEB.tj_matchinfo m ON u.matchid = m.matchid
  340. WHERE m.type = 1
  341. AND m.time_begin <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  342. AND m.time_begin >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  343. GROUP BY username
  344. """), end_time=end_time, start_time=start_time)
  345. sng_entries_records = with_db_context(db, collection_user_sng_entries, bind='orig_wpt_ods')
  346. if target == 'lifetime':
  347. rows = [{'_stats_date': row['stats_date'], '_username': row['username'], 'entries': row['entries']} for row in
  348. sng_entries_records if row['stats_date'] is not None]
  349. else:
  350. rows = [{'_stats_date': someday, '_username': row['username'], 'entries': row['entries']} for row in
  351. sng_entries_records]
  352. if rows:
  353. def sync_collection_user_sng_entries(connection, transaction, stats_date, group_rows):
  354. someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  355. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  356. someday_bi_user_statistic.__table__.c.username == bindparam('_username'))
  357. values = {'sng_gold_entries': bindparam('entries')}
  358. try:
  359. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  360. list(group_rows))
  361. except:
  362. print('process bi_user_statistic_{} about sng entries related transaction.rollback()'.format(
  363. str(stats_date)))
  364. transaction.rollback()
  365. raise
  366. else:
  367. transaction.commit()
  368. print('process bi_user_statistic_{} about sng entries related transaction.commit()'.format(
  369. str(stats_date)))
  370. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  371. for stats_date, group_rows in rows_group_by_states:
  372. with_db_context(db, sync_collection_user_sng_entries, stats_date=str(stats_date), group_rows=group_rows,
  373. bind='bi_user_statistic')
  374. def collection_user_mtt_entries(connection, transaction):
  375. if target == 'lifetime':
  376. return connection.execute(text("""
  377. SELECT TO_CHAR(CAST(CAST(m.time_begin AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  378. AS stats_date,
  379. u.username username,
  380. SUM(CASE
  381. WHEN u.type = 1 THEN 1
  382. WHEN u.type = 2 THEN -1
  383. ELSE 0
  384. END) AS entries
  385. FROM OGDZWEB.tj_flow_usersign u
  386. LEFT JOIN OGDZWEB.tj_matchinfo m
  387. ON u.matchid = m.matchid
  388. WHERE m.type = 2
  389. GROUP BY TO_CHAR(CAST(CAST(m.time_begin AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd'),
  390. username;
  391. """), timezone_offset=timezone_offset)
  392. else:
  393. return connection.execute(text("""
  394. SELECT SUM(CASE
  395. WHEN u.type = 1 THEN 1
  396. WHEN u.type = 2 THEN -1
  397. ELSE 0
  398. END) AS entries,
  399. u.username username
  400. FROM OGDZWEB.tj_flow_usersign u
  401. LEFT JOIN OGDZWEB.tj_matchinfo m ON u.matchid = m.matchid
  402. WHERE m.type = 2
  403. AND m.time_begin <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  404. AND m.time_begin >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  405. GROUP BY username
  406. """), end_time=end_time, start_time=start_time)
  407. mtt_entries_records = with_db_context(db, collection_user_mtt_entries, bind='orig_wpt_ods')
  408. if target == 'lifetime':
  409. rows = [{'_stats_date': row['stats_date'], '_username': row['username'], 'entries': row['entries']} for row in
  410. mtt_entries_records if row['stats_date'] is not None]
  411. else:
  412. rows = [{'_stats_date': someday, '_username': row['username'], 'entries': row['entries']} for row in
  413. mtt_entries_records]
  414. if rows:
  415. def sync_collection_user_mtt_entries(connection, transaction, stats_date, group_rows):
  416. someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  417. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  418. someday_bi_user_statistic.__table__.c.username == bindparam('_username'))
  419. values = {'mtt_gold_entries': bindparam('entries')}
  420. try:
  421. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  422. list(group_rows))
  423. except:
  424. print('process bi_user_statistic_{} about mtt entries related transaction.rollback()'.format(
  425. str(stats_date)))
  426. transaction.rollback()
  427. raise
  428. else:
  429. transaction.commit()
  430. print('process bi_user_statistic_{} about mtt entries related transaction.commit()'.format(
  431. str(stats_date)))
  432. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  433. for stats_date, group_rows in rows_group_by_states:
  434. with_db_context(db, sync_collection_user_mtt_entries, stats_date=str(stats_date), group_rows=group_rows,
  435. bind='bi_user_statistic')
  436. def collection_user_slots_spins_records(connection, transaction):
  437. if target == 'lifetime':
  438. return connection.execute(text("""
  439. SELECT username
  440. AS
  441. og_account,
  442. TO_CHAR(CAST(CAST(recdate AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  443. AS stats_date,
  444. Count(*)
  445. AS slots_spins
  446. FROM gl.gamecoin_detail
  447. WHERE DATE (CONVERT_TZ(recdate, '+08:00', :timezone_offset)) >= '2016-06-01'
  448. GROUP BY username,
  449. TO_CHAR(CAST(CAST(recdate AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  450. )
  451. """), timezone_offset=timezone_offset)
  452. else:
  453. return connection.execute(text("""
  454. SELECT username AS og_account,
  455. COUNT(*) AS slots_spins
  456. FROM gl.gamecoin_detail
  457. WHERE recdate <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  458. AND recdate >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  459. GROUP BY username
  460. """), end_time=end_time, start_time=start_time)
  461. slots_related_records = with_db_context(db, collection_user_slots_spins_records, bind='orig_wpt_ods')
  462. if target == 'lifetime':
  463. rows = [{'_stats_date': row['stats_date'], '_og_account': row['og_account'], 'slots_spins': row['slots_spins']}
  464. for row in slots_related_records if row['stats_date'] is not None]
  465. else:
  466. rows = [{'_stats_date': someday, '_og_account': row['og_account'], 'slots_spins': row['slots_spins']} for row in
  467. slots_related_records]
  468. if rows:
  469. def sync_collection_user_slots_spins(connection, transaction, stats_date, group_rows):
  470. someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  471. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  472. someday_bi_user_statistic.__table__.c.og_account == bindparam('_og_account'))
  473. values = {'slots_spins': bindparam('slots_spins')}
  474. try:
  475. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  476. list(group_rows))
  477. except:
  478. print('process bi_user_statistic_{} about slots spins records transaction.rollback()'.format(
  479. str(stats_date)))
  480. transaction.rollback()
  481. raise
  482. else:
  483. transaction.commit()
  484. print('process bi_user_statistic_{} about slots spins records transaction.commit()'.format(
  485. str(stats_date)))
  486. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  487. for stats_date, group_rows in rows_group_by_states:
  488. with_db_context(db, sync_collection_user_slots_spins, stats_date=str(stats_date), group_rows=group_rows,
  489. bind='bi_user_statistic')
  490. def collection_user_slots_winnings(connection, transaction):
  491. if target == 'lifetime':
  492. return connection.execute(text("""
  493. SELECT username AS og_account,
  494. TO_CHAR(CAST(CAST(recdate AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd') AS stats_date,
  495. SUM(gamecoin) AS slots_winnings
  496. FROM gl.gamecoin_detail
  497. WHERE gamecoin > 0
  498. GROUP BY username,
  499. TO_CHAR(CAST(CAST(recdate AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  500. """), timezone_offset=timezone_offset)
  501. else:
  502. return connection.execute(text("""
  503. SELECT username AS og_account,
  504. SUM(gamecoin) AS slots_winnings
  505. FROM gl.gamecoin_detail
  506. WHERE gamecoin > 0
  507. AND recdate <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  508. AND recdate >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  509. GROUP BY username
  510. """), end_time=end_time, start_time=start_time)
  511. slots_related_records = with_db_context(db, collection_user_slots_winnings, bind='orig_wpt_ods')
  512. if target == 'lifetime':
  513. rows = [{'_stats_date': row['stats_date'], '_og_account': row['og_account'],
  514. 'slots_winnings': row['slots_winnings']} for row in slots_related_records if
  515. row['stats_date'] is not None]
  516. else:
  517. rows = [{'_stats_date': someday, '_og_account': row['og_account'], 'slots_winnings': row['slots_winnings']} for
  518. row in slots_related_records]
  519. if rows:
  520. def sync_collection_user_slots_related(connection, transaction, stats_date, group_rows):
  521. someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  522. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  523. someday_bi_user_statistic.__table__.c.og_account == bindparam('_og_account'))
  524. values = {'slots_winnings': bindparam('slots_winnings')}
  525. try:
  526. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  527. list(group_rows))
  528. except:
  529. print('process bi_user_statistic_{} about slots winnings related transaction.rollback()'.format(
  530. str(stats_date)))
  531. transaction.rollback()
  532. raise
  533. else:
  534. transaction.commit()
  535. print('process bi_user_statistic_{} about slots winnings related transaction.commit()'.format(
  536. str(stats_date)))
  537. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  538. for stats_date, group_rows in rows_group_by_states:
  539. with_db_context(db, sync_collection_user_slots_related, stats_date=str(stats_date), group_rows=group_rows,
  540. bind='bi_user_statistic')
  541. def collection_user_slots_wagered(connection, transaction):
  542. if target == 'lifetime':
  543. return connection.execute(text("""
  544. SELECT username AS og_account,
  545. TO_CHAR(CAST(CAST(recdate AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd') AS stats_date,
  546. SUM(gamecoin) AS slots_wagered
  547. FROM gl.gamecoin_detail
  548. WHERE gamecoin < 0
  549. GROUP BY username,
  550. TO_CHAR(CAST(CAST(recdate AS TIMESTAMP) AT TIME ZONE '-8:00' AS TIMESTAMP) AT TIME ZONE 'America/New_York', 'yyyy-mm-dd')
  551. """), timezone_offset=timezone_offset)
  552. else:
  553. return connection.execute(text("""
  554. SELECT username AS og_account,
  555. SUM(gamecoin) AS slots_wagered
  556. FROM gl.gamecoin_detail
  557. WHERE gamecoin < 0
  558. AND recdate <= TO_DATE(:end_time, 'yyyy-mm-dd,hh24:mi:ss')
  559. AND recdate >= TO_DATE(:start_time, 'yyyy-mm-dd,hh24:mi:ss')
  560. GROUP BY username
  561. """), end_time=end_time, start_time=start_time)
  562. slots_related_records = with_db_context(db, collection_user_slots_wagered, bind='orig_wpt_ods')
  563. if target == 'lifetime':
  564. rows = [
  565. {'_stats_date': row['stats_date'], '_og_account': row['og_account'], 'slots_wagered': row['slots_wagered']}
  566. for row in slots_related_records if row['stats_date'] is not None]
  567. else:
  568. rows = [{'_stats_date': someday, '_og_account': row['og_account'], 'slots_wagered': row['slots_wagered']} for
  569. row in slots_related_records]
  570. if rows:
  571. def sync_collection_user_slots_related(connection, transaction, stats_date, group_rows):
  572. someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  573. where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  574. someday_bi_user_statistic.__table__.c.og_account == bindparam('_og_account'))
  575. values = {'slots_wagered': bindparam('slots_wagered')}
  576. try:
  577. connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values),
  578. list(group_rows))
  579. except:
  580. print('process bi_user_statistic_{} about slots wagered related transaction.rollback()'.format(
  581. str(stats_date)))
  582. transaction.rollback()
  583. raise
  584. else:
  585. transaction.commit()
  586. print('process bi_user_statistic_{} about slots wagered related transaction.commit()'.format(
  587. str(stats_date)))
  588. rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  589. for stats_date, group_rows in rows_group_by_states:
  590. with_db_context(db, sync_collection_user_slots_related, stats_date=str(stats_date), group_rows=group_rows,
  591. bind='bi_user_statistic')
  592. #
  593. #
  594. # def collection_user_mtt_rebuy_value_records(connection, transaction):
  595. #
  596. # if target == 'lifetime':
  597. #
  598. # return connection.execute(text("""
  599. # SELECT DISTINCT r.username AS
  600. # og_account,
  601. # DATE(CONVERT_TZ(r.endtime, '+08:00', :timezone_offset)) AS
  602. # stats_date,
  603. # SUM(r.rebuycount * s.sign_totals) AS
  604. # mtt_gold_rebuy_value
  605. # FROM usermatchrecord r
  606. # INNER JOIN tj_matchinfo m
  607. # ON r.matchid = m.matchid
  608. # INNER JOIN tj_flow_usersign s
  609. # ON r.matchid = s.matchid
  610. # WHERE m.type = 2
  611. # AND r.rank IS NOT NULL
  612. # AND r.rank > 0
  613. # GROUP BY og_account,
  614. # stats_date
  615. # """), timezone_offset=timezone_offset)
  616. #
  617. # else:
  618. #
  619. # return connection.execute(text("""
  620. # SELECT DISTINCT r.username AS
  621. # og_account,
  622. # SUM(r.rebuycount * s.sign_totals) AS
  623. # mtt_gold_rebuy_value
  624. # FROM usermatchrecord r
  625. # INNER JOIN tj_matchinfo m
  626. # ON r.matchid = m.matchid
  627. # INNER JOIN tj_flow_usersign s
  628. # ON r.matchid = s.matchid
  629. # WHERE m.type = 2
  630. # AND r.rank IS NOT NULL
  631. # AND r.rank > 0
  632. # AND DATE(CONVERT_TZ(r.endtime, '+08:00', :timezone_offset)) = :stats_date
  633. # GROUP BY og_account
  634. # """), timezone_offset=timezone_offset, stats_date=someday)
  635. #
  636. # result_proxy = with_db_context(db, collection_user_mtt_rebuy_value_records, bind='orig_wpt_ods')
  637. #
  638. # if target == 'lifetime':
  639. #
  640. # rows = [{'_stats_date': row['stats_date'], '_og_account': row['og_account'], 'mtt_gold_rebuy_value': row['mtt_gold_rebuy_value']} for row in result_proxy if row['stats_date'] is not None]
  641. #
  642. # else:
  643. #
  644. # rows = [{'_stats_date': someday, '_og_account': row['og_account'], 'mtt_gold_rebuy_value': row['mtt_gold_rebuy_value']} for row in result_proxy ]
  645. #
  646. # if rows:
  647. #
  648. # def sync_collection_user_mtt_rebuy_values_reocrds(connection, transaction, stats_date, group_rows):
  649. #
  650. # someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  651. #
  652. # where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  653. # someday_bi_user_statistic.__table__.c.og_account == bindparam('_og_account'))
  654. #
  655. # values = [{'mtt_gold_rebuy_value': bindparam('mtt_gold_rebuy_value')}]
  656. #
  657. # try:
  658. # connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values), list(group_rows))
  659. # except:
  660. # print('process bi_user_statistic_{} about mtt rebuy value transaction.rollback'.format(str(stats_date)))
  661. # transaction.rollback()
  662. # raise
  663. # else:
  664. # transaction.commit()
  665. # print('process bi_user_statistic_{} about mtt rebuy value transaction.commit()'.format(str(stats_date)))
  666. #
  667. # rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  668. # for stats_date, group_rows in rows_group_by_states:
  669. #
  670. # with_db_context(db, sync_collection_user_mtt_rebuy_values_reocrds, stats_date = str(stats_date), group_rows=group_rows)
  671. #
  672. #
  673. # def collection_user_mtt_rebuy_count_records(connection, transaction):
  674. #
  675. # if target == 'lifetime':
  676. #
  677. # return connection.execute(text("""
  678. # SELECT DISTINCT r.username AS
  679. # og_account,
  680. # DATE(CONVERT_TZ(r.endtime, '+08:00', :timezone_offset)) AS
  681. # stats_date,
  682. # SUM(rebuycount) AS
  683. # mtt_gold_rebuy_count
  684. # FROM usermatchrecord r
  685. # INNER JOIN tj_matchinfo m
  686. # ON r.matchid = m.matchid
  687. # WHERE m.type = 2
  688. # AND r.rank IS NOT NULL
  689. # AND r.rank > 0
  690. # GROUP BY og_account,
  691. # stats_date
  692. # """), timezone_offset=timezone_offset)
  693. #
  694. # else:
  695. #
  696. # return connection.execute(text("""
  697. # SELECT DISTINCT r.username AS og_account,
  698. # SUM(rebuycount) AS mtt_gold_rebuy_count
  699. # FROM usermatchrecord r
  700. # INNER JOIN tj_matchinfo m
  701. # ON r.matchid = m.matchid
  702. # WHERE m.type = 2
  703. # AND r.rank IS NOT NULL
  704. # AND r.rank > 0
  705. # AND DATE(CONVERT_TZ(r.endtime, '+08:00', :timezone_offset)) = :stats_date
  706. # GROUP BY og_account
  707. # """), timezone_offset=timezone_offset, stats_date=someday)
  708. #
  709. # result_proxy = with_db_context(db, collection_user_mtt_rebuy_count_records, bind='orig_wpt_ods')
  710. #
  711. # if target == 'lifetime':
  712. #
  713. # rows = [{'_stats_date': row['stats_date'], '_og_account': row['og_account'], 'mtt_gold_rebuy_value': row['mtt_gold_rebuy_count']} for row in result_proxy if row['stats_date'] is not None]
  714. #
  715. # else:
  716. #
  717. # rows = [{'_stats_date': someday, '_og_account': row['og_account'], 'mtt_gold_rebuy_value': row['mtt_gold_rebuy_count']} for row in result_proxy ]
  718. #
  719. # if rows:
  720. #
  721. # def sync_collection_user_mtt_rebuy_count_reocrds(connection, transaction, stats_date, group_rows):
  722. #
  723. # someday_bi_user_statistic = BIUserStatisticSharding.model(str(stats_date))
  724. #
  725. # where = and_(someday_bi_user_statistic.__table__.c.stats_date == bindparam('_stats_date'),
  726. # someday_bi_user_statistic.__table__.c.og_account == bindparam('_og_account'))
  727. #
  728. # values = [{'mtt_gold_rebuy_count': bindparam('mtt_gold_rebuy_count')}]
  729. #
  730. # try:
  731. # connection.execute(someday_bi_user_statistic.__table__.update().where(where).values(values), list(group_rows))
  732. # except:
  733. # print('process bi_user_statistic_{} about mtt rebuy count transaction.rollback'.format(str(stats_date)))
  734. # transaction.rollback()
  735. # raise
  736. # else:
  737. # transaction.commit()
  738. # print('process bi_user_statistic_{} about mtt rebuy count transaction.commit()'.format(str(stats_date)))
  739. #
  740. # rows_group_by_states = groupby(sorted(rows, key=itemgetter('_stats_date')), itemgetter('_stats_date'))
  741. # for stats_date, group_rows in rows_group_by_states:
  742. #
  743. # with_db_context(db, sync_collection_user_mtt_rebuy_count_reocrds, stats_date = str(stats_date), group_rows=group_rows)