channel_group.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # -*- coding: utf-8 -*-
  2. # Author: dukun
  3. import re
  4. import os
  5. import sys
  6. import time
  7. import json
  8. import pymysql
  9. import hashlib
  10. import requests
  11. import pandas as pd
  12. from DBUtils.PooledDB import PooledDB
  13. comment_re = re.compile(
  14. '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
  15. re.DOTALL | re.MULTILINE
  16. )
  17. #解析带注释的json
  18. def parse_json(filename):
  19. """ Parse a JSON file
  20. First remove comments and then use the json module package
  21. Comments look like :
  22. // ...
  23. or
  24. /*
  25. ...
  26. */
  27. """
  28. with open(filename,encoding='utf8') as f:
  29. content = ''.join(f.readlines())
  30. ## Looking for comments
  31. match = comment_re.search(content)
  32. while match:
  33. # single line comment
  34. content = content[:match.start()] + content[match.end():]
  35. match = comment_re.search(content)
  36. #print(content)
  37. # Return json file
  38. return json.loads(content)
  39. def changeChannelGroup():
  40. que_sql = """ SELECT seq_id FROM channel_to_group WHERE channel_group_id = {} AND channel_seq_id = {} AND weight >= 45"""
  41. que_sql_45 = """ SELECT seq_id FROM channel_to_group WHERE channel_group_id = {} AND channel_seq_id = {} AND weight < 45 """
  42. del_sql = """ DELETE FROM channel_to_group WHERE seq_id = %s"""
  43. up_sql = """ UPDATE channel_to_group SET weight = %s WHERE seq_id = %s """
  44. ins_sql = """ INSERT INTO channel_to_group VALUES (0,%s,%s,%s) """
  45. json_data = parse_json(r'./group.json')
  46. #print(json_data)
  47. cursor = conn.cursor()
  48. #print(cursor)
  49. #start = time.time()
  50. for item in json_data:
  51. channel_group_id = item['channel_group_id']
  52. for channel in item['channels']:
  53. channel_seq_id = channel['channel_seq_id']
  54. count = channel['run_count']
  55. weight = channel['low_weight']
  56. temp_que_sql = que_sql.format(channel_group_id,channel_seq_id)
  57. temp_que_sql_45 = que_sql_45.format(channel_group_id,channel_seq_id)
  58. df = pd.DataFrame()
  59. if(weight >= 45):
  60. df = pd.read_sql(temp_que_sql,conn)
  61. #print(" +40===================={}".format(len(df)))
  62. else :
  63. df = pd.read_sql(temp_que_sql_45,conn)
  64. #print(" <40********************{}".format(len(df)))
  65. #print(df)
  66. #print(df)
  67. que_count = len(df)
  68. #print('---que_count---:{}'.format(que_count))
  69. #print(que_count)
  70. ins_list = []
  71. up_list = []
  72. del_list = []
  73. try:
  74. if(count == que_count):
  75. #up_list = []
  76. for i in range(count):
  77. up_list.append((weight + i,df['seq_id'].values.tolist()[i]))
  78. cursor.executemany(up_sql,up_list)
  79. elif( count > que_count):
  80. #print('cuunt:{}'.format(count))
  81. #print('que_count:{}'.format(que_count))
  82. num = count - que_count
  83. #print('-----num----:{}'.format(num))
  84. #ins_list = []
  85. #up_list = []
  86. for i in range(num):
  87. ins_list.append((channel_group_id,channel_seq_id,weight + i))
  88. cursor.executemany(ins_sql,ins_list)
  89. temp_df = pd.DataFrame()
  90. if(weight >= 45):
  91. temp_df = pd.read_sql(temp_que_sql,conn)
  92. else :
  93. #print(temp_que_sql_45)
  94. temp_df = pd.read_sql(temp_que_sql_45,conn)
  95. for i in range(count):
  96. up_list.append((weight + i,temp_df['seq_id'].values.tolist()[i]))
  97. cursor.executemany(up_sql,up_list)
  98. else:
  99. for i in range(count):
  100. up_list.append((weight + i,df['seq_id'].values.tolist()[i]))
  101. cursor.executemany(up_sql,up_list)
  102. for i in range(count,que_count):
  103. del_list.append(df['seq_id'].values.tolist()[i])
  104. #print(del_list)
  105. cursor.executemany(del_sql,del_list)
  106. except:
  107. conn.rollback()
  108. print ("数据回滚")
  109. cursor.close()
  110. conn.close()
  111. #创建数据库连接池
  112. def createPool(db_config):
  113. spool = PooledDB(pymysql, 5, **db_config)
  114. return spool
  115. if __name__ == '__main__':
  116. #数据库连接信息
  117. chanel_db_config = {
  118. 'host' : '47.95.217.180',
  119. 'port' : 3306,
  120. 'user' : 'root',
  121. 'password' : '93DkChZMgZRyCbWh',
  122. 'db' : 'fmp',
  123. 'charset' : 'utf8',
  124. 'autocommit' : 1
  125. }
  126. #数据库连接信息
  127. test_db_config = {
  128. 'host' : '192.168.101.7',
  129. 'port' : 3306,
  130. 'user' : 'root',
  131. 'password' : 'nibuzhidaowozhidao',
  132. 'db' : 'fmp',
  133. 'charset' : 'utf8',
  134. 'autocommit' : 1
  135. }
  136. conn = createPool(chanel_db_config).connection()
  137. changeChannelGroup()