Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+67
@@ -0,0 +1,67 @@
|
||||
// @vitest-environment node
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
tenantFindMany: vi.fn(),
|
||||
visitorGroupBy: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
dbRead: {
|
||||
tenant: { findMany: mocks.tenantFindMany },
|
||||
analyticsUniqueVisitor: { groupBy: mocks.visitorGroupBy },
|
||||
},
|
||||
}));
|
||||
|
||||
import { groupUniqueVisitorsBy } from "./analyticsUniqueVisitors.db.server";
|
||||
|
||||
describe("admin analytics visitor grouping", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.tenantFindMany.mockImplementation(async () => {
|
||||
expect(getRlsExecutionContext()).toEqual({ userId: "operator_1" });
|
||||
return [{ id: "tenant_1" }, { id: "tenant_2" }];
|
||||
});
|
||||
mocks.visitorGroupBy.mockImplementation(async () => {
|
||||
const context = getRlsExecutionContext();
|
||||
if (context?.tenantId === "tenant_1") {
|
||||
expect(context).toEqual({ userId: "operator_1", tenantId: "tenant_1" });
|
||||
return [
|
||||
{ browser: "Chrome", _count: 2 },
|
||||
{ browser: "Safari", _count: 1 },
|
||||
];
|
||||
}
|
||||
if (context?.tenantId === "tenant_2") {
|
||||
expect(context).toEqual({ userId: "operator_1", tenantId: "tenant_2" });
|
||||
return [
|
||||
{ browser: "Chrome", _count: 3 },
|
||||
{ browser: null, _count: 2 },
|
||||
];
|
||||
}
|
||||
if (context?.platformAnalytics) {
|
||||
expect(context).toEqual({ platformAnalytics: true });
|
||||
return [
|
||||
{ browser: "Chrome", _count: 4 },
|
||||
{ browser: "Firefox", _count: 5 },
|
||||
];
|
||||
}
|
||||
throw new Error(`Unexpected RLS context: ${JSON.stringify(context)}`);
|
||||
});
|
||||
});
|
||||
|
||||
it("merges tenant-scoped counts with isolated anonymous platform counts", async () => {
|
||||
const result = await runWithRlsContext({ userId: "operator_1" }, () => groupUniqueVisitorsBy("browser"));
|
||||
|
||||
expect(result).toEqual([
|
||||
{ name: "Chrome", count: 9 },
|
||||
{ name: "Safari", count: 1 },
|
||||
{ name: "", count: 2 },
|
||||
{ name: "Firefox", count: 5 },
|
||||
]);
|
||||
expect(mocks.tenantFindMany).toHaveBeenCalledWith({ select: { id: true }, orderBy: { id: "asc" } });
|
||||
expect(mocks.visitorGroupBy).toHaveBeenCalledTimes(3);
|
||||
expect(mocks.visitorGroupBy).toHaveBeenNthCalledWith(1, { by: ["browser"], _count: true });
|
||||
expect(getRlsExecutionContext()).toBeNull();
|
||||
});
|
||||
});
|
||||
+28
-5
@@ -1,12 +1,35 @@
|
||||
import { Prisma } from "@hivegrid/db/prisma-browser";
|
||||
import { runWithIsolatedPlatformAnalyticsRlsContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { dbRead } from "~/utils/db.server";
|
||||
|
||||
export async function groupUniqueVisitorsBy(by: Prisma.AnalyticsUniqueVisitorScalarFieldEnum) {
|
||||
// Pure aggregation for analytics dashboards — eventually-consistent is fine.
|
||||
return (
|
||||
await dbRead.analyticsUniqueVisitor.groupBy({
|
||||
const tenantIds = await dbRead.tenant.findMany({
|
||||
select: { id: true },
|
||||
orderBy: { id: "asc" },
|
||||
});
|
||||
const counts = new Map<string, number>();
|
||||
|
||||
async function addGroupsVisibleInCurrentContext() {
|
||||
// Pure aggregation for analytics dashboards — eventually-consistent is fine.
|
||||
const groups = await dbRead.analyticsUniqueVisitor.groupBy({
|
||||
by: [by],
|
||||
_count: true,
|
||||
})
|
||||
).map((f) => ({ name: f[by]?.toString() ?? ("" as string), count: f._count }));
|
||||
});
|
||||
for (const group of groups) {
|
||||
const name = group[by]?.toString() ?? "";
|
||||
counts.set(name, (counts.get(name) ?? 0) + group._count);
|
||||
}
|
||||
}
|
||||
|
||||
// Keep tenant reads independently scoped. Sequential execution also avoids
|
||||
// opening an unbounded number of read-replica transactions for large estates.
|
||||
for (const tenant of tenantIds) {
|
||||
await runWithRlsContext({ tenantId: tenant.id }, addGroupsVisibleInCurrentContext);
|
||||
}
|
||||
|
||||
// Tenantless analytics is a separate anonymous capability and must never
|
||||
// inherit the authenticated operator identity from the admin request.
|
||||
await runWithIsolatedPlatformAnalyticsRlsContext(addGroupsVisibleInCurrentContext);
|
||||
|
||||
return [...counts].map(([name, count]) => ({ name, count }));
|
||||
}
|
||||
Reference in new issue
Block a user