数据分析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,4 @@
import supa from '@/components/supadb/aksupainstance.uts'
import { rpcOrValue } from './rpc.uts'
import { rpcOrEmptyArray, rpcOrValue } from './rpc.uts'
export type CustomReportListItem = {
id: string
@@ -24,41 +23,41 @@ export type UpdateCustomReportParams = {
period: string | null
}
function safeString(v: any): string {
return v != null ? `${v}` : ''
}
// 改造:不再直查 analytics_reports 表,统一通过 RPC 获取当前用户的报表列表
export async function listCustomReports(ownerUserId: string): Promise<Array<CustomReportListItem>> {
const res: any = await supa
.from('analytics_reports')
.select('id, title, description, period, updated_at')
.eq('type', 'custom')
.eq('owner_user_id', ownerUserId)
.order('updated_at', { ascending: false } as any)
if (res?.error != null) {
throw res.error
}
const rows: Array<any> = Array.isArray(res.data) ? (res.data as Array<any>) : []
const rows = await rpcOrEmptyArray('rpc_get_custom_reports', {} as any)
const list: Array<CustomReportListItem> = []
for (let i = 0; i < rows.length; i++) {
const r = rows[i]
const r: any = rows[i]
list.push({
id: `${r.id}`,
title: `${r.title}`,
description: `${r.description || ''}`,
period: `${r.period || ''}`,
updated_at: `${r.updated_at || ''}`
id: safeString(r.getAny?.('id') ?? r.getString?.('id')),
title: safeString(r.getAny?.('title') ?? r.getString?.('title')),
description: safeString(r.getAny?.('description') ?? r.getString?.('description')),
// 兼容旧 UI 字段custom-report 页面里可能还在用 period 字段
period: '',
updated_at: safeString(r.getAny?.('updated_at') ?? r.getString?.('updated_at'))
})
}
return list
}
// 改造RPC 参数改为 p_definitionJSONB承载 period/metrics/chartType
export async function createCustomReport(params: CreateCustomReportParams): Promise<string> {
const definition = {
period: params.period,
metrics: params.metrics,
chartType: params.chartType || 'line'
}
const data = await rpcOrValue('rpc_create_custom_report', {
p_title: params.title,
p_description: params.description || '',
p_period: params.period,
p_metrics: params.metrics,
p_chart_type: params.chartType || 'line'
} as UTSJSONObject)
p_definition: definition
} as any)
if (data == null) {
throw new Error('保存失败未返回报表ID')
@@ -68,12 +67,17 @@ export async function createCustomReport(params: CreateCustomReportParams): Prom
}
export async function updateCustomReport(params: UpdateCustomReportParams): Promise<boolean> {
// 注意:旧 UI 只传 title/description/period这里把 period 合并进 definition
const definition = {
period: params.period
}
await rpcOrValue('rpc_update_custom_report', {
p_report_id: params.reportId,
p_title: params.title,
p_description: params.description,
p_period: params.period
} as UTSJSONObject)
p_definition: definition
} as any)
return true
}
@@ -81,7 +85,7 @@ export async function updateCustomReport(params: UpdateCustomReportParams): Prom
export async function deleteCustomReport(reportId: string): Promise<boolean> {
await rpcOrValue('rpc_delete_custom_report', {
p_report_id: reportId
} as UTSJSONObject)
} as any)
return true
}