fixed the date format issue and add monthly registered users metric

This commit is contained in:
weicao 2025-09-21 15:04:40 +08:00
parent 81628407df
commit 83b9fdbf9e
2 changed files with 34 additions and 18 deletions

View File

@ -19,15 +19,27 @@ class StarRocksMetricsService:
"freeleaps": { "freeleaps": {
"daily_registered_users": """ "daily_registered_users": """
SELECT SELECT
date_id, date,
product_id, product_id,
registered_cnt, value,
updated_at updated_date
FROM dws_daily_registered_users FROM dws_daily_registered_users_test
WHERE date_id >= %s WHERE date >= %s
AND date_id < %s AND date < %s
AND product_id = %s AND product_id = %s
ORDER BY date_id ASC ORDER BY date ASC
""",
"monthly_registered_users": """
SELECT
date,
product_id,
value,
updated_date
FROM dws_monthly_registered_users_test
WHERE date >= %s
AND date < %s
AND product_id = %s
ORDER BY date ASC
""", """,
}, },
"magicleaps": { "magicleaps": {
@ -125,22 +137,22 @@ class StarRocksMetricsService:
# Parse date strings if they are strings # Parse date strings if they are strings
if isinstance(start_date, str): if isinstance(start_date, str):
try: try:
start_dt = datetime.strptime(start_date, '%Y-%m-%d').date() start_dt = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
except ValueError: except ValueError:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail="Invalid start_date format. Expected YYYY-MM-DD" detail="Invalid start_date format. Expected YYYY-MM-DD HH:MM:SS"
) )
else: else:
start_dt = start_date start_dt = start_date
if isinstance(end_date, str): if isinstance(end_date, str):
try: try:
end_dt = datetime.strptime(end_date, '%Y-%m-%d').date() end_dt = datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S')
except ValueError: except ValueError:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail="Invalid end_date format. Expected YYYY-MM-DD" detail="Invalid start_date format. Expected YYYY-MM-DD HH:MM:SS"
) )
else: else:
end_dt = end_date end_dt = end_date
@ -168,7 +180,7 @@ class StarRocksMetricsService:
f"Querying metric '{metric_name}' from product '{product_id}' from {start_dt} to {end_dt}") f"Querying metric '{metric_name}' from product '{product_id}' from {start_dt} to {end_dt}")
# Execute the query # Execute the query
result = self.starrocks_client.execute_query( result = await self.starrocks_client.execute_query(
query=sql_query, query=sql_query,
params=(start_dt, end_dt, product_id) params=(start_dt, end_dt, product_id)
) )
@ -200,17 +212,21 @@ class StarRocksMetricsService:
for row in starrocks_result: for row in starrocks_result:
# Format the date # Format the date
date_value = row.get("date_id") date_value = row.get("date")
if date_value: if date_value:
if isinstance(date_value, str): if isinstance(date_value, str):
date_str = date_value date_str = date_value
else: else:
date_str = str(date_value) # 如果是datetime对象格式化为字符串
if hasattr(date_value, 'strftime'):
date_str = date_value.strftime('%Y-%m-%d %H:%M:%S')
else:
date_str = str(date_value)
else: else:
continue continue
# Get the value # Get the value
value = row.get("registered_cnt", 0) value = row.get("value", 0)
if value is None: if value is None:
value = 0 value = 0

View File

@ -26,8 +26,8 @@ class MetricQueryRequest(BaseModel):
"""Request model for metric query.""" """Request model for metric query."""
product_id: str = Field(..., description="Product ID to identify which product's data to query") product_id: str = Field(..., description="Product ID to identify which product's data to query")
metric_name: str = Field(..., description="Name of the metric to query") metric_name: str = Field(..., description="Name of the metric to query")
start_date: str = Field(..., description="Start date in YYYY-MM-DD format") start_date: str = Field(..., description="Start date in YYYY-MM-DD HH:MM:SS format")
end_date: str = Field(..., description="End date in YYYY-MM-DD format") end_date: str = Field(..., description="End date in YYYY-MM-DD HH:MM:SS format")
router = APIRouter() router = APIRouter()