feat: export business ability metrics
This commit is contained in:
@@ -4,7 +4,11 @@ import type {
|
||||
AudienceProfileDistributionItem,
|
||||
AudienceProfileExportRow,
|
||||
AudienceProfileKind,
|
||||
AudienceProfileResult
|
||||
AudienceProfileResult,
|
||||
BusinessAbilityDurationKind,
|
||||
BusinessAbilityEstimateMetrics,
|
||||
BusinessAbilityVideoKind,
|
||||
BusinessAbilityVideoMetrics
|
||||
} from "./audience-profile-types";
|
||||
|
||||
type AudienceProfileCsvColumn = {
|
||||
@@ -43,12 +47,54 @@ const CROWD_LABELS = [
|
||||
"小镇青年"
|
||||
];
|
||||
|
||||
const BUSINESS_VIDEO_LAYOUTS: Array<{
|
||||
key: BusinessAbilityVideoKind;
|
||||
label: string;
|
||||
}> = [
|
||||
{ key: "personalVideo", label: "个人视频" },
|
||||
{ key: "xingtuVideo", label: "星图视频" }
|
||||
];
|
||||
|
||||
const BUSINESS_VIDEO_METRIC_LAYOUTS: Array<{
|
||||
key: keyof BusinessAbilityVideoMetrics;
|
||||
label: string;
|
||||
}> = [
|
||||
{ key: "medianPlay", label: "播放量中位数" },
|
||||
{ key: "finishRate", label: "完播率" },
|
||||
{ key: "interactionRate", label: "互动率" },
|
||||
{ key: "publishedItems", label: "发布作品" },
|
||||
{ key: "averageDuration", label: "平均时长" },
|
||||
{ key: "averageLike", label: "平均点赞" },
|
||||
{ key: "averageComment", label: "平均评论" },
|
||||
{ key: "averageShare", label: "平均转发" }
|
||||
];
|
||||
|
||||
const BUSINESS_ESTIMATE_LAYOUTS: Array<{
|
||||
key: BusinessAbilityDurationKind;
|
||||
label: string;
|
||||
}> = [
|
||||
{ key: "oneToTwenty", label: "1-20s视频" },
|
||||
{ key: "twentyToSixty", label: "20-60s视频" },
|
||||
{ key: "overSixty", label: "60s以上视频" }
|
||||
];
|
||||
|
||||
const BUSINESS_ESTIMATE_METRIC_LAYOUTS: Array<{
|
||||
key: keyof BusinessAbilityEstimateMetrics;
|
||||
label: string;
|
||||
}> = [
|
||||
{ key: "expectedCpm", label: "预期CPM" },
|
||||
{ key: "expectedCpe", label: "预期CPE" },
|
||||
{ key: "expectedPlay", label: "预期播放量" },
|
||||
{ key: "hotRate", label: "爆文率" }
|
||||
];
|
||||
|
||||
export function buildAudienceProfileCsv(
|
||||
rows: AudienceProfileExportRow[]
|
||||
): string {
|
||||
const marketColumns = buildMarketCsvColumns(rows.map((row) => row.record));
|
||||
const csvColumns = [
|
||||
...marketColumns.map(toMarketColumn),
|
||||
...buildBusinessAbilityColumns(),
|
||||
...PROFILE_LAYOUTS.flatMap((layout) => buildProfileColumns(layout))
|
||||
];
|
||||
const headerLine = csvColumns.map((column) => column.header).join(",");
|
||||
@@ -59,6 +105,51 @@ export function buildAudienceProfileCsv(
|
||||
return [headerLine, ...rowLines].join("\n");
|
||||
}
|
||||
|
||||
function buildBusinessAbilityColumns(): AudienceProfileCsvColumn[] {
|
||||
return [
|
||||
...BUSINESS_VIDEO_LAYOUTS.flatMap((videoLayout) =>
|
||||
BUSINESS_VIDEO_METRIC_LAYOUTS.map((metricLayout) => ({
|
||||
header: `商业能力-${videoLayout.label}-${metricLayout.label}`,
|
||||
readValue: (row: AudienceProfileExportRow) =>
|
||||
readBusinessVideoValue(row, videoLayout.key, metricLayout.key)
|
||||
}))
|
||||
),
|
||||
...BUSINESS_ESTIMATE_LAYOUTS.flatMap((durationLayout) =>
|
||||
BUSINESS_ESTIMATE_METRIC_LAYOUTS.map((metricLayout) => ({
|
||||
header: `商业能力-${durationLayout.label}-${metricLayout.label}`,
|
||||
readValue: (row: AudienceProfileExportRow) =>
|
||||
readBusinessEstimateValue(row, durationLayout.key, metricLayout.key)
|
||||
}))
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
function readBusinessVideoValue(
|
||||
row: AudienceProfileExportRow,
|
||||
videoKey: BusinessAbilityVideoKind,
|
||||
metricKey: keyof BusinessAbilityVideoMetrics
|
||||
): string {
|
||||
const businessAbility = row.businessAbility;
|
||||
if (!businessAbility || businessAbility.status !== "success") {
|
||||
return "";
|
||||
}
|
||||
|
||||
return businessAbility.videos[videoKey]?.[metricKey] ?? "";
|
||||
}
|
||||
|
||||
function readBusinessEstimateValue(
|
||||
row: AudienceProfileExportRow,
|
||||
durationKey: BusinessAbilityDurationKind,
|
||||
metricKey: keyof BusinessAbilityEstimateMetrics
|
||||
): string {
|
||||
const businessAbility = row.businessAbility;
|
||||
if (!businessAbility || businessAbility.status !== "success") {
|
||||
return "";
|
||||
}
|
||||
|
||||
return businessAbility.estimates[durationKey]?.[metricKey] ?? "";
|
||||
}
|
||||
|
||||
function toMarketColumn(column: CsvColumn): AudienceProfileCsvColumn {
|
||||
return {
|
||||
header: column.header,
|
||||
|
||||
@@ -37,6 +37,51 @@ export type AudienceProfileResult =
|
||||
| AudienceProfileFailure;
|
||||
|
||||
export interface AudienceProfileExportRow {
|
||||
businessAbility?: BusinessAbilityResult;
|
||||
profiles: AudienceProfileSet;
|
||||
record: MarketRecord;
|
||||
}
|
||||
|
||||
export type BusinessAbilityVideoKind =
|
||||
| "personalVideo"
|
||||
| "xingtuVideo";
|
||||
|
||||
export interface BusinessAbilityVideoMetrics {
|
||||
averageComment: string;
|
||||
averageDuration: string;
|
||||
averageLike: string;
|
||||
averageShare: string;
|
||||
finishRate: string;
|
||||
interactionRate: string;
|
||||
medianPlay: string;
|
||||
publishedItems: string;
|
||||
}
|
||||
|
||||
export type BusinessAbilityDurationKind =
|
||||
| "oneToTwenty"
|
||||
| "twentyToSixty"
|
||||
| "overSixty";
|
||||
|
||||
export interface BusinessAbilityEstimateMetrics {
|
||||
expectedCpe: string;
|
||||
expectedCpm: string;
|
||||
expectedPlay: string;
|
||||
hotRate: string;
|
||||
}
|
||||
|
||||
export interface BusinessAbilitySuccess {
|
||||
estimates: Partial<
|
||||
Record<BusinessAbilityDurationKind, BusinessAbilityEstimateMetrics>
|
||||
>;
|
||||
status: "success";
|
||||
videos: Partial<Record<BusinessAbilityVideoKind, BusinessAbilityVideoMetrics>>;
|
||||
}
|
||||
|
||||
export interface BusinessAbilityFailure {
|
||||
failureReason?: string;
|
||||
status: "failed";
|
||||
}
|
||||
|
||||
export type BusinessAbilityResult =
|
||||
| BusinessAbilitySuccess
|
||||
| BusinessAbilityFailure;
|
||||
|
||||
@@ -0,0 +1,281 @@
|
||||
import type { MarketRecord } from "./types";
|
||||
import type {
|
||||
BusinessAbilityDurationKind,
|
||||
BusinessAbilityEstimateMetrics,
|
||||
BusinessAbilityResult,
|
||||
BusinessAbilitySuccess,
|
||||
BusinessAbilityVideoMetrics
|
||||
} from "./audience-profile-types";
|
||||
|
||||
interface FetchResponseLike {
|
||||
json(): Promise<unknown>;
|
||||
ok: boolean;
|
||||
}
|
||||
|
||||
type FetchLike = (
|
||||
input: string,
|
||||
init?: RequestInit
|
||||
) => Promise<FetchResponseLike>;
|
||||
|
||||
interface BusinessAbilityClientOptions {
|
||||
baseUrl?: string;
|
||||
fetchImpl?: FetchLike;
|
||||
timeoutMs?: number;
|
||||
}
|
||||
|
||||
const VIDEO_TYPES = {
|
||||
personalVideo: 1,
|
||||
xingtuVideo: 2
|
||||
} as const;
|
||||
|
||||
export function createBusinessAbilityClient(
|
||||
options: BusinessAbilityClientOptions = {}
|
||||
) {
|
||||
const baseUrl = options.baseUrl ?? resolveBaseUrl();
|
||||
const fetchImpl = options.fetchImpl ?? defaultFetch;
|
||||
const timeoutMs = options.timeoutMs ?? 8000;
|
||||
|
||||
return {
|
||||
async loadBusinessAbility(record: MarketRecord): Promise<BusinessAbilityResult> {
|
||||
const personalVideo = await loadJson(
|
||||
buildBusinessAbilityVideoUrl(record.authorId, baseUrl, VIDEO_TYPES.personalVideo)
|
||||
);
|
||||
const xingtuVideo = await loadJson(
|
||||
buildBusinessAbilityVideoUrl(record.authorId, baseUrl, VIDEO_TYPES.xingtuVideo)
|
||||
);
|
||||
const estimates = await loadJson(
|
||||
buildBusinessAbilityEstimateUrl(record.authorId, baseUrl)
|
||||
);
|
||||
|
||||
if (!personalVideo.ok || !xingtuVideo.ok || !estimates.ok) {
|
||||
return {
|
||||
failureReason:
|
||||
personalVideo.failureReason ??
|
||||
xingtuVideo.failureReason ??
|
||||
estimates.failureReason,
|
||||
status: "failed"
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
estimates: mapBusinessAbilityEstimateResponse(estimates.payload),
|
||||
status: "success",
|
||||
videos: {
|
||||
personalVideo: mapBusinessAbilityVideoResponse(personalVideo.payload),
|
||||
xingtuVideo: mapBusinessAbilityVideoResponse(xingtuVideo.payload)
|
||||
}
|
||||
} satisfies BusinessAbilitySuccess;
|
||||
}
|
||||
};
|
||||
|
||||
async function loadJson(url: string): Promise<
|
||||
| { ok: true; payload: unknown }
|
||||
| { failureReason: string; ok: false }
|
||||
> {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
||||
|
||||
try {
|
||||
const response = await fetchImpl(url, {
|
||||
credentials: "include",
|
||||
method: "GET",
|
||||
signal: controller.signal
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return { failureReason: "request-failed", ok: false };
|
||||
}
|
||||
|
||||
return { ok: true, payload: await response.json() };
|
||||
} catch (error) {
|
||||
return {
|
||||
failureReason:
|
||||
error instanceof Error && error.name === "AbortError"
|
||||
? "timeout"
|
||||
: "request-failed",
|
||||
ok: false
|
||||
};
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function buildBusinessAbilityVideoUrl(
|
||||
authorId: string,
|
||||
baseUrl: string,
|
||||
videoType: number
|
||||
): string {
|
||||
const url = new URL("/gw/api/data_sp/get_author_spread_info", baseUrl);
|
||||
url.searchParams.set("o_author_id", authorId);
|
||||
url.searchParams.set("platform_source", "1");
|
||||
url.searchParams.set("platform_channel", "1");
|
||||
url.searchParams.set("type", String(videoType));
|
||||
url.searchParams.set("flow_type", "0");
|
||||
url.searchParams.set("only_assign", "true");
|
||||
url.searchParams.set("range", "2");
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
export function buildBusinessAbilityEstimateUrl(
|
||||
authorId: string,
|
||||
baseUrl: string
|
||||
): string {
|
||||
const url = new URL(
|
||||
"/gw/api/aggregator/get_author_commerce_spread_info",
|
||||
baseUrl
|
||||
);
|
||||
url.searchParams.set("o_author_id", authorId);
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
export function mapBusinessAbilityVideoResponse(
|
||||
payload: unknown
|
||||
): BusinessAbilityVideoMetrics {
|
||||
const data = getPayloadData(payload);
|
||||
|
||||
return {
|
||||
averageComment: formatWan(readNumber(data?.comment_avg)),
|
||||
averageDuration: formatDuration(readNumber(data?.avg_duration)),
|
||||
averageLike: formatWan(readNumber(data?.like_avg)),
|
||||
averageShare: formatWan(readNumber(data?.share_avg)),
|
||||
finishRate: formatBasisPointRate(readNestedNumber(data, "play_over_rate", "value")),
|
||||
interactionRate: formatBasisPointRate(
|
||||
readNestedNumber(data, "interact_rate", "value")
|
||||
),
|
||||
medianPlay: formatWan(readNumber(data?.play_mid)),
|
||||
publishedItems: formatPublishedItems(readNumber(data?.item_num))
|
||||
};
|
||||
}
|
||||
|
||||
export function mapBusinessAbilityEstimateResponse(
|
||||
payload: unknown
|
||||
): Partial<Record<BusinessAbilityDurationKind, BusinessAbilityEstimateMetrics>> {
|
||||
const data = getPayloadData(payload);
|
||||
const expectedPlay = formatWan(readNumber(data?.vv));
|
||||
const hotRate = formatDecimalRate(readNumber(data?.platform_hot_rate));
|
||||
|
||||
return {
|
||||
oneToTwenty: {
|
||||
expectedCpe: formatDecimal(readNumber(data?.cpe_1_20), 1),
|
||||
expectedCpm: formatDecimal(readNumber(data?.cpm_1_20), 0),
|
||||
expectedPlay,
|
||||
hotRate
|
||||
},
|
||||
overSixty: {
|
||||
expectedCpe: formatDecimal(readNumber(data?.cpe_60), 1),
|
||||
expectedCpm: formatDecimal(readNumber(data?.cpm_60), 0),
|
||||
expectedPlay,
|
||||
hotRate
|
||||
},
|
||||
twentyToSixty: {
|
||||
expectedCpe: formatDecimal(readNumber(data?.cpe_20_60), 1),
|
||||
expectedCpm: formatDecimal(readNumber(data?.cpm_20_60), 0),
|
||||
expectedPlay,
|
||||
hotRate
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function formatPublishedItems(value: number | null): string {
|
||||
if (value === null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return value > 0 && value < 5 ? "<5" : formatDecimal(value, 0);
|
||||
}
|
||||
|
||||
function formatDuration(value: number | null): string {
|
||||
if (value === null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return `${formatDecimal(value / 100, 0)}s`;
|
||||
}
|
||||
|
||||
function formatBasisPointRate(value: number | null): string {
|
||||
if (value === null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return `${formatDecimal(value / 100, 1)}%`;
|
||||
}
|
||||
|
||||
function formatDecimalRate(value: number | null): string {
|
||||
if (value === null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return `${formatDecimal(value * 100, 0)}%`;
|
||||
}
|
||||
|
||||
function formatWan(value: number | null): string {
|
||||
if (value === null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (Math.abs(value) >= 10000) {
|
||||
return `${formatDecimal(value / 10000, 1)}w`;
|
||||
}
|
||||
|
||||
return formatDecimal(value, 0);
|
||||
}
|
||||
|
||||
function formatDecimal(value: number | null, digits: number): string {
|
||||
if (value === null || !Number.isFinite(value)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const fixed = value.toFixed(digits);
|
||||
return fixed.replace(/\.0+$/, "").replace(/(\.\d*?)0+$/, "$1");
|
||||
}
|
||||
|
||||
function readNestedNumber(
|
||||
data: Record<string, unknown> | null,
|
||||
objectKey: string,
|
||||
valueKey: string
|
||||
): number | null {
|
||||
const objectValue = data?.[objectKey];
|
||||
if (!isRecord(objectValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return readNumber(objectValue[valueKey]);
|
||||
}
|
||||
|
||||
function readNumber(value: unknown): number | null {
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const numericValue = Number(value);
|
||||
return Number.isFinite(numericValue) ? numericValue : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getPayloadData(payload: unknown): Record<string, unknown> | null {
|
||||
if (!isRecord(payload)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return isRecord(payload.data) ? payload.data : payload;
|
||||
}
|
||||
|
||||
function resolveBaseUrl(): string {
|
||||
if (typeof location !== "undefined" && location.origin) {
|
||||
return location.origin;
|
||||
}
|
||||
|
||||
return "https://xingtu.cn";
|
||||
}
|
||||
|
||||
async function defaultFetch(input: string, init?: RequestInit) {
|
||||
return fetch(input, init);
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
createAudienceProfileClient,
|
||||
type AudienceProfileRequestTarget
|
||||
} from "./audience-profile-client";
|
||||
import { createBusinessAbilityClient } from "./business-ability-client";
|
||||
import { promptForBatchName } from "./batch-name-dialog";
|
||||
import { createBatchPayload, type BatchPayload } from "./batch-payload";
|
||||
import {
|
||||
@@ -36,7 +37,8 @@ import { isBackendMetricsResponseMessage } from "../../shared/backend-metrics-me
|
||||
import type {
|
||||
AudienceProfileExportRow,
|
||||
AudienceProfileKind,
|
||||
AudienceProfileResult
|
||||
AudienceProfileResult,
|
||||
BusinessAbilityResult
|
||||
} from "./audience-profile-types";
|
||||
import type {
|
||||
BackendMetrics,
|
||||
@@ -57,6 +59,9 @@ export interface CreateMarketControllerOptions {
|
||||
buildCsv?: (records: MarketRecord[]) => string;
|
||||
document: Document;
|
||||
getAuthState?: () => Promise<AuthStateValue>;
|
||||
loadBusinessAbility?: (
|
||||
record: MarketRecord
|
||||
) => Promise<BusinessAbilityResult>;
|
||||
loadAudienceProfile?: (
|
||||
record: MarketRecord,
|
||||
target: AudienceProfileRequestTarget
|
||||
@@ -78,6 +83,7 @@ export interface CreateMarketControllerOptions {
|
||||
export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
const marketApiClient = createMarketApiClient();
|
||||
const audienceProfileClient = createAudienceProfileClient();
|
||||
const businessAbilityClient = createBusinessAbilityClient();
|
||||
const sendRuntimeMessage = createRuntimeMessageSender();
|
||||
const resultStore = options.resultStore ?? createMarketResultStore();
|
||||
const loadAuthorMetrics =
|
||||
@@ -89,6 +95,8 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
const buildAudienceCsv = options.buildAudienceProfileCsv ?? buildAudienceProfileCsv;
|
||||
const loadAudienceProfile =
|
||||
options.loadAudienceProfile ?? audienceProfileClient.loadAudienceProfile;
|
||||
const loadBusinessAbility =
|
||||
options.loadBusinessAbility ?? businessAbilityClient.loadBusinessAbility;
|
||||
const getAuthState = options.getAuthState ?? (() => readAuthState(sendRuntimeMessage));
|
||||
const mutationObserverFactory =
|
||||
options.mutationObserverFactory ??
|
||||
@@ -227,8 +235,12 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
toolbar,
|
||||
`画像导出中 ${index + 1}/${selectedRecords.length}...`
|
||||
);
|
||||
const profiles = await loadAudienceProfileSet(record);
|
||||
const [profiles, businessAbility] = await Promise.all([
|
||||
loadAudienceProfileSet(record),
|
||||
loadBusinessAbilitySafe(record)
|
||||
]);
|
||||
rows.push({
|
||||
businessAbility,
|
||||
profiles,
|
||||
record
|
||||
});
|
||||
@@ -680,6 +692,20 @@ export function createMarketController(options: CreateMarketControllerOptions) {
|
||||
return profiles;
|
||||
}
|
||||
|
||||
async function loadBusinessAbilitySafe(
|
||||
record: MarketRecord
|
||||
): Promise<BusinessAbilityResult> {
|
||||
try {
|
||||
return await loadBusinessAbility(record);
|
||||
} catch (error) {
|
||||
return {
|
||||
failureReason:
|
||||
error instanceof Error ? error.message : "request-failed",
|
||||
status: "failed"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function prepareCurrentPageForExport(): Promise<void> {
|
||||
await runSyncCycle();
|
||||
await harvestCurrentPageForExport();
|
||||
|
||||
Reference in New Issue
Block a user