From 3d677cc226def4eb6a8995099046b1bb4cb2b1bf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=B0=B9=E8=88=9F?= <13007110208@163.com>
Date: Wed, 5 Feb 2025 14:27:23 +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

---
 config/config.ini   |   8 ++++++
 docker-compose.yaml |  13 ++++++++++
 dockerfile          |  20 ++++++++++++++
 requirements.txt    | Bin 0 -> 98 bytes
 run.py              |  21 +++++++++++++++
 utils/__init__.py   |   0
 utils/ali_client.py |  62 ++++++++++++++++++++++++++++++++++++++++++++
 utils/date_time.py  |  53 +++++++++++++++++++++++++++++++++++++
 utils/loadconfig.py |  36 +++++++++++++++++++++++++
 utils/log.py        |  58 +++++++++++++++++++++++++++++++++++++++++
 10 files changed, 271 insertions(+)
 create mode 100644 config/config.ini
 create mode 100644 docker-compose.yaml
 create mode 100644 dockerfile
 create mode 100644 requirements.txt
 create mode 100644 run.py
 create mode 100644 utils/__init__.py
 create mode 100644 utils/ali_client.py
 create mode 100644 utils/date_time.py
 create mode 100644 utils/loadconfig.py
 create mode 100644 utils/log.py

