添加数据库异常判断

This commit is contained in:
workpc 2024-12-03 14:57:47 +08:00
parent 08c10b19b9
commit 7099e71b5c

View File

@ -139,24 +139,27 @@ def predict_main():
sqlitedb.create_table('most_model', columns="ds datetime, most_common_model TEXT") sqlitedb.create_table('most_model', columns="ds datetime, most_common_model TEXT")
sqlitedb.insert_data('most_model', (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), most_common_model,), columns=('ds', 'most_common_model',)) sqlitedb.insert_data('most_model', (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), most_common_model,), columns=('ds', 'most_common_model',))
if is_weekday: try:
logger.info('今天是周一,更新预测模型') if is_weekday:
# 上传预警信息到数据库 logger.info('今天是周一,更新预测模型')
warning_data_df = df_zhibiaoliebiao.copy() # 上传预警信息到数据库
warning_data_df = warning_data_df[warning_data_df['停更周期']> 3 ][['指标名称', '指标id', '频度','更新周期','指标来源','最后更新时间','停更周期']] warning_data_df = df_zhibiaoliebiao.copy()
# 重命名列名 warning_data_df = warning_data_df[warning_data_df['停更周期']> 3 ][['指标名称', '指标id', '频度','更新周期','指标来源','最后更新时间','停更周期']]
warning_data_df = warning_data_df.rename(columns={'指标名称': 'INDICATOR_NAME', '指标id': 'INDICATOR_ID', '频度': 'FREQUENCY', '更新周期': 'UPDATE_FREQUENCY', '指标来源': 'DATA_SOURCE', '最后更新时间': 'LAST_UPDATE_DATE', '停更周期': 'UPDATE_SUSPENSION_CYCLE'}) # 重命名列名
from sqlalchemy import create_engine warning_data_df = warning_data_df.rename(columns={'指标名称': 'INDICATOR_NAME', '指标id': 'INDICATOR_ID', '频度': 'FREQUENCY', '更新周期': 'UPDATE_FREQUENCY', '指标来源': 'DATA_SOURCE', '最后更新时间': 'LAST_UPDATE_DATE', '停更周期': 'UPDATE_SUSPENSION_CYCLE'})
engine = create_engine(f'mysql+pymysql://{dbusername}:{password}@{host}:{port}/{dbname}') from sqlalchemy import create_engine
warning_data_df['WARNING_DATE'] = datetime.date.today().strftime("%Y-%m-%d") engine = create_engine(f'mysql+pymysql://{dbusername}:{password}@{host}:{port}/{dbname}')
# 插入数据之前查询表数据然后新增id列 warning_data_df['WARNING_DATE'] = datetime.date.today().strftime("%Y-%m-%d")
existing_data = pd.read_sql(f"SELECT * FROM {table_name}", engine) # 插入数据之前查询表数据然后新增id列
if not existing_data.empty: existing_data = pd.read_sql(f"SELECT * FROM {table_name}", engine)
max_id = existing_data['id'].max() if not existing_data.empty:
warning_data_df['id'] = range(max_id + 1, max_id + 1 + len(warning_data_df)) max_id = existing_data['id'].max()
else: warning_data_df['id'] = range(max_id + 1, max_id + 1 + len(warning_data_df))
warning_data_df['id'] = range(1, 1 + len(warning_data_df)) else:
warning_data_df.to_sql(table_name, con=engine, if_exists='append', index=False) warning_data_df['id'] = range(1, 1 + len(warning_data_df))
warning_data_df.to_sql(table_name, con=engine, if_exists='append', index=False)
except:
logger.info('上传预警信息到数据库失败')
if is_corr: if is_corr: