Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Add import status lib

Edward Cuib9661149db36
diff --git a/server/src/lib/importStatus.ts b/server/src/lib/importStatus.ts
new file mode 100644
index 000000000..ab2fc3fe0
--- /dev/null
+++ b/server/src/lib/importStatus.ts
@@ -0,0 +1,70 @@
+import { db } from "../db/postgres/postgres.js";
+import { eq, and, desc } from 'drizzle-orm';
+import { importStatus } from "../db/postgres/schema.js";
+
+type SelectImportStatus = typeof importStatus.$inferSelect;
+type InsertImportStatus = typeof importStatus.$inferInsert;
+
+export class ImportStatusManager {
+ static async createImportStatus(data: InsertImportStatus): Promise<void> {
+ await db.insert(importStatus).values(data);
+ }
+
+ static async updateStatus(
+ importId: string,
+ status: InsertImportStatus["status"],
+ errorMessage?: string
+ ): Promise<void> {
+ const completedAt = status === "completed" || status === "failed" ? new Date() : null;
+
+ await db.update(importStatus)
+ .set({ status, errorMessage, completedAt })
+ .where(eq(importStatus.importId, importId));
+ }
+
+ static async updateProgress(
+ importId: string,
+ chunksCompleted: number,
+ processedRows: number,
+ totalChunks?: number,
+ totalRows?: number
+ ): Promise<void> {
+ const updateData: Partial<InsertImportStatus> = {
+ chunksCompleted,
+ processedRows,
+ };
+
+ if (totalChunks !== undefined) {
+ updateData.totalChunks = totalChunks;
+ }
+ if (totalRows !== undefined) {
+ updateData.totalRows = totalRows;
+ }
+
+ await db.update(importStatus)
+ .set(updateData)
+ .where(eq(importStatus.importId, importId));
+ }
+
+ static async getImportStatus(importId: string): Promise<SelectImportStatus | null> {
+ const result = await db.query.importStatus.findFirst({
+ where: eq(importStatus.importId, importId),
+ });
+ return result || null;
+ }
+
+ static async getImportsForSite(
+ siteId: number,
+ organization: string,
+ limit = 50
+ ): Promise<SelectImportStatus[]> {
+ return db.query.importStatus.findMany({
+ where: and(
+ eq(importStatus.siteId, siteId),
+ eq(importStatus.organizationId, organization)
+ ),
+ orderBy: [desc(importStatus.startedAt)],
+ limit,
+ });
+ }
+}