# coding:utf-8
"""
更新事件表视图
"""
import json
import os

from settings import settings

game = ''
db = settings.GAME
svrid_file = f'{game}_svrid.json'
server_list_url = f'http://gametools.legu.cc/?app=api&act=getServerList&game={game}'

import pandas as pd
from clickhouse_driver import Client

client = Client(**settings.CK_CONFIG)

df = pd.read_json(server_list_url)
df = df[~df['hostname'].isin(['119.3.89.14', '119.3.105.109'])]
serverid = tuple((str(i) for i in df['serverid'].to_list()))

# if os.path.exists(svrid_file):
#     with open(svrid_file, 'r') as f:
#         old_svrid = json.load(f)
#         if set(old_svrid) == set(serverid):
#             exit(0)

sql = f"""drop table if exists {db}.event_view"""
res1 = client.execute(sql)
# 筛选有效数据
sql = f"""create view {db}.event_view as
select *
from {db}.event
where (`#os`is null or lower(`#os`) != 'windows')
  and svrindex in {serverid}
  and not startsWith(`orderid`,'debugPay')
  """
res2 = client.execute(sql)
with open(svrid_file, 'w') as f:
    json.dump(sorted(serverid), f)
