Skip to content

Commit ddf0d8d

Browse files
committed
feat: optimized funnels, goals, autocomplete
1 parent 71822f9 commit ddf0d8d

File tree

3 files changed

+535
-410
lines changed

3 files changed

+535
-410
lines changed

packages/rpc/src/routers/autocomplete.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { chQuery } from '@databuddy/db';
2+
import { createDrizzleCache, redis } from '@databuddy/redis';
23
import { TRPCError } from '@trpc/server';
34
import { z } from 'zod/v4';
45
import { logger } from '../lib/logger';
56
import { createTRPCRouter, protectedProcedure } from '../trpc';
67
import { authorizeWebsiteAccess } from '../utils/auth';
78

9+
const drizzleCache = createDrizzleCache({ redis, namespace: 'autocomplete' });
10+
11+
const CACHE_TTL = 1800;
12+
813
const analyticsDateRangeSchema = z.object({
914
websiteId: z.string(),
1015
startDate: z.string().optional(),
@@ -136,34 +141,40 @@ const categorizeAutocompleteResults = (
136141
export const autocompleteRouter = createTRPCRouter({
137142
get: protectedProcedure
138143
.input(analyticsDateRangeSchema)
139-
.query(async ({ ctx, input }) => {
140-
const website = await authorizeWebsiteAccess(
141-
ctx,
142-
input.websiteId,
143-
'read'
144-
);
144+
.query(({ ctx, input }) => {
145145
const { startDate, endDate } =
146146
input.startDate && input.endDate
147147
? { startDate: input.startDate, endDate: input.endDate }
148148
: getDefaultDateRange();
149-
const params = { websiteId: website.id, startDate, endDate };
150149

151-
try {
152-
const results = await chQuery<{
153-
category: string;
154-
value: string;
155-
}>(getAutocompleteQuery(), params);
150+
const cacheKey = `autocomplete:${input.websiteId}:${startDate}:${endDate}`;
151+
152+
return drizzleCache.withCache({
153+
key: cacheKey,
154+
ttl: CACHE_TTL,
155+
tables: ['websites'],
156+
queryFn: async () => {
157+
await authorizeWebsiteAccess(ctx, input.websiteId, 'read');
158+
const params = { websiteId: input.websiteId, startDate, endDate };
159+
160+
try {
161+
const results = await chQuery<{
162+
category: string;
163+
value: string;
164+
}>(getAutocompleteQuery(), params);
156165

157-
return categorizeAutocompleteResults(results);
158-
} catch (error) {
159-
logger.error('Failed to fetch autocomplete data', {
160-
error: error instanceof Error ? error.message : String(error),
161-
websiteId: website.id,
162-
});
163-
throw new TRPCError({
164-
code: 'INTERNAL_SERVER_ERROR',
165-
message: 'Failed to fetch autocomplete data',
166-
});
167-
}
166+
return categorizeAutocompleteResults(results);
167+
} catch (error) {
168+
logger.error('Failed to fetch autocomplete data', {
169+
error: error instanceof Error ? error.message : String(error),
170+
websiteId: input.websiteId,
171+
});
172+
throw new TRPCError({
173+
code: 'INTERNAL_SERVER_ERROR',
174+
message: 'Failed to fetch autocomplete data',
175+
});
176+
}
177+
},
178+
});
168179
}),
169180
});

0 commit comments

Comments
 (0)