105 lines
3.2 KiB
JavaScript
105 lines
3.2 KiB
JavaScript
export const METRIC_REPORTS_LIST_PATH = "/metric-reports";
|
|
|
|
export function metricReportDetailPath(metricViewId) {
|
|
if (!metricViewId) return METRIC_REPORTS_LIST_PATH;
|
|
return `${METRIC_REPORTS_LIST_PATH}/${encodeURIComponent(metricViewId)}`;
|
|
}
|
|
|
|
export function parseMetricViewIdFromReportUrl(reportViewUrl) {
|
|
if (!reportViewUrl || typeof reportViewUrl !== "string") return null;
|
|
try {
|
|
const u = new URL(
|
|
reportViewUrl.startsWith("http")
|
|
? reportViewUrl
|
|
: `https://placeholder.local${reportViewUrl.startsWith("/") ? "" : "/"}${reportViewUrl}`
|
|
);
|
|
return u.searchParams.get("id");
|
|
} catch {
|
|
const m = String(reportViewUrl).match(/[?&]id=([^&]+)/);
|
|
return m ? decodeURIComponent(m[1]) : null;
|
|
}
|
|
}
|
|
|
|
function extractMetricsArray(payload) {
|
|
if (!payload || typeof payload !== "object") return null;
|
|
if (Array.isArray(payload)) return payload;
|
|
if (Array.isArray(payload.metrics)) return payload.metrics;
|
|
if (Array.isArray(payload.items)) return payload.items;
|
|
if (Array.isArray(payload.reports)) return payload.reports;
|
|
if (Array.isArray(payload.list)) return payload.list;
|
|
return null;
|
|
}
|
|
|
|
/** Normalize GET metric/metric_report/list response to an array of items. */
|
|
export function parseMetricReportListResponse(apiResponse) {
|
|
if (!apiResponse) return [];
|
|
|
|
const root = apiResponse;
|
|
const isSuccess =
|
|
root?.status === "success" ||
|
|
root?.statusCode === 200 ||
|
|
root?.statusCode === "200";
|
|
|
|
if (isSuccess && root.data != null) {
|
|
const fromData = extractMetricsArray(root.data);
|
|
if (fromData) return fromData;
|
|
}
|
|
|
|
const fromRoot = extractMetricsArray(root);
|
|
if (fromRoot) return fromRoot;
|
|
|
|
if (root?.data != null && typeof root.data === "object") {
|
|
const fromNested = extractMetricsArray(root.data);
|
|
if (fromNested) return fromNested;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/** Value-tab graph shape from a list API metric object. */
|
|
export function listItemToValueGraph(item) {
|
|
if (!item || typeof item !== "object") return null;
|
|
return {
|
|
name: item.name ?? item.label ?? "Value",
|
|
value: item.value,
|
|
values: Array.isArray(item.values) ? item.values : [],
|
|
};
|
|
}
|
|
|
|
export function getMetricReportItemId(item) {
|
|
if (!item || typeof item !== "object") return null;
|
|
return (
|
|
item.id ??
|
|
item.metric_view_id ??
|
|
item.metricViewId ??
|
|
parseMetricViewIdFromReportUrl(item.report_view_url)
|
|
);
|
|
}
|
|
|
|
export function getMetricReportItemTitle(item) {
|
|
if (!item || typeof item !== "object") return "Metric";
|
|
return String(
|
|
item.name ?? item.title ?? item.value_title ?? item.label ?? "Metric"
|
|
).trim();
|
|
}
|
|
|
|
export function getMetricReportDefaultTab(item) {
|
|
const aggType = String(item?.agg_type || "").toLowerCase();
|
|
if (aggType === "team") return "Detail";
|
|
if (aggType === "individual") return "Trend";
|
|
return "Detail";
|
|
}
|
|
|
|
export function getMetricReportCurrentPeriod(item) {
|
|
const period = item?.current_period;
|
|
if (period == null || String(period).trim() === "") return null;
|
|
return String(period).trim();
|
|
}
|
|
|
|
/** Human-readable label for list-card agg_type badge (e.g. team → Team). */
|
|
export function formatMetricReportAggTypeLabel(aggType) {
|
|
const raw = String(aggType || "").trim();
|
|
if (!raw) return null;
|
|
return raw.charAt(0).toUpperCase() + raw.slice(1).toLowerCase();
|
|
}
|