截断JD
This commit is contained in:
+4
-1
@@ -131,8 +131,11 @@ class JobApplication(Base):
|
||||
ai_reason = Column(String(255), nullable=True)
|
||||
# 跳过原因
|
||||
skip_reason = Column(String(255), nullable=True)
|
||||
# 职位 JD 全文(投递/跳过时归档,供复盘与面试准备)
|
||||
# 职位 JD 全文(投递/跳过时归档,供复盘与面试准备);
|
||||
# 超过 JD_TEXT_LIMIT 的超长 JD 不入本列,落盘到项目根目录 jd_archive/,本列留空、改存 jd_file 路径
|
||||
jd_text = Column(Text, nullable=True)
|
||||
# 超长 JD 归档文件路径(相对项目根目录,如 jd_archive/1_0daaa9fd.txt)
|
||||
jd_file = Column(String(255), nullable=True)
|
||||
# 实际发送的打招呼语(仅 chat 方式有值)
|
||||
greeting = Column(String(500), nullable=True)
|
||||
applied_at = Column(DateTime, default=utcnow, nullable=False)
|
||||
|
||||
+66
-8
@@ -1,11 +1,14 @@
|
||||
"""
|
||||
投递记录路由:上报投递结果、查询历史、统计
|
||||
"""
|
||||
import re
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
from typing import Optional, Tuple
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func
|
||||
from loguru import logger
|
||||
|
||||
from ..auth import get_current_user
|
||||
from ..database import get_db
|
||||
@@ -15,10 +18,33 @@ from ..schemas import ApplicationIn, ApplicationOut, Message, StatsOut
|
||||
|
||||
router = APIRouter(prefix="/api/jobs", tags=["投递记录"])
|
||||
|
||||
# jd_text 归档上限(字符):控制写入包体积,避免触发 MySQL max_allowed_packet(1153);
|
||||
# 正常 JD 全文远低于此值,超限基本意味着抓取异常(如抓到整页文本)
|
||||
# jd_text 入库上限(字符):超过则落盘为文件(库内只存路径),
|
||||
# 控制写入包体积,避免触发 MySQL max_allowed_packet(1153)
|
||||
JD_TEXT_LIMIT = 20000
|
||||
|
||||
# 归档目录:项目根目录下的 jd_archive/(本文件位于 server/routers/,上溯两级即项目根)
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
JD_DIR_NAME = "jd_archive"
|
||||
|
||||
|
||||
def _archive_jd(user_id: int, job_id: str, jd_text: Optional[str]) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""JD 存储分流:≤ 上限直接入库;超限写入 jd_archive/ 文件。
|
||||
返回 (jd_text, jd_file):两者必有一空。文件名按 用户_职位 去重,重复上报覆盖"""
|
||||
if not jd_text or len(jd_text) <= JD_TEXT_LIMIT:
|
||||
return jd_text, None
|
||||
safe_job = re.sub(r"[^\w\-]", "_", job_id or "")[:48] or "unknown"
|
||||
rel = f"{JD_DIR_NAME}/{user_id}_{safe_job}.txt"
|
||||
try:
|
||||
target = PROJECT_ROOT / rel
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_text(jd_text, encoding="utf-8")
|
||||
logger.info(f"超长 JD 已归档到文件({len(jd_text)} 字符):{rel}")
|
||||
return None, rel
|
||||
except OSError as e:
|
||||
# 落盘失败也不能把巨包塞进数据库:截断到上限入库,保底不丢全部信息且不炸库
|
||||
logger.error(f"JD 归档落盘失败,截断入库:{e}")
|
||||
return jd_text[:JD_TEXT_LIMIT], None
|
||||
|
||||
|
||||
def _clip(value: Optional[str], limit: int) -> Optional[str]:
|
||||
"""按列长度截断文本,防止超长写入导致 1153 / Data too long"""
|
||||
@@ -26,7 +52,8 @@ def _clip(value: Optional[str], limit: int) -> Optional[str]:
|
||||
|
||||
|
||||
def _sanitize(req: ApplicationIn) -> ApplicationIn:
|
||||
"""上报字段统一裁剪(与 models.py 列长度保持一致)"""
|
||||
"""上报字段统一裁剪(与 models.py 列长度保持一致);
|
||||
jd_text 不在此截断——由 _archive_jd 按上限分流到数据库/文件"""
|
||||
req.job_title = _clip(req.job_title, 128)
|
||||
req.company = _clip(req.company, 128)
|
||||
req.salary = _clip(req.salary, 32)
|
||||
@@ -35,7 +62,6 @@ def _sanitize(req: ApplicationIn) -> ApplicationIn:
|
||||
req.ai_reason = _clip(req.ai_reason, 255)
|
||||
req.skip_reason = _clip(req.skip_reason, 255)
|
||||
req.greeting = _clip(req.greeting, 500)
|
||||
req.jd_text = _clip(req.jd_text, JD_TEXT_LIMIT)
|
||||
return req
|
||||
|
||||
|
||||
@@ -51,6 +77,8 @@ def report_application(
|
||||
同一用户 + 同一职位 ID 去重,已存在则更新。
|
||||
"""
|
||||
req = _sanitize(req)
|
||||
# JD 分流:短文本入库,超长落盘为文件(库存路径)
|
||||
jd_text, jd_file = _archive_jd(current_user.id, req.job_id, req.jd_text)
|
||||
existing = db.query(JobApplication).filter(
|
||||
JobApplication.user_id == current_user.id,
|
||||
JobApplication.job_id == req.job_id,
|
||||
@@ -65,7 +93,9 @@ def report_application(
|
||||
# 否则记录停留在首次上报时间,看起来像“漏报”
|
||||
existing.applied_at = datetime.utcnow()
|
||||
if req.jd_text:
|
||||
existing.jd_text = req.jd_text
|
||||
# 新 JD 覆盖旧值;文本与归档文件二者只留其一,避免读到陈旧数据
|
||||
existing.jd_text = jd_text
|
||||
existing.jd_file = jd_file
|
||||
if req.greeting:
|
||||
existing.greeting = req.greeting
|
||||
if session_id:
|
||||
@@ -84,7 +114,8 @@ def report_application(
|
||||
ai_score=req.ai_score,
|
||||
ai_reason=req.ai_reason,
|
||||
skip_reason=req.skip_reason,
|
||||
jd_text=req.jd_text,
|
||||
jd_text=jd_text,
|
||||
jd_file=jd_file,
|
||||
greeting=req.greeting,
|
||||
)
|
||||
db.add(record)
|
||||
@@ -208,6 +239,7 @@ def page_applications(
|
||||
rows = query.order_by(JobApplication.applied_at.desc()) \
|
||||
.offset((page - 1) * page_size).limit(page_size).all()
|
||||
items = [{
|
||||
"id": r.id,
|
||||
"job_title": r.job_title,
|
||||
"company": r.company,
|
||||
"salary": r.salary,
|
||||
@@ -217,8 +249,10 @@ def page_applications(
|
||||
"ai_reason": r.ai_reason,
|
||||
"skip_reason": r.skip_reason,
|
||||
"greeting": r.greeting,
|
||||
# JD 全文截断,供客户端悬停摘要展示,控制分页响应体积
|
||||
# JD 全文截断,供客户端悬停摘要展示,控制分页响应体积;
|
||||
# 完整内容(含归档文件)通过 /{app_id}/jd 接口按需获取,has_jd 标记有无 JD 可看
|
||||
"jd_text": (r.jd_text or "")[:500],
|
||||
"has_jd": bool(r.jd_text or r.jd_file),
|
||||
"applied_at": (r.applied_at + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S"),
|
||||
} for r in rows]
|
||||
return {
|
||||
@@ -256,3 +290,27 @@ def get_stats(
|
||||
total_skipped=count(apply_type="skipped"),
|
||||
today_applied=today_applied,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{app_id}/jd", response_model=dict)
|
||||
def get_application_jd(
|
||||
app_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""查看单条投递记录的完整 JD:先取 jd_text 字段,为空则读 jd_file 归档文件"""
|
||||
record = db.query(JobApplication).filter(
|
||||
JobApplication.id == app_id,
|
||||
JobApplication.user_id == current_user.id,
|
||||
).first()
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="投递记录不存在")
|
||||
if record.jd_text:
|
||||
return {"id": app_id, "source": "db", "jd_text": record.jd_text}
|
||||
if record.jd_file:
|
||||
target = (PROJECT_ROOT / record.jd_file).resolve()
|
||||
# 防路径穿越:归档文件必须位于项目根目录内,防止库内路径被篡改后读到任意文件
|
||||
if target.is_file() and str(target).startswith(str(PROJECT_ROOT)):
|
||||
return {"id": app_id, "source": "file", "jd_text": target.read_text(encoding="utf-8")}
|
||||
raise HTTPException(status_code=410, detail="JD 归档文件不存在或已丢失")
|
||||
raise HTTPException(status_code=404, detail="该记录未包含 JD")
|
||||
|
||||
@@ -151,6 +151,8 @@ class ApplicationOut(BaseModel):
|
||||
ai_reason: Optional[str]
|
||||
skip_reason: Optional[str]
|
||||
jd_text: Optional[str]
|
||||
# 超长 JD 归档文件路径(相对项目根目录);与 jd_text 二者只有一非空
|
||||
jd_file: Optional[str] = None
|
||||
greeting: Optional[str]
|
||||
applied_at: datetime
|
||||
|
||||
|
||||
+48
-2
@@ -72,6 +72,10 @@
|
||||
.bl-btn { border: 1px solid #ffd6d6; background: #fff; color: #e64340; border-radius: 4px; font-size: 12px; padding: 1px 8px; cursor: pointer; margin-left: 6px; }
|
||||
.bl-btn:hover { background: #fff1f0; }
|
||||
|
||||
/* JD 查看按钮 */
|
||||
.jd-btn { border: 1px solid #91d5ff; background: #fff; color: #1890ff; border-radius: 4px; font-size: 12px; padding: 1px 8px; cursor: pointer; }
|
||||
.jd-btn:hover { background: #e6f7ff; }
|
||||
|
||||
/* 黑名单页 */
|
||||
.bl-add { display: flex; gap: 10px; background: #fff; border-radius: 10px; padding: 16px 18px; box-shadow: 0 2px 8px rgba(0,0,0,.05); margin-bottom: 16px; }
|
||||
.bl-add input { flex: 1; padding: 9px 12px; border: 1px solid #d9d9d9; border-radius: 6px; font-size: 14px; }
|
||||
@@ -156,7 +160,7 @@
|
||||
|
||||
<table>
|
||||
<thead><tr>
|
||||
<th>时间</th><th>职位</th><th>公司</th><th>薪资</th><th>结果</th><th>AI评分</th><th>备注</th>
|
||||
<th>时间</th><th>职位</th><th>公司</th><th>薪资</th><th>结果</th><th>AI评分</th><th>备注</th><th style="width:60px">JD</th>
|
||||
</tr></thead>
|
||||
<tbody id="tbody"></tbody>
|
||||
</table>
|
||||
@@ -371,9 +375,12 @@ async function load() {
|
||||
const company = r.company || '';
|
||||
const blBtn = (company && !blSet.has(company))
|
||||
? '<button class="bl-btn" data-company="' + esc(company) + '">拉黑</button>' : '';
|
||||
// JD 列:有 JD(库内字段或归档文件)才可点开查看全文,走 /api/jobs/{id}/jd 按需拉取
|
||||
const jdBtn = r.has_jd
|
||||
? '<button class="jd-btn" data-id="' + r.id + '">查看</button>' : '<span style="color:#ccc;">-</span>';
|
||||
return '<tr><td>' + esc(r.applied_at) + '</td><td>' + esc(r.job_title) + '</td><td>' + esc(company) + blBtn +
|
||||
'</td><td>' + esc(r.salary) + '</td><td>' + type + '</td><td>' + score +
|
||||
'</td><td class="remark" title="' + esc(remark) + '">' + esc(remark) + '</td></tr>';
|
||||
'</td><td class="remark" title="' + esc(remark) + '">' + esc(remark) + '</td><td>' + jdBtn + '</td></tr>';
|
||||
}).join('');
|
||||
document.getElementById('empty').classList.toggle('hidden', (d.items || []).length > 0);
|
||||
|
||||
@@ -499,6 +506,28 @@ async function testAiConfig() {
|
||||
} catch (e) { alert('测试失败:' + (e.message || '')); }
|
||||
}
|
||||
|
||||
/* ---------- 岗位 JD 查看(先取库内字段,为空由服务端读归档文件) ---------- */
|
||||
async function showJd(appId) {
|
||||
const body = document.getElementById('jdBody');
|
||||
const tip = document.getElementById('jdTip');
|
||||
document.getElementById('jdModal').classList.remove('hidden');
|
||||
tip.textContent = '加载中…';
|
||||
body.textContent = '';
|
||||
try {
|
||||
const d = await api('/api/jobs/' + appId + '/jd');
|
||||
tip.textContent = '来源:' + (d.source === 'file' ? '归档文件' : '数据库字段') +
|
||||
' · ' + (d.jd_text || '').length + ' 字';
|
||||
body.textContent = d.jd_text || '';
|
||||
} catch (e) {
|
||||
tip.textContent = '加载失败';
|
||||
body.textContent = e.message || '无 JD 数据';
|
||||
}
|
||||
}
|
||||
|
||||
function closeJd() {
|
||||
document.getElementById('jdModal').classList.add('hidden');
|
||||
}
|
||||
|
||||
/* ---------- 页面切换 ---------- */
|
||||
function switchPage(page) {
|
||||
document.querySelectorAll('aside .menu-item').forEach(el =>
|
||||
@@ -588,6 +617,8 @@ document.getElementById('blTbody').addEventListener('click', e => {
|
||||
if (btn) removeBlacklist(btn.dataset.id);
|
||||
});
|
||||
document.getElementById('tbody').addEventListener('click', e => {
|
||||
const jdBtn = e.target.closest('.jd-btn');
|
||||
if (jdBtn) { showJd(jdBtn.dataset.id); return; }
|
||||
const btn = e.target.closest('.bl-btn');
|
||||
if (btn && confirm('确认拉黑公司「' + btn.dataset.company + '」?拉黑后将不再投递该公司。')) {
|
||||
addBlacklist(btn.dataset.company);
|
||||
@@ -720,5 +751,20 @@ setInterval(() => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 岗位 JD 查看弹窗 -->
|
||||
<div id="jdModal" class="hidden" style="position:fixed;inset:0;background:rgba(0,0,0,0.45);z-index:2000;display:flex;align-items:center;justify-content:center;">
|
||||
<div style="background:#fff;border-radius:8px;padding:24px;width:760px;max-width:95vw;max-height:85vh;display:flex;flex-direction:column;">
|
||||
<h3 style="margin:0 0 8px;">岗位 JD 全文</h3>
|
||||
<div id="jdTip" style="font-size:12px;color:#999;margin-bottom:10px;"></div>
|
||||
<pre id="jdBody" style="flex:1;min-height:0;overflow:auto;white-space:pre-wrap;word-break:break-all;background:#fafafa;border:1px solid #f0f0f0;border-radius:6px;padding:14px;font-size:13px;line-height:1.7;"></pre>
|
||||
<div style="margin-top:16px;text-align:right;">
|
||||
<button id="jdClose" style="padding:6px 14px;border-radius:4px;border:1px solid #d9d9d9;background:#fff;cursor:pointer;">关闭</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
document.getElementById('jdClose').onclick = closeJd;
|
||||
document.getElementById('jdModal').addEventListener('click', e => { if (e.target.id === 'jdModal') closeJd(); });
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user