-
Notifications
You must be signed in to change notification settings - Fork 11
chore: in memory deployment variable value resource selector #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
278 changes: 278 additions & 0 deletions
278
apps/event-queue/src/selector/in-memory/deployment-variable-value-resource.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,278 @@ | ||
| import type { FullResource } from "@ctrlplane/events"; | ||
| import _ from "lodash"; | ||
| import { isPresent } from "ts-is-present"; | ||
|
|
||
| import { and, eq, inArray } from "@ctrlplane/db"; | ||
| import { db as dbClient } from "@ctrlplane/db/client"; | ||
| import * as schema from "@ctrlplane/db/schema"; | ||
|
|
||
| import type { Selector } from "../selector.js"; | ||
| import { resourceMatchesSelector } from "./resource-match.js"; | ||
|
|
||
| type InMemoryDeploymentVariableValueResourceSelectorOptions = { | ||
| initialEntities: FullResource[]; | ||
| initialSelectors: schema.DeploymentVariableValue[]; | ||
| }; | ||
|
|
||
| const entityMatchesSelector = ( | ||
| entity: FullResource, | ||
| selector: schema.DeploymentVariableValue, | ||
| ) => { | ||
| if (selector.resourceSelector == null) return false; | ||
| return resourceMatchesSelector(entity, selector.resourceSelector); | ||
| }; | ||
|
|
||
| export class InMemoryDeploymentVariableValueResourceSelector | ||
| implements Selector<schema.DeploymentVariableValue, FullResource> | ||
| { | ||
| private entities: Map<string, FullResource>; | ||
| private selectors: Map<string, schema.DeploymentVariableValue>; | ||
| private matches: Map<string, Set<string>>; // resourceId -> deploymentId | ||
|
|
||
| constructor(opts: InMemoryDeploymentVariableValueResourceSelectorOptions) { | ||
| this.entities = new Map( | ||
| opts.initialEntities.map((entity) => [entity.id, entity]), | ||
| ); | ||
| this.selectors = new Map( | ||
| opts.initialSelectors.map((selector) => [selector.id, selector]), | ||
| ); | ||
| this.matches = new Map(); | ||
|
|
||
| for (const entity of opts.initialEntities) { | ||
| this.matches.set(entity.id, new Set()); | ||
|
|
||
| for (const selector of opts.initialSelectors) { | ||
| const match = | ||
| selector.resourceSelector != null && | ||
| resourceMatchesSelector(entity, selector.resourceSelector); | ||
| if (match) this.matches.get(entity.id)?.add(selector.id); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| get selectorMatches() { | ||
| return this.matches; | ||
| } | ||
|
|
||
| static async create(workspaceId: string, initialEntities: FullResource[]) { | ||
| const allSelectors = await dbClient | ||
| .select() | ||
| .from(schema.deploymentVariableValue) | ||
| .leftJoin( | ||
| schema.deploymentVariableValueDirect, | ||
| eq( | ||
| schema.deploymentVariableValue.id, | ||
| schema.deploymentVariableValueDirect.variableValueId, | ||
| ), | ||
| ) | ||
| .leftJoin( | ||
| schema.deploymentVariableValueReference, | ||
| eq( | ||
| schema.deploymentVariableValue.id, | ||
| schema.deploymentVariableValueReference.variableValueId, | ||
| ), | ||
| ) | ||
| .innerJoin( | ||
| schema.deploymentVariable, | ||
| eq( | ||
| schema.deploymentVariableValue.variableId, | ||
| schema.deploymentVariable.id, | ||
| ), | ||
| ) | ||
| .innerJoin( | ||
| schema.deployment, | ||
| eq(schema.deploymentVariable.deploymentId, schema.deployment.id), | ||
| ) | ||
| .innerJoin( | ||
| schema.system, | ||
| eq(schema.deployment.systemId, schema.system.id), | ||
| ) | ||
| .where(eq(schema.system.workspaceId, workspaceId)) | ||
| .then((results) => | ||
| results.map((result) => { | ||
| if (result.deployment_variable_value_direct != null) | ||
| return { | ||
| ...result.deployment_variable_value_direct, | ||
| ...result.deployment_variable_value, | ||
| }; | ||
| if (result.deployment_variable_value_reference != null) | ||
| return { | ||
| ...result.deployment_variable_value_reference, | ||
| ...result.deployment_variable_value, | ||
| }; | ||
| return null; | ||
| }), | ||
| ) | ||
| .then((results) => results.filter(isPresent)); | ||
|
|
||
| return new InMemoryDeploymentVariableValueResourceSelector({ | ||
| initialEntities, | ||
| initialSelectors: allSelectors, | ||
| }); | ||
| } | ||
|
|
||
| async upsertEntity(entity: FullResource): Promise<void> { | ||
| if (this.matches.get(entity.id) == null) | ||
| this.matches.set(entity.id, new Set()); | ||
| this.entities.set(entity.id, entity); | ||
|
|
||
| const previouslyMatchingDeployments = | ||
| this.matches.get(entity.id) ?? new Set(); | ||
| const currentlyMatchingDeployments = new Set<string>(); | ||
|
|
||
| for (const selector of this.selectors.values()) { | ||
| const match = entityMatchesSelector(entity, selector); | ||
| if (match) currentlyMatchingDeployments.add(selector.id); | ||
| } | ||
|
|
||
| const unmatchedDeployments = Array.from( | ||
| previouslyMatchingDeployments, | ||
| ).filter((deploymentId) => !currentlyMatchingDeployments.has(deploymentId)); | ||
|
|
||
| const newlyMatchedDeployments = Array.from( | ||
| currentlyMatchingDeployments, | ||
| ).filter( | ||
| (deploymentId) => !previouslyMatchingDeployments.has(deploymentId), | ||
| ); | ||
|
|
||
| for (const deploymentId of unmatchedDeployments) | ||
| this.matches.get(entity.id)?.delete(deploymentId); | ||
|
|
||
| for (const deploymentId of newlyMatchedDeployments) | ||
| this.matches.get(entity.id)?.add(deploymentId); | ||
|
|
||
| if (unmatchedDeployments.length > 0) | ||
| await dbClient | ||
| .delete(schema.computedDeploymentResource) | ||
| .where( | ||
| and( | ||
| eq(schema.computedDeploymentResource.resourceId, entity.id), | ||
| inArray( | ||
| schema.computedDeploymentResource.deploymentId, | ||
| unmatchedDeployments, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| if (newlyMatchedDeployments.length > 0) | ||
| await dbClient | ||
| .insert(schema.computedDeploymentResource) | ||
| .values( | ||
| newlyMatchedDeployments.map((deploymentId) => ({ | ||
| resourceId: entity.id, | ||
| deploymentId, | ||
| })), | ||
| ) | ||
| .onConflictDoNothing(); | ||
| } | ||
| async removeEntity(entity: FullResource): Promise<void> { | ||
| this.entities.delete(entity.id); | ||
| this.matches.delete(entity.id); | ||
| await dbClient | ||
| .delete(schema.computedDeploymentResource) | ||
| .where(eq(schema.computedDeploymentResource.resourceId, entity.id)); | ||
| } | ||
|
|
||
| async upsertSelector( | ||
| selector: schema.DeploymentVariableValue, | ||
| ): Promise<void> { | ||
| this.selectors.set(selector.id, selector); | ||
|
|
||
| const previouslyMatchingResources: string[] = []; | ||
| for (const [resourceId, deploymentIds] of this.matches) | ||
| if (deploymentIds.has(selector.id)) | ||
| previouslyMatchingResources.push(resourceId); | ||
|
|
||
| const currentlyMatchingResources: string[] = []; | ||
| for (const entity of this.entities.values()) | ||
| if (entityMatchesSelector(entity, selector)) | ||
| currentlyMatchingResources.push(entity.id); | ||
|
|
||
| const unmatchedResources = previouslyMatchingResources.filter( | ||
| (resourceId) => !currentlyMatchingResources.includes(resourceId), | ||
| ); | ||
| const newlyMatchedResources = currentlyMatchingResources.filter( | ||
| (resourceId) => !previouslyMatchingResources.includes(resourceId), | ||
| ); | ||
|
|
||
| for (const resourceId of unmatchedResources) | ||
| this.matches.get(resourceId)?.delete(selector.id); | ||
|
|
||
| for (const resourceId of newlyMatchedResources) | ||
| this.matches.get(resourceId)?.add(selector.id); | ||
|
|
||
| if (unmatchedResources.length > 0) | ||
| await dbClient | ||
| .delete(schema.computedDeploymentResource) | ||
| .where( | ||
| and( | ||
| eq(schema.computedDeploymentResource.deploymentId, selector.id), | ||
| inArray( | ||
| schema.computedDeploymentResource.resourceId, | ||
| unmatchedResources, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| if (newlyMatchedResources.length > 0) | ||
| await dbClient | ||
| .insert(schema.computedDeploymentResource) | ||
| .values( | ||
| newlyMatchedResources.map((resourceId) => ({ | ||
| resourceId, | ||
| deploymentId: selector.id, | ||
| })), | ||
| ) | ||
| .onConflictDoNothing(); | ||
| } | ||
| async removeSelector( | ||
| selector: schema.DeploymentVariableValue, | ||
| ): Promise<void> { | ||
| this.selectors.delete(selector.id); | ||
|
|
||
| for (const deploymentIds of this.matches.values()) | ||
| if (deploymentIds.has(selector.id)) deploymentIds.delete(selector.id); | ||
|
|
||
| await dbClient | ||
| .delete(schema.computedDeploymentResource) | ||
| .where(eq(schema.computedDeploymentResource.deploymentId, selector.id)); | ||
| } | ||
| getEntitiesForSelector( | ||
| selector: schema.DeploymentVariableValue, | ||
| ): Promise<FullResource[]> { | ||
| const resourceIds: string[] = []; | ||
| for (const [resourceId, deploymentIds] of this.matches) | ||
| if (deploymentIds.has(selector.id)) resourceIds.push(resourceId); | ||
|
|
||
| return Promise.resolve( | ||
| resourceIds | ||
| .map((resourceId) => this.entities.get(resourceId)) | ||
| .filter(isPresent), | ||
| ); | ||
| } | ||
|
|
||
| getSelectorsForEntity( | ||
| entity: FullResource, | ||
| ): Promise<schema.DeploymentVariableValue[]> { | ||
| const matchingDeploymentIds = | ||
| this.matches.get(entity.id) ?? new Set<string>(); | ||
| const matchingDeployments = Array.from(matchingDeploymentIds) | ||
| .map((deploymentId) => this.selectors.get(deploymentId)) | ||
| .filter(isPresent); | ||
| return Promise.resolve(matchingDeployments); | ||
| } | ||
| getAllEntities(): Promise<FullResource[]> { | ||
| return Promise.resolve(Array.from(this.entities.values())); | ||
| } | ||
| getAllSelectors(): Promise<schema.DeploymentVariableValue[]> { | ||
| return Promise.resolve(Array.from(this.selectors.values())); | ||
| } | ||
| isMatch( | ||
| entity: FullResource, | ||
| selector: schema.DeploymentVariableValue, | ||
| ): Promise<boolean> { | ||
| return Promise.resolve( | ||
| this.matches.get(entity.id)?.has(selector.id) ?? false, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Persist actual deployment IDs in
computedDeploymentResource.newlyMatchedDeploymentsholdsselector.id, which is the deployment-variable-value ID. Writing that intocomputedDeploymentResource.deploymentIdstores the wrong key (and will break any FK / downstream lookup that expects a real deployment ID). We need to persist the owning deployment’s ID instead—derive it when loading/upserting selectors (and dedupe when multiple values belong to the same deployment) and use that value for both the insert and delete paths here and below.🤖 Prompt for AI Agents