feat: 完成 rust cutover phase 8 收口
This commit is contained in:
@@ -14,6 +14,61 @@ async function requireWorkspaceMember(ctx: any, workspaceId: string, userId: str
|
||||
return member;
|
||||
}
|
||||
|
||||
function sortByNewest<T extends Record<string, any>>(rows: T[]) {
|
||||
return [...rows].sort((left, right) => {
|
||||
const leftTime = String(left.created_at ?? left.finished_at ?? "");
|
||||
const rightTime = String(right.created_at ?? right.finished_at ?? "");
|
||||
return rightTime.localeCompare(leftTime);
|
||||
});
|
||||
}
|
||||
|
||||
function decodeCursor(raw: string | null | undefined) {
|
||||
if (!raw) return null;
|
||||
try {
|
||||
const decoded = JSON.parse(raw) as {
|
||||
createdAt?: string | null;
|
||||
id?: string | null;
|
||||
};
|
||||
const createdAt = typeof decoded.createdAt === "string" ? decoded.createdAt : "";
|
||||
const id = typeof decoded.id === "string" ? decoded.id : "";
|
||||
if (!createdAt || !id) return null;
|
||||
return { createdAt, id };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function encodeCursor(row: { created_at?: string | null; id?: string | null } | null) {
|
||||
if (!row?.created_at || !row?.id) return null;
|
||||
return JSON.stringify({
|
||||
createdAt: row.created_at,
|
||||
id: row.id,
|
||||
});
|
||||
}
|
||||
|
||||
function matchesCursor<T extends Record<string, any>>(
|
||||
row: T,
|
||||
cursor: { createdAt: string; id: string } | null,
|
||||
) {
|
||||
if (!cursor) return true;
|
||||
const createdAt = String(row.created_at ?? row.finished_at ?? "");
|
||||
const id = String(row.id ?? "");
|
||||
if (!createdAt || !id) return false;
|
||||
if (createdAt < cursor.createdAt) return true;
|
||||
if (createdAt > cursor.createdAt) return false;
|
||||
return id < cursor.id;
|
||||
}
|
||||
|
||||
function normalizeStatusFilter(raw: string | null | undefined) {
|
||||
const normalized = String(raw ?? "").trim();
|
||||
return normalized.length > 0 ? normalized : null;
|
||||
}
|
||||
|
||||
function normalizeObjectFilter(raw: string | null | undefined) {
|
||||
const normalized = String(raw ?? "").trim();
|
||||
return normalized.length > 0 ? normalized : null;
|
||||
}
|
||||
|
||||
export const recordCommandLog = mutation({
|
||||
args: {
|
||||
workspaceId: v.string(),
|
||||
@@ -171,3 +226,107 @@ export const listByRequest = query({
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export const listByCommand = query({
|
||||
args: { workspaceId: v.string(), commandId: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
await requireWorkspaceMember(ctx, args.workspaceId, userId);
|
||||
|
||||
const commandLogs = await ctx.db
|
||||
.query("command_logs")
|
||||
.withIndex("by_workspace_command", (q) =>
|
||||
q.eq("workspace_id", args.workspaceId).eq("command_id", args.commandId),
|
||||
)
|
||||
.collect();
|
||||
const domainEvents = await ctx.db
|
||||
.query("domain_events")
|
||||
.withIndex("by_workspace_command", (q) =>
|
||||
q.eq("workspace_id", args.workspaceId).eq("command_id", args.commandId),
|
||||
)
|
||||
.collect();
|
||||
|
||||
return {
|
||||
command_id: args.commandId,
|
||||
command_logs: commandLogs,
|
||||
domain_events: domainEvents,
|
||||
generated_at: nowIso(),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export const listWorkspaceOverview = query({
|
||||
args: {
|
||||
workspaceId: v.string(),
|
||||
limit: v.optional(v.number()),
|
||||
cursor: v.optional(v.union(v.string(), v.null())),
|
||||
commandStatus: v.optional(v.union(v.string(), v.null())),
|
||||
eventStatus: v.optional(v.union(v.string(), v.null())),
|
||||
targetPageId: v.optional(v.union(v.string(), v.null())),
|
||||
targetBlockId: v.optional(v.union(v.string(), v.null())),
|
||||
aggregateType: v.optional(v.union(v.string(), v.null())),
|
||||
aggregateId: v.optional(v.union(v.string(), v.null())),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const userId = await requireUserId(ctx);
|
||||
await requireWorkspaceMember(ctx, args.workspaceId, userId);
|
||||
|
||||
const limit = Math.max(1, Math.min(100, Math.floor(args.limit ?? 50)));
|
||||
const cursor = decodeCursor(args.cursor ?? null);
|
||||
const commandStatus = normalizeStatusFilter(args.commandStatus);
|
||||
const eventStatus = normalizeStatusFilter(args.eventStatus);
|
||||
const targetPageId = normalizeObjectFilter(args.targetPageId);
|
||||
const targetBlockId = normalizeObjectFilter(args.targetBlockId);
|
||||
const aggregateType = normalizeObjectFilter(args.aggregateType);
|
||||
const aggregateId = normalizeObjectFilter(args.aggregateId);
|
||||
|
||||
const allCommandLogs = await ctx.db
|
||||
.query("command_logs")
|
||||
.withIndex("by_workspace_request", (q) => q.eq("workspace_id", args.workspaceId))
|
||||
.collect();
|
||||
const allDomainEvents = await ctx.db
|
||||
.query("domain_events")
|
||||
.withIndex("by_workspace_request", (q) => q.eq("workspace_id", args.workspaceId))
|
||||
.collect();
|
||||
|
||||
const filteredCommandLogs = sortByNewest(
|
||||
allCommandLogs.filter((row: any) => {
|
||||
if (commandStatus && row.status !== commandStatus) return false;
|
||||
if (targetPageId && String(row.target_page_id ?? "") !== targetPageId) return false;
|
||||
if (targetBlockId && String(row.target_block_id ?? "") !== targetBlockId) return false;
|
||||
return matchesCursor(row, cursor);
|
||||
}),
|
||||
);
|
||||
|
||||
const pageCommandLogs = filteredCommandLogs.slice(0, limit);
|
||||
const nextCursor = encodeCursor(pageCommandLogs[pageCommandLogs.length - 1] ?? null);
|
||||
const commandIds = new Set(pageCommandLogs.map((row: any) => String(row.command_id)));
|
||||
|
||||
const domainEvents = sortByNewest(
|
||||
allDomainEvents.filter((row: any) => {
|
||||
if (commandIds.size > 0 && !commandIds.has(String(row.command_id ?? ""))) return false;
|
||||
if (eventStatus && row.status !== eventStatus) return false;
|
||||
if (aggregateType && String(row.aggregate_type ?? "") !== aggregateType) return false;
|
||||
if (aggregateId && String(row.aggregate_id ?? "") !== aggregateId) return false;
|
||||
return true;
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
workspace_id: args.workspaceId,
|
||||
command_logs: pageCommandLogs,
|
||||
domain_events: domainEvents,
|
||||
next_cursor: nextCursor,
|
||||
has_more: filteredCommandLogs.length > pageCommandLogs.length,
|
||||
filters: {
|
||||
command_status: commandStatus,
|
||||
event_status: eventStatus,
|
||||
target_page_id: targetPageId,
|
||||
target_block_id: targetBlockId,
|
||||
aggregate_type: aggregateType,
|
||||
aggregate_id: aggregateId,
|
||||
},
|
||||
generated_at: nowIso(),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user