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": {
"daily_registered_users": """
SELECT
date_id,
date,
product_id,
registered_cnt,
updated_at
FROM dws_daily_registered_users
WHERE date_id >= %s
AND date_id < %s
AND product_id = %s
ORDER BY date_id ASC
value,
updated_date
FROM dws_daily_registered_users_test
WHERE date >= %s
AND date < %s
AND product_id = %s
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": {
@ -125,22 +137,22 @@ class StarRocksMetricsService:
# Parse date strings if they are strings
if isinstance(start_date, str):
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:
raise HTTPException(
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:
start_dt = start_date
if isinstance(end_date, str):
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:
raise HTTPException(
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:
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}")
# Execute the query
result = self.starrocks_client.execute_query(
result = await self.starrocks_client.execute_query(
query=sql_query,
params=(start_dt, end_dt, product_id)
)
@ -200,17 +212,21 @@ class StarRocksMetricsService:
for row in starrocks_result:
# Format the date
date_value = row.get("date_id")
date_value = row.get("date")
if date_value:
if isinstance(date_value, str):
date_str = date_value
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:
continue
# Get the value
value = row.get("registered_cnt", 0)
value = row.get("value", 0)
if value is None:
value = 0

View File

@ -26,8 +26,8 @@ class MetricQueryRequest(BaseModel):
"""Request model for metric 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")
start_date: str = Field(..., description="Start date in YYYY-MM-DD format")
end_date: str = Field(..., description="End 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 HH:MM:SS format")
router = APIRouter()