更新Company页面的UI为FUI风格
This commit is contained in:
238
app.py
238
app.py
@@ -10992,6 +10992,244 @@ def get_hot_events():
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
|
||||
@app.route('/api/events/mainline', methods=['GET'])
|
||||
def get_events_by_mainline():
|
||||
"""
|
||||
获取按主线(lv2概念)分组的事件列表
|
||||
|
||||
逻辑:
|
||||
1. 根据筛选条件获取事件列表
|
||||
2. 通过 related_concepts 表关联概念
|
||||
3. 调用 concept-api/hierarchy 获取概念的 lv2 归属
|
||||
4. 按 lv2 分组返回
|
||||
|
||||
参数:
|
||||
- recent_days: 近N天(默认7天)
|
||||
- importance: 重要性筛选(S,A,B,C 或 all)
|
||||
- limit: 每个主线最多返回多少事件(默认20)
|
||||
|
||||
返回:
|
||||
{
|
||||
"success": true,
|
||||
"data": {
|
||||
"mainlines": [
|
||||
{
|
||||
"lv2_id": "L2_AI_INFRA",
|
||||
"lv2_name": "AI基础设施 (算力/CPO/PCB)",
|
||||
"lv1_name": "TMT (科技/媒体/通信)",
|
||||
"event_count": 15,
|
||||
"events": [...]
|
||||
},
|
||||
...
|
||||
],
|
||||
"total_events": 100,
|
||||
"ungrouped_count": 5
|
||||
}
|
||||
}
|
||||
"""
|
||||
try:
|
||||
import requests
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy import exists
|
||||
|
||||
# 获取请求参数
|
||||
recent_days = request.args.get('recent_days', 7, type=int)
|
||||
importance = request.args.get('importance', 'all')
|
||||
limit_per_mainline = request.args.get('limit', 20, type=int)
|
||||
|
||||
# 计算日期范围
|
||||
since_date = datetime.now() - timedelta(days=recent_days)
|
||||
|
||||
# ==================== 1. 获取概念层级映射 ====================
|
||||
# 调用 concept-api 获取层级结构
|
||||
concept_hierarchy_map = {} # { concept_name: { lv1, lv2, lv1_id, lv2_id } }
|
||||
|
||||
try:
|
||||
# 内部调用 concept-api
|
||||
hierarchy_resp = requests.get(
|
||||
'http://127.0.0.1:6801/hierarchy',
|
||||
timeout=5
|
||||
)
|
||||
if hierarchy_resp.status_code == 200:
|
||||
hierarchy_data = hierarchy_resp.json()
|
||||
hierarchy_list = hierarchy_data.get('hierarchy', [])
|
||||
|
||||
# 构建概念名称 -> lv2 映射
|
||||
for lv1 in hierarchy_list:
|
||||
lv1_name = lv1.get('name', '')
|
||||
lv1_id = lv1.get('id', '')
|
||||
|
||||
for lv2 in lv1.get('children', []):
|
||||
lv2_name = lv2.get('name', '')
|
||||
lv2_id = lv2.get('id', '')
|
||||
|
||||
# lv2 直接包含 concepts
|
||||
for concept in lv2.get('concepts', []):
|
||||
concept_name = concept if isinstance(concept, str) else concept.get('name', '')
|
||||
if concept_name:
|
||||
concept_hierarchy_map[concept_name] = {
|
||||
'lv1': lv1_name,
|
||||
'lv1_id': lv1_id,
|
||||
'lv2': lv2_name,
|
||||
'lv2_id': lv2_id
|
||||
}
|
||||
|
||||
# lv3 children
|
||||
for lv3 in lv2.get('children', []):
|
||||
for concept in lv3.get('concepts', []):
|
||||
concept_name = concept if isinstance(concept, str) else concept.get('name', '')
|
||||
if concept_name:
|
||||
concept_hierarchy_map[concept_name] = {
|
||||
'lv1': lv1_name,
|
||||
'lv1_id': lv1_id,
|
||||
'lv2': lv2_name,
|
||||
'lv2_id': lv2_id
|
||||
}
|
||||
|
||||
app.logger.info(f'[mainline] 加载概念层级映射: {len(concept_hierarchy_map)} 个概念')
|
||||
except Exception as e:
|
||||
app.logger.warning(f'[mainline] 获取概念层级失败: {e}')
|
||||
|
||||
# ==================== 2. 查询事件及其关联概念 ====================
|
||||
query = Event.query.options(joinedload(Event.creator))
|
||||
|
||||
# 只返回有关联股票的事件
|
||||
query = query.filter(
|
||||
exists().where(RelatedStock.event_id == Event.id)
|
||||
)
|
||||
|
||||
# 状态筛选
|
||||
query = query.filter(Event.status == 'active')
|
||||
|
||||
# 日期筛选
|
||||
query = query.filter(Event.created_at >= since_date)
|
||||
|
||||
# 重要性筛选
|
||||
if importance != 'all':
|
||||
if ',' in importance:
|
||||
importance_list = [imp.strip() for imp in importance.split(',') if imp.strip()]
|
||||
query = query.filter(Event.importance.in_(importance_list))
|
||||
else:
|
||||
query = query.filter(Event.importance == importance)
|
||||
|
||||
# 按时间倒序
|
||||
query = query.order_by(Event.created_at.desc())
|
||||
|
||||
# 获取事件(限制总数防止性能问题)
|
||||
events = query.limit(500).all()
|
||||
|
||||
app.logger.info(f'[mainline] 查询到 {len(events)} 个事件')
|
||||
|
||||
# ==================== 3. 获取事件的关联概念 ====================
|
||||
event_ids = [e.id for e in events]
|
||||
|
||||
# 批量查询 related_concepts
|
||||
related_concepts_query = db.session.query(
|
||||
RelatedConcept.event_id,
|
||||
RelatedConcept.concept
|
||||
).filter(RelatedConcept.event_id.in_(event_ids)).all()
|
||||
|
||||
# 构建 event_id -> concepts 映射
|
||||
event_concepts_map = {} # { event_id: [concept1, concept2, ...] }
|
||||
for event_id, concept in related_concepts_query:
|
||||
if event_id not in event_concepts_map:
|
||||
event_concepts_map[event_id] = []
|
||||
event_concepts_map[event_id].append(concept)
|
||||
|
||||
app.logger.info(f'[mainline] 查询到 {len(related_concepts_query)} 条概念关联')
|
||||
|
||||
# ==================== 4. 按 lv2 分组事件 ====================
|
||||
mainline_groups = {} # { lv2_id: { info: {...}, events: [...] } }
|
||||
ungrouped_events = []
|
||||
|
||||
def find_concept_hierarchy(concept_name):
|
||||
"""查找概念的层级信息(支持模糊匹配)"""
|
||||
# 精确匹配
|
||||
if concept_name in concept_hierarchy_map:
|
||||
return concept_hierarchy_map[concept_name]
|
||||
|
||||
# 模糊匹配
|
||||
for key in concept_hierarchy_map:
|
||||
if concept_name in key or key in concept_name:
|
||||
return concept_hierarchy_map[key]
|
||||
|
||||
return None
|
||||
|
||||
for event in events:
|
||||
concepts = event_concepts_map.get(event.id, [])
|
||||
|
||||
# 找出该事件所属的所有 lv2
|
||||
event_lv2s = set()
|
||||
for concept in concepts:
|
||||
hierarchy = find_concept_hierarchy(concept)
|
||||
if hierarchy:
|
||||
event_lv2s.add((hierarchy['lv2_id'], hierarchy['lv2'], hierarchy['lv1']))
|
||||
|
||||
# 事件数据
|
||||
event_data = {
|
||||
'id': event.id,
|
||||
'title': event.title,
|
||||
'description': event.description,
|
||||
'importance': event.importance,
|
||||
'created_at': event.created_at.isoformat() if event.created_at else None,
|
||||
'related_avg_chg': event.related_avg_chg,
|
||||
'related_max_chg': event.related_max_chg,
|
||||
'expectation_surprise_score': event.expectation_surprise_score,
|
||||
'hot_score': event.hot_score,
|
||||
'related_concepts': [{'concept': c} for c in concepts],
|
||||
'creator': {
|
||||
'username': event.creator.username if event.creator else 'Anonymous'
|
||||
}
|
||||
}
|
||||
|
||||
if event_lv2s:
|
||||
# 添加到每个相关的 lv2 分组
|
||||
for lv2_id, lv2_name, lv1_name in event_lv2s:
|
||||
if lv2_id not in mainline_groups:
|
||||
mainline_groups[lv2_id] = {
|
||||
'lv2_id': lv2_id,
|
||||
'lv2_name': lv2_name,
|
||||
'lv1_name': lv1_name,
|
||||
'events': []
|
||||
}
|
||||
mainline_groups[lv2_id]['events'].append(event_data)
|
||||
else:
|
||||
ungrouped_events.append(event_data)
|
||||
|
||||
# ==================== 5. 整理返回数据 ====================
|
||||
mainlines = []
|
||||
for lv2_id, group in mainline_groups.items():
|
||||
# 按时间倒序,限制每组数量
|
||||
group['events'] = sorted(
|
||||
group['events'],
|
||||
key=lambda x: x['created_at'] or '',
|
||||
reverse=True
|
||||
)[:limit_per_mainline]
|
||||
group['event_count'] = len(group['events'])
|
||||
mainlines.append(group)
|
||||
|
||||
# 按事件数量排序
|
||||
mainlines.sort(key=lambda x: x['event_count'], reverse=True)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data': {
|
||||
'mainlines': mainlines,
|
||||
'total_events': len(events),
|
||||
'mainline_count': len(mainlines),
|
||||
'ungrouped_count': len(ungrouped_events)
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
app.logger.error(f'[mainline] 错误: {e}', exc_info=True)
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
@app.route('/api/events/keywords/popular', methods=['GET'])
|
||||
def get_popular_keywords():
|
||||
"""获取热门关键词"""
|
||||
|
||||
Reference in New Issue
Block a user