From 54b723fefdc0929bf7d4318e437f31107f9c2b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E8=88=9F?= <13007110208@163.com> Date: Tue, 11 Feb 2025 16:04:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 4 +++ docker-compose.yaml | 14 +++++++++ dockerfile | 20 ++++++++++++ requirements.txt | Bin 0 -> 66 bytes run.py | 40 ++++++++++++++++++++++++ sqllineage.py | 40 ++++++++++++++++++++++++ utils/log.py | 69 +++++++++++++++++++++++++++++++++++++++++ utils/query_weather.py | 12 +++++++ 8 files changed, 199 insertions(+) create mode 100644 .dockerignore create mode 100644 docker-compose.yaml create mode 100644 dockerfile create mode 100644 requirements.txt create mode 100644 run.py create mode 100644 sqllineage.py create mode 100644 utils/log.py create mode 100644 utils/query_weather.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e833929 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.venv/ +.idea/ +.deploy/ +logs/ diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..a4ba181 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,14 @@ +version: '3.4' +services: + sql-runner: + build: + context: . + dockerfile: Dockerfile + restart: always + container_name: yin_home + image: registry.cn-hangzhou.aliyuncs.com/yinzhou_docker_hub/yin_home:latest + ports: + - "1314:1314" + + +# docker-compose up --build \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..23cf465 --- /dev/null +++ b/dockerfile @@ -0,0 +1,20 @@ +# 使用阿里云的 Python 3.11 镜像 +FROM registry.cn-hangzhou.aliyuncs.com/yinzhou_docker_hub/python:3.11-alpine + +# 设置工作目录 +WORKDIR /opt/yin_home + +# 设置时区为 Asia/Shanghai +ENV TZ=Asia/Shanghai + +# 将 requirements.txt 文件复制到容器中 +COPY requirements.txt . + +# 安装依赖 +RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple + +# 将其他文件复制到容器中 +COPY . . + +# 运行应用程序 +ENTRYPOINT ["python3", "run.py"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..f7edad77f3297c0b8a5e7c1e31534b2b1e25299e GIT binary patch literal 66 zcmezWFO4CGA(5e&A)CP#2#p!^7z}~ffPt5Ri=l`im7$QK6i5~`lmIbEg%MEJ7)cEP D!1W5h literal 0 HcmV?d00001 diff --git a/run.py b/run.py new file mode 100644 index 0000000..f7fcac5 --- /dev/null +++ b/run.py @@ -0,0 +1,40 @@ +from flask import Flask +from utils import query_weather +from utils.log import Log + +app = Flask(__name__) + + +@app.route('/weather', methods=['GET']) +def weather(): + log = Log().getlog() + response = query_weather.query_weather() + log.info(response) + + return response, 200 + + +@app.route('/weather_all', methods=['GET']) +def weather_all(): + log = Log().getlog() + response = query_weather.query_weather(extensions='all') + log.info(response) + + return response, 200 + + +@app.route('/weather_flag', methods=['GET']) +def weather_flag(): + log = Log().getlog() + response = query_weather.query_weather() + log.info(response) + weather = response['lives'][0]['weather'] + # weather包含雨字符串 + if '雨' in weather: + return {"weather":1}, 200 + else: + return {"weather":0}, 200 + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=1314, debug=False) diff --git a/sqllineage.py b/sqllineage.py new file mode 100644 index 0000000..c4af145 --- /dev/null +++ b/sqllineage.py @@ -0,0 +1,40 @@ +from flask import Flask, render_template, request, jsonify +from utils.sql_parse import parse_create_table_sql +from utils.log import Log + + +app = Flask(__name__) +log = Log().getlog() + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/convert', methods=['POST']) +def convert_sql(): + + sql_input = request.form['sql'] + if sql_input.strip()=='': + + return jsonify({ + 'parsed_result': "请在上面输入框输入hologres建表语句", + 'message': 'SQL processed.' + }) + + hologres_connection = request.form['hologresConnection'] + log.info("SQL Input: %s", sql_input) + log.info("SQL hologres_connection: %s", hologres_connection) + try: + parsed_result=parse_create_table_sql(sql_input,hologres_connection) + result = { + 'parsed_result': parsed_result, + 'message': 'SQL processed successfully.' + } + except Exception as e: + result = {'error': f'请检查输入sql:\n\n {str(e)}'} + log.info("SQL result: %s", result) + return jsonify(result) + +if __name__ == '__main__': + # 指定host和port,这里使用0.0.0.0可以让服务器被外部访问 + app.run(host='0.0.0.0', port=8778, debug=True) \ No newline at end of file diff --git a/utils/log.py b/utils/log.py new file mode 100644 index 0000000..4b64d58 --- /dev/null +++ b/utils/log.py @@ -0,0 +1,69 @@ +import logging +import os +from datetime import datetime + +# 定义全局变量 log_path +cur_path = os.path.dirname(os.path.realpath(__file__)) +log_path = os.path.join(os.path.dirname(cur_path), 'logs') + + +class Log(): + def __init__(self, logger_name='my_logger'): + self.logger = logging.getLogger(logger_name) + if self.logger.hasHandlers(): + self.logger.handlers.clear() + self.logger.setLevel(logging.INFO) + + if not os.path.exists(log_path): + os.makedirs(log_path) + + self.update_log_file() + + def update_log_file(self): + current_date = datetime.now().strftime("%Y_%m_%d") + self.log_name = os.path.join(log_path, f'{current_date}.log') + + for handler in self.logger.handlers[:]: + self.logger.removeHandler(handler) + + fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8') + fh.setLevel(logging.INFO) + + ch = logging.StreamHandler() + ch.setLevel(logging.INFO) + + formatter = logging.Formatter( + '[%(asctime)s] %(filename)s line:%(lineno)d [%(levelname)s]%(message)s', + datefmt="%Y-%m-%d %H:%M:%S" + ) + fh.setFormatter(formatter) + ch.setFormatter(formatter) + + self.logger.addHandler(fh) + self.logger.addHandler(ch) + + def getlog(self): + current_date = datetime.now().strftime("%Y_%m_%d") + log_date = os.path.basename(self.log_name).split('.')[0] + if current_date != log_date: + self.update_log_file() + return self.logger + + def info(self, msg, *args, **kwargs): + logger = self.getlog() + logger.info(msg, *args, **kwargs) + + def error(self, msg, *args, **kwargs): + logger = self.getlog() + logger.error(msg, *args, **kwargs) + + def warning(self, msg, *args, **kwargs): + logger = self.getlog() + logger.warning(msg, *args, **kwargs) + + +if __name__ == "__main__": + log = Log() + log.info("---测试开始----") + log.error("操作步骤1,2,3") + log.warning("----测试结束----") \ No newline at end of file diff --git a/utils/query_weather.py b/utils/query_weather.py new file mode 100644 index 0000000..053ed94 --- /dev/null +++ b/utils/query_weather.py @@ -0,0 +1,12 @@ +import requests + + +def query_weather(key="3add99f10c5144ebd64bfa0bfb6b1c20", city="420115", extensions="base", output="JSON"): + url = "https://restapi.amap.com/v3/weather/weatherInfo" + + querystring = {"key": key, "city": city, "extensions": extensions, + "output": output} + + response = requests.request("GET", url, params=querystring) + + return response.json()