Skip to content

Commit

Permalink
Delete Trackers data when scrubbing workspace data
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Dec 19, 2024
1 parent 55829d1 commit fa03d4b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 15 deletions.
75 changes: 60 additions & 15 deletions front/lib/resources/tracker_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,26 @@ export class TrackerConfigurationResource extends ResourceWithSpace<TrackerConfi

static async listBySpace(
auth: Authenticator,
space: SpaceResource
space: SpaceResource,
{ includeDeleted }: { includeDeleted?: boolean } = {}
): Promise<TrackerConfigurationResource[]> {
return this.baseFetch(auth, {
where: {
vaultId: space.id,
},
includeDeleted,
});
}

static async listByWorkspace(
auth: Authenticator,
{ includeDeleted }: { includeDeleted?: boolean } = {}
): Promise<TrackerConfigurationResource[]> {
return this.baseFetch(auth, {
where: {
workspaceId: auth.getNonNullableWorkspace().id,
},
includeDeleted,
});
}

Expand Down Expand Up @@ -438,6 +452,7 @@ export class TrackerConfigurationResource extends ResourceWithSpace<TrackerConfi
[Op.not]: null,
},
lastNotifiedAt: { [Op.or]: [{ [Op.lt]: new Date(lookBackMs) }, null] },
deletedAt: null,
},
include: [
{
Expand Down Expand Up @@ -537,7 +552,6 @@ export class TrackerConfigurationResource extends ResourceWithSpace<TrackerConfi

dsConfigs = dsConfigs.filter((c) => validDsViewIds.has(c.dataSourceViewId));

// Fetch the associated tracker configurations
// Fetch the associated tracker configurations
const trackerIds = _.uniq(
dsConfigs.map((config) => config.trackerConfigurationId)
Expand All @@ -556,19 +570,30 @@ export class TrackerConfigurationResource extends ResourceWithSpace<TrackerConfi
protected async hardDelete(
auth: Authenticator
): Promise<Result<number, Error>> {
const workspaceId = auth.getNonNullableWorkspace().id;
const deletedCount = await frontSequelize.transaction(async (t) => {
// TODO Daph: Delete all related resources.
// await TrackerDataSourceConfigurationResource.deleteAllByTrackerId(this.id, t);
// await TrackerGenerationResource.deleteAllByTrackerId(this.id, t);

await TrackerGenerationModel.destroy({
where: {
trackerConfigurationId: this.id,
workspaceId,
},
transaction: t,
hardDelete: true,
});
await TrackerDataSourceConfigurationModel.destroy({
where: {
trackerConfigurationId: this.id,
workspaceId,
},
transaction: t,
hardDelete: true,
});
return TrackerConfigurationModel.destroy({
where: {
workspaceId: auth.getNonNullableWorkspace().id,
id: this.id,
workspaceId,
},
transaction: t,
// Use 'hardDelete: true' to ensure the record is permanently deleted from the database,
// bypassing the soft deletion in place.
hardDelete: true,
});
});
Expand All @@ -579,12 +604,32 @@ export class TrackerConfigurationResource extends ResourceWithSpace<TrackerConfi
protected async softDelete(
auth: Authenticator
): Promise<Result<number, Error>> {
const deletedCount = await TrackerConfigurationModel.destroy({
where: {
workspaceId: auth.getNonNullableWorkspace().id,
id: this.id,
},
hardDelete: false,
const workspaceId = auth.getNonNullableWorkspace().id;
const deletedCount = await frontSequelize.transaction(async (t) => {
await TrackerGenerationModel.destroy({
where: {
trackerConfigurationId: this.id,
workspaceId,
},
transaction: t,
hardDelete: false,
});
await TrackerDataSourceConfigurationModel.destroy({
where: {
trackerConfigurationId: this.id,
workspaceId,
},
transaction: t,
hardDelete: false,
});
return TrackerConfigurationModel.destroy({
where: {
id: this.id,
workspaceId,
},
transaction: t,
hardDelete: false,
});
});

return new Ok(deletedCount);
Expand Down
16 changes: 16 additions & 0 deletions front/poke/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
LabsTranscriptsHistoryModel,
} from "@app/lib/resources/storage/models/labs_transcripts";
import { UserMetadataModel } from "@app/lib/resources/storage/models/user";
import { TrackerConfigurationResource } from "@app/lib/resources/tracker_resource";
import { UserResource } from "@app/lib/resources/user_resource";
import { renderLightWorkspaceType } from "@app/lib/workspace";
import logger from "@app/logger/logger";
Expand Down Expand Up @@ -507,6 +508,21 @@ export async function deleteRunOnDustAppsActivity({
}
}

export const deleteTrackersActivity = async ({
workspaceId,
}: {
workspaceId: string;
}) => {
const auth = await Authenticator.internalAdminForWorkspace(workspaceId);
const trackers = await TrackerConfigurationResource.listByWorkspace(auth, {
includeDeleted: true,
});

for (const tracker of trackers) {
await tracker.delete(auth, { hardDelete: true });
}
};

export async function deleteMembersActivity({
workspaceId,
}: {
Expand Down
2 changes: 2 additions & 0 deletions front/poke/temporal/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const activityProxies = proxyActivities<typeof activities>({
const {
deleteAgentsActivity,
deleteAppsActivity,
deleteTrackersActivity,
deleteConversationsActivity,
deleteMembersActivity,
deleteRunOnDustAppsActivity,
Expand Down Expand Up @@ -54,6 +55,7 @@ export async function deleteWorkspaceWorkflow({
await deleteAgentsActivity({ workspaceId });
await deleteAppsActivity({ workspaceId });
await deleteRunOnDustAppsActivity({ workspaceId });
await deleteTrackersActivity({ workspaceId });
await deleteMembersActivity({ workspaceId });
await deleteSpacesActivity({ workspaceId });
await deleteTranscriptsActivity({ workspaceId });
Expand Down
16 changes: 16 additions & 0 deletions front/temporal/scrub_workspace/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { subscriptionForWorkspaces } from "@app/lib/plans/subscription";
import { DataSourceResource } from "@app/lib/resources/data_source_resource";
import { MembershipResource } from "@app/lib/resources/membership_resource";
import { SpaceResource } from "@app/lib/resources/space_resource";
import { TrackerConfigurationResource } from "@app/lib/resources/tracker_resource";
import { UserResource } from "@app/lib/resources/user_resource";
import { CustomerioServerSideTracking } from "@app/lib/tracking/customerio/server";
import { renderLightWorkspaceType } from "@app/lib/workspace";
Expand Down Expand Up @@ -89,6 +90,7 @@ export async function scrubWorkspaceData({
const auth = await Authenticator.internalAdminForWorkspace(workspaceId);
await deleteAllConversations(auth);
await archiveAssistants(auth);
await deleteTrackers(auth);
await deleteDatasources(auth);
await deleteSpaces(auth);
await cleanupCustomerio(auth);
Expand Down Expand Up @@ -151,6 +153,20 @@ async function archiveAssistants(auth: Authenticator) {
}
}

async function deleteTrackers(auth: Authenticator) {
const workspace = auth.workspace();
if (!workspace) {
throw new Error("No workspace found");
}

const trackers = await TrackerConfigurationResource.listByWorkspace(auth, {
includeDeleted: true,
});
for (const tracker of trackers) {
await tracker.delete(auth, { hardDelete: true });
}
}

async function deleteDatasources(auth: Authenticator) {
const globalAndSystemSpaces = await SpaceResource.listWorkspaceDefaultSpaces(
auth,
Expand Down

0 comments on commit fa03d4b

Please sign in to comment.