Compare-PBI-Data/app.py
2024-12-22 16:46:52 +08:00

139 lines
4.9 KiB
Python

from flask import Flask, request, render_template, redirect, url_for
import os
import threading
import config
from getDataFromAS import ASDataFetcher
from getQueries import getQueries_From_json
from compareData import Comparator
app = Flask(__name__)
result = ""
@app.route('/')
def index():
global result
return render_template('index.html', config=config, result=result)
@app.route('/update_config', methods=['POST'])
def update_config():
for key, value in request.form.items():
if hasattr(config, key) and key not in ['CURRENT_DIR_PATH', 'os', 'NET_FOLDER']:
setattr(config, key, value)
return redirect(url_for('index'))
@app.route('/run', methods=['POST'])
def run():
global result
result = ""
# 拼接导出页面查询json文件夹路径
json_folder_path = os.path.join(config.CURRENT_DIR_PATH, "Export_Json")
# 拼接查询结果excel文件夹路径
result_folder_path = os.path.join(config.CURRENT_DIR_PATH, "Evaluate_Result")
# 检查文件夹是否存在,如果不存在则创建它
if not os.path.exists(json_folder_path):
os.makedirs(json_folder_path)
if not os.path.exists(result_folder_path):
os.makedirs(result_folder_path)
# 拼接导出页面查询json文件路径
export_page_json_path_1 = os.path.join(json_folder_path, config.EXPORT_PAGE_JSON_NAME_1)
export_page_json_path_2 = os.path.join(json_folder_path, config.EXPORT_PAGE_JSON_NAME_2)
# 读取json文件中的查询
queries_1 = getQueries_From_json(export_page_json_path_1)
queries_2 = getQueries_From_json(export_page_json_path_2)
# 创建 ASDataFetcher 实例
fetcher_1 = ASDataFetcher(
workspace=config.WORKSPACE_1,
username=config.USERNAME_1,
password=config.PASSWORD_1,
queries=queries_1,
json_name=config.EXPORT_PAGE_JSON_NAME_1,
result_folder_path=result_folder_path,
isAdmin=config.IS_ADMIN_1,
catalog=config.CATALOG_1,
effective_username=config.EFFECTIVE_USERNAME_1,
customdata=config.CUSTOMER_DATA_1,
role=config.ROLE_1,
model_number=1,
)
# 创建 ASDataFetcher 实例
fetcher_2 = ASDataFetcher(
workspace=config.WORKSPACE_2,
username=config.USERNAME_2,
password=config.PASSWORD_2,
queries=queries_2,
json_name=config.EXPORT_PAGE_JSON_NAME_2,
result_folder_path=result_folder_path,
isAdmin=config.IS_ADMIN_2,
catalog=config.CATALOG_2,
effective_username=config.EFFECTIVE_USERNAME_2,
customdata=config.CUSTOMER_DATA_2,
role=config.ROLE_2,
model_number=2,
)
# 定义线程任务
def run_fetcher(fetcher, print_lock):
global result
try:
fetcher.writeToExcel(print_lock)
with print_lock:
result += f"\n-------------------------\n{fetcher.json_name} 查询完成\n"
result += "\n".join(fetcher.log_messages) + "\n"
except Exception as e:
with print_lock:
result += f"错误提示: {e}\n"
result += "\n".join(fetcher.log_messages) + "\n"
return
# 创建锁
print_lock = threading.Lock()
# 创建线程
thread_1 = threading.Thread(target=run_fetcher, args=(fetcher_1, print_lock))
thread_2 = threading.Thread(target=run_fetcher, args=(fetcher_2, print_lock))
try:
result += "---*********************************************************---\n"
result += " 开始查询\n"
result += "---*********************************************************---\n"
# 启动线程
thread_1.start()
thread_2.start()
# 等待线程完成
thread_1.join()
thread_2.join()
result += "\n---*********************************************************---\n"
result += " 查询完成\n"
result += "---*********************************************************---\n"
except Exception as e:
result += f"错误提示: {e}\n"
return redirect(url_for('index'))
try:
result += "\n---------------------------------------------------------------\n"
result += " 开始比较\n"
result += "---------------------------------------------------------------\n"
comparator = Comparator(fetcher_1.result_full_excel_name, fetcher_2.result_full_excel_name)
comparator.compare_ExcelFiles()
result += "\n".join(comparator.log_messages) + "\n"
result += "\n---------------------------------------------------------------\n"
result += " 比较完成\n"
result += "---------------------------------------------------------------\n"
except Exception as e:
result += f"错误提示: {e}\n"
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)