rocket-a


git -C '/home/opc/rocketa.git' show f703852 -- oci_func_prod/result/func.py

commit f70385251317348253f5ebffeffc00e3a1d6d563
Author: Satoshi Ujihara <satoshi_ujihara@fivegate.jp>
Date:   Thu Jan 29 17:46:01 2026 +0900

    gitignore 更新
    compser.json 更新
    本番環境oci_func 作成

diff --git a/oci_func_prod/result/func.py b/oci_func_prod/result/func.py
new file mode 100644
index 0000000..6b19337
--- /dev/null
+++ b/oci_func_prod/result/func.py
@@ -0,0 +1,213 @@
+import io
+import json
+import logging
+import json
+import datetime
+import urllib.parse
+from sqlalchemy import create_engine, text
+
+from fdk import response
+
+logging.basicConfig(level=logging.INFO)
+
+int_param_list = {
+    'ad_id',
+    'client_id',
+    'amount',
+    'sales_count',
+    'stage',
+}
+
+
+def handler(ctx, data: io.BytesIO = None):
+    logging.info("handler started")
+    headers = ctx.Headers()
+
+    full_url = ctx.RequestURL()
+    query_string = urllib.parse.urlparse(full_url).query
+    logging.info(f"■■■28■■■{query_string}")
+    query_params = urllib.parse.parse_qs(query_string)
+
+    DB_HOST = "api.rocket-a.com"
+    DB_USER = "root"
+    DB_PASSWORD = "buSDonry4%h6rm-0fy"
+    DB_NAME = "rocketa-api"
+
+    # 接続URLの作成: mysql+pymysql://user:password@host/dbname
+    DB_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}"
+    engine = create_engine(DB_URL)
+    connection = engine.connect()
+
+    send_param_list = {
+        'sid'          : '',
+        'ad_id'        : '',
+        'client_id'    : '',
+        'uid'          : '',
+        'uid2'         : '',
+        'product_code' : '',
+        'amount'       : '',
+        'sales_count'  : '',
+        'stage'        : '',
+        'domain'       : '',
+        'sender'       : '',
+    }
+    dt_now      = datetime.datetime.now() + datetime.timedelta(hours = 9)
+    format_date = dt_now.strftime('%Y-%m-%d %H:%M:%S')
+    created_ym  = dt_now.strftime('%Y%m')
+    error       = 0
+    parameters  = query_string
+    ip          = ''
+    param_event = ''
+
+    i = 0
+    for key, value_list in query_params.items():
+        logging.info(f"■■■{key}■■■")
+        # 1. キーが格納先のリストに存在するかチェック
+        if key in send_param_list:
+            
+            # 2. 値のリストが空ではないかチェック (あれば処理を続行)
+            if value_list:
+                
+                # 3. リストの最初の要素 [0] を取り出し、格納先の辞書に代入
+                #    (通常、クエリパラメータで同じキーが複数回渡されることは稀なため)
+                send_param_list[key] = value_list[0]
+                i += 1
+            else:
+                pass
+    
+    if i == 0:
+
+        return response.Response(
+            ctx,
+            response_data=json.dumps('Bad Request'),
+            headers={"Content-Type": "application/json"}
+        ) 
+
+
+
+    # ソケット判定 謎の値を送られてきても対応できるように
+    if send_param_list['sender'] != '2' and send_param_list['sender'] != '3':
+        send_param_list['sender'] = 1
+
+        # クライアントのip取得
+        ip = headers.get('x-real-ip')
+
+    # 必須パラメータチェック
+    if send_param_list['sid'] == '':
+        error = 1
+
+    #if send_param_list['ad_id'] == '':
+    #    error = 1
+
+    #if send_param_list['client_id'] == '':
+    #    error = 1
+    
+    # 廃棄イベントのリストに有る場合は処理を止める 
+    if query_params.get('event') is not None:
+        param_event = query_params.get('event')
+        
+        sql_query  = 'SELECT '
+        sql_query += '    id '
+        sql_query += 'FROM '
+        sql_query += '    dispose_events '
+        sql_query += 'WHERE '
+        sql_query += '    event = :event '
+        sql_query += 'LIMIT 1 '
+
+        params = {
+            "event": param_event,
+        }
+        # SQLAlchemyが安全にクエリを組み立て、SQLインジェクションを防ぐ
+        result = connection.execute(text(sql_query), params)
+
+        dispose_event = result.fetchone()
+    
+        if dispose_event:
+            return response.Response(
+                ctx,
+                response_data=json.dumps('Dispose Event'),
+                headers={"Content-Type": "application/json"}
+            ) 
+        
+    # 成果データ保存
+    sql_query  = 'INSERT INTO '
+    sql_query += '    result_records '
+    sql_query += '( '
+    sql_query += '    ip, '
+    sql_query += '    sid, '
+    sql_query += '    ad_id, '
+    sql_query += '    client_id, '
+    sql_query += '    uid, '
+    sql_query += '    uid2, '
+    sql_query += '    product_code, '
+    sql_query += '    amount, '
+    sql_query += '    sales_count, '
+    sql_query += '    stage, '
+    sql_query += '    domain, '
+    sql_query += '    sender, '
+    sql_query += '    parameters, '
+    sql_query += '    created_ym '
+    sql_query += ') VALUES ( '
+    sql_query += '    :ip, '
+    sql_query += '    :sid, '
+    sql_query += '    :ad_id, '
+    sql_query += '    :client_id, '
+    sql_query += '    :uid, '
+    sql_query += '    :uid2, '
+    sql_query += '    :product_code, '
+    sql_query += '    :amount, '
+    sql_query += '    :sales_count, '
+    sql_query += '    :stage, '
+    sql_query += '    :domain, '
+    sql_query += '    :sender, '
+    sql_query += '    :parameters, '
+    sql_query += '    :created_ym '
+    sql_query += ') '
+
+    params = {
+        "ip": ip if ip else None,
+        "sid": send_param_list['sid'] if send_param_list['sid'] else None,
+        "ad_id": int(send_param_list['ad_id']) if send_param_list['ad_id'] else None,
+        "client_id": int(send_param_list['client_id']) if send_param_list['client_id'] else None,
+        "uid": send_param_list['uid'] if send_param_list['uid'] else None,
+        "uid2": send_param_list['uid2'] if send_param_list['uid2'] else None,
+        "product_code": send_param_list['product_code'] if send_param_list['product_code'] else None,
+        "amount": int(send_param_list['amount']) if send_param_list['amount'] else None,
+        "sales_count": int(send_param_list['sales_count']) if send_param_list['sales_count'] else None,
+        "stage": int(send_param_list['stage']) if send_param_list['stage'] else None,
+        "domain": send_param_list['domain'] if send_param_list['domain'] else None,
+        "sender": int(send_param_list['sender']),
+        "parameters": parameters,
+        "created_ym": int(created_ym), 
+    }
+    result = connection.execute(text(sql_query), params)
+
+    # トランザクションをコミットして変更を永続化
+    connection.commit()
+
+
+
+    # 終了ステータス表示
+    if error == 0:
+        return response.Response(
+            ctx,
+            status_code=200,
+            response_data=json.dumps('OK'),
+            headers={"Access-Control-Allow-Origin": "*","Access-Control-Allow-Headers": "Content-Type"}
+        )
+    else:
+        return response.Response(
+            ctx,
+            status_code=200,
+            response_data=json.dumps('Bad Request'),
+            headers={"Access-Control-Allow-Origin": "*","Access-Control-Allow-Headers": "Content-Type"}
+        )
+'''
+    # testdic = send_param_list._asdict()
+    logging.info(send_param_list)
+    return response.Response(
+        ctx,
+        response_data=f"Hello",
+        headers={"Content-Type": "application/json"}
+    ) 
+'''
\ No newline at end of file

diff.txt · 最終更新: by root