Browse Source

kuma 監測、log

Mia 5 months ago
parent
commit
e33a5d57cd
8 changed files with 92 additions and 12 deletions
  1. 0 0
      101_restart.sh
  2. 0 0
      README.md
  3. BIN
      __pycache__/main.cpython-312.pyc
  4. BIN
      api/__pycache__/db_router.cpython-312.pyc
  5. 81 12
      api/db_router.py
  6. 0 0
      credentials.json
  7. 11 0
      main.py
  8. 0 0
      requirements.txt

+ 0 - 0
101_restart.sh


+ 0 - 0
README.md


BIN
__pycache__/main.cpython-312.pyc


BIN
api/__pycache__/db_router.cpython-312.pyc


+ 81 - 12
api/db_router.py

@@ -23,18 +23,60 @@ dbRouter = APIRouter()
 
 
 @dbRouter.get("/click")
-def add_click_time():
+def add_click_time(click_type :str = None,brand:str = None):
 
     try:
+        if click_type == None :
 
-        response = supabase.table('click_time').select("*").execute()
+            response = supabase.table('click_time').select("*").eq('id', 1).execute()
 
-        click_time  = response.data[0]['click_time'] + 1
+            click_time  = response.data[0]['click_time'] + 1
     
-        data, count = supabase.table('click_time') \
-            .update({'click_time':click_time,'update_time':str(datetime.now())})\
-            .eq('id', 1)\
-            .execute()
+            data, count = supabase.table('click_time') \
+                .update({'click_time':click_time,'update_time':str(datetime.now())})\
+                .eq('id', 1)\
+                .execute()
+            
+        elif click_type == "card" :
+            response = supabase.table('click_time').select("*").eq('id', 2).execute()
+
+            click_time  = response.data[0]['click_time'] + 1
+    
+            data, count = supabase.table('click_time') \
+                .update({'click_time':click_time,'update_time':str(datetime.now())})\
+                .eq('id', 2)\
+                .execute()
+            
+        elif click_type == "card-en" :
+            response = supabase.table('click_time').select("*").eq('name', "國際貴賓卡-en").execute()
+
+            click_time  = response.data[0]['click_time'] + 1
+            print(response)
+    
+            data, count = supabase.table('click_time') \
+                .update({'click_time':click_time,'update_time':str(datetime.now())})\
+                .eq('id', 3)\
+                .execute()
+            
+        elif click_type == "click_url" and brand:
+            response = supabase.table('101_brand').select("*").eq("name",brand).execute()
+
+            click_time  = response.data[0][click_type] + 1
+    
+            data, count = supabase.table('101_brand') \
+                .update({click_type:click_time})\
+                .eq("name",brand)\
+                .execute()
+            
+        elif click_type == "click_address" and brand:
+            response = supabase.table('101_brand').select("*").eq("name",brand).execute()
+
+            click_time  = response.data[0][click_type] + 1
+    
+            data, count = supabase.table('101_brand') \
+                .update({click_type:click_time})\
+                .eq("name",brand)\
+                .execute()
         
         return {"state":"success","click_time" : click_time}
 
@@ -58,7 +100,7 @@ def find_brand(keyword:str = None,language :str = "ch",page_num : int = None,pag
     if search_name:
         query = query.like('name', f'%{search_name}%')
 
-    result,_ = query.execute()
+    result,_ = query.order("id", desc=True).execute()
     count = len(result[1])
 
     if page_num and page_amount :
@@ -167,6 +209,7 @@ def read_root(type:str,language :str = "ch"):
         .eq("floor","館外")\
         .eq("language", language)\
         .or_(f"tags.ilike.%{keyword1}%,tags.ilike.%{keyword2}%")\
+        .order("id", desc=True)\
         .execute()
     
     result = data[1]
@@ -183,16 +226,38 @@ def read_root(type:str,language :str = "ch"):
     return {"data": random_row}
 
 @dbRouter.post("/message_not_in_cache")
-def message_not_in_cache(question :str ,answer :str,client_id : str = "0" ):
+def message_not_in_cache(question :str ,answer :str,data_list :str='[]',client_id : str = "0" ):
 
     try:
         data, count = supabase.table('client_message').select('*').eq("question",question).execute()
-
+        
         if len(data[1]) != 0 :
             return {"state": 200 , "message" : "have saved"}
         
         data, count = supabase.table('client_message').insert({"client_id": client_id, "question": question,"answer":answer}).execute()
-        
+        return {"state": 200 , "message" : "success"}
+    
+    except Exception as e:
+
+        return {"state": 500 , "message" : str(e)}
+    
+from typing import List, Optional
+from pydantic import BaseModel
+    
+class MessageSaveRequest(BaseModel):
+    question: str
+    answer: str
+    data_list: str
+
+    
+@dbRouter.post("/message_save")
+def message_save(request:MessageSaveRequest):
+    try :
+        data, count = supabase.table("log_record").insert({
+            "question": request.question,
+            "answer": request.answer,
+            "data_list": request.data_list
+        }).execute()
         return {"state": 200 , "message" : "success"}
     
     except Exception as e:
@@ -268,7 +333,7 @@ def insert_table(data: dataform):
         email_content = f"問題類別:{response[1][0]['type']}\n標題:{response[1][0]['title']}\n內容:{response[1][0]['content']}\n姓名:{response[1][0]['client_name']}\n性別:{response[1][0]['gender']}\n電子郵件:{response[1][0]['email']}\n聯絡電話:{response[1][0]['phone']}\n"
 
         try:
-            send_email("steven@choozmo.com","mia@choozmo.com",str(email_content)) # mallservice@tfc101.com.tw
+            send_email("mallservice@tfc101.com.tw","mia@choozmo.com",str(email_content)) # mallservice@tfc101.com.tw
         except Exception as e:
             print(str(e))
 
@@ -316,8 +381,12 @@ def video_cache(client_message :str ):
 
         result = ask_question(client_message)
 
+        # result[0]["answer"]
+
         if result == None :
             return {"state": 500 , "message" : "no data"}
+
+        data, count = supabase.table("log_record").insert({"question":client_message, "answer":result[0]["answer"]}).execute()
         
         return {"state": 200 , "message" : result }
     

+ 0 - 0
credentials.json


+ 11 - 0
main.py

@@ -10,6 +10,11 @@ from fastapi.responses import RedirectResponse
 import logging
 from logging.handlers import TimedRotatingFileHandler
 from urllib.parse import unquote
+import requests as r
+
+param = 'QHLPQyttnL'
+result = r.get('http://cmm.ai:3001/api/push/' + param + '?status=up&msg=OK&ping=')
+# http://cmm.ai:3001/api/push/cDJyD2r3ae?status=up&msg=OK&ping=
 
 log_folder = 'log'
 log_file = f'{log_folder}/app.log'
@@ -180,5 +185,11 @@ scheduler.start()
 def shutdown_event():
     scheduler.shutdown()
 
+
+from starlette.responses import JSONResponse
+@app.get("/health") # 使用網址給 kuma 監測
+async def health_check():
+    return JSONResponse(content={"status": "ok"}, status_code=200)
+
 if __name__ == "__main__":
     uvicorn.run("main:app", host="0.0.0.0", port=9101, reload=False, log_config=None)

+ 0 - 0
requirements.txt