From 325d73808508ef4fb60ff157a15e0d07b7834d6b Mon Sep 17 00:00:00 2001 From: Henry Fontanier Date: Tue, 17 Dec 2024 16:32:16 +0100 Subject: [PATCH] feat: rm dsconfigs for which doc is out of view and impl fetchMaintainedScope (#9458) Co-authored-by: Henry Fontanier --- front/lib/resources/tracker_resource.ts | 71 +++++++++++++++++++++++-- front/temporal/tracker/activities.ts | 7 ++- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/front/lib/resources/tracker_resource.ts b/front/lib/resources/tracker_resource.ts index eeb72b38eada..79a5510a9fff 100644 --- a/front/lib/resources/tracker_resource.ts +++ b/front/lib/resources/tracker_resource.ts @@ -27,6 +27,16 @@ import type { ReadonlyAttributesType } from "@app/lib/resources/storage/types"; import { getResourceIdFromSId, makeSId } from "@app/lib/resources/string_ids"; import type { ResourceFindOptions } from "@app/lib/resources/types"; +type TrackerMaintainedScopeType = Array<{ + dataSourceViewId: string; + filter: { + parents: { + in: string[]; + not: string[]; + }; + }; +}>; + // Attributes are marked as read-only to reflect the stateless nature of our Resource. // This design will be moved up to BaseResource once we transition away from Sequelize. // eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-unsafe-declaration-merging @@ -218,6 +228,29 @@ export class TrackerConfigurationResource extends ResourceWithSpace { + const maintainedDataSources = + await TrackerDataSourceConfigurationModel.findAll({ + where: { + trackerConfigurationId: this.id, + scope: "maintained", + }, + }); + + return maintainedDataSources.map((m) => ({ + dataSourceViewId: makeSId("data_source_view", { + id: m.dataSourceViewId, + workspaceId: this.workspaceId, + }), + filter: { + parents: { + in: m.parentsIn ?? [], + not: m.parentsNotIn ?? [], + }, + }, + })); + } + private static async baseFetch( auth: Authenticator, options?: ResourceFindOptions @@ -336,16 +369,23 @@ export class TrackerConfigurationResource extends ResourceWithSpace { + const owner = auth.getNonNullableWorkspace(); + const dataSourceModelId = getResourceIdFromSId(dataSourceId); if (!dataSourceModelId) { throw new Error(`Invalid data source ID: ${dataSourceId}`); } - const dsConfigs = await TrackerDataSourceConfigurationModel.findAll({ + let dsConfigs = await TrackerDataSourceConfigurationModel.findAll({ where: { dataSourceId: dataSourceModelId, scope: "watched", @@ -356,9 +396,32 @@ export class TrackerConfigurationResource extends ResourceWithSpace + makeSId("data_source_view", { + id: c.dataSourceViewId, + workspaceId: owner.id, + }) + ); + const parentsSet = new Set(parentIds); + const dsViews = await DataSourceViewResource.fetchByIds(auth, dsViewIds); + // These are the data source views that contain the document. + const validDsViewIds = new Set( + dsViews + .filter( + (dsView) => + dsView.parentsIn === null || + dsView.parentsIn.some((p) => parentsSet.has(p)) + ) + .map((dsView) => dsView.id) + ); + + 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) diff --git a/front/temporal/tracker/activities.ts b/front/temporal/tracker/activities.ts index fd7728f1c085..45c192815bcb 100644 --- a/front/temporal/tracker/activities.ts +++ b/front/temporal/tracker/activities.ts @@ -84,11 +84,10 @@ export async function trackersGenerationActivity( } const trackers = - await TrackerConfigurationResource.fetchAllWatchedForDocument( - auth, + await TrackerConfigurationResource.fetchAllWatchedForDocument(auth, { dataSourceId, - docParentIds - ); + parentIds: docParentIds, + }); if (!trackers.length) { localLogger.info("No active trackers found for document.");