Skip to content

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

Get imports

Edward Cuic4540b5d6a25
diff --git a/client/src/api/sites.ts b/client/src/api/sites.ts
index f20b61a61..3183aebba 100644
--- a/client/src/api/sites.ts
+++ b/client/src/api/sites.ts
@@ -1,15 +1,18 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
+import { authedFetch } from "@/api/utils";
+import { useStore } from "@/lib/store";
+import { APIResponse } from "@/api/types";
-type SiteImport = {
+interface SiteImport {
importId: string;
- source: string;
- status: string;
- importedEvents: number | null;
+ source: "umami";
+ status: "pending" | "processing" | "completed" | "failed";
+ importedEvents: number;
errorMessage: string | null;
startedAt: string;
completedAt: string | null;
fileName: string;
-};
+}
interface ImportSiteDataParams {
file: File;
@@ -18,15 +21,6 @@ interface ImportSiteDataParams {
endDate?: string;
}
-async function getSiteImports(siteId: number): Promise<{ imports: SiteImport[] }> {
- const res = await fetch(`/api/sites/${siteId}/imports`);
- if (!res.ok) {
- const errorData = await res.json().catch(() => ({ message: 'Failed to fetch site imports' }));
- throw new Error(errorData.message || 'Failed to fetch site imports');
- }
- return res.json();
-}
-
async function importSiteData(siteId: number, params: ImportSiteDataParams) {
const formData = new FormData();
formData.append("file", params.file);
@@ -46,33 +40,26 @@ async function importSiteData(siteId: number, params: ImportSiteDataParams) {
});
if (!res.ok) {
- const errorData = await res.json().catch(() => ({ message: 'Failed to import site data' }));
- throw new Error(errorData.message || errorData.error || 'Failed to import site data');
+ const errorData = await res.json().catch(() => ({ message: "Failed to import site data" }));
+ throw new Error(errorData.message || errorData.error || "Failed to import site data");
}
return res.json();
}
-export function useGetSiteImports(siteId: number) {
+export function useGetSiteImports() {
+ const { site } = useStore();
+
return useQuery({
- queryKey: ["siteImports", siteId],
- queryFn: () => getSiteImports(siteId),
+ queryKey: ["get-site-imports", site],
+ queryFn: async () => await authedFetch<APIResponse<SiteImport[]>>(`/get-site-imports/${site}`),
refetchInterval: (data) => {
- // Only refetch automatically if there are active imports
- const hasActiveImports = data.state.data?.imports?.some(imp =>
- imp.status === 'processing' || imp.status === 'pending'
+ const hasActiveImports = data.state.data?.data.some(imp =>
+ imp.status === "processing" || imp.status === "pending"
);
- return hasActiveImports ? 5000 : false; // 5 seconds if active, otherwise no auto-refetch
- },
- retry: (failureCount, error) => {
- // Don't retry if it's a client error (400-499)
- if (error.message.includes('403') || error.message.includes('404')) {
- return false;
- }
- return failureCount < 3;
+ return hasActiveImports ? 5000 : false;
},
- staleTime: 30000, // Consider data stale after 30 seconds
- gcTime: 5 * 60 * 1000, // Keep in cache for 5 minutes
+ staleTime: 30000,
});
}
@@ -89,8 +76,8 @@ export function useImportSiteData(siteId: number) {
});
},
onError: (error) => {
- console.error('Import failed:', error);
+ console.error("Import failed:", error);
},
- retry: false, // Don't retry file uploads automatically
+ retry: false, // Don"t retry file uploads automatically
});
}
diff --git a/server/src/api/sites/listSiteImports.ts b/server/src/api/sites/getSiteImports.ts
similarity index 64%
rename from server/src/api/sites/listSiteImports.ts
rename to server/src/api/sites/getSiteImports.ts
index 97b8fbb30..d5ef4ba47 100644
--- a/server/src/api/sites/listSiteImports.ts
+++ b/server/src/api/sites/getSiteImports.ts
@@ -3,30 +3,27 @@ import { getUserHasAccessToSite } from "../../lib/auth-utils.js";
import { ImportStatusManager } from "../../services/import/importStatusManager.js";
import { z } from "zod";
-const listSiteImportsRequestSchema = z.object({
+const getSiteImportsRequestSchema = z.object({
params: z.object({
site: z.string().min(1),
}),
}).strict();
-type ListSiteImportsRequest = {
- Params: z.infer<typeof listSiteImportsRequestSchema.shape.params>;
+type GetSiteImportsRequest = {
+ Params: z.infer<typeof getSiteImportsRequestSchema.shape.params>;
};
-export async function listSiteImports(
- request: FastifyRequest<ListSiteImportsRequest>,
+export async function getSiteImports(
+ request: FastifyRequest<GetSiteImportsRequest>,
reply: FastifyReply,
) {
try {
- const parsed = listSiteImportsRequestSchema.safeParse({
+ const parsed = getSiteImportsRequestSchema.safeParse({
params: request.params,
});
if (!parsed.success) {
- return reply.status(400).send({
- error: "Validation failed",
- details: parsed.error.flatten(),
- });
+ return reply.status(400).send({ error: "Validation failed" });
}
const { site } = parsed.data.params;
@@ -39,19 +36,19 @@ export async function listSiteImports(
const imports = await ImportStatusManager.getImportsForSite(Number(site));
return reply.send({
- imports: imports.map(imp => ({
+ data: imports.map(imp => ({
importId: imp.importId,
source: imp.source,
status: imp.status,
importedEvents: imp.importedEvents,
errorMessage: imp.errorMessage,
- startedAt: imp.startedAt, // maybe return import time, or no time data at all
+ startedAt: imp.startedAt,
completedAt: imp.completedAt,
fileName: imp.fileName,
}))
});
} catch (error) {
- console.error("Error listing imports:", error);
+ console.error("Error fetching imports:", error);
return reply.status(500).send({ error: "Internal server error" });
}
}
diff --git a/server/src/index.ts b/server/src/index.ts
index c62b38d02..3539a6cb2 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -78,7 +78,7 @@ import { importSiteData } from "./api/sites/importSiteData.js";
import boss from "./db/postgres/boss.js";
import { registerCsvParseWorker } from "./services/import/workers/csvParseWorker.js";
import { registerDataInsertWorker } from "./services/import/workers/dataInsertWorker.js";
-import { listSiteImports } from "./api/sites/listSiteImports.js";
+import { getSiteImports } from "./api/sites/getSiteImports.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@@ -290,7 +290,7 @@ server.get("/api/session-replay/:sessionId/:site", getSessionReplayEvents);
// Imports
server.post("/api/import-site-data/:site", importSiteData);
-server.get("/api/list-site-imports/:site", listSiteImports);
+server.get("/api/get-site-imports/:site", getSiteImports);
// Administrative
server.get("/api/config", getConfig);