Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/host-service/src/trpc/router/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ export const projectRouter = router({
try {
// Per-row so each deletion broadcasts.
for (const ws of localWorkspaces) {
deleteLocalWorkspace({ db: ctx.db, eventBus: ctx.eventBus }, ws.id);
deleteLocalWorkspace(ctx, ws.id);
}
ctx.db.delete(projects).where(eq(projects.id, input.projectId)).run();
emitProjectChanged(ctx.eventBus, "deleted", input.projectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
export type EnsureMainWorkspaceContext = Pick<
HostServiceContext,
"db" | "git" | "eventBus"
>;
> &
Partial<
Pick<HostServiceContext, "api" | "organizationId" | "clientMachineId">
>;

async function getCurrentBranchName(
git: Awaited<ReturnType<EnsureMainWorkspaceContext["git"]>>,
Expand Down Expand Up @@ -73,7 +76,13 @@ export async function ensureMainWorkspaceStrict(
});
}

const store = { db: ctx.db, eventBus: ctx.eventBus };
const store = {
db: ctx.db,
eventBus: ctx.eventBus,
api: ctx.api,
organizationId: ctx.organizationId,
clientMachineId: ctx.clientMachineId,
};

const existing = ctx.db.query.workspaces
.findFirst({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ async function runDestroy(
// The local row is the commit point and the only record. The cloud
// delete is best-effort legacy cleanup for rows mirrored before
// workspaces went fully local.
deleteLocalWorkspace(
{ db: ctx.db, eventBus: ctx.eventBus },
input.workspaceId,
);
deleteLocalWorkspace(ctx, input.workspaceId);
let cloudDeleted = false;
try {
await ctx.api.v2Workspace.delete.mutate({ id: input.workspaceId });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ export async function adoptExistingWorktree(
idempotencyId,
taskId,
} = args;
const store: WorkspaceStoreContext = { db: ctx.db, eventBus: ctx.eventBus };
const store: WorkspaceStoreContext = {
db: ctx.db,
eventBus: ctx.eventBus,
api: ctx.api,
organizationId: ctx.organizationId,
clientMachineId: ctx.clientMachineId,
};

if (existingWorkspaceId) {
await recordBaseBranch(git, branch, baseBranch);
Expand Down
49 changes: 49 additions & 0 deletions packages/host-service/src/workspaces/local-workspace-store.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,63 @@
import { randomUUID } from "node:crypto";
import hostServicePackageJson from "@superset/host-service/package.json" with {
type: "json",
};
import { getHostId } from "@superset/shared/host-info";
import { eq } from "drizzle-orm";
import type { HostDb } from "../db";
import { workspaces } from "../db/schema";
import type { EventBus } from "../events";
import type { WorkspaceSnapshot } from "../events/types";
import type { ApiClient } from "../types";

export type HostWorkspaceRow = typeof workspaces.$inferSelect;

/**
* `api`/`organizationId`/`clientMachineId` mirror `HostServiceContext` field
* names so a full request context satisfies this interface as-is. When `api`
* is absent the store still works but skips telemetry.
*/
export interface WorkspaceStoreContext {
db: HostDb;
eventBus: EventBus;
api?: ApiClient;
organizationId?: string;
clientMachineId?: string;
}

/**
* Workspaces have no cloud mirror since local-first (#5731), so the cloud
* capture in `v2Workspace.create`/`delete` never fires for local rows —
* the host relays the event through `analytics.captureEvent` instead.
*/
function trackWorkspaceEvent(
ctx: WorkspaceStoreContext,
event: "workspace_created" | "workspace_deleted",
row: HostWorkspaceRow,
): void {
if (!ctx.api) return;
const clientMachineId = ctx.clientMachineId ?? getHostId();
try {
void ctx.api.analytics.captureEvent
.mutate({
source: "host_service",
event,
properties: {
workspace_id: row.id,
project_id: row.projectId,
organization_id: ctx.organizationId ?? null,
host_id: getHostId(),
branch: row.branch,
type: row.type,
host_kind: clientMachineId === getHostId() ? "local" : "remote",
client_machine_id: clientMachineId,
host_service_version: hostServicePackageJson.version,
},
})
.catch(() => {});
} catch {
// Telemetry must never fail the workspace operation.
}
}

/**
Expand Down Expand Up @@ -115,6 +162,7 @@ export function insertLocalWorkspace(
const row = getLocalWorkspace(ctx.db, id);
if (!row) throw new Error(`Workspace insert readback failed: ${id}`);
emitWorkspaceChanged(ctx.eventBus, "created", row);
trackWorkspaceEvent(ctx, "workspace_created", row);
return row;
}

Expand Down Expand Up @@ -161,6 +209,7 @@ export function deleteLocalWorkspace(
workspace: null,
occurredAt: Date.now(),
});
trackWorkspaceEvent(ctx, "workspace_deleted", existing);
}
}

Expand Down
Loading