From 7099e71b5cc158d8e5dd21c4a496e935451f120c Mon Sep 17 00:00:00 2001 From: workpc Date: Tue, 3 Dec 2024 14:57:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main_yuanyou.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/main_yuanyou.py b/main_yuanyou.py index 453917d..83f9255 100644 --- a/main_yuanyou.py +++ b/main_yuanyou.py @@ -139,24 +139,27 @@ def predict_main(): 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',)) - 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 = 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 - engine = create_engine(f'mysql+pymysql://{dbusername}:{password}@{host}:{port}/{dbname}') - warning_data_df['WARNING_DATE'] = datetime.date.today().strftime("%Y-%m-%d") - # 插入数据之前查询表数据然后新增id列 - existing_data = pd.read_sql(f"SELECT * FROM {table_name}", engine) - if not existing_data.empty: - max_id = existing_data['id'].max() - warning_data_df['id'] = range(max_id + 1, max_id + 1 + len(warning_data_df)) - else: - 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) + try: + 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 = 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 + engine = create_engine(f'mysql+pymysql://{dbusername}:{password}@{host}:{port}/{dbname}') + warning_data_df['WARNING_DATE'] = datetime.date.today().strftime("%Y-%m-%d") + # 插入数据之前查询表数据然后新增id列 + existing_data = pd.read_sql(f"SELECT * FROM {table_name}", engine) + if not existing_data.empty: + max_id = existing_data['id'].max() + warning_data_df['id'] = range(max_id + 1, max_id + 1 + len(warning_data_df)) + else: + 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: