Mia 6 months ago
parent
commit
d355017180
5 changed files with 97 additions and 27 deletions
  1. BIN
      __pycache__/main.cpython-312.pyc
  2. BIN
      api/__pycache__/db_router.cpython-312.pyc
  3. 26 26
      api/db_router.py
  4. 40 0
      log/app.log
  5. 31 1
      main.py

BIN
__pycache__/main.cpython-312.pyc


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


+ 26 - 26
api/db_router.py

@@ -42,50 +42,46 @@ def add_click_time():
     
 
 @dbRouter.get("/find_brand")
-def find_brand(keyword:str = None,language :str = "ch"):
+def find_brand(keyword:str = None,language :str = "ch",page_num : int = None,page_amount: int = None,search_name : str = None):
 
-    if keyword is None :
-        data, count = supabase.table('101_brand')\
-        .select('*')\
-        .eq("language", language)\
-        .execute()
 
-        result = []
-
-        for shop in data[1] :
-
-
-            json = {
-                "type" : shop["type"],
-                "info" : shop
-            }
-
-            result.append(json)
+    if keyword is None :
+        query = supabase.table('101_brand').select('*').eq("language", language)
+    else :
+        keyword_list = keyword.split(",")
+        query= supabase.table('101_brand').select('*').eq("language", language)
+        for keyword_tmp in keyword_list :
+            query = query.like('tags', f'%{keyword_tmp}%')
 
+    if search_name:
+        query = query.like('name', f'%{search_name}%')
 
-        return {"state":"success","data" : result}
+    result,_ = query.execute()
+    count = len(result[1])
 
-    try :
-        data, count = supabase.table('101_brand')\
-        .select('*')\
-        .eq("language", language)\
-        .like('tags', f'%{keyword}%')\
-        .execute()
+    if page_num and page_amount :
+        offset = (page_num - 1) * page_amount
+        query = query.range(offset, offset + page_amount-1)
 
+    try:
+        data,_ = query.execute()
         result = []
 
         for shop in data[1] :
 
-
             json = {
                 "type" : shop["type"],
                 "info" : shop
             }
 
+            if language != "ch" :
+                if shop["floor"] == "館外" :
+                    json["info"]["floor"] = "outside"
+
             result.append(json)
 
 
-        return {"state":"success","data" : result}
+        return {"state":"success","all_num" : count,"data" : result}
 
     except Exception as e:
         return {"state":"fail","message" :str(e)}
@@ -176,6 +172,10 @@ def read_root(type:str,language :str = "ch"):
     # 從結果中隨機選擇一筆資料
     random_row = choice(result)
 
+    if language != "ch" :
+        if random_row["floor"] == "館外" :
+            random_row["floor"] = "outside"
+
     #print(random_row)
     
     return {"data": random_row}

+ 40 - 0
log/app.log

