Spaces:
Sleeping
Sleeping
ktsn-ud commited on
Commit ·
afca41c
1
Parent(s): 13dac0a
PostHogとの接続・ログ送信コードを追加
Browse files- app/main.py +20 -0
app/main.py
CHANGED
|
@@ -11,6 +11,8 @@ from pydantic import BaseModel
|
|
| 11 |
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
|
|
|
|
|
|
|
| 14 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 15 |
sys.path.append(os.path.dirname(__file__))
|
| 16 |
|
|
@@ -27,6 +29,8 @@ API_KEY_NAME = "X-API-KEY"
|
|
| 27 |
API_SECRET_KEY = os.getenv("API_SECRET_KEY")
|
| 28 |
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=True)
|
| 29 |
|
|
|
|
|
|
|
| 30 |
|
| 31 |
async def get_api_key(key: str = Security(api_key_header)):
|
| 32 |
"""APIキーを検証する依存関係"""
|
|
@@ -54,6 +58,11 @@ async def lifespan(app: FastAPI):
|
|
| 54 |
|
| 55 |
app = FastAPI(lifespan=lifespan)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
@app.get("/", include_in_schema=False)
|
| 59 |
def root():
|
|
@@ -125,4 +134,15 @@ def search(request: SearchRequest):
|
|
| 125 |
)
|
| 126 |
pairs = result # type: ignore
|
| 127 |
ids = [pid for pid, _ in pairs]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
return schema_projects.ProjectIds(projectIds=ids)
|
|
|
|
| 11 |
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
|
| 14 |
+
from posthog import Posthog
|
| 15 |
+
|
| 16 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 17 |
sys.path.append(os.path.dirname(__file__))
|
| 18 |
|
|
|
|
| 29 |
API_SECRET_KEY = os.getenv("API_SECRET_KEY")
|
| 30 |
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=True)
|
| 31 |
|
| 32 |
+
POSTHOG_PROJECT_API_KEY = os.getenv("POSTHOG_PROJECT_API_KEY")
|
| 33 |
+
|
| 34 |
|
| 35 |
async def get_api_key(key: str = Security(api_key_header)):
|
| 36 |
"""APIキーを検証する依存関係"""
|
|
|
|
| 58 |
|
| 59 |
app = FastAPI(lifespan=lifespan)
|
| 60 |
|
| 61 |
+
# PostHog(ログ分析)
|
| 62 |
+
posthog = Posthog(
|
| 63 |
+
project_api_key=POSTHOG_PROJECT_API_KEY, host="https://us.i.posthog.com"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
|
| 67 |
@app.get("/", include_in_schema=False)
|
| 68 |
def root():
|
|
|
|
| 134 |
)
|
| 135 |
pairs = result # type: ignore
|
| 136 |
ids = [pid for pid, _ in pairs]
|
| 137 |
+
|
| 138 |
+
# ログ送信
|
| 139 |
+
posthog.capture(
|
| 140 |
+
event="projects searched",
|
| 141 |
+
properties={
|
| 142 |
+
"query": request.query,
|
| 143 |
+
"result_count": len(ids),
|
| 144 |
+
"$process_person_profile": False,
|
| 145 |
+
},
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
return schema_projects.ProjectIds(projectIds=ids)
|