Feat: add revenue calculation button and better logging
Browse files- backend/app/api/tasks.py +22 -6
- frontend/src/api/index.js +1 -0
- frontend/src/views/Crawl.vue +28 -0
backend/app/api/tasks.py
CHANGED
|
@@ -46,6 +46,16 @@ def test_youtube_api():
|
|
| 46 |
return {"success": False, "error": str(e)}
|
| 47 |
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
@router.get("/schedule")
|
| 50 |
def get_schedule_status():
|
| 51 |
from app.main import scheduler, US_EASTERN
|
|
@@ -75,16 +85,22 @@ def trigger_crawl_now(db: Session = Depends(get_db)):
|
|
| 75 |
|
| 76 |
|
| 77 |
def _run_post_crawl_analysis(db: Session):
|
|
|
|
|
|
|
|
|
|
| 78 |
try:
|
| 79 |
viral_analyzer = ViralAnalyzer(db)
|
| 80 |
-
viral_analyzer.calculate_viral_scores()
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
try:
|
| 84 |
revenue_analyzer = RevenueAnalyzer(db)
|
| 85 |
-
revenue_analyzer.batch_estimate_revenue()
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
@router.post("/youtube")
|
|
|
|
| 46 |
return {"success": False, "error": str(e)}
|
| 47 |
|
| 48 |
|
| 49 |
+
@router.post("/calculate-revenue")
|
| 50 |
+
def calculate_revenue(db: Session = Depends(get_db)):
|
| 51 |
+
try:
|
| 52 |
+
revenue_analyzer = RevenueAnalyzer(db)
|
| 53 |
+
count = revenue_analyzer.batch_estimate_revenue()
|
| 54 |
+
return {"success": True, "message": f"成功估算 {count} 条视频收入", "count": count}
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return {"success": False, "error": str(e)}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
@router.get("/schedule")
|
| 60 |
def get_schedule_status():
|
| 61 |
from app.main import scheduler, US_EASTERN
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
def _run_post_crawl_analysis(db: Session):
|
| 88 |
+
import logging
|
| 89 |
+
logger = logging.getLogger(__name__)
|
| 90 |
+
|
| 91 |
try:
|
| 92 |
viral_analyzer = ViralAnalyzer(db)
|
| 93 |
+
count = viral_analyzer.calculate_viral_scores()
|
| 94 |
+
logger.info(f"[分析] 爆红指数计算完成: {count} 条")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
logger.error(f"[分析] 爆红指数计算失败: {e}")
|
| 97 |
+
|
| 98 |
try:
|
| 99 |
revenue_analyzer = RevenueAnalyzer(db)
|
| 100 |
+
count = revenue_analyzer.batch_estimate_revenue()
|
| 101 |
+
logger.info(f"[分析] 收入估算完成: {count} 条")
|
| 102 |
+
except Exception as e:
|
| 103 |
+
logger.error(f"[分析] 收入估算失败: {e}")
|
| 104 |
|
| 105 |
|
| 106 |
@router.post("/youtube")
|
frontend/src/api/index.js
CHANGED
|
@@ -30,6 +30,7 @@ export const tasksApi = {
|
|
| 30 |
startCrawl: (platform, params) => api.post(`/crawl/${platform}`, null, { params }),
|
| 31 |
seedDemo: () => api.post('/crawl/seed-demo'),
|
| 32 |
resetAndCrawl: () => api.post('/crawl/reset-and-crawl'),
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
export const crawlApi = {
|
|
|
|
| 30 |
startCrawl: (platform, params) => api.post(`/crawl/${platform}`, null, { params }),
|
| 31 |
seedDemo: () => api.post('/crawl/seed-demo'),
|
| 32 |
resetAndCrawl: () => api.post('/crawl/reset-and-crawl'),
|
| 33 |
+
calculateRevenue: () => api.post('/crawl/calculate-revenue'),
|
| 34 |
}
|
| 35 |
|
| 36 |
export const crawlApi = {
|
frontend/src/views/Crawl.vue
CHANGED
|
@@ -38,6 +38,13 @@
|
|
| 38 |
<span class="demo-hint">无需 API Key,一键生成模拟数据体验功能</span>
|
| 39 |
</div>
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
<div class="task-grid">
|
| 42 |
<div class="card task-card">
|
| 43 |
<div class="task-header">
|
|
@@ -142,6 +149,7 @@ export default {
|
|
| 142 |
ttRunning: false,
|
| 143 |
seeding: false,
|
| 144 |
resetting: false,
|
|
|
|
| 145 |
logs: [],
|
| 146 |
}
|
| 147 |
},
|
|
@@ -157,6 +165,26 @@ export default {
|
|
| 157 |
console.error('加载配置状态失败:', e)
|
| 158 |
}
|
| 159 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
async startYoutube() {
|
| 161 |
this.ytRunning = true
|
| 162 |
const startTime = new Date().toLocaleTimeString()
|
|
|
|
| 38 |
<span class="demo-hint">无需 API Key,一键生成模拟数据体验功能</span>
|
| 39 |
</div>
|
| 40 |
|
| 41 |
+
<div class="demo-section">
|
| 42 |
+
<button class="btn btn-primary" @click="calculateRevenue" :disabled="calculating">
|
| 43 |
+
{{ calculating ? '⏳ 计算中...' : '💰 计算收入估算' }}
|
| 44 |
+
</button>
|
| 45 |
+
<span class="demo-hint">为所有视频计算收入估算数据</span>
|
| 46 |
+
</div>
|
| 47 |
+
|
| 48 |
<div class="task-grid">
|
| 49 |
<div class="card task-card">
|
| 50 |
<div class="task-header">
|
|
|
|
| 149 |
ttRunning: false,
|
| 150 |
seeding: false,
|
| 151 |
resetting: false,
|
| 152 |
+
calculating: false,
|
| 153 |
logs: [],
|
| 154 |
}
|
| 155 |
},
|
|
|
|
| 165 |
console.error('加载配置状态失败:', e)
|
| 166 |
}
|
| 167 |
},
|
| 168 |
+
async calculateRevenue() {
|
| 169 |
+
this.calculating = true
|
| 170 |
+
const startTime = new Date().toLocaleTimeString()
|
| 171 |
+
try {
|
| 172 |
+
const res = await tasksApi.calculateRevenue()
|
| 173 |
+
this.logs.unshift({
|
| 174 |
+
time: startTime,
|
| 175 |
+
status: 'success',
|
| 176 |
+
message: res.data.message || `收入估算完成`,
|
| 177 |
+
})
|
| 178 |
+
} catch (e) {
|
| 179 |
+
this.logs.unshift({
|
| 180 |
+
time: startTime,
|
| 181 |
+
status: 'error',
|
| 182 |
+
message: `收入估算失败: ${e.response?.data?.detail || e.message}`,
|
| 183 |
+
})
|
| 184 |
+
} finally {
|
| 185 |
+
this.calculating = false
|
| 186 |
+
}
|
| 187 |
+
},
|
| 188 |
async startYoutube() {
|
| 189 |
this.ytRunning = true
|
| 190 |
const startTime = new Date().toLocaleTimeString()
|