From df7b9a0cb39b78d207c842eb37a57bccc2cc418b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B9=9B=E5=85=AE?=
{{ reasons[row.decision.reason] ?? row.decision.reason }}
@@ -339,8 +347,12 @@ onMounted(async () => { >{{
JSON.stringify(detail.decision?.value, null, 2)
diff --git a/src/views/digital/experiments.vue b/src/views/digital/experiments.vue
index 3b00d00..ef17ee3 100644
--- a/src/views/digital/experiments.vue
+++ b/src/views/digital/experiments.vue
@@ -8,6 +8,7 @@ import {
digital,
type Content,
type Experiment,
+ type ExperimentGroup,
type ExperimentType
} from "@/api/access";
type Account = { account: string; name: string; createdAt: string };
@@ -50,9 +51,8 @@ type Details = {
const guideVisible = ref(false);
const router = useRouter();
const scenes = ref([]);
-const validA = ref(true),
- validB = ref(true),
- filterType = ref("");
+const groupValid = ref>({});
+const filterType = ref("");
const filtered = computed(() =>
list.value.filter(e => !filterType.value || e.scope === filterType.value)
);
@@ -98,12 +98,53 @@ const trafficPercent = computed({
if (edit.value) edit.value.config.traffic = Math.round((value ?? 0) * 100);
}
});
-const weightBPercent = computed({
- get: () => (edit.value?.config.weightB ?? 0) / 100,
- set: (value: number | undefined) => {
- if (edit.value) edit.value.config.weightB = Math.round((value ?? 0) * 100);
+const groupTotal = computed(
+ () => edit.value?.config.groups.reduce((sum, g) => sum + g.weight, 0) ?? 0
+);
+const groupKeys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
+/** 两组时自动互补;更多组由用户显式填写,不悄悄改动其他组。 */
+function setWeight(group: ExperimentGroup, percentage: number | undefined) {
+ if (!edit.value) return;
+ group.weight = Math.round((percentage ?? 0) * 100);
+ if (edit.value.config.groups.length === 2) {
+ const other = edit.value.config.groups.find(g => g.key !== group.key)!;
+ other.weight = 10000 - group.weight;
}
-});
+}
+function addGroup() {
+ if (!edit.value || !editingType.value) return;
+ const key = groupKeys.find(
+ k => !edit.value!.config.groups.some(g => g.key === k)
+ );
+ if (key)
+ edit.value.config.groups.push({
+ key,
+ weight: 0,
+ value: JSON.parse(JSON.stringify(editingType.value.defaultValue))
+ });
+}
+function removeGroup(key: string) {
+ if (!edit.value || edit.value.config.groups.length <= 1) return;
+ edit.value.config.groups = edit.value.config.groups.filter(
+ g => g.key !== key
+ );
+ delete groupValid.value[key];
+}
+function distributeEvenly() {
+ if (!edit.value) return;
+ const groups = edit.value.config.groups;
+ const each = Math.floor(10000 / groups.length),
+ extra = 10000 % groups.length;
+ groups.forEach((g, i) => {
+ g.weight = each + (i < extra ? 1 : 0);
+ });
+}
+function fillRemaining(group: ExperimentGroup) {
+ group.weight = Math.max(0, 10000 - (groupTotal.value - group.weight));
+}
+function groupSummary(item: Experiment) {
+ return item.config.groups.map(g => `${g.key} ${g.weight / 100}%`).join(" · ");
+}
const editingType = computed(() =>
scenes.value.find(s => s.id === edit.value?.scope)
);
@@ -112,23 +153,21 @@ const presetOptions = computed(() =>
? presets.value.map(p => ({ label: p.name, value: p.id }))
: undefined
);
-/** 类型切换时重新初始化值,编辑历史方案时兼容旧预设字段。 */
+/** 类型切换只重置返回值,不改变用户已经配置的组别和比例。 */
function initializeValues(reset = false) {
if (!edit.value || !editingType.value) return;
- const t = editingType.value,
- c = edit.value.config;
- c.values = JSON.parse(
- JSON.stringify(
- t.contract.fixedValues ??
- (!reset && c.values
- ? c.values
- : t.contract.optionsSource === "presets"
- ? { A: c.presetA ?? "", B: c.presetB ?? "" }
- : { A: t.defaultValue, B: t.defaultValue })
- )
- );
- validA.value = true;
- validB.value = true;
+ const type = editingType.value;
+ for (const group of edit.value.config.groups) {
+ if (reset || group.value === undefined)
+ group.value = JSON.parse(
+ JSON.stringify(
+ type.valueType === "boolean" && group.key === "B"
+ ? !type.defaultValue
+ : type.defaultValue
+ )
+ );
+ }
+ groupValid.value = {};
}
function users(item: Experiment) {
router.push({
@@ -173,11 +212,14 @@ function open(item?: Experiment) {
audience: "all",
accounts: [],
traffic: 10000,
- weightB: 5000,
- baseline: "A",
+ groups: [
+ { key: "A", weight: 5000, value: undefined },
+ { key: "B", weight: 5000, value: undefined }
+ ],
description: ""
}
};
+ edit.value.config.groups.sort((a, b) => a.key.localeCompare(b.key));
initializeValues();
editor.value = true;
}
@@ -188,9 +230,11 @@ function createTrial() {
edit.value.config.audience = "specified";
edit.value.config.accounts = [];
edit.value.config.traffic = 10000;
- edit.value.config.weightB = 10000;
+ edit.value.config.groups.forEach(g => {
+ g.weight = g.key === "B" ? 10000 : 0;
+ });
edit.value.config.description =
- "选择你创建并接入的实验类型,只选择自己的账号;在指定用户中切换A/B对照,验收完成后另建正式方案。";
+ "选择你创建并接入的实验类型,只选择自己的账号;在指定用户中切换各组对照,验收完成后另建正式方案。";
initializeValues(true);
guideVisible.value = false;
}
@@ -207,8 +251,10 @@ async function save() {
!edit.value.config.accounts.length
)
return ElMessage.warning("请选择至少一个入组账号,验收时先选择自己");
- if (!validA.value || !validB.value)
- return ElMessage.warning("请修正 A/B 返回值的 JSON 格式");
+ if (groupTotal.value !== 10000)
+ return ElMessage.warning("所有分组比例合计必须为 100%");
+ if (Object.values(groupValid.value).some(valid => !valid))
+ return ElMessage.warning("请修正分组返回值的 JSON 格式");
saving.value = true;
try {
await digital(`experiments/${edit.value.id}`, "put", edit.value);
@@ -224,15 +270,15 @@ async function save() {
async function move(
item: Experiment,
state: "running" | "paused" | "finished",
- winner?: "A" | "B"
+ winner?: string
) {
try {
await ElMessageBox.confirm(
state === "running"
? `启动「${item.name}」?启动后人群、比例和方案将冻结。`
: state === "paused"
- ? "暂停后用户回到默认方案,已有分组保留。"
- : `结束后所有用户采用 ${winner} 方案,实验不能重新启动。`,
+ ? "暂停后用户使用类型默认返回值,已有分组保留。"
+ : `结束后所有用户采用 ${winner} 组返回值,实验不能重新启动。`,
state === "finished" ? "结束实验" : "确认状态变更",
{ type: "warning", confirmButtonText: "确认", cancelButtonText: "取消" }
);
@@ -312,7 +358,7 @@ onMounted(load);
>
{{ audienceNames[row.config.audience] }}
流量 {{ row.config.traffic / 100 }}% · B组
- {{ row.config.weightB / 100 }}%实验流量 {{ row.config.traffic / 100 }}%
{{
+ groupSummary(row)
+ }} + 目标人群中有多少人参与实验;未参与的用户使用类型默认返回值。 +
++ 默认 A、B 两组,可添加到 Z。0% + 组不会自动分流,仍可人工指定;返回值不因比例改变。 +
++ 未参与实验或实验暂停时,返回类型默认值:{{ + JSON.stringify(editingType.defaultValue) + }},不属于任何实验组。 +
{{ JSON.stringify(row.value) }}- 选择类型,配置 A/B 返回值、人群与百分比。100% 流量、B 组 50% - 表示所有入组用户 A/B - 各一半。保存只是草稿,启动才生效;启动后规则冻结。 + 选择类型,配置 A~Z 各组的返回值、分流比例与人群。各组比例合计必须为 + 100%;A 100%、B 0% 时,自动流量全部进入 + A。保存只是草稿,启动才生效;启动后规则冻结。
从方案行点击「用户管理」,搜索账号,查看只读分组预览。点击「指定分组」可指定 - A/B、有效期与原因;「恢复自动」取消人工指定,回到原稳定分组。预览本身不会分配用户或产生曝光。 + 任意配置组、有效期与原因;「恢复自动」取消人工指定,回到原稳定分组。预览本身不会分配用户或产生曝光。
选择你已接入的类型,指定名单只选自己,流量 100%、B 组 - 100%,启动后在应用检查 B 组效果。再到用户管理指定 A/B - 对照。其他账号按默认组。验收完成另建正式方案。 + 100%,启动后在应用检查 B 组效果。再到用户管理指定不同组 + 对照。其他账号使用类型默认返回值。验收完成另建正式方案。
- 在「效果与审计」查看决策、曝光、漏斗和操作记录。暂停回方案默认组,恢复保留原分组;结束时选择最终 - A/B,之后对所有用户长期生效。新类型须在消费者实际展示后调用曝光接口,不能把决策请求当作曝光。 + 在「效果与审计」查看决策、曝光、漏斗和操作记录。暂停回类型默认返回值,恢复保留原分组;结束时选择最终 + 任意组,之后对所有用户长期生效。新类型须在消费者实际展示后调用曝光接口,不能把决策请求当作曝光。