Skip to content

Commit

Permalink
Merge branch 'dev' into api-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
fomalhautb authored Jan 7, 2025
2 parents db96ef8 + bca8202 commit e53a28e
Show file tree
Hide file tree
Showing 86 changed files with 1,908 additions and 365 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
*.untracked.*


*.cpuprofile



.vercel

Expand Down
26 changes: 26 additions & 0 deletions apps/backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
# @stackframe/stack-backend

## 2.7.4

### Patch Changes

- Various changes
- @stackframe/stack-emails@2.7.4
- @stackframe/stack-shared@2.7.4

## 2.7.3

### Patch Changes

- Various changes
- Updated dependencies
- @stackframe/stack-shared@2.7.3
- @stackframe/stack-emails@2.7.3

## 2.7.2

### Patch Changes

- Various changes
- Updated dependencies
- @stackframe/stack-shared@2.7.2
- @stackframe/stack-emails@2.7.2

## 2.7.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stackframe/stack-backend",
"version": "2.7.1",
"version": "2.7.4",
"private": true,
"scripts": {
"clean": "rimraf .next && rimraf node_modules",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- It's very common to query by userId, projectId, and eventStartedAt at the same time.
-- We can use a composite index to speed up the query.
-- Sadly we can't add this to the Prisma schema itself because Prisma does not understand composite indexes of JSONB fields.
-- So we have to add it manually.
CREATE INDEX idx_event_userid_projectid_eventstartedat ON "Event" ((data->>'projectId'), (data->>'userId'), "eventStartedAt");
19 changes: 17 additions & 2 deletions apps/backend/scripts/verify-data-integrity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PrismaClient } from "@prisma/client";
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
import { StackAssertionError, throwErr } from "@stackframe/stack-shared/dist/utils/errors";
import { filterUndefined } from "@stackframe/stack-shared/dist/utils/objects";
import { wait } from "@stackframe/stack-shared/dist/utils/promises";
import { deindent } from "@stackframe/stack-shared/dist/utils/strings";
Expand Down Expand Up @@ -70,7 +70,7 @@ async function main() {
for (let i = 0; i < projects.length; i++) {
const projectId = projects[i].id;
await recurse(`[project ${i + 1}/${projects.length}] ${projectId} ${projects[i].displayName}`, async (recurse) => {
await Promise.all([
const [currentProject, users] = await Promise.all([
expectStatusCode(200, `/api/v1/projects/current`, {
method: "GET",
headers: {
Expand All @@ -88,6 +88,21 @@ async function main() {
},
}),
]);
if (users.pagination?.next_cursor) throwErr("Users are paginated? Please update the verify-data-integrity.ts script to handle this.");

for (let j = 0; j < users.items.length; j++) {
const user = users.items[j];
await recurse(`[user ${j + 1}/${users.items.length}] ${user.display_name ?? user.primary_email}`, async (recurse) => {
await expectStatusCode(200, `/api/v1/users/${user.id}`, {
method: "GET",
headers: {
"x-stack-project-id": projectId,
"x-stack-access-type": "admin",
"x-stack-development-override-key": getEnvVariable("STACK_SEED_INTERNAL_PROJECT_SUPER_SECRET_ADMIN_KEY"),
},
});
});
}
});
}

Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/app/api/v1/internal/metrics/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function loadTotalUsers(projectId: string, now: Date): Promise<DataPoints>
return (await prismaClient.$queryRaw<{date: Date, dailyUsers: bigint, cumUsers: bigint}[]>`
WITH date_series AS (
SELECT GENERATE_SERIES(
${now}::date - INTERVAL '1 month',
${now}::date - INTERVAL '30 days',
${now}::date,
'1 day'
)
Expand All @@ -72,7 +72,7 @@ async function loadDailyActiveUsers(projectId: string, now: Date) {
const res = await prismaClient.$queryRaw<{day: Date, dau: bigint}[]>`
WITH date_series AS (
SELECT GENERATE_SERIES(
${now}::date - INTERVAL '1 month',
${now}::date - INTERVAL '30 days',
${now}::date,
'1 day'
)
Expand All @@ -83,7 +83,7 @@ async function loadDailyActiveUsers(projectId: string, now: Date) {
DATE_TRUNC('day', "eventStartedAt") AS "day",
COUNT(DISTINCT "data"->'userId') AS "dau"
FROM "Event"
WHERE "eventStartedAt" >= ${now} - INTERVAL '1 month'
WHERE "eventStartedAt" >= ${now} - INTERVAL '30 days'
AND "eventStartedAt" < ${now}
AND '$user-activity' = ANY("systemEventTypeIds"::text[])
AND "data"->>'projectId' = ${projectId}
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/app/api/v1/team-member-profiles/crud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const teamMemberProfilesCrudHandlers = createLazyProxy(() => createCrudHa
include: fullInclude,
});

const lastActiveAtMillis = await getUsersLastActiveAtMillis(db.map(user => user.projectUserId), db.map(user => user.createdAt));
const lastActiveAtMillis = await getUsersLastActiveAtMillis(auth.project.id, db.map(user => user.projectUserId), db.map(user => user.createdAt));

return {
items: db.map((user, index) => prismaToCrud(user, lastActiveAtMillis[index])),
Expand Down Expand Up @@ -118,7 +118,7 @@ export const teamMemberProfilesCrudHandlers = createLazyProxy(() => createCrudHa
throw new KnownErrors.TeamMembershipNotFound(params.team_id, params.user_id);
}

return prismaToCrud(db, await getUserLastActiveAtMillis(db.projectUser.projectUserId) ?? db.projectUser.createdAt.getTime());
return prismaToCrud(db, await getUserLastActiveAtMillis(auth.project.id, db.projectUser.projectUserId) ?? db.projectUser.createdAt.getTime());
});
},
onUpdate: async ({ auth, data, params }) => {
Expand Down Expand Up @@ -151,7 +151,7 @@ export const teamMemberProfilesCrudHandlers = createLazyProxy(() => createCrudHa
include: fullInclude,
});

return prismaToCrud(db, await getUserLastActiveAtMillis(db.projectUser.projectUserId) ?? db.projectUser.createdAt.getTime());
return prismaToCrud(db, await getUserLastActiveAtMillis(auth.project.id, db.projectUser.projectUserId) ?? db.projectUser.createdAt.getTime());
});
},
}));
Loading

0 comments on commit e53a28e

Please sign in to comment.