Skip to content

Commit d3a2bd1

Browse files
committed
Don't save repeated queries to the history
1 parent e7ed8ff commit d3a2bd1

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

apps/webapp/app/services/queryService.server.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,39 @@ export async function executeQuery<TOut extends z.ZodSchema>(
166166
// If query succeeded and history options provided, save to history
167167
// Skip history for EXPLAIN queries (admin debugging) and when explicitly skipped (e.g., impersonating)
168168
if (result[0] === null && history && !history.skip && !baseOptions.explain) {
169-
const stats = result[1].stats;
170-
const byteSeconds = parseFloat(stats.byte_seconds) || 0;
171-
const costInCents = byteSeconds * env.CENTS_PER_QUERY_BYTE_SECOND;
172-
173-
await prisma.customerQuery.create({
174-
data: {
175-
query: options.query,
176-
scope: scopeToEnum[scope],
177-
stats: { ...stats },
178-
costInCents,
179-
source: history.source,
169+
// Check if this query is the same as the last one saved (avoid duplicate history entries)
170+
const lastQuery = await prisma.customerQuery.findFirst({
171+
where: {
180172
organizationId,
181-
projectId: scope === "project" || scope === "environment" ? projectId : null,
182-
environmentId: scope === "environment" ? environmentId : null,
173+
source: history.source,
183174
userId: history.userId ?? null,
184175
},
176+
orderBy: { createdAt: "desc" },
177+
select: { query: true, scope: true },
185178
});
179+
180+
const isDuplicate =
181+
lastQuery && lastQuery.query === options.query && lastQuery.scope === scopeToEnum[scope];
182+
183+
if (!isDuplicate) {
184+
const stats = result[1].stats;
185+
const byteSeconds = parseFloat(stats.byte_seconds) || 0;
186+
const costInCents = byteSeconds * env.CENTS_PER_QUERY_BYTE_SECOND;
187+
188+
await prisma.customerQuery.create({
189+
data: {
190+
query: options.query,
191+
scope: scopeToEnum[scope],
192+
stats: { ...stats },
193+
costInCents,
194+
source: history.source,
195+
organizationId,
196+
projectId: scope === "project" || scope === "environment" ? projectId : null,
197+
environmentId: scope === "environment" ? environmentId : null,
198+
userId: history.userId ?? null,
199+
},
200+
});
201+
}
186202
}
187203

188204
return result;

0 commit comments

Comments
 (0)