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 { join } from "node:path";
|
||||
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 { trackEvent } from "../lib/analytics.js";
|
||||
import {
|
||||
acquireInstallLock,
|
||||
getAiDir,
|
||||
@@ -133,6 +134,9 @@ export async function registerFeatureRoutes(app: FastifyInstance): Promise<void>
|
||||
const manifestPath = getManifestPath();
|
||||
const modelsDir = getModelsDir();
|
||||
|
||||
const installStartTime = Date.now();
|
||||
const reqRef = request;
|
||||
|
||||
const child = spawn(pythonPath, [scriptPath, bundleId, manifestPath, modelsDir], {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env: {
|
||||
@@ -192,6 +196,11 @@ export async function registerFeatureRoutes(app: FastifyInstance): Promise<void>
|
||||
shutdownDispatcher();
|
||||
setInstallProgress(null, null, null);
|
||||
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 {
|
||||
const errorDetail =
|
||||
lastStderrLines.filter((l) => !l.startsWith("{")).join("\n") || stdoutBuffer.trim();
|
||||
@@ -264,6 +273,12 @@ export async function registerFeatureRoutes(app: FastifyInstance): Promise<void>
|
||||
markUninstalled(bundleId);
|
||||
shutdownDispatcher();
|
||||
|
||||
trackEvent(request, ANALYTICS_EVENTS.AI_BUNDLE_ACTION, {
|
||||
bundle_id: bundleId,
|
||||
action: "uninstalled",
|
||||
duration_ms: 0,
|
||||
});
|
||||
|
||||
return reply.send({ ok: true });
|
||||
},
|
||||
);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
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 { eq } from "drizzle-orm";
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
@@ -17,6 +17,7 @@ import PQueue from "p-queue";
|
||||
import { z } from "zod";
|
||||
import { env } from "../config.js";
|
||||
import { db, schema } from "../db/index.js";
|
||||
import { trackEvent } from "../lib/analytics.js";
|
||||
import { autoOrient } from "../lib/auto-orient.js";
|
||||
import { resolveConcurrency } from "../lib/env.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
|
||||
const startTime = Date.now();
|
||||
let currentBuffer = fileBuffer;
|
||||
let currentFilename = filename;
|
||||
const stepResults: Array<{ step: number; toolId: string; size: number }> = [];
|
||||
@@ -244,6 +246,13 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
||||
}
|
||||
} catch (err) {
|
||||
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({
|
||||
error: message,
|
||||
completedSteps: stepResults,
|
||||
@@ -260,6 +269,14 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
||||
const inputPath = join(workspacePath, "input", filename);
|
||||
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({
|
||||
jobId,
|
||||
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(currentFilename)}`,
|
||||
@@ -508,6 +525,7 @@ export async function registerPipelineRoutes(app: FastifyInstance): Promise<void
|
||||
}
|
||||
|
||||
// ── Progress tracking ────────────────────────────────────────────
|
||||
const batchStartTime = Date.now();
|
||||
const jobId = clientJobId || randomUUID();
|
||||
|
||||
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 (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({
|
||||
error: "All files failed processing",
|
||||
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 ──────────────────────────────────────────
|
||||
reply.hijack();
|
||||
reply.raw.writeHead(200, {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
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 type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import sharp from "sharp";
|
||||
import type { z } from "zod";
|
||||
import { db, schema } from "../db/index.js";
|
||||
import { trackEvent } from "../lib/analytics.js";
|
||||
import { autoOrient } from "../lib/auto-orient.js";
|
||||
import { formatZodErrors } from "../lib/errors.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)
|
||||
const startTime = Date.now();
|
||||
try {
|
||||
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({
|
||||
jobId,
|
||||
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
|
||||
const message = err instanceof Error ? err.message : "Image 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({
|
||||
error: "Processing failed",
|
||||
details: message,
|
||||
|
||||
Reference in New Issue
Block a user