fixed some code standard

This commit is contained in:
weicao 2025-09-19 15:32:00 +08:00
parent aa6dc2feab
commit b7858c193e
7 changed files with 26 additions and 86 deletions

View File

@ -16,21 +16,15 @@ COPY local.env .
# Copy application code
COPY . .
ENV MONGODB_NAME = "freeleaps2"
ENV MONGODB_URI = "mongodb://freeleaps2-mongodb:27017"
# StarRocks settings
ENV STARROCKS_HOST: str = "freeleaps-starrocks-cluster-fe-service.freeleaps-data-platform.svc"
ENV STARROCKS_PORT: int = 9030
ENV STARROCKS_USER: str = "root"
ENV STARROCKS_PASSWORD: str = ""
ENV STARROCKS_DATABASE: str = "freeleaps"
#app_settings
ENV GITEA_TOKEN = ""
ENV GITEA_URL = ""
ENV GITEA_DEPOT_ORGANIZATION = ""
ENV CODE_DEPOT_HTTP_PORT = ""
ENV CODE_DEPOT_SSH_PORT = ""
ENV CODE_DEPOT_DOMAIN_NAME = ""
#log_settings
ENV LOG_BASE_PATH = "./logs"
ENV BACKEND_LOG_FILE_NAME = "freeleaps-metrics"
ENV APPLICATION_ACTIVITY_LOG = "freeleaps-metrics-activity"
# Prometheus settings
ENV PROMETHEUS_ENDPOINT: str = "http://localhost:9090"
# Expose port
EXPOSE 8009

View File

@ -1,7 +1,7 @@
import pymysql
from typing import List, Dict, Any, Optional
from datetime import date
from loguru import logger
from common.log.module_logger import ModuleLogger
from common.config.app_settings import app_settings
@ -15,8 +15,9 @@ class StarRocksClient:
self.password = app_settings.STARROCKS_PASSWORD
self.database = app_settings.STARROCKS_DATABASE
self.connection = None
self.module_logger = ModuleLogger(__file__)
def connect(self) -> bool:
async def connect(self) -> bool:
"""Establish connection to StarRocks database"""
try:
self.connection = pymysql.connect(
@ -28,63 +29,32 @@ class StarRocksClient:
charset='utf8mb4',
autocommit=True
)
logger.info(f"Successfully connected to StarRocks at {self.host}:{self.port}")
await self.module_logger.log_info(f"Successfully connected to StarRocks at {self.host}:{self.port}")
return True
except Exception as e:
logger.error(f"Failed to connect to StarRocks: {e}")
await self.module_logger.log_error(f"Failed to connect to StarRocks: {e}")
return False
def disconnect(self):
async def disconnect(self):
"""Close database connection"""
if self.connection:
self.connection.close()
self.connection = None
logger.info("Disconnected from StarRocks")
await self.module_logger.log_info("Disconnected from StarRocks")
def execute_query(self, query: str, params: Optional[tuple] = None) -> List[Dict[str, Any]]:
async def execute_query(self, query: str, params: Optional[tuple] = None) -> List[Dict[str, Any]]:
"""Execute SQL query and return results"""
if not self.connection:
if not self.connect():
if not await self.connect():
raise Exception("Failed to connect to StarRocks database")
try:
with self.connection.cursor(pymysql.cursors.DictCursor) as cursor:
cursor.execute(query, params)
results = cursor.fetchall()
logger.info(f"Query executed successfully, returned {len(results)} rows")
await self.module_logger.log_info(f"Query executed successfully, returned {len(results)} rows")
return results
except Exception as e:
logger.error(f"Query execution failed: {e}")
await self.module_logger.log_error(f"Query execution failed: {e}")
raise e
def get_daily_registered_users(
self,
start_date: date,
end_date: date,
product_id: str = "freeleaps"
) -> List[Dict[str, Any]]:
"""Query daily registered users from StarRocks"""
query = """
SELECT
date_id,
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
"""
params = (start_date, end_date, product_id)
return self.execute_query(query, params)
def __enter__(self):
"""Context manager entry"""
self.connect()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit"""
self.disconnect()

View File

@ -1,26 +0,0 @@
from pydantic import BaseModel
from datetime import date, datetime
from typing import List, Optional
class DailyRegisteredUsers(BaseModel):
"""Daily registered users data model"""
date_id: date
product_id: str = "freeleaps"
registered_cnt: int
updated_at: Optional[datetime] = None
class UserRegistrationQuery(BaseModel):
"""Query parameters for user registration data"""
start_date: date
end_date: date
product_id: str = "freeleaps"
class UserRegistrationResponse(BaseModel):
"""Response model for user registration data"""
dates: List[str]
counts: List[int]
total_registrations: int
query_period: str

View File

@ -264,5 +264,5 @@ class StarRocksMetricsService:
"product_id": product_id,
"metric_name": metric_name,
"sql_query": self.METRIC_SQL_MAP[product_id][metric_name].strip(),
"description": "Daily registered users count from StarRocks table dws_daily_registered_users"
"description": f"{metric_name} count from StarRocks table dws_{metric_name}"
}

View File

@ -29,7 +29,7 @@ class AppSettings(BaseSettings):
class Config:
env_file = ".env"
env_file = "local.env"
app_settings = AppSettings()

View File

@ -3,12 +3,14 @@ from fastapi.responses import RedirectResponse
import uvicorn
from webapi.bootstrap.application import create_app
from webapi.routes.metrics import registration_metrics
from webapi.routes.starrocks_metrics import api_router as starrocks_metrics_router
from webapi.routes.prometheus_metrics import api_router as prometheus_metrics_router
app = create_app()
# Include routers
app.include_router(registration_metrics.router)
app.include_router(starrocks_metrics_router, prefix="/metrics", tags=["starrocks-metrics"])
app.include_router(prometheus_metrics_router, prefix="/metrics", tags=["prometheus-metrics"])
@app.get("/", status_code=301)