版本修改

This commit is contained in:
王文艳
2026-07-22 11:41:49 +08:00
parent 327b36e5a0
commit 9467a62152
9 changed files with 389 additions and 522 deletions
+23 -23
View File
@@ -1,23 +1,23 @@
from airflow.operators.bash import BashOperator # from airflow.operators.bash import BashOperator
import pendulum # import pendulum
from airflow import DAG # from airflow import DAG
#
with DAG( # with DAG(
dag_id="ai_stock_dag", # dag_id="ai_stock_dag",
# 每天运行 # # 每天运行
schedule="30 8 * * 1-5", # schedule="30 8 * * 1-5",
start_date=pendulum.datetime(2025, 11, 10, tz="Asia/Shanghai"), # start_date=pendulum.datetime(2025, 11, 10, tz="Asia/Shanghai"),
catchup=True, # catchup=True,
tags=["ai_stock"], # tags=["ai_stock"],
) as dag: # ) as dag:
#
t1 = BashOperator( # t1 = BashOperator(
task_id='stock_strategy', # task_id='stock_strategy',
bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock_strategy.py', # bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock_strategy.py',
) # )
#
t2 = BashOperator( # t2 = BashOperator(
task_id='ai_stock', # task_id='ai_stock',
bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock.py', # bash_command='python /opt/airflow/dags/repo/dags/ai/stock/stock.py',
) # )
t1 >> t2 # t1 >> t2
+38 -38
View File
@@ -1,38 +1,38 @@
from goojia_utils import MysqlClient, ArkAIClient, dingutils # from goojia_utils import MysqlClient, ArkAIClient, dingutils
from goojia_utils import dateutils # from goojia_utils import dateutils
from datetime import datetime # from datetime import datetime
import requests # import requests
#
db_util = MysqlClient( # db_util = MysqlClient(
host="192.168.0.93", # host="192.168.0.93",
port=3306, # port=3306,
database="stock", # database="stock",
user="root", # user="root",
password="Goojia123456" # password="Goojia123456"
) # )
date = int(dateutils.format_date(dateutils.add_months(datetime.today(), months=-1), fm='%Y%m%d')) # date = int(dateutils.format_date(dateutils.add_months(datetime.today(), months=-1), fm='%Y%m%d'))
select_stocks = db_util.scan_by_sql_to_data_frame("select ts_code as 股票代码, stock_name as 股票名称,close as 股价, pe as PE,turnover_rate as 换手率 from mu_stocks_new where `trade_date`= (select max(`trade_date`) from mu_stocks_new)") # select_stocks = db_util.scan_by_sql_to_data_frame("select ts_code as 股票代码, stock_name as 股票名称,close as 股价, pe as PE,turnover_rate as 换手率 from mu_stocks_new where `trade_date`= (select max(`trade_date`) from mu_stocks_new)")
stat = db_util.scan_by_sql_to_data_frame(f"select ts_code as 股票代码, count(ts_code) as 命中次数 from mu_stocks_new where `trade_date` > {date} group by ts_code") # stat = db_util.scan_by_sql_to_data_frame(f"select ts_code as 股票代码, count(ts_code) as 命中次数 from mu_stocks_new where `trade_date` > {date} group by ts_code")
select_stocks = select_stocks.merge(stat, on='股票代码', how='left') # select_stocks = select_stocks.merge(stat, on='股票代码', how='left')
select_stocks['命中次数'] = select_stocks['命中次数'].fillna(0) # select_stocks['命中次数'] = select_stocks['命中次数'].fillna(0)
select_stocks = select_stocks.sort_values(by=['命中次数', 'PE'], ascending=[False, True]) # select_stocks = select_stocks.sort_values(by=['命中次数', 'PE'], ascending=[False, True])
def df_to_nested_list(df): # def df_to_nested_list(df):
"""转换为嵌套的 Markdown 列表""" # """转换为嵌套的 Markdown 列表"""
lines = [] # lines = []
i= 1 # i= 1
for _, row in df.iterrows(): # for _, row in df.iterrows():
lines.append(f"{i}. **{row['股票名称']}** ({row['股票代码']}) PE: {row['PE']} 股价: {row['股价']:.2f} 出现次数: {row['命中次数']} 换手率: {row['换手率']:.2f}") # lines.append(f"{i}. **{row['股票名称']}** ({row['股票代码']}) PE: {row['PE']} 股价: {row['股价']:.2f} 出现次数: {row['命中次数']} 换手率: {row['换手率']:.2f}")
i = i + 1 # i = i + 1
return '\n'.join(lines) # return '\n'.join(lines)
data = { # data = {
"msgtype": "markdown", # "msgtype": "markdown",
"markdown": { # "markdown": {
"title": dateutils.format_date(datetime.today(), fm='%Y%m%d') + "选股策略播报", # "title": dateutils.format_date(datetime.today(), fm='%Y%m%d') + "选股策略播报",
"text": df_to_nested_list(select_stocks) # "text": df_to_nested_list(select_stocks)
} # }
} # }
access_token = "1e5f9aaeb90b091bc79652b52800cad2f1dee1ac0990149252511965f6d1c12a" # access_token = "1e5f9aaeb90b091bc79652b52800cad2f1dee1ac0990149252511965f6d1c12a"
# 发送消息至群 # # 发送消息至群
msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=' + access_token, json=data).json() # msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=' + access_token, json=data).json()
msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=45ac47a34dc5491535d77d643512244a85d91d7e74676e9c42446dc06357326e', json=data).json() # msg = requests.post(url='https://oapi.dingtalk.com/robot/send?access_token=45ac47a34dc5491535d77d643512244a85d91d7e74676e9c42446dc06357326e', json=data).json()
print(msg) # print(msg)
+59 -59
View File
@@ -1,59 +1,59 @@
from airflow.providers.docker.operators.docker import DockerOperator # from airflow.providers.docker.operators.docker import DockerOperator
from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import KubernetesPodOperator # from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import KubernetesPodOperator
import pendulum # import pendulum
from airflow import DAG # from airflow import DAG
from docker.types import Mount # from docker.types import Mount
from kubernetes.client import models as k8s # from kubernetes.client import models as k8s
#
with DAG( # with DAG(
dag_id="comm_add_dag", # dag_id="comm_add_dag",
# 每月20日1点整分运行 # # 每月20日1点整分运行
schedule="0 1 20 * *", # schedule="0 1 20 * *",
start_date=pendulum.datetime(2023, 5, 8, tz="Asia/Shanghai"), # start_date=pendulum.datetime(2023, 5, 8, tz="Asia/Shanghai"),
catchup=True, # catchup=True,
description="小区自动化入库" # description="小区自动化入库"
) as dag: # ) as dag:
# k = KubernetesPodOperator( # # k = KubernetesPodOperator(
# name="comm-add", # # name="comm-add",
# image="192.168.0.19:9012/comm-add:1.0.1", # # image="192.168.0.19:9012/comm-add:1.0.1",
# cmds=["bash", "-cx"], # # cmds=["bash", "-cx"],
# arguments=["echo", "10"], # # arguments=["echo", "10"],
# labels={"foo": "bar"}, # # labels={"foo": "bar"},
# task_id="comm_add", # # task_id="comm_add",
# do_xcom_push=True, # # do_xcom_push=True,
# ) # # )
# k.dry_run() # # k.dry_run()
#
production_task = KubernetesPodOperator(namespace='airflow-plus', # production_task = KubernetesPodOperator(namespace='airflow-plus',
# image="my-production-job:release-1.0.1", <-- old release # # image="my-production-job:release-1.0.1", <-- old release
image="192.168.0.19:9012/comm-add:1.0.1", # image="192.168.0.19:9012/comm-add:1.0.1",
image_pull_policy="Always", # image_pull_policy="Always",
image_pull_secrets=[k8s.V1LocalObjectReference("nexus-registry-secret")], # image_pull_secrets=[k8s.V1LocalObjectReference("nexus-registry-secret")],
cmds=["python", "main.py"], # cmds=["python", "main.py"],
arguments=["--city", '{{dag_run.conf["city"] if "city" in dag_run.conf else "all"}}'], # arguments=["--city", '{{dag_run.conf["city"] if "city" in dag_run.conf else "all"}}'],
name="comm_add", # name="comm_add",
task_id="comm_add", # task_id="comm_add",
get_logs=True # get_logs=True
) # )
#
production_task # production_task
# t1 = DockerOperator( # # t1 = DockerOperator(
# # 刚才定义的conn id # # # 刚才定义的conn id
# docker_conn_id="docker_repo", # # docker_conn_id="docker_repo",
# # WebServer中点击Admin -> Providers -> Docker对应的Version,如果是auto,可能会报错 # # # WebServer中点击Admin -> Providers -> Docker对应的Version,如果是auto,可能会报错
# api_version="2.2.0", # # api_version="2.2.0",
# auto_remove=True, # # auto_remove=True,
# docker_url='unix://var/run/docker.sock', # # docker_url='unix://var/run/docker.sock',
# network_mode='bridge', # # network_mode='bridge',
# image='192.168.0.19:9012/comm-add:1.0.1', # # image='192.168.0.19:9012/comm-add:1.0.1',
# # 临沂为我们的传参,python代码中, 镜像中部分代码 # # # 临沂为我们的传参,python代码中, 镜像中部分代码
# # @ click.command() # # # @ click.command()
# # @ click.option('--city', default="all", help='要计算的城市') # # # @ click.option('--city', default="all", help='要计算的城市')
# #command='python main.py --city {{dag_run.conf["city"] if "city" in dag_run.conf}} else all', # # #command='python main.py --city {{dag_run.conf["city"] if "city" in dag_run.conf}} else all',
# command='echo "this is a test message shown from within the container', # # command='echo "this is a test message shown from within the container',
# mounts=[ # # mounts=[
# Mount(source="/var/run/docker.sock", target="/var/run/docker.sock", type="bind") # # Mount(source="/var/run/docker.sock", target="/var/run/docker.sock", type="bind")
# ], # # ],
# task_id='comm_add' # # task_id='comm_add'
# ) # # )
# t1 # # t1
+1 -1
View File
@@ -3,7 +3,7 @@
""" """
from airflow.operators.bash import BashOperator from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator, BranchPythonOperator from airflow.operators.python import PythonOperator, BranchPythonOperator
from office.rent.base_price import calc from rent.base_price import calc
import pendulum import pendulum
from airflow import DAG from airflow import DAG
+2 -2
View File
@@ -1,8 +1,8 @@
from airflow.operators.bash import BashOperator from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator, BranchPythonOperator from airflow.operators.python import PythonOperator, BranchPythonOperator
from airflow.operators.empty import EmptyOperator from airflow.operators.empty import EmptyOperator
from office.sell.base_price import calc from sell.base_price import calc
from office.sell.fill_base_price import fill_base_price from sell.fill_base_price import fill_base_price
import pendulum import pendulum
from airflow import DAG from airflow import DAG
@@ -1,249 +1,249 @@
""" # """
统计城市 区域 价格涨跌幅 # 统计城市 区域 价格涨跌幅
""" # """
import logging # import logging
import pandas as pd # import pandas as pd
from sqlalchemy import create_engine # from sqlalchemy import create_engine
from goojia_utils import dateutils # from goojia_utils import dateutils
import datetime # import datetime
import os # import os
pathAll = "/home/airflow/data/cityPrice" # pathAll = "/home/airflow/data/cityPrice"
if not os.path.exists(pathAll): # if not os.path.exists(pathAll):
os.makedirs(pathAll) # os.makedirs(pathAll)
# 创建数据库引擎 # # 创建数据库引擎
engine235 = create_engine("postgresql+psycopg2://postgres:Goojia123456@192.168.0.235:5433/fangjia?client_encoding=utf8", # engine235 = create_engine("postgresql+psycopg2://postgres:Goojia123456@192.168.0.235:5433/fangjia?client_encoding=utf8",
echo=False) # echo=False)
pd.set_option('mode.chained_assignment', None) # pd.set_option('mode.chained_assignment', None)
year_month_day = dateutils.format_date(datetime.datetime.now()) # year_month_day = dateutils.format_date(datetime.datetime.now())
# 执行SQL查询获取城市价格数据 # # 执行SQL查询获取城市价格数据
city_sql = "SELECT DISTINCT urbn_nm FROM price_stat.avm_city_price" # city_sql = "SELECT DISTINCT urbn_nm FROM price_stat.avm_city_price"
city_names_set = set(pd.read_sql(city_sql, engine235)['urbn_nm']) # city_names_set = set(pd.read_sql(city_sql, engine235)['urbn_nm'])
city_names = list(city_names_set) # city_names = list(city_names_set)
# 假设这是你的pgsql数据 # # 假设这是你的pgsql数据
all_city_data = pd.DataFrame() # all_city_data = pd.DataFrame()
# 执行SQL查询获取区域价格数据 # # 执行SQL查询获取区域价格数据
region_sql = "SELECT DISTINCT urbn_nm,gxda_admndiv_nm FROM price_stat.avm_district_price" # region_sql = "SELECT DISTINCT urbn_nm,gxda_admndiv_nm FROM price_stat.avm_district_price"
region_data = pd.read_sql(region_sql, engine235) # region_data = pd.read_sql(region_sql, engine235)
city_data_useinregion = pd.DataFrame() # city_data_useinregion = pd.DataFrame()
# 假设这是你的pgsql数据 # # 假设这是你的pgsql数据
all_region_data = pd.DataFrame() # all_region_data = pd.DataFrame()
#
def up_to_nextcloud(aim_file_name,aim_file_path): # def up_to_nextcloud(aim_file_name,aim_file_path):
""" # """
上传统计信息至nextcloud # 上传统计信息至nextcloud
""" # """
from nextcloud import NextCloud # from nextcloud import NextCloud
NEXTCLOUD_URL = "https://nextcloud.fangjia.com" # NEXTCLOUD_URL = "https://nextcloud.fangjia.com"
NEXTCLOUD_USERNAME = "shenjian@fangjia.com" # NEXTCLOUD_USERNAME = "shenjian@fangjia.com"
NEXTCLOUD_PASSWORD = "goojia123456" # NEXTCLOUD_PASSWORD = "goojia123456"
nxc = NextCloud(endpoint=NEXTCLOUD_URL, user=NEXTCLOUD_USERNAME, password=NEXTCLOUD_PASSWORD, json_output=True) # nxc = NextCloud(endpoint=NEXTCLOUD_URL, user=NEXTCLOUD_USERNAME, password=NEXTCLOUD_PASSWORD, json_output=True)
nxc.upload_file(aim_file_name, aim_file_path) # nxc.upload_file(aim_file_name, aim_file_path)
def city_increase(city_names, all_city_data): # def city_increase(city_names, all_city_data):
global city_data_useinregion # 声明为全局变量 # global city_data_useinregion # 声明为全局变量
# 遍历城市列表,对每个城市进行查询 # # 遍历城市列表,对每个城市进行查询
for city in city_names: # for city in city_names:
query = f"SELECT urbn_nm as 城市, gujiabds_clc_yrmo_val as 月份, gujia_bds_clc_avprc as 估家计算均价, gujia_bds_urbn_avprc as 估家城市均价 FROM price_stat.avm_city_price WHERE urbn_nm = '{city}'" # query = f"SELECT urbn_nm as 城市, gujiabds_clc_yrmo_val as 月份, gujia_bds_clc_avprc as 估家计算均价, gujia_bds_urbn_avprc as 估家城市均价 FROM price_stat.avm_city_price WHERE urbn_nm = '{city}'"
city_data = pd.read_sql(query, engine235) # city_data = pd.read_sql(query, engine235)
#
if city_data.empty: # if city_data.empty:
continue # continue
#
# 复制一份原始的日期列,用于计算涨跌幅时保持原始数据不变 # # 复制一份原始的日期列,用于计算涨跌幅时保持原始数据不变
city_data['original_date'] = city_data['月份'] # city_data['original_date'] = city_data['月份']
#
# 将日期列转换为日期类型 # # 将日期列转换为日期类型
city_data['月份'] = pd.to_datetime(city_data['月份'], format='%Y%m') # city_data['月份'] = pd.to_datetime(city_data['月份'], format='%Y%m')
#
# 对价格数据按照月份排序 # # 对价格数据按照月份排序
city_data.sort_values(by='月份', inplace=True) # city_data.sort_values(by='月份', inplace=True)
#
# 计算涨跌幅 # # 计算涨跌幅
city_data['涨跌幅'] = city_data['估家计算均价'].diff() / city_data['估家计算均价'].shift(1) * 100 # city_data['涨跌幅'] = city_data['估家计算均价'].diff() / city_data['估家计算均价'].shift(1) * 100
#
# 计算涨跌幅,并保留两位小数,展示为百分比形式 # # 计算涨跌幅,并保留两位小数,展示为百分比形式
price_change = city_data['估家计算均价'].pct_change() * 100 # price_change = city_data['估家计算均价'].pct_change() * 100
city_data['涨跌幅'] = (price_change[price_change != 0].fillna(0).round(2).astype(str) + '%').replace('0.0%', '') # city_data['涨跌幅'] = (price_change[price_change != 0].fillna(0).round(2).astype(str) + '%').replace('0.0%', '')
#
# 将原始日期列替换为计算涨跌幅后的日期列 # # 将原始日期列替换为计算涨跌幅后的日期列
city_data['月份'] = city_data['original_date'] # city_data['月份'] = city_data['original_date']
#
# 删除临时列 # # 删除临时列
city_data.drop(columns=['original_date'], inplace=True) # city_data.drop(columns=['original_date'], inplace=True)
#
# 将城市数据添加到总的数据中 # # 将城市数据添加到总的数据中
all_city_data = pd.concat([all_city_data, city_data], ignore_index=True) # all_city_data = pd.concat([all_city_data, city_data], ignore_index=True)
city_data_useinregion = all_city_data # city_data_useinregion = all_city_data
return all_city_data # return all_city_data
#
#
def city_missing(): # def city_missing():
df = city_increase(city_names, all_city_data) # df = city_increase(city_names, all_city_data)
df['月份1'] = df['月份'] # df['月份1'] = df['月份']
# 将日期字符串转换为日期时间对象 # # 将日期字符串转换为日期时间对象
df["月份"] = pd.to_datetime(df["月份"], format="%Y%m") # df["月份"] = pd.to_datetime(df["月份"], format="%Y%m")
#
# 获取当前时间 # # 获取当前时间
current_time = pd.Timestamp.today().replace(hour=0, minute=0, second=0, microsecond=0) # current_time = pd.Timestamp.today().replace(hour=0, minute=0, second=0, microsecond=0)
#
# 计算前一年到上月的时间范围 # # 计算前一年到上月的时间范围
one_year_ago = current_time - pd.DateOffset(years=1, days=current_time.day - 1) # one_year_ago = current_time - pd.DateOffset(years=1, days=current_time.day - 1)
last_month = current_time - pd.DateOffset(months=1, days=current_time.day - 1) # last_month = current_time - pd.DateOffset(months=1, days=current_time.day - 1)
#
# 创建一个包含所有可能月份的日期范围 # # 创建一个包含所有可能月份的日期范围
all_months = pd.date_range(start=one_year_ago, end=last_month, freq="MS") # all_months = pd.date_range(start=one_year_ago, end=last_month, freq="MS")
#
# 获取每个城市缺失的月份 # # 获取每个城市缺失的月份
missing_months = [] # missing_months = []
for city, group in df.groupby("城市"): # for city, group in df.groupby("城市"):
available_months = group["月份"] # available_months = group["月份"]
missing = all_months.difference(available_months) # missing = all_months.difference(available_months)
if len(missing) > 0: # if len(missing) > 0:
missing_str = ",".join([m.strftime("%Y%m") for m in missing]) # missing_str = ",".join([m.strftime("%Y%m") for m in missing])
missing_months.append({"城市": city, "缺失月份": missing_str}) # missing_months.append({"城市": city, "缺失月份": missing_str})
if len(missing_months) > 0: # if len(missing_months) > 0:
# 创建DataFrame来存储结果 # # 创建DataFrame来存储结果
missing_df = pd.DataFrame(missing_months) # missing_df = pd.DataFrame(missing_months)
path = pathAll+'/城市价格数据缺失'+year_month_day+'.xlsx' # path = pathAll+'/城市价格数据缺失'+year_month_day+'.xlsx'
missing_df.to_excel(path, index=False) # missing_df.to_excel(path, index=False)
logging.info(f"城市价格缺失数据已写入Excel文件:{path}") # logging.info(f"城市价格缺失数据已写入Excel文件:{path}")
#
cloudPath = '共享/统计/城市区域涨跌幅/' + '城市价格数据缺失'+year_month_day+'.xlsx' # cloudPath = '共享/统计/城市区域涨跌幅/' + '城市价格数据缺失'+year_month_day+'.xlsx'
up_to_nextcloud(path, cloudPath) # up_to_nextcloud(path, cloudPath)
logging.info(f"区域涨跌幅数据Excel文件已经上传到云盘") # logging.info(f"区域涨跌幅数据Excel文件已经上传到云盘")
#
# print(missing_df) # # print(missing_df)
df['月份'] = df['月份1'] # df['月份'] = df['月份1']
# 删除月份1列 # # 删除月份1列
df.drop(columns=['月份1'], inplace=True) # df.drop(columns=['月份1'], inplace=True)
output_path = pathAll+'/城市价格涨跌幅'+year_month_day+'.xlsx' # output_path = pathAll+'/城市价格涨跌幅'+year_month_day+'.xlsx'
df.to_excel(output_path, index=False) # df.to_excel(output_path, index=False)
logging.info(f"城市涨跌幅数据已写入Excel文件:{output_path}") # logging.info(f"城市涨跌幅数据已写入Excel文件:{output_path}")
#
cloudPath = '共享/统计/城市区域涨跌幅/' + '城市价格涨跌幅'+year_month_day+'.xlsx' # cloudPath = '共享/统计/城市区域涨跌幅/' + '城市价格涨跌幅'+year_month_day+'.xlsx'
up_to_nextcloud(output_path, cloudPath) # up_to_nextcloud(output_path, cloudPath)
logging.info(f"区域涨跌幅数据Excel文件已经上传到云盘") # logging.info(f"区域涨跌幅数据Excel文件已经上传到云盘")
#
#
def region_increase(region_data, all_region_data): # def region_increase(region_data, all_region_data):
# 遍历区域列表,对每个区域进行查询 # # 遍历区域列表,对每个区域进行查询
for index, row in region_data.iterrows(): # for index, row in region_data.iterrows():
city = row['urbn_nm'] # city = row['urbn_nm']
region = row['gxda_admndiv_nm'] # region = row['gxda_admndiv_nm']
query = f"SELECT urbn_nm as 城市, gxda_admndiv_nm as 区域, gujiabds_clc_yrmo_val as 月份, gujia_bds_clc_avprc as 估家计算均价, gujia_bds_urbn_avprc as 估家城市均价 FROM price_stat.avm_district_price WHERE urbn_nm = '{city}' AND gxda_admndiv_nm = '{region}'" # query = f"SELECT urbn_nm as 城市, gxda_admndiv_nm as 区域, gujiabds_clc_yrmo_val as 月份, gujia_bds_clc_avprc as 估家计算均价, gujia_bds_urbn_avprc as 估家城市均价 FROM price_stat.avm_district_price WHERE urbn_nm = '{city}' AND gxda_admndiv_nm = '{region}'"
region_data = pd.read_sql(query, engine235) # region_data = pd.read_sql(query, engine235)
#
if region_data.empty: # if region_data.empty:
continue # continue
#
# 复制一份原始的日期列,用于计算涨跌幅时保持原始数据不变 # # 复制一份原始的日期列,用于计算涨跌幅时保持原始数据不变
region_data['original_date'] = region_data['月份'] # region_data['original_date'] = region_data['月份']
#
# 将日期列转换为日期类型 # # 将日期列转换为日期类型
region_data['月份'] = pd.to_datetime(region_data['月份'], format='%Y%m') # region_data['月份'] = pd.to_datetime(region_data['月份'], format='%Y%m')
#
# 对价格数据按照月份排序 # # 对价格数据按照月份排序
region_data.sort_values(by='月份', inplace=True) # region_data.sort_values(by='月份', inplace=True)
#
# 计算涨跌幅 # # 计算涨跌幅
region_data['涨跌幅'] = region_data['估家计算均价'].diff() / region_data['估家计算均价'].shift(1) * 100 # region_data['涨跌幅'] = region_data['估家计算均价'].diff() / region_data['估家计算均价'].shift(1) * 100
#
# 计算涨跌幅,并保留两位小数,展示为百分比形式 # # 计算涨跌幅,并保留两位小数,展示为百分比形式
price_change = region_data['估家计算均价'].pct_change() * 100 # price_change = region_data['估家计算均价'].pct_change() * 100
region_data['涨跌幅'] = (price_change[price_change != 0].fillna(0).round(2).astype(str) + '%').replace('0.0%', # region_data['涨跌幅'] = (price_change[price_change != 0].fillna(0).round(2).astype(str) + '%').replace('0.0%',
'') # '')
#
# 将原始日期列替换为计算涨跌幅后的日期列 # # 将原始日期列替换为计算涨跌幅后的日期列
region_data['月份'] = region_data['original_date'] # region_data['月份'] = region_data['original_date']
#
# 删除临时列 # # 删除临时列
region_data.drop(columns=['original_date'], inplace=True) # region_data.drop(columns=['original_date'], inplace=True)
#
# 将区域数据添加到总的数据中 # # 将区域数据添加到总的数据中
all_region_data = pd.concat([all_region_data, region_data], ignore_index=True) # all_region_data = pd.concat([all_region_data, region_data], ignore_index=True)
#
return all_region_data # return all_region_data
#
#
# 城市价个和区域价格比较 # # 城市价个和区域价格比较
def add_price_comparison_by_month(region_data): # def add_price_comparison_by_month(region_data):
# 遍历区域数据,比较城市和区域的价格,并添加比值超过3倍的情况到区域数据中 # # 遍历区域数据,比较城市和区域的价格,并添加比值超过3倍的情况到区域数据中
for index, row in region_data.iterrows(): # for index, row in region_data.iterrows():
city = row['城市'] # city = row['城市']
region = row['区域'] # region = row['区域']
region_month = row['月份'] # region_month = row['月份']
region_price = row['估家计算均价'] # region_price = row['估家计算均价']
city_data = city_data_useinregion # city_data = city_data_useinregion
# 获取城市对应月份的价格 # # 获取城市对应月份的价格
city_price_row = city_data[(city_data['城市'] == city) & (city_data['月份'] == region_month)] # city_price_row = city_data[(city_data['城市'] == city) & (city_data['月份'] == region_month)]
if city_price_row.empty: # if city_price_row.empty:
continue # continue
city_price = city_price_row['估家城市均价'].iloc[0] # city_price = city_price_row['估家城市均价'].iloc[0]
#
# 比较价格并计算比值 # # 比较价格并计算比值
price_ratio = max(city_price, region_price) / min(city_price, region_price) # price_ratio = max(city_price, region_price) / min(city_price, region_price)
#
# 如果比值超过3倍,将结果添加到区域数据中 # # 如果比值超过3倍,将结果添加到区域数据中
if price_ratio > 3: # if price_ratio > 3:
region_data.loc[index, '城市区域价格比值超3倍'] = price_ratio # region_data.loc[index, '城市区域价格比值超3倍'] = price_ratio
#
return region_data # return region_data
#
#
def region_missing(): # def region_missing():
df = region_increase(region_data, all_region_data) # df = region_increase(region_data, all_region_data)
df['月份1'] = df['月份'] # df['月份1'] = df['月份']
# 将日期字符串转换为日期时间对象 # # 将日期字符串转换为日期时间对象
df["月份"] = pd.to_datetime(df["月份"], format="%Y%m") # df["月份"] = pd.to_datetime(df["月份"], format="%Y%m")
#
# 获取当前时间 # # 获取当前时间
current_time = pd.Timestamp.today().replace(hour=0, minute=0, second=0, microsecond=0) # current_time = pd.Timestamp.today().replace(hour=0, minute=0, second=0, microsecond=0)
#
# 计算前一年到上月的时间范围 # # 计算前一年到上月的时间范围
one_year_ago = current_time - pd.DateOffset(years=1, days=current_time.day - 1) # one_year_ago = current_time - pd.DateOffset(years=1, days=current_time.day - 1)
last_month = current_time - pd.DateOffset(months=1, days=current_time.day - 1) # last_month = current_time - pd.DateOffset(months=1, days=current_time.day - 1)
#
# 创建一个包含所有可能月份的日期范围 # # 创建一个包含所有可能月份的日期范围
all_months = pd.date_range(start=one_year_ago, end=last_month, freq="MS") # all_months = pd.date_range(start=one_year_ago, end=last_month, freq="MS")
#
# 获取每个城市-区域组合缺失的月份 # # 获取每个城市-区域组合缺失的月份
missing_months = [] # missing_months = []
for (city, region), group in df.groupby(["城市", "区域"]): # for (city, region), group in df.groupby(["城市", "区域"]):
available_months = group["月份"] # available_months = group["月份"]
missing = all_months.difference(available_months) # missing = all_months.difference(available_months)
if len(missing) > 0: # if len(missing) > 0:
missing_str = ",".join([m.strftime("%Y%m") for m in missing]) # missing_str = ",".join([m.strftime("%Y%m") for m in missing])
missing_months.append({"城市": city, "区域": region, "缺失月份": missing_str}) # missing_months.append({"城市": city, "区域": region, "缺失月份": missing_str})
#
if missing_months: # if missing_months:
# 创建DataFrame来存储结果 # # 创建DataFrame来存储结果
missing_df = pd.DataFrame(missing_months) # missing_df = pd.DataFrame(missing_months)
path = pathAll+'/区域价格数据缺失'+year_month_day+'.xlsx' # path = pathAll+'/区域价格数据缺失'+year_month_day+'.xlsx'
missing_df.to_excel(path, index=False) # missing_df.to_excel(path, index=False)
logging.info(f"区域价格缺失数据已写入Excel文件:{path}") # logging.info(f"区域价格缺失数据已写入Excel文件:{path}")
cloudPath = '共享/统计/城市区域涨跌幅/' + '区域价格数据缺失' + year_month_day + '.xlsx' # cloudPath = '共享/统计/城市区域涨跌幅/' + '区域价格数据缺失' + year_month_day + '.xlsx'
up_to_nextcloud(path, cloudPath) # up_to_nextcloud(path, cloudPath)
logging.info(f"区域价格缺失数据Excel文件已经上传到云盘") # logging.info(f"区域价格缺失数据Excel文件已经上传到云盘")
#
# 如果区域在 missing_df 中,则将 df 中的该区域数据删除 # # 如果区域在 missing_df 中,则将 df 中的该区域数据删除
for city, region in zip(missing_df['城市'], missing_df['区域']): # for city, region in zip(missing_df['城市'], missing_df['区域']):
df = df[(df['城市'] != city) | (df['区域'] != region)] # df = df[(df['城市'] != city) | (df['区域'] != region)]
#
df['月份'] = df['月份1'] # df['月份'] = df['月份1']
# 删除月份1列 # # 删除月份1列
df.drop(columns=['月份1'], inplace=True) # df.drop(columns=['月份1'], inplace=True)
#
# 添加价格比值超过3倍的列 # # 添加价格比值超过3倍的列
# print(city_data_useinregion) # # print(city_data_useinregion)
df = add_price_comparison_by_month(df) # df = add_price_comparison_by_month(df)
#
output_path = pathAll+'/区域价格涨跌幅'+year_month_day+'.xlsx' # output_path = pathAll+'/区域价格涨跌幅'+year_month_day+'.xlsx'
df.to_excel(output_path, index=False) # df.to_excel(output_path, index=False)
logging.info(f"区域涨跌幅数据已写入Excel文件:{output_path}") # logging.info(f"区域涨跌幅数据已写入Excel文件:{output_path}")
#
cloudPath = '共享/统计/城市区域涨跌幅/' + '区域价格涨跌幅'+year_month_day+'.xlsx' # cloudPath = '共享/统计/城市区域涨跌幅/' + '区域价格涨跌幅'+year_month_day+'.xlsx'
up_to_nextcloud(output_path, cloudPath) # up_to_nextcloud(output_path, cloudPath)
logging.info(f"区域涨跌幅数据Excel文件已经上传到云盘") # logging.info(f"区域涨跌幅数据Excel文件已经上传到云盘")
#
#
city_missing() # city_missing()
region_missing() # region_missing()
-97
View File
@@ -1,97 +0,0 @@
import logging
import pandas as pd
from sqlalchemy import create_engine
# 创建数据库引擎
engine235 = create_engine("postgresql+psycopg2://postgres:Goojia123456@192.168.0.235:5433/fangjia?client_encoding=utf8",
echo=False)
# 执行SQL查询获取区域价格数据
region_sql = "SELECT DISTINCT urbn_nm,gxda_admndiv_nm FROM price_stat.avm_district_price_copy1"
region_data = pd.read_sql(region_sql, engine235)
# 假设这是你的pgsql数据
all_region_data = pd.DataFrame()
pd.set_option('mode.chained_assignment', None)
def region_increase(region_data, all_region_data):
# 遍历区域列表,对每个区域进行查询
for index, row in region_data.iterrows():
city = row['urbn_nm']
region = row['gxda_admndiv_nm']
query = f"SELECT urbn_nm as 城市, gxda_admndiv_nm as 区域, gujiabds_clc_yrmo_val as 月份, gujia_bds_clc_avprc as 估家计算均价, gujia_bds_urbn_avprc as 估家城市均价 FROM price_stat.avm_district_price_copy1 WHERE urbn_nm = '{city}' AND gxda_admndiv_nm = '{region}'"
region_data = pd.read_sql(query, engine235)
if region_data.empty:
continue
# 复制一份原始的日期列,用于计算涨跌幅时保持原始数据不变
region_data['original_date'] = region_data['月份']
# 将日期列转换为日期类型
region_data['月份'] = pd.to_datetime(region_data['月份'], format='%Y%m')
# 对价格数据按照月份排序
region_data.sort_values(by='月份', inplace=True)
# 计算涨跌幅
region_data['涨跌幅'] = region_data['估家计算均价'].diff() / region_data['估家计算均价'].shift(1) * 100
# 计算涨跌幅,并保留两位小数,展示为百分比形式
price_change = region_data['估家计算均价'].pct_change() * 100
region_data['涨跌幅'] = (price_change[price_change != 0].fillna(0).round(2).astype(str) + '%').replace('0.0%', '')
# 将原始日期列替换为计算涨跌幅后的日期列
region_data['月份'] = region_data['original_date']
# 删除临时列
region_data.drop(columns=['original_date'], inplace=True)
# 将区域数据添加到总的数据中
all_region_data = pd.concat([all_region_data, region_data], ignore_index=True)
return all_region_data
def region_missing():
df = region_increase(region_data, all_region_data)
df['月份1'] = df['月份']
# 将日期字符串转换为日期时间对象
df["月份"] = pd.to_datetime(df["月份"], format="%Y%m")
# 获取当前时间
current_time = pd.Timestamp.today().replace(hour=0, minute=0, second=0, microsecond=0)
# 计算前一年到上月的时间范围
one_year_ago = current_time - pd.DateOffset(years=1, days=current_time.day - 1)
last_month = current_time - pd.DateOffset(months=1, days=current_time.day - 1)
# 创建一个包含所有可能月份的日期范围
all_months = pd.date_range(start=one_year_ago, end=last_month, freq="MS")
# 获取每个城市-区域组合缺失的月份
missing_months = []
for (city, region), group in df.groupby(["城市", "区域"]):
available_months = group["月份"]
missing = all_months.difference(available_months)
if len(missing) > 0:
missing_str = ",".join([m.strftime("%Y%m") for m in missing])
missing_months.append({"城市": city, "区域": region, "缺失月份": missing_str})
if missing_months:
# 创建DataFrame来存储结果
missing_df = pd.DataFrame(missing_months)
path = 'E:\\goojia\\file\\excel文件\\airflow\\missing_region.xlsx'
missing_df.to_excel(path, index=False)
# 如果区域在 missing_df 中,则将 df 中的该区域数据删除
for city, region in zip(missing_df['城市'], missing_df['区域']):
df = df[(df['城市'] != city) | (df['区域'] != region)]
df['月份'] = df['月份1']
# 删除月份1列
df.drop(columns=['月份1'], inplace=True)
output_path = 'E:\\goojia\\file\\excel文件\\airflow\\all_region_price_with_change.xlsx'
df.to_excel(output_path, index=False)
logging.info(f"区域涨跌幅数据已写入Excel文件:{output_path}")
region_missing()
+17 -17
View File
@@ -1,17 +1,17 @@
from airflow.operators.bash import BashOperator # from airflow.operators.bash import BashOperator
import pendulum # import pendulum
from airflow import DAG # from airflow import DAG
#
with DAG( # with DAG(
dag_id="increase_decrease_city_region_dag", # dag_id="increase_decrease_city_region_dag",
# 每月的最后一天运行 # # 每月的最后一天运行
schedule="0 10 L * *", # schedule="0 10 L * *",
start_date=pendulum.datetime(2024, 5, 30, tz="Asia/Shanghai"), # start_date=pendulum.datetime(2024, 5, 30, tz="Asia/Shanghai"),
description="城市区域涨跌幅数据统计", # description="城市区域涨跌幅数据统计",
catchup=True # catchup=True
) as dag: # ) as dag:
t1 = BashOperator( # t1 = BashOperator(
task_id='increase_decrease_city_region', # task_id='increase_decrease_city_region',
bash_command='python /opt/airflow/dags/repo/dags/stat/cityPrice/increase_decrease_city_region.py', # bash_command='python /opt/airflow/dags/repo/dags/stat/cityPrice/increase_decrease_city_region.py',
) # )
t1 # t1
-36
View File
@@ -1,36 +0,0 @@
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime
from goojia_utils import dingutils
# def send_dingding_alert(context):
# print('监测到错误')
# task_instance = context.get('task_instance')
# url = task_instance.log_url.replace("http://localhost:8080", "https://airflow.fangjia.com")
# message = f"【airflow】 任务{task_instance.task_id}错误,请点击{url}查看!"
# print(message)
# dingutils.send_text(message, "ae14f50045e9375c354940a2afb86f14e453bd1f08f78da0ba6e788d35bccec4")
# print("success")
#
def fail_task():
raise ValueError("This task is designed to fail")
#
# default_args = {
# "owner": "airflow",
# "retries": 0,
# "on_failure_callback": send_dingding_alert, # 任务失败时触发钉钉告警
# }
dag = DAG(
'test_failure_notification',
#default_args=default_args,
start_date=datetime(2025, 4, 22),
schedule=None
)
task = PythonOperator(
task_id='failing_task',
python_callable=fail_task,
dag=dag
)