Skip to content

Commit 1d52e9f

Browse files
implement column in db to track using own key
1 parent 7658613 commit 1d52e9f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/app/_actions/cache.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { db } from "~/server/db";
44
import { eq, and } from "drizzle-orm";
55
import { diagramCache } from "~/server/db/schema";
6+
import { sql } from "drizzle-orm";
67

78
export async function getCachedDiagram(username: string, repo: string) {
89
try {
@@ -43,24 +44,45 @@ export async function cacheDiagramAndExplanation(
4344
repo: string,
4445
diagram: string,
4546
explanation: string,
47+
usedOwnKey = false,
4648
) {
4749
try {
4850
await db
4951
.insert(diagramCache)
5052
.values({
5153
username,
5254
repo,
53-
explanation,
5455
diagram,
56+
explanation,
57+
usedOwnKey,
5558
})
5659
.onConflictDoUpdate({
5760
target: [diagramCache.username, diagramCache.repo],
5861
set: {
5962
diagram,
63+
explanation,
64+
usedOwnKey,
6065
updatedAt: new Date(),
6166
},
6267
});
6368
} catch (error) {
6469
console.error("Error caching diagram:", error);
6570
}
6671
}
72+
73+
export async function getDiagramStats() {
74+
try {
75+
const stats = await db
76+
.select({
77+
totalDiagrams: sql`COUNT(*)`,
78+
ownKeyUsers: sql`COUNT(CASE WHEN ${diagramCache.usedOwnKey} = true THEN 1 END)`,
79+
freeUsers: sql`COUNT(CASE WHEN ${diagramCache.usedOwnKey} = false THEN 1 END)`,
80+
})
81+
.from(diagramCache);
82+
83+
return stats[0];
84+
} catch (error) {
85+
console.error("Error getting diagram stats:", error);
86+
return null;
87+
}
88+
}

src/hooks/useDiagram.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,14 @@ export function useDiagram(username: string, repo: string) {
220220

221221
useEffect(() => {
222222
if (state.status === "complete" && state.diagram) {
223-
// Cache the completed diagram
223+
// Cache the completed diagram with the usedOwnKey flag
224+
const hasApiKey = !!localStorage.getItem("openrouter_key");
224225
void cacheDiagramAndExplanation(
225226
username,
226227
repo,
227228
state.diagram,
228229
state.explanation ?? "No explanation provided",
230+
hasApiKey,
229231
);
230232
setDiagram(state.diagram);
231233
void getLastGeneratedDate(username, repo).then((date) =>

src/server/db/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
timestamp,
88
varchar,
99
primaryKey,
10+
boolean,
1011
} from "drizzle-orm/pg-core";
1112

1213
/**
@@ -32,6 +33,7 @@ export const diagramCache = createTable(
3233
updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(
3334
() => new Date(),
3435
),
36+
usedOwnKey: boolean("used_own_key").default(false),
3537
},
3638
(table) => ({
3739
pk: primaryKey({ columns: [table.username, table.repo] }),

0 commit comments

Comments
 (0)