This commit is contained in:
HuangHai
2026-01-21 08:41:47 +08:00
parent 306a3a4127
commit e51dc18d06
40 changed files with 1309 additions and 965 deletions

View File

@@ -7,357 +7,263 @@ import asyncio
from datetime import datetime
from typing import List, Optional
from fastapi import APIRouter, HTTPException, BackgroundTasks
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import pymysql
# Import custom modules
from Config.Config import OBS_CLOUD_PREFIX, OBS_BUCKET, OBS_TMP_PREFIX, DORIS_HOST, DORIS_PORT, DORIS_USER, DORIS_PWD, DORIS_DATABASE, OBS_SERVER
from Util.DouYinDownloader import DouYinDownloader
from Util.ObsUtil import ObsUploader
from Util.ASRClient import ASRClient
from Util.LlmUtil import get_llm_response
# Logger setup
logger = logging.getLogger(__name__)
router = APIRouter()
# Database connection
def get_db_connection():
return pymysql.connect(
host=DORIS_HOST,
port=DORIS_PORT,
user=DORIS_USER,
password=DORIS_PWD,
database=DORIS_DATABASE,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
class ParseRequest(BaseModel):
text: str
class SummaryRequest(BaseModel):
ids: List[str] = []
def update_status(id, status, error_msg=None):
try:
conn = get_db_connection()
cursor = conn.cursor()
if error_msg:
sql = "UPDATE t_douyin_record SET status=%s, error_msg=%s WHERE id=%s"
cursor.execute(sql, (status, error_msg, id))
else:
sql = "UPDATE t_douyin_record SET status=%s WHERE id=%s"
cursor.execute(sql, (status, id))
conn.commit()
conn.close()
except Exception as e:
logger.error(f"DB Error update_status: {e}")
def update_record(id, title, obs_url, transcript, status):
try:
# Truncate title to 100 chars to fit DB schema (approx 400 bytes max for utf8mb4)
if title and len(title) > 100:
title = title[:100] + "..."
conn = get_db_connection()
cursor = conn.cursor()
sql = """
UPDATE t_douyin_record
SET video_name=%s, obs_url=%s, transcript=%s, status=%s
WHERE id=%s
"""
cursor.execute(sql, (title, obs_url, transcript, status, id))
conn.commit()
conn.close()
except Exception as e:
logger.error(f"DB Error update_record: {e}")
async def process_video_task(url: str, request_id: str, share_text: str = ""):
logger.info(f"Processing task {request_id}")
# 1. Update status
await asyncio.to_thread(update_status, request_id, "PROCESSING")
temp_dir = os.path.abspath(f"temp_{request_id}")
try:
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
# 2. Parse & Download
downloader = DouYinDownloader()
# url is passed directly now
if not url:
raise Exception("No valid URL found")
logger.info(f"Downloading from {url}")
# Run download in thread to avoid blocking main loop
local_video_path, title = await asyncio.to_thread(downloader.download_video, url, temp_dir)
# Title handling strategy:
# Priority 1: Extracted from share text (if available and valid)
# Priority 2: Extracted from video download (often "Unknown Title")
# Priority 3: Generated by LLM (done later)
extracted_title = downloader.extract_title_from_text(share_text)
logger.info(f"Extracted title from text: {extracted_title}")
# If we have a valid extracted title, use it.
# But if we don't have a title yet (or it's Unknown), we definitely want to use extracted_title.
# Even if we have a title from yt-dlp, if it's just "Unknown Title", we prefer extracted one.
if extracted_title and extracted_title != "Unknown Title":
title = extracted_title
elif not title:
title = "Unknown Title"
if not local_video_path or not os.path.exists(local_video_path):
raise Exception("Download failed")
# 3. Upload Video to OBS (Long term storage)
logger.info("Uploading video to OBS...")
uploader = ObsUploader()
video_filename = os.path.basename(local_video_path)
obs_video_key = f"{OBS_CLOUD_PREFIX}/DouYin/{video_filename}"
success, _ = await asyncio.to_thread(uploader.upload_file, obs_video_key, local_video_path, OBS_BUCKET)
if not success:
raise Exception("OBS Upload failed")
# Construct public URL (Assuming standard OBS pattern or Config logic)
obs_url = f"https://{OBS_BUCKET}.{OBS_SERVER}/{obs_video_key}"
# 4. Convert to MP3
logger.info("Converting to MP3...")
mp3_path = os.path.splitext(local_video_path)[0] + ".mp3"
cmd = [
"ffmpeg", "-y", "-i", local_video_path,
"-acodec", "libmp3lame", "-ar", "16000", "-ac", "1", "-q:a", "2",
mp3_path
]
# Run ffmpeg in thread
result = await asyncio.to_thread(subprocess.run, cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
if result.returncode != 0:
raise Exception(f"FFmpeg failed: {result.stderr.decode()}")
# 5. ASR (Upload MP3 to tmp and transcribe)
logger.info("Transcribing...")
asr = ASRClient()
# Run ASR in thread
transcript = await asyncio.to_thread(asr.upload_and_transcribe_sync, mp3_path)
if not transcript:
raise Exception("Transcription failed (returned empty)")
# 6. LLM Title Generation (Enhancement)
# If the title is still Unknown or weak, OR if we just want to ensure we have a good title.
# The user said: "Alternatively, call LlmUtil.py to summarize title".
# Let's do it if title is Unknown or matches default filename pattern, OR if extracted title was also missing.
if (not title or title == "Unknown Title" or title == "Unknown"):
try:
logger.info("Generating title from transcript via LLM...")
prompt = f"请根据以下视频文案总结一个简短的标题(20字以内),不要包含任何解释性文字,直接返回标题:\n\n{transcript[:1000]}"
llm_title_chunks = []
# get_llm_response is already async
async for chunk in get_llm_response(prompt, stream=False):
llm_title_chunks.append(chunk)
llm_title = "".join(llm_title_chunks)
if llm_title:
# Clean up quotes if any
llm_title = llm_title.strip().strip('"').strip('“').strip('”')
logger.info(f"LLM generated title: {llm_title}")
# We overwrite the title if LLM succeeds
title = llm_title
except Exception as llm_e:
logger.warning(f"LLM Title generation failed: {llm_e}")
# 7. Save to DB (Update)
logger.info("Saving to DB...")
await asyncio.to_thread(update_record, request_id, title, obs_url, transcript, "COMPLETED")
logger.info(f"Task {request_id} completed successfully.")
except Exception as e:
logger.error(f"Task {request_id} failed: {e}", exc_info=True)
await asyncio.to_thread(update_status, request_id, "FAILED", str(e))
finally:
# 8. Cleanup
if os.path.exists(temp_dir):
try:
# shutil.rmtree is sync, wrap it
await asyncio.to_thread(shutil.rmtree, temp_dir, ignore_errors=True)
except Exception as e:
logger.error(f"Cleanup failed: {e}")
@router.post("/api/parse")
def parse(request: ParseRequest, background_tasks: BackgroundTasks):
downloader = DouYinDownloader()
urls = downloader.extract_urls(request.text)
if not urls:
# If no URLs found, try using the text as is (might be a direct link not caught by regex)
# But regex is quite broad. Let's just fail or try one.
# Let's assume text might be the URL if it's clean.
if request.text.startswith("http"):
urls = [request.text]
else:
raise HTTPException(status_code=400, detail="No valid URLs found")
created_ids = []
try:
conn = get_db_connection()
cursor = conn.cursor()
for url in urls:
req_id = str(uuid.uuid4())
sql = """
INSERT INTO t_douyin_record (id, original_text, status, create_time)
VALUES (%s, %s, 'PENDING', %s)
"""
cursor.execute(sql, (req_id, url, datetime.now()))
created_ids.append(req_id)
# Pass request.text (the full share text) so we can extract title from it
background_tasks.add_task(process_video_task, url, req_id, request.text)
conn.commit()
conn.close()
except Exception as e:
raise HTTPException(status_code=500, detail=f"DB Init Error: {e}")
return {"id": created_ids[0] if created_ids else None, "ids": created_ids, "status": "PENDING"}
@router.get("/api/records")
def get_records():
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM t_douyin_record ORDER BY create_time DESC LIMIT 50")
records = cursor.fetchall()
conn.close()
# Manually handle datetime serialization to be safe
for r in records:
if 'create_time' in r and r['create_time']:
r['create_time'] = r['create_time'].strftime("%Y-%m-%d %H:%M:%S")
if 'update_time' in r and r['update_time']:
r['update_time'] = r['update_time'].strftime("%Y-%m-%d %H:%M:%S")
return records
except Exception as e:
logger.error(f"Get records error: {e}", exc_info=True)
return []
@router.delete("/api/records/{id}")
def delete_record(id: str):
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("DELETE FROM t_douyin_record WHERE id=%s", (id,))
conn.commit()
conn.close()
return {"status": "deleted"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/api/douyin/summary")
async def generate_summary(request: SummaryRequest):
try:
# Fetch transcripts
conn = get_db_connection()
cursor = conn.cursor()
if request.ids:
# Secure way to handle list in SQL
format_strings = ','.join(['%s'] * len(request.ids))
sql = f"SELECT video_name, transcript FROM t_douyin_record WHERE id IN ({format_strings}) AND status='COMPLETED'"
cursor.execute(sql, tuple(request.ids))
else:
# Default to latest 20
cursor.execute("SELECT video_name, transcript FROM t_douyin_record WHERE status='COMPLETED' ORDER BY create_time DESC LIMIT 20")
records = cursor.fetchall()
conn.close()
if not records:
# If no records, just return a simple message stream
async def empty_stream():
yield "未找到可总结的已完成记录,请先解析视频。"
return StreamingResponse(empty_stream(), media_type="text/event-stream")
# Prepare text
full_text = ""
for r in records:
if r['transcript']:
full_text += f"【标题:{r['video_name']}】\n内容:{r['transcript']}\n\n"
if not full_text:
async def empty_text_stream():
yield "记录中没有有效的文案内容。"
return StreamingResponse(empty_text_stream(), media_type="text/event-stream")
# Prompt
prompt = f"""
请对以下充电行业相关的视频内容进行知识精华提取。
要求:
1. 忽略无关闲聊和口语化表达;
2. 按条目列出核心知识点,不要长篇大论;
3. 保持简洁专业,只保留干货;
4. 返回格式为Markdown列表。
内容如下:
{full_text[:15000]}
"""
# Limit context to avoid errors, 15000 chars is roughly safe for most models,
# but if using a small model, might need less. Assuming robust model.
return StreamingResponse(get_llm_response(prompt), media_type="text/event-stream")
except Exception as e:
logger.error(f"Summary generation failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
async def recover_pending_tasks():
"""
Check for tasks stuck in PENDING or PROCESSING state (due to server restart)
and restart them.
"""
logger.info("Scanning for interrupted Douyin tasks...")
try:
# Use asyncio.to_thread for DB operation
def fetch_pending():
conn = get_db_connection()
cursor = conn.cursor()
# Select recent pending/processing tasks (limit 20 to avoid storm)
sql = """
SELECT id, original_text, status
FROM t_douyin_record
WHERE status IN ('PENDING', 'PROCESSING')
ORDER BY create_time DESC LIMIT 20
"""
cursor.execute(sql)
tasks = cursor.fetchall()
conn.close()
return tasks
tasks = await asyncio.to_thread(fetch_pending)
if not tasks:
logger.info("No interrupted tasks found.")
return
logger.info(f"Found {len(tasks)} interrupted tasks. Restarting...")
for task in tasks:
req_id = task['id']
url = task['original_text']
# Restart task in background
# Note: We lost the original share text for title extraction,
# so we pass empty string. It will use the URL or 'Unknown Title'.
# If LLM is enabled, it might fix the title later.
asyncio.create_task(process_video_task(url, req_id, share_text=""))
except Exception as e:
from fastapi import APIRouter, HTTPException, BackgroundTasks
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
# Import custom modules
from Config.Config import OBS_CLOUD_PREFIX, OBS_BUCKET, OBS_TMP_PREFIX, OBS_SERVER
from Util.DouYinDownloader import DouYinDownloader
from Util.ObsUtil import ObsUploader
from Util.ASRClient import ASRClient
from Util.LlmUtil import get_llm_response
from Model.DouYinModel import DouYinModel
# Logger setup
logger = logging.getLogger(__name__)
router = APIRouter()
class ParseRequest(BaseModel):
text: str
class SummaryRequest(BaseModel):
ids: List[str] = []
async def process_video_task(url: str, request_id: str, share_text: str = ""):
logger.info(f"Processing task {request_id}")
model = DouYinModel()
# 1. Update status
await model.update_status(request_id, "PROCESSING")
temp_dir = os.path.abspath(f"temp_{request_id}")
try:
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
# 2. Parse & Download
downloader = DouYinDownloader()
# url is passed directly now
if not url:
raise Exception("No valid URL found")
logger.info(f"Downloading from {url}")
# Run download in thread to avoid blocking main loop
local_video_path, title = await asyncio.to_thread(downloader.download_video, url, temp_dir)
# Title handling strategy:
# Priority 1: Extracted from share text (if available and valid)
# Priority 2: Extracted from video download (often "Unknown Title")
# Priority 3: Generated by LLM (done later)
extracted_title = downloader.extract_title_from_text(share_text)
logger.info(f"Extracted title from text: {extracted_title}")
# If we have a valid extracted title, use it.
# But if we don't have a title yet (or it's Unknown), we definitely want to use extracted_title.
# Even if we have a title from yt-dlp, if it's just "Unknown Title", we prefer extracted one.
if extracted_title and extracted_title != "Unknown Title":
title = extracted_title
elif not title:
title = "Unknown Title"
if not local_video_path or not os.path.exists(local_video_path):
raise Exception("Download failed")
# 3. Upload Video to OBS (Long term storage)
logger.info("Uploading video to OBS...")
uploader = ObsUploader()
video_filename = os.path.basename(local_video_path)
obs_video_key = f"{OBS_CLOUD_PREFIX}/DouYin/{video_filename}"
success, _ = await asyncio.to_thread(uploader.upload_file, obs_video_key, local_video_path, OBS_BUCKET)
if not success:
raise Exception("OBS Upload failed")
# Construct public URL (Assuming standard OBS pattern or Config logic)
obs_url = f"https://{OBS_BUCKET}.{OBS_SERVER}/{obs_video_key}"
# 4. Convert to MP3
logger.info("Converting to MP3...")
mp3_path = os.path.splitext(local_video_path)[0] + ".mp3"
cmd = [
"ffmpeg", "-y", "-i", local_video_path,
"-acodec", "libmp3lame", "-ar", "16000", "-ac", "1", "-q:a", "2",
mp3_path
]
# Run ffmpeg in thread
result = await asyncio.to_thread(subprocess.run, cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
if result.returncode != 0:
raise Exception(f"FFmpeg failed: {result.stderr.decode()}")
# 5. ASR (Upload MP3 to tmp and transcribe)
logger.info("Transcribing...")
asr = ASRClient()
# Run ASR in thread
transcript = await asyncio.to_thread(asr.upload_and_transcribe_sync, mp3_path)
if not transcript:
raise Exception("Transcription failed (returned empty)")
# 6. LLM Title Generation (Enhancement)
# If the title is still Unknown or weak, OR if we just want to ensure we have a good title.
# The user said: "Alternatively, call LlmUtil.py to summarize title".
# Let's do it if title is Unknown or matches default filename pattern, OR if extracted title was also missing.
if (not title or title == "Unknown Title" or title == "Unknown"):
try:
logger.info("Generating title from transcript via LLM...")
prompt = f"请根据以下视频文案总结一个简短的标题(20字以内),不要包含任何解释性文字,直接返回标题:\n\n{transcript[:1000]}"
llm_title_chunks = []
# get_llm_response is already async
async for chunk in get_llm_response(prompt, stream=False):
llm_title_chunks.append(chunk)
llm_title = "".join(llm_title_chunks)
if llm_title:
# Clean up quotes if any
llm_title = llm_title.strip().strip('"').strip('“').strip('”')
logger.info(f"LLM generated title: {llm_title}")
# We overwrite the title if LLM succeeds
title = llm_title
except Exception as llm_e:
logger.warning(f"LLM Title generation failed: {llm_e}")
# 7. Save to DB (Update)
logger.info("Saving to DB...")
await model.update_record(request_id, title, obs_url, transcript, "COMPLETED")
logger.info(f"Task {request_id} completed successfully.")
except Exception as e:
logger.error(f"Task {request_id} failed: {e}", exc_info=True)
await model.update_status(request_id, "FAILED", str(e))
finally:
# 8. Cleanup
if os.path.exists(temp_dir):
try:
# shutil.rmtree is sync, wrap it
await asyncio.to_thread(shutil.rmtree, temp_dir, ignore_errors=True)
except Exception as e:
logger.error(f"Cleanup failed: {e}")
@router.post("/api/parse")
async def parse(request: ParseRequest, background_tasks: BackgroundTasks):
downloader = DouYinDownloader()
urls = downloader.extract_urls(request.text)
if not urls:
# If no URLs found, try using the text as is (might be a direct link not caught by regex)
# But regex is quite broad. Let's just fail or try one.
# Let's assume text might be the URL if it's clean.
if request.text.startswith("http"):
urls = [request.text]
else:
raise HTTPException(status_code=400, detail="No valid URLs found")
created_ids = []
try:
model = DouYinModel()
for url in urls:
req_id = str(uuid.uuid4())
await model.insert_record(req_id, url)
created_ids.append(req_id)
# Pass request.text (the full share text) so we can extract title from it
background_tasks.add_task(process_video_task, url, req_id, request.text)
except Exception as e:
raise HTTPException(status_code=500, detail=f"DB Init Error: {e}")
return {"id": created_ids[0] if created_ids else None, "ids": created_ids, "status": "PENDING"}
@router.get("/api/records")
async def get_records():
try:
model = DouYinModel()
records = await model.get_records()
return records
except Exception as e:
logger.error(f"Get records error: {e}", exc_info=True)
return []
@router.delete("/api/records/{id}")
async def delete_record(id: str):
try:
model = DouYinModel()
await model.delete_record(id)
return {"status": "deleted"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/api/douyin/summary")
async def generate_summary(request: SummaryRequest):
try:
# Fetch transcripts
model = DouYinModel()
records = await model.get_transcripts(ids=request.ids)
if not records:
# If no records, just return a simple message stream
async def empty_stream():
yield "未找到可总结的已完成记录,请先解析视频。"
return StreamingResponse(empty_stream(), media_type="text/event-stream")
# Prepare text
full_text = ""
for r in records:
if r['transcript']:
full_text += f"【标题:{r['video_name']}】\n内容:{r['transcript']}\n\n"
if not full_text:
async def empty_text_stream():
yield "记录中没有有效的文案内容。"
return StreamingResponse(empty_text_stream(), media_type="text/event-stream")
# Prompt
prompt = f"""
请对以下充电行业相关的视频内容进行知识精华提取。
要求:
1. 忽略无关闲聊和口语化表达;
2. 按条目列出核心知识点,不要长篇大论;
3. 保持简洁专业,只保留干货;
4. 返回格式为Markdown列表。
内容如下:
{full_text[:15000]}
"""
# Limit context to avoid errors, 15000 chars is roughly safe for most models,
# but if using a small model, might need less. Assuming robust model.
return StreamingResponse(get_llm_response(prompt), media_type="text/event-stream")
except Exception as e:
logger.error(f"Summary generation failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
async def recover_pending_tasks():
"""
Check for tasks stuck in PENDING or PROCESSING state (due to server restart)
and restart them.
"""
logger.info("Scanning for interrupted Douyin tasks...")
try:
model = DouYinModel()
tasks = await model.get_interrupted_tasks()
if not tasks:
logger.info("No interrupted tasks found.")
return
logger.info(f"Found {len(tasks)} interrupted tasks. Restarting...")
for task in tasks:
req_id = task['id']
url = task['original_text']
# Restart task in background
# Note: We lost the original share text for title extraction,
# so we pass empty string. It will use the URL or 'Unknown Title'.
# If LLM is enabled, it might fix the title later.
asyncio.create_task(process_video_task(url, req_id, share_text=""))
except Exception as e:
logger.error(f"Failed to recover tasks: {e}", exc_info=True)

View File

@@ -9,7 +9,7 @@ from sqlalchemy.sql import text
from Util.BananaClient import BananaClient
from Util.LlmUtil import get_llm_response
from DbKit.Db import Db
from Model.HaiBaoModel import HaiBaoModel
router = APIRouter(prefix="/haibao")
logger = logging.getLogger("HaiBaoController")
@@ -21,43 +21,8 @@ class GenerateRequest(BaseModel):
@router.on_event("startup")
async def startup_event():
"""初始化时检查并创建表"""
db = Db()
await db.init_db()
# Doris 建表语句
create_table_sql = """
CREATE TABLE IF NOT EXISTS haibao_history (
id VARCHAR(50) COMMENT "ID",
prompt TEXT COMMENT "提示词",
image_url VARCHAR(500) COMMENT "图片URL",
scheme_content TEXT COMMENT "文案方案",
created_at DATETIME COMMENT "创建时间"
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES (
"replication_num" = "1"
);
"""
try:
# 使用 engine 直接执行 DDL
async with db.engine.begin() as conn:
await conn.execute(text(create_table_sql))
# 尝试添加列(如果表已存在但列不存在)
# Doris 不支持 IF NOT EXISTS for ADD COLUMN directly nicely in all versions without error if exists
# 所以这里简单捕获异常,如果列已存在则忽略
try:
alter_sql = "ALTER TABLE haibao_history ADD COLUMN scheme_content TEXT COMMENT '文案方案'"
await conn.execute(text(alter_sql))
except Exception as e:
# 忽略列已存在的错误
pass
logger.info("海报历史表检查/更新成功")
except Exception as e:
logger.error(f"海报历史表创建/更新失败: {e}")
"""初始化时逻辑"""
logger.info("海报控制器启动成功")
class RefineRequest(BaseModel):
prompt: str
@@ -184,26 +149,11 @@ async def generate_poster(req: GenerateRequest):
image_url, scheme_content = await asyncio.gather(generate_image_task(), generate_text_task())
# 4. 保存到数据库
db = Db()
model = HaiBaoModel()
record_id = str(uuid.uuid4())
created_at = datetime.now()
insert_sql = """
INSERT INTO haibao_history (id, prompt, image_url, scheme_content, created_at)
VALUES (:id, :prompt, :image_url, :scheme_content, :created_at)
"""
params = {
"id": record_id,
"prompt": req.prompt,
"image_url": image_url,
"scheme_content": scheme_content,
"created_at": created_at
}
async with await db.get_session() as session:
async with session.begin():
await session.execute(text(insert_sql), params)
await model.insert_record(record_id, req.prompt, image_url, scheme_content, created_at)
return {
"id": record_id,
@@ -220,10 +170,9 @@ async def generate_poster(req: GenerateRequest):
@router.get("/history")
async def get_history():
"""获取海报生成历史"""
db = Db()
sql = "SELECT * FROM haibao_history ORDER BY created_at DESC LIMIT 50"
try:
result = await db.find(sql)
model = HaiBaoModel()
result = await model.get_history(50)
formatted_result = []
for item in result:

View File

@@ -6,7 +6,6 @@ from typing import List, Optional, Dict, Any
from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse, FileResponse, JSONResponse
from DbKit.Db import Db
from Config.Config import DB_URL
from Util.LlmUtil import get_llm_response
from Tools.T6_Export import export_excel, DorisExcelExporter, extract_hourly_prices_from_schedule
@@ -24,31 +23,35 @@ from Model.YltAnalyticsModel import (
PriceSeries,
PriceComparisonResponse,
PriceComparisonSummary,
YltAnalyticsModel,
)
from DbKit.Db import Db
router = APIRouter()
db = Db(db_url=DB_URL)
# db = Db(db_url=DB_URL) # Removed direct db instance
async def init_db():
db = Db()
await db.init_db()
async def close_db():
db = Db()
await db.close()
@router.get("/api/operators/hourly-prices")
async def get_operators_hourly_prices():
operators = ["新电途", "特来电", "驿来特", "艾特吉易充"]
exporter = DorisExcelExporter(db_url=DB_URL)
await exporter.init()
model = YltAnalyticsModel()
try:
result = []
for op in operators:
rows = await exporter.fetch_current_station_rows(op)
rows = await model.fetch_current_station_rows(op)
if not rows:
result.append({"operator": op, "series": [None] * 24})
continue
@@ -71,11 +74,72 @@ async def get_operators_hourly_prices():
else:
avg_series.append(None)
result.append({"operator": op, "series": avg_series})
finally:
await exporter.close()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
return {"operators": result}
@router.get("/api/operators/price-trends")
async def get_operators_price_trends(days: int = 7):
operators = ["新电途", "特来电", "驿来特", "艾特吉易充"]
model = YltAnalyticsModel()
rows = await model.get_operators_price_trends(days)
# 数据结构: { operator: { date_str: [sums_of_24h, counts_of_24h] } }
trend_data = {}
for op in operators:
trend_data[op] = {}
for row in rows:
op = row.get("operator")
if op not in trend_data:
continue
d_str = str(row.get("date_str"))
schedule_json = row.get("schedule_json")
if d_str not in trend_data[op]:
trend_data[op][d_str] = {"sums": [0.0] * 24, "counts": [0] * 24}
series = extract_hourly_prices_from_schedule(schedule_json)
for i in range(24):
v = series[i]
if v is not None:
trend_data[op][d_str]["sums"][i] += float(v)
trend_data[op][d_str]["counts"][i] += 1
# 转换为 ECharts 友好格式
# 1. 获取所有日期并排序
all_dates = sorted(list(set(str(row.get("date_str")) for row in rows)))
# 2. 为每个运营商计算每天的平均价格(24小时的平均值)
series_result = []
for op in operators:
op_trend = []
for d in all_dates:
if d in trend_data[op]:
day_stats = trend_data[op][d]
day_avg_sum = 0.0
day_hour_count = 0
for i in range(24):
if day_stats["counts"][i] > 0:
day_avg_sum += (day_stats["sums"][i] / day_stats["counts"][i])
day_hour_count += 1
if day_hour_count > 0:
op_trend.append(round(day_avg_sum / day_hour_count, 4))
else:
op_trend.append(None)
else:
op_trend.append(None)
series_result.append({"name": op, "data": op_trend})
return {
"dates": all_dates,
"series": series_result
}
@router.get("/api/export/prices-zip")
async def export_prices_zip():
operators = ["新电途", "特来电", "驿来特", "艾特吉易充"]
@@ -177,26 +241,8 @@ async def ai_pricing_strategy_summary():
@router.get("/api/ylt/stations", response_model=List[StationBase])
async def list_ylt_stations(q: Optional[str] = None):
base_sql = """
SELECT
p.station_hash,
p.operator,
p.station_name,
p.address,
p.coord_x,
p.coord_y,
s.current_price
FROM t_station_profile_scd p
LEFT JOIN t_station_status_scd s
ON p.station_hash = s.station_hash AND s.is_current = 1
WHERE p.operator = '驿来特'
"""
params: Dict[str, Any] = {}
if q:
base_sql += " AND (p.station_name LIKE :kw OR p.address LIKE :kw)"
params["kw"] = f"%{q}%"
base_sql += " ORDER BY p.station_name"
rows = await db.find(base_sql, params)
model = YltAnalyticsModel()
rows = await model.list_ylt_stations(q)
result: List[StationBase] = []
for r in rows:
result.append(
@@ -225,21 +271,8 @@ def haversine_km(lon1: float, lat1: float, lon2: float, lat2: float) -> float:
async def fetch_current_stations() -> List[dict]:
sql = """
SELECT
p.station_hash,
p.operator,
p.station_name,
p.address,
p.coord_x,
p.coord_y,
s.current_price
FROM t_station_profile_scd p
LEFT JOIN t_station_status_scd s
ON p.station_hash = s.station_hash AND s.is_current = 1
WHERE p.is_current = 1
"""
return await db.find(sql, {})
model = YltAnalyticsModel()
return await model.fetch_current_stations()
async def build_geo_competition(station_hash: str, radius_km: float = 3.0) -> GeoCompetitionResponse:
@@ -318,17 +351,8 @@ async def build_geo_competition(station_hash: str, radius_km: float = 3.0) -> Ge
async def fetch_station_schedule_json(station_hash: str) -> Optional[str]:
sql = """
SELECT schedule_json
FROM t_station_price_schedule_scd
WHERE station_hash = :h AND is_current = 1
ORDER BY valid_start_time DESC
LIMIT 1
"""
rows = await db.find(sql, {"h": station_hash})
if not rows:
return None
value = rows[0].get("schedule_json")
model = YltAnalyticsModel()
value = await model.fetch_station_schedule_json(station_hash)
if value is None:
return None
if isinstance(value, str):