fix(douyu): 优化供应商直充会话与任务时间索引
This commit is contained in:
@@ -40,7 +40,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
|||||||
PROJECT_ROOT = TESTS_DIR.parent
|
PROJECT_ROOT = TESTS_DIR.parent
|
||||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||||
|
|
||||||
HEAD_REVISION = "20261119_0098"
|
HEAD_REVISION = "20261119_0099"
|
||||||
|
|
||||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||||
EXTRA_INDEXES = (
|
EXTRA_INDEXES = (
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
"""add douyu_tasks.created_at index
|
||||||
|
|
||||||
|
历史任务清理和按时间范围统计都以 created_at 为条件。表已接近 50 万行,
|
||||||
|
没有索引时清理作业需要扫描约 20 万行;单列索引即可覆盖这些低频但重扫描的查询。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
revision: str = "20261119_0099"
|
||||||
|
down_revision: str | None = "20261119_0098"
|
||||||
|
branch_labels: str | None = None
|
||||||
|
depends_on: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
inspector = sa.inspect(op.get_bind())
|
||||||
|
indexes = {item["name"] for item in inspector.get_indexes("douyu_tasks")}
|
||||||
|
if "ix_douyu_tasks_created_at" not in indexes:
|
||||||
|
op.create_index(
|
||||||
|
"ix_douyu_tasks_created_at",
|
||||||
|
"douyu_tasks",
|
||||||
|
["created_at"],
|
||||||
|
unique=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
inspector = sa.inspect(op.get_bind())
|
||||||
|
indexes = {item["name"] for item in inspector.get_indexes("douyu_tasks")}
|
||||||
|
if "ix_douyu_tasks_created_at" in indexes:
|
||||||
|
op.drop_index("ix_douyu_tasks_created_at", table_name="douyu_tasks")
|
||||||
@@ -356,7 +356,9 @@ class DouyuTask(Base):
|
|||||||
message: Mapped[str] = mapped_column(String(512), default="")
|
message: Mapped[str] = mapped_column(String(512), default="")
|
||||||
result: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
result: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||||
created_by: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
|
created_by: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
|
||||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
|
UTCDateTime, default=_utcnow, index=True
|
||||||
|
)
|
||||||
finished_at: Mapped[datetime | None] = mapped_column(UTCDateTime, nullable=True)
|
finished_at: Mapped[datetime | None] = mapped_column(UTCDateTime, nullable=True)
|
||||||
platform_task_id: Mapped[int | None] = mapped_column(
|
platform_task_id: Mapped[int | None] = mapped_column(
|
||||||
BigInteger,
|
BigInteger,
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ import {
|
|||||||
hasBindQrcode,
|
hasBindQrcode,
|
||||||
hasPaymentQrcode,
|
hasPaymentQrcode,
|
||||||
paymentArrived,
|
paymentArrived,
|
||||||
|
isSupplierApiRechargeTask,
|
||||||
bindPhaseText,
|
bindPhaseText,
|
||||||
goodsLabel,
|
goodsLabel,
|
||||||
xpdGoodsLabel,
|
xpdGoodsLabel,
|
||||||
@@ -84,6 +85,7 @@ interface DouyuPaySessionView {
|
|||||||
doneCount: number;
|
doneCount: number;
|
||||||
errorCount: number;
|
errorCount: number;
|
||||||
active: boolean;
|
active: boolean;
|
||||||
|
supplierApi: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseAccountNames(text: string): string[] {
|
function parseAccountNames(text: string): string[] {
|
||||||
@@ -785,6 +787,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
message.warning('该充值批次属于其他用户,请在自己的充值会话中操作');
|
message.warning('该充值批次属于其他用户,请在自己的充值会话中操作');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (isSupplierApiRechargeTask(ownerTask)) return;
|
||||||
const ordered = [...batchTasks].sort((a, b) => a.id - b.id);
|
const ordered = [...batchTasks].sort((a, b) => a.id - b.id);
|
||||||
addPaySession({
|
addPaySession({
|
||||||
batchId,
|
batchId,
|
||||||
@@ -1000,7 +1003,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
});
|
});
|
||||||
// 只有本页面创建的批次才允许主动弹二维码,防止其他工作台/浏览器的任务打扰当前操作。
|
// 只有本页面创建的批次才允许主动弹二维码,防止其他工作台/浏览器的任务打扰当前操作。
|
||||||
locallyStartedBatchIds.current.add(result.batch_id);
|
locallyStartedBatchIds.current.add(result.batch_id);
|
||||||
if (DOUYU_PAYMENT_TASK_TYPE_SET.has(taskType)) {
|
if (
|
||||||
|
DOUYU_PAYMENT_TASK_TYPE_SET.has(taskType)
|
||||||
|
&& !(taskType === 'create_gold_qr' && goldRechargeChannel === 'supplier_api')
|
||||||
|
) {
|
||||||
addPaySession({
|
addPaySession({
|
||||||
batchId: result.batch_id,
|
batchId: result.batch_id,
|
||||||
taskType,
|
taskType,
|
||||||
@@ -1901,6 +1907,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
for (const task of batchTasks.sort((a, b) => a.id - b.id)) {
|
for (const task of batchTasks.sort((a, b) => a.id - b.id)) {
|
||||||
taskByAccount.set(task.account_id, task);
|
taskByAccount.set(task.account_id, task);
|
||||||
}
|
}
|
||||||
|
const supplierApi = (
|
||||||
|
batchTasks.length > 0
|
||||||
|
&& batchTasks.every((task) => isSupplierApiRechargeTask(task))
|
||||||
|
);
|
||||||
const accountIds = [
|
const accountIds = [
|
||||||
...session.accountIds,
|
...session.accountIds,
|
||||||
...Array.from(taskByAccount.keys()).filter(
|
...Array.from(taskByAccount.keys()).filter(
|
||||||
@@ -2030,6 +2040,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
doneCount,
|
doneCount,
|
||||||
errorCount,
|
errorCount,
|
||||||
active,
|
active,
|
||||||
|
supplierApi,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
), [
|
), [
|
||||||
@@ -2040,9 +2051,63 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
runningBatchIds,
|
runningBatchIds,
|
||||||
tasks,
|
tasks,
|
||||||
]);
|
]);
|
||||||
const activePaySessionView = paySessionViews.find(
|
const visiblePaySessionViews = useMemo(
|
||||||
|
() => paySessionViews.filter((view) => (
|
||||||
|
!view.supplierApi
|
||||||
|
&& !(
|
||||||
|
view.doneCount > 0
|
||||||
|
&& view.doneCount === view.session.accountIds.length
|
||||||
|
&& view.items.length === 0
|
||||||
|
)
|
||||||
|
)),
|
||||||
|
[paySessionViews],
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
const removable = new Set(
|
||||||
|
paySessionViews
|
||||||
|
.filter((view) => (
|
||||||
|
view.supplierApi
|
||||||
|
|| (
|
||||||
|
view.doneCount > 0
|
||||||
|
&& view.doneCount === view.session.accountIds.length
|
||||||
|
&& view.items.length === 0
|
||||||
|
)
|
||||||
|
))
|
||||||
|
.map((view) => view.session.batchId),
|
||||||
|
);
|
||||||
|
if (removable.size === 0) return;
|
||||||
|
setPaySessionsSafely((sessions) => {
|
||||||
|
const next = sessions.filter(
|
||||||
|
(session) => !removable.has(session.batchId),
|
||||||
|
);
|
||||||
|
return next.length === sessions.length ? sessions : next;
|
||||||
|
});
|
||||||
|
setPaySessionTasks((current) => {
|
||||||
|
const next = { ...current };
|
||||||
|
let changed = false;
|
||||||
|
removable.forEach((batchId) => {
|
||||||
|
if (!(batchId in next)) return;
|
||||||
|
delete next[batchId];
|
||||||
|
changed = true;
|
||||||
|
});
|
||||||
|
return changed ? next : current;
|
||||||
|
});
|
||||||
|
setActivePayBatchId((current) => (
|
||||||
|
current && removable.has(current) ? null : current
|
||||||
|
));
|
||||||
|
}, [paySessionViews, setPaySessionsSafely]);
|
||||||
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
payOpen
|
||||||
|
&& paySessions.length > 0
|
||||||
|
&& visiblePaySessionViews.length === 0
|
||||||
|
) {
|
||||||
|
setPayOpen(false);
|
||||||
|
}
|
||||||
|
}, [payOpen, paySessions.length, visiblePaySessionViews.length]);
|
||||||
|
const activePaySessionView = visiblePaySessionViews.find(
|
||||||
(view) => view.session.batchId === activePayBatchId,
|
(view) => view.session.batchId === activePayBatchId,
|
||||||
) || paySessionViews[0];
|
) || visiblePaySessionViews[0];
|
||||||
const payReadyCount = activePaySessionView?.readyCount || 0;
|
const payReadyCount = activePaySessionView?.readyCount || 0;
|
||||||
const payQueuedCount = activePaySessionView?.queuedCount || 0;
|
const payQueuedCount = activePaySessionView?.queuedCount || 0;
|
||||||
const payDoneCount = activePaySessionView?.doneCount || 0;
|
const payDoneCount = activePaySessionView?.doneCount || 0;
|
||||||
@@ -2051,6 +2116,32 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
activePaySessionView
|
activePaySessionView
|
||||||
&& activePaySessionView.session.ownerUserId === user?.id,
|
&& activePaySessionView.session.ownerUserId === user?.id,
|
||||||
);
|
);
|
||||||
|
const historicalPaySessionIds = useMemo(
|
||||||
|
() => paySessionViews
|
||||||
|
.filter((view) => (
|
||||||
|
!view.active
|
||||||
|
&& !view.items.some((item) => item.phase === 'ready')
|
||||||
|
))
|
||||||
|
.map((view) => view.session.batchId),
|
||||||
|
[paySessionViews],
|
||||||
|
);
|
||||||
|
const clearHistoricalPaySessions = () => {
|
||||||
|
if (historicalPaySessionIds.length === 0) return;
|
||||||
|
const remove = new Set(historicalPaySessionIds);
|
||||||
|
const remaining = paySessionsRef.current.filter(
|
||||||
|
(session) => !remove.has(session.batchId),
|
||||||
|
);
|
||||||
|
setPaySessionsSafely((sessions) => sessions.filter(
|
||||||
|
(session) => !remove.has(session.batchId),
|
||||||
|
));
|
||||||
|
setPaySessionTasks((current) => {
|
||||||
|
const next = { ...current };
|
||||||
|
remove.forEach((batchId) => delete next[batchId]);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
setActivePayBatchId(remaining[0]?.batchId || null);
|
||||||
|
if (remaining.length === 0) setPayOpen(false);
|
||||||
|
};
|
||||||
const zoomPayUrl = taskQrUrl(zoomPayTask);
|
const zoomPayUrl = taskQrUrl(zoomPayTask);
|
||||||
const copyPaymentLink = async (value: string) => {
|
const copyPaymentLink = async (value: string) => {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
@@ -3024,6 +3115,12 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
</Button>
|
</Button>
|
||||||
</Popconfirm>
|
</Popconfirm>
|
||||||
)}
|
)}
|
||||||
|
<Button
|
||||||
|
onClick={clearHistoricalPaySessions}
|
||||||
|
disabled={historicalPaySessionIds.length === 0}
|
||||||
|
>
|
||||||
|
清理历史会话
|
||||||
|
</Button>
|
||||||
<Button onClick={closeActivePaySession} disabled={!activePaySessionView}>
|
<Button onClick={closeActivePaySession} disabled={!activePaySessionView}>
|
||||||
关闭当前会话
|
关闭当前会话
|
||||||
</Button>
|
</Button>
|
||||||
@@ -3032,14 +3129,14 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
|||||||
centered
|
centered
|
||||||
width={920}
|
width={920}
|
||||||
>
|
>
|
||||||
{paySessionViews.length > 0 ? (
|
{visiblePaySessionViews.length > 0 ? (
|
||||||
<Tabs
|
<Tabs
|
||||||
activeKey={activePaySessionView?.session.batchId}
|
activeKey={activePaySessionView?.session.batchId}
|
||||||
onChange={(batchId) => {
|
onChange={(batchId) => {
|
||||||
setActivePayBatchId(batchId);
|
setActivePayBatchId(batchId);
|
||||||
void loadPaySessionTasks([batchId]);
|
void loadPaySessionTasks([batchId]);
|
||||||
}}
|
}}
|
||||||
items={paySessionViews.map((view) => ({
|
items={visiblePaySessionViews.map((view) => ({
|
||||||
key: view.session.batchId,
|
key: view.session.batchId,
|
||||||
label: (
|
label: (
|
||||||
<Space size={4}>
|
<Space size={4}>
|
||||||
|
|||||||
@@ -208,8 +208,19 @@ export function hasPaymentQrcode(task: DouyuTaskItem | null | undefined): boolea
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isSupplierApiRechargeTask(
|
||||||
|
task: DouyuTaskItem | null | undefined,
|
||||||
|
): boolean {
|
||||||
|
return Boolean(
|
||||||
|
task
|
||||||
|
&& task.task_type === 'create_gold_qr'
|
||||||
|
&& resultText(task.result, 'recharge_channel') === 'supplier_api',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function paymentArrived(task: DouyuTaskItem | null | undefined): boolean {
|
export function paymentArrived(task: DouyuTaskItem | null | undefined): boolean {
|
||||||
if (!task || !DOUYU_PAYMENT_TASK_TYPE_SET.has(task.task_type)) return false;
|
if (!task || !DOUYU_PAYMENT_TASK_TYPE_SET.has(task.task_type)) return false;
|
||||||
|
if (isSupplierApiRechargeTask(task) && task.status === 'success') return true;
|
||||||
if (task.status !== 'success') return false;
|
if (task.status !== 'success') return false;
|
||||||
return task.result?.gold_recharged === true
|
return task.result?.gold_recharged === true
|
||||||
|| task.result?.elite_opened === true
|
|| task.result?.elite_opened === true
|
||||||
|
|||||||
Reference in New Issue
Block a user