Skip to content

Commit

Permalink
improve logging for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Dec 7, 2023
1 parent 87fbf29 commit 1ae2a35
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE `ApiRequest` ADD COLUMN `apiVersion` VARCHAR(191) NOT NULL DEFAULT 'v0',
MODIFY `type` ENUM('GET_CONFIG', 'GET_CONFIG_SCRIPT', 'TRACK_VIEW') NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- You are about to alter the column `apiVersion` on the `ApiRequest` table. The data in that column could be lost. The data in that column will be cast from `VarChar(191)` to `Enum(EnumId(3))`.
*/
-- AlterTable
ALTER TABLE `ApiRequest` MODIFY `apiVersion` ENUM('V0', 'V1') NOT NULL DEFAULT 'V0';
7 changes: 7 additions & 0 deletions apps/web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,19 @@ enum ApiRequestType {
TRACK_VIEW
}

enum ApiVersion {
V0
V1
}

model ApiRequest {
id String @id @default(cuid())
createdAt DateTime @default(now())
type ApiRequestType
durationInMs Int
apiVersion ApiVersion @default(V0)
projectId String
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
Expand Down
15 changes: 15 additions & 0 deletions apps/web/src/pages/api/dashboard/[projectId]/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import { RequestCache } from "server/services/RequestCache";
import { transformFlagValue } from "lib/flags";
import { LegacyAbbyDataResponse } from "@tryabby/core";
import { RequestService } from "server/services/RequestService";
import createCache from "server/common/memory-cache";

const incomingQuerySchema = z.object({
projectId: z.string(),
environment: z.string().optional(),
});

const configCache = createCache<string, LegacyAbbyDataResponse>({
name: "legacyConfigCache",
expireAfterMilliseconds: 1000 * 10,
});

export default async function getWeightsHandler(
req: NextApiRequest,
res: NextApiResponse
Expand Down Expand Up @@ -45,6 +51,12 @@ export default async function getWeightsHandler(
return;
}

const cachedData = configCache.get(projectId + environment);
if (cachedData) {
res.json(cachedData);
return;
}

const [tests, flags] = await Promise.all([
prisma.test.findMany({
where: {
Expand Down Expand Up @@ -82,6 +94,8 @@ export default async function getWeightsHandler(

res.json(response);

configCache.set(projectId + environment, response);

if (is80PercentOfLimit) {
await trackPlanOverage(projectId, plan, is80PercentOfLimit);
}
Expand All @@ -92,6 +106,7 @@ export default async function getWeightsHandler(
projectId,
type: "GET_CONFIG",
durationInMs: duration,
apiVersion: "V0",
}).catch((e) => {
console.error("Unable to store request", e);
});
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/pages/api/v1/data/[projectId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function getAbbyResponseWithCache({
environment,
projectId,
}: z.infer<typeof incomingQuerySchema>) {
const cachedConfig = configCache.get(projectId);
const cachedConfig = configCache.get(projectId + environment);

if (cachedConfig) {
return cachedConfig;
Expand Down Expand Up @@ -71,7 +71,7 @@ export async function getAbbyResponseWithCache({
}),
} satisfies AbbyDataResponse;

configCache.set(projectId, response);
configCache.set(projectId + environment, response);
return response;
}

Expand Down Expand Up @@ -100,6 +100,8 @@ export default async function getWeightsHandler(

res.send(response);

const duration = performance.now() - now;

const { events, planLimits, plan, is80PercentOfLimit } =
await EventService.getEventsForCurrentPeriod(projectId);

Expand All @@ -117,12 +119,11 @@ export default async function getWeightsHandler(

await RequestCache.increment(projectId);

const duration = performance.now() - now;

RequestService.storeRequest({
projectId,
type: "GET_CONFIG",
durationInMs: duration,
apiVersion: "V1",
}).catch((e) => {
console.error("Unable to store request", e);
});
Expand Down

1 comment on commit 1ae2a35

@vercel
Copy link

@vercel vercel bot commented on 1ae2a35 Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.