diff --git a/config/config.ini b/config/config.ini
new file mode 100644
index 0000000..bd63e5d
--- /dev/null
+++ b/config/config.ini
@@ -0,0 +1,8 @@
+[996]
+access_key_id=1
+access_key_secret=1
+endpoint=dataworks.cn-hangzhou.aliyuncs.com
+[xianxia]
+access_key_id=LTAI5t8Uv8MUS2Kn5Yn5scUK
+access_key_secret=RB17dFF6ITVAXt5OqG1WFmeZNj1xgj
+endpoint=dataworks.cn-hangzhou.aliyuncs.com
\ No newline at end of file
diff --git a/docker-compose.yaml b/docker-compose.yaml
new file mode 100644
index 0000000..d4a7c25
--- /dev/null
+++ b/docker-compose.yaml
@@ -0,0 +1,13 @@
+version: '3.4'
+services:
+    sql-runner:
+        build:
+            context: .
+            dockerfile: Dockerfile
+        restart: always
+        container_name: aliapi
+        image: aliapi:latest
+
+
+
+# docker-compose up --build
\ No newline at end of file
diff --git a/dockerfile b/dockerfile
new file mode 100644
index 0000000..d93653a
--- /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/aliapi
+
+# 设置时区为 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..2dbdf802f083cbed606b127296cf66bd698102b1
GIT binary patch
literal 98
zcmezWub82bA%~%WA(5eop_n0+!4?P&81xuSf!LUVmw^i?3Q~~{#Q6**V0l9ZL!hiV
Z5F3J3qygm;f$Flsvc_O>J)kKd-2j!D4?O??

literal 0
HcmV?d00001

diff --git a/run.py b/run.py
new file mode 100644
index 0000000..46eb5da
--- /dev/null
+++ b/run.py
@@ -0,0 +1,21 @@
+from utils.ali_client import create_client,get_listtask,get_task_detail
+import json
+from utils.log import Log
+
+
+if __name__ == '__main__':
+    # 创建一个新的Log实例,确保每天创建一个新的日志文件
+    log = Log().getlog()
+
+    aliclient=create_client('xianxia');
+
+    # response_dict=get_listtask(aliclient,39023);
+    # response = json.loads(response_dict)
+    # print(response_dict)
+    # print('1111111111111111111111')
+    # print(response['body'])
+    # print(response['body']['PagingInfo']['TotalCount'])
+
+    # response_dict = get_task_detail(aliclient, 1023194720,'Dev');
+    response_dict = get_task_detail(aliclient, 1023194720,'Prod');
+    log.info(response_dict)
diff --git a/utils/__init__.py b/utils/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/utils/ali_client.py b/utils/ali_client.py
new file mode 100644
index 0000000..4c0be77
--- /dev/null
+++ b/utils/ali_client.py
@@ -0,0 +1,62 @@
+from alibabacloud_tea_openapi import models as open_api_models
+from alibabacloud_dataworks_public20240518.client import Client as dataworks_public20240518Client
+from utils.loadconfig import get_ali_config
+from alibabacloud_dataworks_public20240518 import models as dataworks_public_20240518_models
+from alibabacloud_tea_util import models as util_models
+import json
+from alibabacloud_tea_util.client import Client as UtilClient
+from utils.log import Log
+
+
+def create_client(env):
+    # 创建一个新的Log实例,确保每天创建一个新的日志文件
+    log = Log().getlog()
+    db_config = get_ali_config(env)
+    log.info(db_config)
+    config = open_api_models.Config(
+        # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
+        access_key_id=db_config.get('access_key_id'),
+        access_key_secret=db_config.get('access_key_secret')
+        # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
+    )
+    # Endpoint 请参考 https://api.aliyun.com/product/dataworks-public
+    config.endpoint =db_config.get('endpoint')
+    return dataworks_public20240518Client(config)
+
+
+# 获取任务列表
+def get_listtask(client,project_id):
+    list_tasks_request = dataworks_public_20240518_models.ListTasksRequest(
+        project_id=project_id
+    )
+    runtime = util_models.RuntimeOptions()
+    try:
+        # 复制代码运行请自行打印 API 的返回值
+        response = client.list_tasks_with_options(list_tasks_request, runtime)
+        # 打印 API 的返回值
+        return json.dumps(response.to_map(), indent=2, ensure_ascii=False)
+    except Exception as error:
+        # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
+        # 错误 message
+        print(error.message)
+        # 诊断地址
+        print(error.data.get("Recommend"))
+        UtilClient.assert_as_string(error.message)
+
+def get_task_detail(client,task_id,project_env):
+    get_task_request = dataworks_public_20240518_models.GetTaskRequest(
+        id=1023194720,
+        project_env=project_env
+    )
+    runtime = util_models.RuntimeOptions()
+    try:
+        # 调用 API 并获取返回值
+        response = client.get_task_with_options(get_task_request, runtime)
+        # 打印 API 的返回值
+        return json.dumps(response.to_map(), indent=2, ensure_ascii=False)
+    except Exception as error:
+        print(f"Error occurred: {error.message}")
+        if hasattr(error, 'data') and error.data:
+            print(f"Recommend: {error.data.get('Recommend')}")
+
+
diff --git a/utils/date_time.py b/utils/date_time.py
new file mode 100644
index 0000000..6e7a4c4
--- /dev/null
+++ b/utils/date_time.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import datetime
+import time
+
+
+def str_time():
+    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
+
+
+def bizdate(day):
+    # 获取当前日期
+    current_date = datetime.date.today()
+    # 计算前一天的日期
+    previous_date = current_date - datetime.timedelta(days=day)
+    # 格式化日期输出
+    return previous_date.strftime('%Y%m%d')
+
+
+def t_month(day):
+    # 获取当前日期
+    current_date = datetime.date.today()
+    # 计算前一天的日期
+    previous_date = current_date - datetime.timedelta(days=day)
+    # 格式化日期输出
+    return previous_date.strftime('%Y%m00')
+
+
+def get_ltv_day():
+    day=(1, 2, 3, 4, 5, 6, 7, 14, 30, 60, 90, 150, 300)
+    return ','.join(["'"+bizdate(i)+"'" for i in day])
+
+def get_parameter():
+    return {
+        '${bizdate}': bizdate(1),
+        '${t_month}': t_month(1),
+        '${intra_day}': bizdate(0),
+        '${30_days_later}': bizdate(30),
+        '${300_days_later}': bizdate(301),
+        '${ltv_day}': get_ltv_day(),
+    }
+
+
+if __name__ == "__main__":
+    # hhh = {
+    #     '${bizdate}': bizdate(1),
+    #     '${t_month}': t_month(1),
+    #     '${intra_day}': bizdate(0),
+    #     '${30_days_later}': bizdate(30),
+    #     '${300_days_later}': bizdate(301)
+    # }
+    print(get_ltv_day())
diff --git a/utils/loadconfig.py b/utils/loadconfig.py
new file mode 100644
index 0000000..cf67f71
--- /dev/null
+++ b/utils/loadconfig.py
@@ -0,0 +1,36 @@
+from configparser import ConfigParser
+import os
+
+
+ENVIRONMENT_VARIABLE = os.getenv("ENVIRONMENT_VARIABLE", 996)
+def get_path():
+    current_directory = os.path.dirname(os.path.abspath(__file__))
+    root_path = os.path.abspath(os.path.dirname(current_directory) + os.path.sep + ".")
+    return root_path
+
+
+def get_pg_config():
+    conf = ConfigParser()
+    conf.read(get_path() + '/config/config.ini')
+    return {
+        "database": conf[ENVIRONMENT_VARIABLE]['database'],
+        "user": conf[ENVIRONMENT_VARIABLE]['user'],
+        "password": conf[ENVIRONMENT_VARIABLE]['password'],
+        "host": conf[ENVIRONMENT_VARIABLE]['host'],
+        "port": conf[ENVIRONMENT_VARIABLE]['port'],
+        # 设置默认超时时间statement_timeout=五分钟
+        "options":"-c statement_timeout=500000 -c idle_in_transaction_session_timeout=600000"
+    }
+
+def get_ali_config(ENVIRONMENT_VARIABLE):
+
+    conf = ConfigParser()
+    conf.read(get_path() + '/config/config.ini')
+    return {
+        "access_key_id": conf[ENVIRONMENT_VARIABLE]['access_key_id'],
+        "access_key_secret": conf[ENVIRONMENT_VARIABLE]['access_key_secret'],
+        "endpoint": conf[ENVIRONMENT_VARIABLE]['endpoint']
+    }
+
+if __name__ == '__main__':
+    print(get_ali_config())
diff --git a/utils/log.py b/utils/log.py
new file mode 100644
index 0000000..3650a39
--- /dev/null
+++ b/utils/log.py
@@ -0,0 +1,58 @@
+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')
+        fh.setFormatter(formatter)
+        ch.setFormatter(formatter)
+
+        self.logger.addHandler(fh)
+        self.logger.addHandler(ch)
+
+    def getlog(self):
+        today = datetime.now().strftime("%Y_%m_%d")
+        log_date = os.path.basename(self.log_name).split('.')[0]
+        if today != log_date:
+            self.update_log_file()
+        return self.logger
+
+    def info(self, msg, *args, **kwargs):
+        logger = self.getlog()
+        logger.info(msg, *args, **kwargs)
+
+
+if __name__ == "__main__":
+    log = Log().getlog()
+    log.info("---测试开始----")
+    log.error("操作步骤1,2,3")
+    log.warning("----测试结束----")