Compare commits
6 Commits
93dcf74c87
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 1428851779 | |||
| ff2e5f4ce1 | |||
| e59c7b3acd | |||
| 4776261fe2 | |||
| 25920f5415 | |||
| 38bf60cf36 |
@@ -899,7 +899,7 @@ docker compose ps
|
|||||||
| `growth.recommendations.ts` | 兴趣、收藏、完成回复、点击和跳过共同排序;保留探索槽,缓存按账号及配置版本隔离,最多1000项/60秒。 |
|
| `growth.recommendations.ts` | 兴趣、收藏、完成回复、点击和跳过共同排序;保留探索槽,缓存按账号及配置版本隔离,最多1000项/60秒。 |
|
||||||
| `growth.quota.ts` | 单次请求预占回复次数、全站并发及字符预算;与消息插入共用事务。空失败释放次数,有部分回复则消耗一次;结算通过条件状态转换幂等。180秒后可回收异常预占,无法确认的上游成本按预占字符预算保守计入。 |
|
| `growth.quota.ts` | 单次请求预占回复次数、全站并发及字符预算;与消息插入共用事务。空失败释放次数,有部分回复则消耗一次;结算通过条件状态转换幂等。180秒后可回收异常预占,无法确认的上游成本按预占字符预算保守计入。 |
|
||||||
| `growth.memory.ts` | 用户主动开启后,只提取明确的“请记住”事实及历史原文摘录。任务持久化、租约重领、最多3次尝试;每段故事最多80条有效记忆。删除保留来源标记,后续任务不能复活同一来源。 |
|
| `growth.memory.ts` | 用户主动开启后,只提取明确的“请记住”事实及历史原文摘录。任务持久化、租约重领、最多3次尝试;每段故事最多80条有效记忆。删除保留来源标记,后续任务不能复活同一来源。 |
|
||||||
| `growth.campaigns.ts` | 站内生命周期卡片,按访问前状态分为新用户、活跃、7天未访问;有活动时间窗、固定对照组、每人每日最多一张及每活动版本一次的频控。按访问时匹配,不会外发消息。 |
|
| `growth.campaigns.ts` | 站内生命周期卡片,按访问前状态分为新用户、活跃、7天未访问;活动日程按北京时间比较且不依赖数据库会话时区,有固定对照组、每人每日最多一张及每活动版本一次的频控。按访问时匹配,不会外发消息。 |
|
||||||
| `growth.analytics.ts` | 账号严格顺序漏斗、成熟队列次日活跃、推荐回执归因、质量原因、首字时延、额度流水和估算成本。漏斗读取上限50000事件,截断时显式提示。 |
|
| `growth.analytics.ts` | 账号严格顺序漏斗、成熟队列次日活跃、推荐回执归因、质量原因、首字时延、额度流水和估算成本。漏斗读取上限50000事件,截断时显式提示。 |
|
||||||
| `growth.routes.ts` | 前台仅能读写本人偏好、反馈与记忆;后台策略、权益赠送和活动配置限定超级管理员,使用版本/幂等请求号与审计。 |
|
| `growth.routes.ts` | 前台仅能读写本人偏好、反馈与记忆;后台策略、权益赠送和活动配置限定超级管理员,使用版本/幂等请求号与审计。 |
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/** 站内生命周期运营:服务端分群、稳定对照组、每日频控与独立曝光/点击回执。 */
|
/** 站内生命周期运营:日程按北京时间比较,不依赖MySQL会话时区;频控沿用数据库自然日。 */
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { rows, write, transaction } from "../digital.repository";
|
import { rows, write, transaction } from "../digital.repository";
|
||||||
import { bucket } from "./growth.policy";
|
import { bucket } from "./growth.policy";
|
||||||
@@ -11,7 +11,7 @@ export async function lifecycle(account: string) {
|
|||||||
inactive: number | null;
|
inactive: number | null;
|
||||||
replies: number;
|
replies: number;
|
||||||
}>(
|
}>(
|
||||||
"SELECT TIMESTAMPDIFF(DAY,?,NOW(3)) age,(SELECT TIMESTAMPDIFF(DAY,MAX(created_at),NOW(3)) FROM dh_growth_events WHERE account=? AND event='page_view') inactive,(SELECT COUNT(*) FROM dh_growth_requests WHERE account=? AND state='consumed') replies",
|
"SELECT TIMESTAMPDIFF(DAY,?,NOW(3)) age,(SELECT TIMESTAMPDIFF(DAY,MAX(created_at),NOW(3)) FROM dh_growth_events WHERE account=? AND event IN ('page_view','chat_requested')) inactive,(SELECT COUNT(*) FROM dh_growth_requests WHERE account=? AND state='consumed' AND outcome='complete' AND purpose<>'suggestion') replies",
|
||||||
[user.created_at, account, account],
|
[user.created_at, account, account],
|
||||||
);
|
);
|
||||||
return activity.age <= 7 && Number(activity.replies) < 3
|
return activity.age <= 7 && Number(activity.replies) < 3
|
||||||
@@ -44,7 +44,7 @@ export async function campaignInbox(account: string) {
|
|||||||
revision: number;
|
revision: number;
|
||||||
holdout_percent: number;
|
holdout_percent: number;
|
||||||
}>(
|
}>(
|
||||||
"SELECT id,revision,holdout_percent FROM dh_growth_campaigns WHERE enabled=TRUE AND segment=? AND starts_at<=NOW(3) AND ends_at>NOW(3) AND NOT EXISTS(SELECT 1 FROM dh_growth_deliveries d WHERE d.campaign_id=dh_growth_campaigns.id AND d.account=? AND d.campaign_revision=dh_growth_campaigns.revision) ORDER BY starts_at DESC LIMIT 1",
|
"SELECT id,revision,holdout_percent FROM dh_growth_campaigns WHERE enabled=TRUE AND segment=? AND starts_at<=DATE_ADD(UTC_TIMESTAMP(3),INTERVAL 8 HOUR) AND ends_at>DATE_ADD(UTC_TIMESTAMP(3),INTERVAL 8 HOUR) AND NOT EXISTS(SELECT 1 FROM dh_growth_deliveries d WHERE d.campaign_id=dh_growth_campaigns.id AND d.account=? AND d.campaign_revision=dh_growth_campaigns.revision) ORDER BY starts_at DESC LIMIT 1",
|
||||||
[segment, account],
|
[segment, account],
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
@@ -63,7 +63,7 @@ export async function campaignInbox(account: string) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
return rows<{ id: string; title: string; content: string }>(
|
return rows<{ id: string; title: string; content: string }>(
|
||||||
"SELECT d.id,c.title,c.content FROM dh_growth_deliveries d JOIN dh_growth_campaigns c ON c.id=d.campaign_id AND c.revision=d.campaign_revision WHERE d.account=? AND d.held_out=FALSE AND c.enabled=TRUE AND c.starts_at<=NOW(3) AND c.ends_at>NOW(3) AND d.created_at>DATE_SUB(NOW(3),INTERVAL 1 DAY) ORDER BY d.created_at DESC LIMIT 1",
|
"SELECT d.id,c.title,c.content FROM dh_growth_deliveries d JOIN dh_growth_campaigns c ON c.id=d.campaign_id AND c.revision=d.campaign_revision WHERE d.account=? AND d.held_out=FALSE AND c.enabled=TRUE AND c.starts_at<=DATE_ADD(UTC_TIMESTAMP(3),INTERVAL 8 HOUR) AND c.ends_at>DATE_ADD(UTC_TIMESTAMP(3),INTERVAL 8 HOUR) AND d.created_at>DATE_SUB(NOW(3),INTERVAL 1 DAY) ORDER BY d.created_at DESC LIMIT 1",
|
||||||
[account],
|
[account],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ export async function campaignAction(
|
|||||||
const field = action === "exposure" ? "exposed_at" : "clicked_at";
|
const field = action === "exposure" ? "exposed_at" : "clicked_at";
|
||||||
await transaction(async (c) => {
|
await transaction(async (c) => {
|
||||||
const r = await write(
|
const r = await write(
|
||||||
`UPDATE dh_growth_deliveries d JOIN dh_growth_campaigns p ON p.id=d.campaign_id AND p.revision=d.campaign_revision SET d.${field}=COALESCE(d.${field},NOW(3)) WHERE d.id=? AND d.account=? AND d.held_out=FALSE AND p.enabled=TRUE AND p.ends_at>NOW(3) AND d.created_at>DATE_SUB(NOW(3),INTERVAL 1 DAY)${action === "click" ? " AND d.exposed_at IS NOT NULL" : ""}`,
|
`UPDATE dh_growth_deliveries d JOIN dh_growth_campaigns p ON p.id=d.campaign_id AND p.revision=d.campaign_revision SET d.${field}=COALESCE(d.${field},NOW(3)) WHERE d.id=? AND d.account=? AND d.held_out=FALSE AND p.enabled=TRUE AND p.ends_at>DATE_ADD(UTC_TIMESTAMP(3),INTERVAL 8 HOUR) AND d.created_at>DATE_SUB(NOW(3),INTERVAL 1 DAY)${action === "click" ? " AND d.exposed_at IS NOT NULL" : ""}`,
|
||||||
[id, account],
|
[id, account],
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -182,16 +182,20 @@ export async function mergeGuest(guest: string, account: string) {
|
|||||||
existingProfile = profiles.find((p) => p.account === account);
|
existingProfile = profiles.find((p) => p.account === account);
|
||||||
if (guestProfile) {
|
if (guestProfile) {
|
||||||
const sourceProfile = parseDigitalProfile(guestProfile.body),
|
const sourceProfile = parseDigitalProfile(guestProfile.body),
|
||||||
targetProfile = parseDigitalProfile(existingProfile?.body);
|
targetProfile = parseDigitalProfile(existingProfile?.body),
|
||||||
|
targetBody = existingProfile
|
||||||
|
? (json<Record<string, unknown>>(existingProfile.body) ?? {})
|
||||||
|
: {};
|
||||||
|
// 开户时可能只有NSFW字段,记录存在不等于已经设置称呼;显式空身份也必须保留。
|
||||||
await mergeDigitalProfile(
|
await mergeDigitalProfile(
|
||||||
account,
|
account,
|
||||||
{
|
{
|
||||||
...(existingProfile
|
...(typeof targetBody.persona === "string"
|
||||||
? {}
|
? {}
|
||||||
: {
|
: { persona: sourceProfile.persona }),
|
||||||
persona: sourceProfile.persona,
|
...(typeof targetBody.userName === "string"
|
||||||
userName: sourceProfile.userName,
|
? {}
|
||||||
}),
|
: { userName: sourceProfile.userName }),
|
||||||
favorites: [
|
favorites: [
|
||||||
...new Set([
|
...new Set([
|
||||||
...targetProfile.favorites,
|
...targetProfile.favorites,
|
||||||
|
|||||||
@@ -161,9 +161,29 @@ test(
|
|||||||
await transaction((c) =>
|
await transaction((c) =>
|
||||||
settleReply(guest, request, "stopped", 500, true, 20, 200, c),
|
settleReply(guest, request, "stopped", 500, true, 20, 200, c),
|
||||||
);
|
);
|
||||||
|
await connection.query(
|
||||||
|
"INSERT INTO dh_profiles(account,body) VALUES (?,?), (?,?)",
|
||||||
|
[
|
||||||
|
guest,
|
||||||
|
JSON.stringify({
|
||||||
|
userName: "游客称呼",
|
||||||
|
persona: "游客身份",
|
||||||
|
favorites: ["card"],
|
||||||
|
nsfwEnabled: false,
|
||||||
|
}),
|
||||||
|
"user:9",
|
||||||
|
JSON.stringify({ nsfwEnabled: false }),
|
||||||
|
],
|
||||||
|
);
|
||||||
await mergeGuest(guest, "user:9");
|
await mergeGuest(guest, "user:9");
|
||||||
await mergeGuest(guest, "user:9");
|
await mergeGuest(guest, "user:9");
|
||||||
assert.equal((await identity("user:9")).subject, subject);
|
assert.equal((await identity("user:9")).subject, subject);
|
||||||
|
const [profiles] = await connection.query<RowDataPacket[]>(
|
||||||
|
"SELECT body FROM dh_profiles WHERE account='user:9'",
|
||||||
|
);
|
||||||
|
assert.equal(profiles[0].body.userName, "游客称呼");
|
||||||
|
assert.equal(profiles[0].body.persona, "游客身份");
|
||||||
|
assert.equal(profiles[0].body.nsfwEnabled, false);
|
||||||
await assert.rejects(identity(guest));
|
await assert.rejects(identity(guest));
|
||||||
assert.equal((await entitlement("user:9")).used, 1);
|
assert.equal((await entitlement("user:9")).used, 1);
|
||||||
const [chats] = await connection.query<RowDataPacket[]>(
|
const [chats] = await connection.query<RowDataPacket[]>(
|
||||||
@@ -381,6 +401,37 @@ test(
|
|||||||
);
|
);
|
||||||
assert.equal(events.length, 1);
|
assert.equal(events.length, 1);
|
||||||
assert.ok(Math.abs(Number(events[0].happenedAt) - occurredAt) < 2);
|
assert.ok(Math.abs(Number(events[0].happenedAt) - occurredAt) < 2);
|
||||||
|
const { bucket } = await import(
|
||||||
|
"../src/modules/digital/growth/growth.policy"
|
||||||
|
);
|
||||||
|
let campaignId = randomUUID();
|
||||||
|
while (bucket("user:16", `campaign:${campaignId}:1`) < 500)
|
||||||
|
campaignId = randomUUID();
|
||||||
|
const beijing = (ms: number) =>
|
||||||
|
new Date(ms + 8 * 3600000)
|
||||||
|
.toISOString()
|
||||||
|
.slice(0, 19)
|
||||||
|
.replace("T", " ");
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: `/api/digital-admin/growth/campaigns/${campaignId}`,
|
||||||
|
headers,
|
||||||
|
payload: {
|
||||||
|
title: "北京时间窗口验收",
|
||||||
|
content: "隔离数据库中的站内卡片",
|
||||||
|
segment: "new",
|
||||||
|
enabled: true,
|
||||||
|
holdoutPercent: 5,
|
||||||
|
startsAt: beijing(Date.now() - 60000),
|
||||||
|
endsAt: beijing(Date.now() + 3600000),
|
||||||
|
revision: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
assert.equal(response.statusCode, 200, response.body);
|
||||||
|
assert.equal(
|
||||||
|
(await campaignInbox("user:16"))[0]?.title,
|
||||||
|
"北京时间窗口验收",
|
||||||
|
);
|
||||||
} finally {
|
} finally {
|
||||||
await app.close();
|
await app.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -578,3 +578,70 @@ test("服务端事件幂等键按故事隔离,游客承接不改变同一故
|
|||||||
assert.equal(ids[2], ids[3]);
|
assert.equal(ids[2], ids[3]);
|
||||||
assert.equal(ids[0].length, 64);
|
assert.equal(ids[0].length, 64);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("游客资料只填补新账号缺失字段,保留既有称呼和显式空身份", async (t) => {
|
||||||
|
const { mergeGuest } = await import(
|
||||||
|
"../src/modules/digital/growth/growth.repository"
|
||||||
|
);
|
||||||
|
for (const target of [
|
||||||
|
{ nsfwEnabled: false },
|
||||||
|
{ nsfwEnabled: false, userName: "既有称呼", persona: "" },
|
||||||
|
]) {
|
||||||
|
let patch: Record<string, unknown> | undefined;
|
||||||
|
const query = async (q: { sql: string }, args: unknown[]) => {
|
||||||
|
if (q.sql.startsWith("SELECT * FROM dh_growth_identities"))
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
args[0] === "guest:fixture"
|
||||||
|
? { merged_into: null, subject: "guest:fixture" }
|
||||||
|
: { subject: "user:7" },
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
];
|
||||||
|
if (
|
||||||
|
q.sql.startsWith("SELECT request_id") ||
|
||||||
|
q.sql.startsWith("SELECT id FROM dh_conversations")
|
||||||
|
)
|
||||||
|
return [[], []];
|
||||||
|
if (q.sql.startsWith("SELECT account,body FROM dh_profiles"))
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
account: "guest:fixture",
|
||||||
|
body: {
|
||||||
|
userName: "游客称呼",
|
||||||
|
persona: "游客身份",
|
||||||
|
favorites: ["role"],
|
||||||
|
nsfwEnabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ account: "user:7", body: target },
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
];
|
||||||
|
if (q.sql.startsWith("SELECT DATE_FORMAT"))
|
||||||
|
return [[{ day: "2026-10-28" }], []];
|
||||||
|
if (q.sql.startsWith("SELECT used FROM")) return [[{ used: 1 }], []];
|
||||||
|
if (q.sql.startsWith("INSERT INTO dh_profiles"))
|
||||||
|
patch = JSON.parse(String(args[1]));
|
||||||
|
return [{ affectedRows: 1 }, []];
|
||||||
|
};
|
||||||
|
t.mock.method(pool, "getConnection", async () => ({
|
||||||
|
query,
|
||||||
|
beginTransaction: async () => {},
|
||||||
|
commit: async () => {},
|
||||||
|
rollback: async () => {},
|
||||||
|
release: () => {},
|
||||||
|
}));
|
||||||
|
await mergeGuest("guest:fixture", "user:7");
|
||||||
|
assert.ok(patch);
|
||||||
|
assert.equal(patch.nsfwEnabled, undefined);
|
||||||
|
if ("userName" in target) {
|
||||||
|
assert.equal(patch.userName, undefined);
|
||||||
|
assert.equal(patch.persona, undefined);
|
||||||
|
} else {
|
||||||
|
assert.equal(patch.userName, "游客称呼");
|
||||||
|
assert.equal(patch.persona, "游客身份");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user