@@ -0,0 +1,40 @@
+2024-07-10 05:01:02 - root - INFO - Root endpoint was called
+2024-07-10 05:01:33 - root - INFO - Root endpoint was called
+2024-07-10 05:03:29 - uvicorn.error - INFO - Started server process [149550]
+2024-07-10 05:03:29 - uvicorn.error - INFO - Waiting for application startup.
+2024-07-10 05:03:29 - uvicorn.error - INFO - Application startup complete.
+2024-07-10 05:03:29 - uvicorn.error - INFO - Uvicorn running on http://0.0.0.0:9101 (Press CTRL+C to quit)
+2024-07-10 05:06:05 - uvicorn.error - INFO - Shutting down
+2024-07-10 05:06:05 - uvicorn.error - INFO - Waiting for application shutdown.
+2024-07-10 05:06:05 - uvicorn.error - INFO - Application shutdown complete.
+2024-07-10 05:06:05 - uvicorn.error - INFO - Finished server process [149550]
+2024-07-10 05:06:09 - uvicorn.error - INFO - Started server process [149765]
+2024-07-10 05:06:09 - uvicorn.error - INFO - Started server process [149765]
+2024-07-10 05:06:09 - uvicorn.error - INFO - Waiting for application startup.
+2024-07-10 05:06:09 - uvicorn.error - INFO - Waiting for application startup.
+2024-07-10 05:06:09 - uvicorn.error - INFO - Application startup complete.
+2024-07-10 05:06:09 - uvicorn.error - INFO - Application startup complete.
+2024-07-10 05:06:09 - uvicorn.error - INFO - Uvicorn running on http://0.0.0.0:9101 (Press CTRL+C to quit)
+2024-07-10 05:06:09 - uvicorn.error - INFO - Uvicorn running on http://0.0.0.0:9101 (Press CTRL+C to quit)
+2024-07-10 05:06:20 - uvicorn.access - INFO - 172.105.194.225:46250 - "GET /docs HTTP/1.0" 200
+2024-07-10 05:06:20 - uvicorn.access - INFO - 172.105.194.225:46250 - "GET /docs HTTP/1.0" 200
+2024-07-10 05:06:20 - uvicorn.access - INFO - 172.105.194.225:46258 - "GET /openapi.json HTTP/1.0" 200
+2024-07-10 05:06:20 - uvicorn.access - INFO - 172.105.194.225:46258 - "GET /openapi.json HTTP/1.0" 200
+2024-07-10 05:06:22 - uvicorn.access - INFO - 172.105.194.225:46268 - "GET /docs HTTP/1.0" 200
+2024-07-10 05:06:22 - uvicorn.access - INFO - 172.105.194.225:46268 - "GET /docs HTTP/1.0" 200
+2024-07-10 05:06:22 - uvicorn.access - INFO - 172.105.194.225:46280 - "GET /openapi.json HTTP/1.0" 200
+2024-07-10 05:06:22 - uvicorn.access - INFO - 172.105.194.225:46280 - "GET /openapi.json HTTP/1.0" 200
+2024-07-10 05:07:35 - uvicorn.error - INFO - Shutting down
+2024-07-10 05:07:35 - uvicorn.error - INFO - Shutting down
+2024-07-10 05:07:35 - uvicorn.error - INFO - Waiting for application shutdown.
+2024-07-10 05:07:35 - uvicorn.error - INFO - Waiting for application shutdown.
+2024-07-10 05:07:35 - uvicorn.error - INFO - Application shutdown complete.
+2024-07-10 05:07:35 - uvicorn.error - INFO - Application shutdown complete.
+2024-07-10 05:07:35 - uvicorn.error - INFO - Finished server process [149765]
+2024-07-10 05:07:35 - uvicorn.error - INFO - Finished server process [149765]
+2024-07-10 05:07:38 - uvicorn.error - INFO - Started server process [149961]
+2024-07-10 05:07:38 - uvicorn.error - INFO - Waiting for application startup.
+2024-07-10 05:07:38 - uvicorn.error - INFO - Application startup complete.
+2024-07-10 05:07:38 - uvicorn.error - INFO - Uvicorn running on http://0.0.0.0:9101 (Press CTRL+C to quit)
+2024-07-10 05:07:46 - uvicorn.access - INFO - 172.105.194.225:35344 - "GET /docs HTTP/1.0" 200
+2024-07-10 05:07:46 - uvicorn.access - INFO - 172.105.194.225:35354 - "GET /openapi.json HTTP/1.0" 200

+ 31 - 1
main.py

@@ -7,6 +7,35 @@ from datetime import datetime
 from fastapi.staticfiles import StaticFiles
 from datetime import datetime
 from fastapi.responses import RedirectResponse
+import logging
+from logging.handlers import TimedRotatingFileHandler
+
+# 設定日誌配置
+log_folder = 'log'
+log_file = f'{log_folder}/app.log'
+
+# 設定日誌格式
+log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
+date_format = '%Y-%m-%d %H:%M:%S'
+
+# 設定 TimedRotatingFileHandler
+handler = TimedRotatingFileHandler(
+    log_file,
+    when='midnight',
+    interval=1,
+    backupCount=7  # 保留7天的日誌
+)
+handler.setFormatter(logging.Formatter(log_format, datefmt=date_format))
+
+# 設定根日誌
+logging.basicConfig(
+    handlers=[handler],
+    level=logging.INFO,
+    format=log_format,
+    datefmt=date_format
+)
+console_handler = logging.StreamHandler()
+
 
 app = FastAPI()
 
@@ -26,6 +55,7 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
 # 根目錄導向docs
 @app.get("/")
 async def root():
+    logging.info("Root endpoint was called")
     return RedirectResponse(url="/docs#")
 
 
@@ -101,4 +131,4 @@ async def image_check(image_file : UploadFile):
 
 
 if __name__ == "__main__":
-    uvicorn.run("main:app", host="0.0.0.0", port=9101, reload=False)
+    uvicorn.run("main:app", host="0.0.0.0", port=9101, reload=False, log_config=None)