mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: instrument tool_used, pipeline_executed, ai_bundle_action events
This commit is contained in:
@@ -12,8 +12,9 @@ import crypto from "node:crypto";
|
|||||||
import { existsSync, readdirSync, readFileSync, statSync, unlinkSync } from "node:fs";
|
import { existsSync, readdirSync, readFileSync, statSync, unlinkSync } from "node:fs";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { shutdownDispatcher } from "@ashim/ai";
|
import { shutdownDispatcher } from "@ashim/ai";
|
||||||
import { FEATURE_BUNDLES } from "@ashim/shared";
|
import { ANALYTICS_EVENTS, FEATURE_BUNDLES } from "@ashim/shared";
|
||||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||||
|
import { trackEvent } from "../lib/analytics.js";
|
||||||
import {
|
import {
|
||||||
acquireInstallLock,
|
acquireInstallLock,
|
||||||
getAiDir,
|
getAiDir,
|
||||||
@@ -133,6 +134,9 @@ export async function registerFeatureRoutes(app: FastifyInstance): Promise<void>
|
|||||||
const manifestPath = getManifestPath();
|
const manifestPath = getManifestPath();
|
||||||
const modelsDir = getModelsDir();
|
const modelsDir = getModelsDir();
|
||||||
|
|
||||||
|
const installStartTime = Date.now();
|
||||||
|
const reqRef = request;
|
||||||
|
|
||||||
const child = spawn(pythonPath, [scriptPath, bundleId, manifestPath, modelsDir], {
|
const child = spawn(pythonPath, [scriptPath, bundleId, manifestPath, modelsDir], {
|
||||||
stdio: ["ignore", "pipe", "pipe"],
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
env: {
|
env: {
|
||||||
@@ -192,6 +196,11 @@ export async function registerFeatureRoutes(app: FastifyInstance): Promise<void>
|
|||||||
shutdownDispatcher();
|
shutdownDispatcher();
|
||||||
setInstallProgress(null, null, null);
|
setInstallProgress(null, null, null);
|
||||||
updateSingleFileProgress({ jobId, phase: "complete", percent: 100, stage: "Complete" });
|
updateSingleFileProgress({ jobId, phase: "complete", percent: 100, stage: "Complete" });
|
||||||
|
trackEvent(reqRef, ANALYTICS_EVENTS.AI_BUNDLE_ACTION, {
|
||||||
|
bundle_id: bundleId,
|
||||||
|
action: "installed",
|
||||||
|
duration_ms: Date.now() - installStartTime,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
const errorDetail =
|
const errorDetail =
|
||||||
lastStderrLines.filter((l) => !l.startsWith("{")).join("\n") || stdoutBuffer.trim();
|
lastStderrLines.filter((l) => !l.startsWith("{")).join("\n") || stdoutBuffer.trim();
|
||||||
@@ -264,6 +273,12 @@ export async function registerFeatureRoutes(app: FastifyInstance): Promise<void>
|
|||||||
markUninstalled(bundleId);
|
markUninstalled(bundleId);
|
||||||
shutdownDispatcher();
|
shutdownDispatcher();
|
||||||
|
|
||||||
|
trackEvent(request, ANALYTICS_EVENTS.AI_BUNDLE_ACTION, {
|
||||||
|
bundle_id: bundleId,
|
||||||
|
action: "uninstalled",
|
||||||
|
duration_ms: 0,
|
||||||
|
});
|
||||||
|
|
||||||
return reply.send({ ok: true });
|
return reply.send({ ok: true });
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { writeFile } from "node:fs/promises";
|
import { writeFile } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { getBundleForTool, TOOL_BUNDLE_MAP } from "@ashim/shared";
|
import { ANALYTICS_EVENTS, getBundleForTool, TOOL_BUNDLE_MAP } from "@ashim/shared";
|
||||||
import archiver from "archiver";
|
import archiver from "archiver";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||||
@@ -17,6 +17,7 @@ import PQueue from "p-queue";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { env } from "../config.js";
|
import { env } from "../config.js";
|
||||||
import { db, schema } from "../db/index.js";
|
import { db, schema } from "../db/index.js";
|
||||||
|
import { trackEvent } from "../lib/analytics.js";
|
||||||
import { autoOrient } from "../lib/auto-orient.js";
|
import { autoOrient } from "../lib/auto-orient.js";
|
||||||
import { resolveConcurrency } from "../lib/env.js";
|
import { resolveConcurrency } from "../lib/env.js";
|
||||||
import { formatZodErrors } from "../lib/errors.js";
|
import { formatZodErrors } from "../lib/errors.js";
|
||||||
@@ -204,6 +205,7 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Execute the pipeline: pass the buffer through each step sequentially
|
// Execute the pipeline: pass the buffer through each step sequentially
|
||||||
|
const startTime = Date.now();
|
||||||
let currentBuffer = fileBuffer;
|
let currentBuffer = fileBuffer;
|
||||||
let currentFilename = filename;
|
let currentFilename = filename;
|
||||||
const stepResults: Array<{ step: number; toolId: string; size: number }> = [];
|
const stepResults: Array<{ step: number; toolId: string; size: number }> = [];
|
||||||
@@ -244,6 +246,13 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
|||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : "Pipeline processing failed";
|
const message = err instanceof Error ? err.message : "Pipeline processing failed";
|
||||||
|
trackEvent(request, ANALYTICS_EVENTS.PIPELINE_EXECUTED, {
|
||||||
|
step_count: pipeline.steps.length,
|
||||||
|
tool_ids: pipeline.steps.map((s: { toolId: string }) => s.toolId),
|
||||||
|
is_batch: false,
|
||||||
|
duration_ms: Date.now() - startTime,
|
||||||
|
status: "failed",
|
||||||
|
});
|
||||||
return reply.status(422).send({
|
return reply.status(422).send({
|
||||||
error: message,
|
error: message,
|
||||||
completedSteps: stepResults,
|
completedSteps: stepResults,
|
||||||
@@ -260,6 +269,14 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
|||||||
const inputPath = join(workspacePath, "input", filename);
|
const inputPath = join(workspacePath, "input", filename);
|
||||||
await writeFile(inputPath, fileBuffer);
|
await writeFile(inputPath, fileBuffer);
|
||||||
|
|
||||||
|
trackEvent(request, ANALYTICS_EVENTS.PIPELINE_EXECUTED, {
|
||||||
|
step_count: pipeline.steps.length,
|
||||||
|
tool_ids: pipeline.steps.map((s: { toolId: string }) => s.toolId),
|
||||||
|
is_batch: false,
|
||||||
|
duration_ms: Date.now() - startTime,
|
||||||
|
status: "completed",
|
||||||
|
});
|
||||||
|
|
||||||
return reply.send({
|
return reply.send({
|
||||||
jobId,
|
jobId,
|
||||||
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(currentFilename)}`,
|
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(currentFilename)}`,
|
||||||
@@ -508,6 +525,7 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── Progress tracking ────────────────────────────────────────────
|
// ── Progress tracking ────────────────────────────────────────────
|
||||||
|
const batchStartTime = Date.now();
|
||||||
const jobId = clientJobId || randomUUID();
|
const jobId = clientJobId || randomUUID();
|
||||||
|
|
||||||
const progress: JobProgress = {
|
const progress: JobProgress = {
|
||||||
@@ -651,12 +669,29 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
|||||||
|
|
||||||
// If every file failed, return an error instead of an empty ZIP
|
// If every file failed, return an error instead of an empty ZIP
|
||||||
if (progress.status === "failed") {
|
if (progress.status === "failed") {
|
||||||
|
trackEvent(request, ANALYTICS_EVENTS.PIPELINE_EXECUTED, {
|
||||||
|
step_count: pipeline.steps.length,
|
||||||
|
tool_ids: pipeline.steps.map((s: { toolId: string }) => s.toolId),
|
||||||
|
is_batch: true,
|
||||||
|
file_count: files.length,
|
||||||
|
duration_ms: Date.now() - batchStartTime,
|
||||||
|
status: "failed",
|
||||||
|
});
|
||||||
return reply.status(422).send({
|
return reply.status(422).send({
|
||||||
error: "All files failed processing",
|
error: "All files failed processing",
|
||||||
errors: progress.errors,
|
errors: progress.errors,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trackEvent(request, ANALYTICS_EVENTS.PIPELINE_EXECUTED, {
|
||||||
|
step_count: pipeline.steps.length,
|
||||||
|
tool_ids: pipeline.steps.map((s: { toolId: string }) => s.toolId),
|
||||||
|
is_batch: true,
|
||||||
|
file_count: files.length,
|
||||||
|
duration_ms: Date.now() - batchStartTime,
|
||||||
|
status: "completed",
|
||||||
|
});
|
||||||
|
|
||||||
// ── Stream ZIP response ──────────────────────────────────────────
|
// ── Stream ZIP response ──────────────────────────────────────────
|
||||||
reply.hijack();
|
reply.hijack();
|
||||||
reply.raw.writeHead(200, {
|
reply.raw.writeHead(200, {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { writeFile } from "node:fs/promises";
|
import { writeFile } from "node:fs/promises";
|
||||||
import { extname, join } from "node:path";
|
import { extname, join } from "node:path";
|
||||||
import { getBundleForTool, TOOL_BUNDLE_MAP } from "@ashim/shared";
|
import { ANALYTICS_EVENTS, getBundleForTool, TOOL_BUNDLE_MAP, TOOLS } from "@ashim/shared";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
import type { z } from "zod";
|
import type { z } from "zod";
|
||||||
import { db, schema } from "../db/index.js";
|
import { db, schema } from "../db/index.js";
|
||||||
|
import { trackEvent } from "../lib/analytics.js";
|
||||||
import { autoOrient } from "../lib/auto-orient.js";
|
import { autoOrient } from "../lib/auto-orient.js";
|
||||||
import { formatZodErrors } from "../lib/errors.js";
|
import { formatZodErrors } from "../lib/errors.js";
|
||||||
import { isToolInstalled } from "../lib/feature-status.js";
|
import { isToolInstalled } from "../lib/feature-status.js";
|
||||||
@@ -242,6 +243,7 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process the image (worker thread or main thread)
|
// Process the image (worker thread or main thread)
|
||||||
|
const startTime = Date.now();
|
||||||
try {
|
try {
|
||||||
let result: { buffer: Buffer; filename: string; contentType: string };
|
let result: { buffer: Buffer; filename: string; contentType: string };
|
||||||
|
|
||||||
@@ -381,6 +383,14 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trackEvent(request, ANALYTICS_EVENTS.TOOL_USED, {
|
||||||
|
tool_id: config.toolId,
|
||||||
|
status: "completed",
|
||||||
|
duration_ms: Date.now() - startTime,
|
||||||
|
category: TOOLS.find((t) => t.id === config.toolId)?.category ?? "unknown",
|
||||||
|
is_ai_tool: getBundleForTool(config.toolId) !== null,
|
||||||
|
});
|
||||||
|
|
||||||
return reply.send({
|
return reply.send({
|
||||||
jobId,
|
jobId,
|
||||||
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(result.filename)}`,
|
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(result.filename)}`,
|
||||||
@@ -393,6 +403,13 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
|||||||
// Catch Sharp / processing errors and return a clean API error
|
// Catch Sharp / processing errors and return a clean API error
|
||||||
const message = err instanceof Error ? err.message : "Image processing failed";
|
const message = err instanceof Error ? err.message : "Image processing failed";
|
||||||
request.log.error({ err, toolId: config.toolId }, "Tool processing failed");
|
request.log.error({ err, toolId: config.toolId }, "Tool processing failed");
|
||||||
|
trackEvent(request, ANALYTICS_EVENTS.TOOL_USED, {
|
||||||
|
tool_id: config.toolId,
|
||||||
|
status: "failed",
|
||||||
|
duration_ms: Date.now() - startTime,
|
||||||
|
category: TOOLS.find((t) => t.id === config.toolId)?.category ?? "unknown",
|
||||||
|
is_ai_tool: getBundleForTool(config.toolId) !== null,
|
||||||
|
});
|
||||||
return reply.status(422).send({
|
return reply.status(422).send({
|
||||||
error: "Processing failed",
|
error: "Processing failed",
|
||||||
details: message,
|
details: message,
|
||||||
|
|||||||
Reference in New Issue
Block a user