数据分析ui补充完善,接入数据库

This commit is contained in:
comlibmb
2026-01-31 21:47:42 +08:00
parent 8f181b2b6a
commit 6716398175
71 changed files with 6501 additions and 10593 deletions

View File

@@ -1,5 +1,5 @@
import { computeDateRange, toDateOnly } from './dateRange.uts'
import { rpcOrEmptyArray, rpcOrNull } from './rpc.uts'
import { rpcOrEmptyArray, rpcOrNull, rpcOrValue } from './rpc.uts'
export type TrendData = { x: Array<string>; gmv: Array<number>; orders: Array<number> }
export type SegmentItem = { name: string; value: number }
@@ -17,10 +17,9 @@ export async function fetchDashboardTrend(period: string): Promise<TrendData> {
const p_start_date = toDateOnly(startIso)
const p_end_date = toDateOnly(endIso)
const rows = await rpcOrEmptyArray('rpc_analytics_trend_data', {
const rows = await rpcOrEmptyArray('rpc_analytics_sales_trend', {
p_start_date,
p_end_date,
p_merchant_id: null
p_end_date
} as any)
const x: Array<string> = []
@@ -28,45 +27,30 @@ export async function fetchDashboardTrend(period: string): Promise<TrendData> {
const orders: Array<number> = []
for (let i = 0; i < rows.length; i++) {
const row: any = rows[i]
const d = `${row.getString?.('date') ?? row.getString?.('day') ?? row.getString?.('date_key') ?? ''}`
if (d && d.length >= 10) x.push(d.slice(5))
else x.push(`${i + 1}`)
gmv.push(safeNumber(row.getAny?.('gmv') ?? row.getAny?.('total_amount') ?? 0))
orders.push(safeNumber(row.getAny?.('orders') ?? row.getAny?.('order_count') ?? 0))
const d = `${row.getAny?.('date') ?? ''}`
x.push(d.length >= 10 ? d.slice(5) : d)
gmv.push(safeNumber(row.getAny?.('gmv') ?? 0))
orders.push(safeNumber(row.getAny?.('orders') ?? 0))
}
return { x, gmv, orders }
}
export async function fetchDashboardRealtime(): Promise<any> {
const now = new Date()
const today0 = new Date(now.getFullYear(), now.getMonth(), now.getDate())
const todayISO = today0.toISOString()
const [kpiRow, onlineUsersVal] = await Promise.all([
rpcOrNull('rpc_analytics_realtime_kpis', {} as any),
rpcOrValue('rpc_analytics_online_users', {} as any)
])
const ySame = new Date(now.getTime() - 24 * 60 * 60 * 1000)
const y0 = new Date(ySame.getFullYear(), ySame.getMonth(), ySame.getDate())
const obj: any = kpiRow != null ? kpiRow : ({} as any)
const row = await rpcOrNull('rpc_analytics_realtime_kpis', {
p_start: todayISO,
p_end: now.toISOString(),
p_compare_start: y0.toISOString(),
p_compare_end: ySame.toISOString(),
p_merchant_id: null
} as any)
const safe = (v: any): number => {
const n = Number(v)
return isFinite(n) ? n : 0
}
const obj: any = row != null ? row : ({} as any)
return {
gmv: Math.round(safe(obj.getAny?.('gmv') ?? obj.getAny?.('total_gmv') ?? obj.getAny?.('revenue') ?? 0)),
gmv_growth: safe(obj.getAny?.('gmv_growth') ?? obj.getAny?.('gmv_growth_rate') ?? obj.getAny?.('revenue_growth') ?? 0),
orders: Math.round(safe(obj.getAny?.('orders') ?? obj.getAny?.('order_count') ?? obj.getAny?.('total_orders') ?? 0)),
order_growth: safe(obj.getAny?.('order_growth') ?? obj.getAny?.('order_growth_rate') ?? 0),
online_users: Math.round(safe(obj.getAny?.('online_users') ?? obj.getAny?.('active_users') ?? obj.getAny?.('current_users') ?? 0)),
conversion_rate: safe(obj.getAny?.('conversion_rate') ?? obj.getAny?.('conversion') ?? 0),
conversion_growth: safe(obj.getAny?.('conversion_growth') ?? obj.getAny?.('conversion_growth_rate') ?? 0)
gmv: Math.round(safeNumber(obj.getAny?.('gmv') ?? 0)),
gmv_growth: safeNumber(obj.getAny?.('gmv_growth') ?? 0),
orders: Math.round(safeNumber(obj.getAny?.('orders') ?? 0)),
order_growth: safeNumber(obj.getAny?.('order_growth') ?? 0),
online_users: Math.round(safeNumber(onlineUsersVal ?? 0)),
conversion_rate: safeNumber(obj.getAny?.('conversion_rate') ?? 0),
conversion_growth: safeNumber(obj.getAny?.('conversion_growth') ?? 0)
}
}
@@ -75,8 +59,7 @@ export async function fetchDashboardTopProducts(period: string, limit: number =
const rows = await rpcOrEmptyArray('rpc_analytics_top_products', {
p_start_date: toDateOnly(startIso),
p_end_date: toDateOnly(endIso),
p_limit: limit,
p_merchant_id: null
p_limit: limit
} as any)
const list: Array<TopProductItem> = []
@@ -86,7 +69,7 @@ export async function fetchDashboardTopProducts(period: string, limit: number =
id: `${row.getAny?.('id') ?? i}`,
rank: i + 1,
name: `${row.getAny?.('name') ?? '未知商品'}`,
sales: safeNumber(row.getAny?.('sales') ?? row.getAny?.('total_amount') ?? 0)
sales: safeNumber(row.getAny?.('sales') ?? 0)
})
}
return list
@@ -107,8 +90,8 @@ export async function fetchDashboardTopMerchants(period: string, limit: number =
id: `${row.getAny?.('id') ?? i}`,
rank: i + 1,
name: `${row.getAny?.('name') ?? row.getAny?.('shop_name') ?? '未知商家'}`,
sales: safeNumber(row.getAny?.('sales') ?? row.getAny?.('total_amount') ?? 0),
growth: safeNumber(row.getAny?.('growth') ?? row.getAny?.('growth_rate') ?? 0)
sales: safeNumber(row.getAny?.('sales') ?? 0),
growth: safeNumber(row.getAny?.('growth') ?? 0)
})
}
return list