-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(api-service): implement multi subscription with condition to topic fixes NV-6810 #9370
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
Open
djabarovgeorge
wants to merge
7
commits into
next
Choose a base branch
from
nv-6810-implement-multi-subscription-with-condition-to-topic
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d18bcc
feat(api): add resource and subscriber conditions for topic subscript…
djabarovgeorge be18246
refactor(api): rename resource and subscriber conditions to a unified…
djabarovgeorge 0ca8c98
refactor(api): unify condition fields in topic subscriptions and upda…
djabarovgeorge c2cc377
Merge branch 'next' into nv-6810-implement-multi-subscription-with-co…
djabarovgeorge 9c48297
fix(trigger-multicast): wrap command payload in an object for conditi…
djabarovgeorge d8cc55b
refactor(api): polish
djabarovgeorge ca1aef6
feat(api-servide): add test
djabarovgeorge 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
Empty file.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ import { | |
TriggerRecipientSubscriber, | ||
TriggerRecipientsTypeEnum, | ||
} from '@novu/shared'; | ||
|
||
import jsonLogic, { type AdditionalOperation, type RulesLogic } from 'json-logic-js'; | ||
import { PinoLogger } from 'nestjs-pino'; | ||
import { InstrumentUsecase } from '../../instrumentation'; | ||
import { CacheService, FeatureFlagsService } from '../../services'; | ||
|
@@ -21,8 +21,6 @@ import { TriggerMulticastCommand } from './trigger-multicast.command'; | |
const QUEUE_CHUNK_SIZE = Number(process.env.MULTICAST_QUEUE_CHUNK_SIZE) || 100; | ||
const SUBSCRIBER_TOPIC_DISTINCT_BATCH_SIZE = Number(process.env.SUBSCRIBER_TOPIC_DISTINCT_BATCH_SIZE) || 100; | ||
|
||
const isNotTopic = (recipient: TriggerRecipient): recipient is TriggerRecipientSubscriber => !isTopic(recipient); | ||
|
||
const isTopic = (recipient: TriggerRecipient): recipient is ITopic => | ||
(recipient as ITopic).type && (recipient as ITopic).type === TriggerRecipientsTypeEnum.TOPIC; | ||
|
||
|
@@ -66,8 +64,10 @@ export class TriggerMulticast extends TriggerBase { | |
const allTopicExcludedSubscribers = Array.from( | ||
new Set([...Array.from(topicExclusions.values()).flatMap((set) => Array.from(set))]) | ||
); | ||
let subscribersList: { subscriberId: string; topics: Pick<TopicEntity, '_id' | 'key'>[] }[] = []; | ||
const getTopicDistinctSubscribersGenerator = this.topicSubscribersRepository.getTopicDistinctSubscribers({ | ||
let totalSubscriptionsEvaluated = 0; | ||
let totalSubscriptionsFiltered = 0; | ||
|
||
const getTopicSubscriptionsGenerator = this.topicSubscribersRepository.getTopicSubscriptionsWithConditions({ | ||
query: { | ||
_organizationId: organizationId, | ||
_environmentId: environmentId, | ||
|
@@ -77,26 +77,50 @@ export class TriggerMulticast extends TriggerBase { | |
batchSize: SUBSCRIBER_TOPIC_DISTINCT_BATCH_SIZE, | ||
}); | ||
|
||
for await (const externalSubscriberIdGroup of getTopicDistinctSubscribersGenerator) { | ||
const externalSubscriberId = externalSubscriberIdGroup._id; | ||
const subscribersMap = new Map<string, { subscriberId: string; topics: Pick<TopicEntity, '_id' | 'key'>[] }>(); | ||
|
||
if (actor && actor.subscriberId === externalSubscriberId) { | ||
continue; | ||
} | ||
for await (const subscriptionsBatch of getTopicSubscriptionsGenerator) { | ||
totalSubscriptionsEvaluated += subscriptionsBatch.length; | ||
|
||
subscribersList.push({ | ||
subscriberId: externalSubscriberId, | ||
topics: topics?.map((topic) => ({ _id: topic._id, key: topic.key })), | ||
const passingSubscriptions = subscriptionsBatch.filter((subscription) => { | ||
return this.evaluateConditions( | ||
subscription.conditions as Record<string, unknown>, | ||
{ payload: command.payload } as Record<string, unknown> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here we will need to add workflow entity variable as well |
||
); | ||
}); | ||
|
||
if (subscribersList.length === SUBSCRIBER_TOPIC_DISTINCT_BATCH_SIZE) { | ||
await this.sendToProcessSubscriberService(command, subscribersList, SubscriberSourceEnum.TOPIC); | ||
totalProcessed += subscribersList.length; | ||
totalSubscriptionsFiltered += subscriptionsBatch.length - passingSubscriptions.length; | ||
|
||
for (const subscription of passingSubscriptions) { | ||
const externalSubscriberId = subscription.externalSubscriberId; | ||
|
||
if (actor && actor.subscriberId === externalSubscriberId) { | ||
continue; | ||
} | ||
|
||
if (!subscribersMap.has(externalSubscriberId)) { | ||
subscribersMap.set(externalSubscriberId, { | ||
subscriberId: externalSubscriberId, | ||
topics: topics?.map((topic) => ({ _id: topic._id, key: topic.key })), | ||
}); | ||
} | ||
} | ||
|
||
if (subscribersMap.size >= SUBSCRIBER_TOPIC_DISTINCT_BATCH_SIZE) { | ||
const batchToProcess = Array.from(subscribersMap.values()); | ||
await this.sendToProcessSubscriberService(command, batchToProcess, SubscriberSourceEnum.TOPIC); | ||
totalProcessed += batchToProcess.length; | ||
|
||
subscribersList = []; | ||
subscribersMap.clear(); | ||
} | ||
} | ||
|
||
if (subscribersMap.size > 0) { | ||
const finalBatch = Array.from(subscribersMap.values()); | ||
await this.sendToProcessSubscriberService(command, finalBatch, SubscriberSourceEnum.TOPIC); | ||
totalProcessed += finalBatch.length; | ||
} | ||
|
||
await this.createMulticastTrace( | ||
command, | ||
'request_subscriber_processing_completed', | ||
|
@@ -109,13 +133,10 @@ export class TriggerMulticast extends TriggerBase { | |
singleSubscribers: subscribersToProcess.length, | ||
topicSubscribers: totalProcessed - subscribersToProcess.length, | ||
topicsUsed: topics.length, | ||
subscriptionsEvaluated: totalSubscriptionsEvaluated, | ||
subscriptionsFiltered: totalSubscriptionsFiltered, | ||
} | ||
); | ||
|
||
if (subscribersList.length > 0) { | ||
await this.sendToProcessSubscriberService(command, subscribersList, SubscriberSourceEnum.TOPIC); | ||
totalProcessed += subscribersList.length; | ||
} | ||
} catch (e) { | ||
const error = e as Error; | ||
await this.createMulticastTrace( | ||
|
@@ -146,6 +167,23 @@ export class TriggerMulticast extends TriggerBase { | |
} | ||
} | ||
|
||
private evaluateConditions( | ||
conditions: Record<string, unknown> | undefined, | ||
payload: Record<string, unknown> | ||
): boolean { | ||
if (!conditions) { | ||
return true; | ||
} | ||
|
||
try { | ||
const result = jsonLogic.apply(conditions as RulesLogic<AdditionalOperation>, payload); | ||
|
||
return typeof result === 'boolean' ? result : false; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
private async createMulticastTrace( | ||
command: TriggerMulticastCommand, | ||
eventType: EventType, | ||
|
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,11 @@ | ||
import { createHash } from 'crypto'; | ||
|
||
export function generateConditionHash(condition?: Record<string, unknown>): string | undefined { | ||
if (!condition) { | ||
return undefined; | ||
} | ||
|
||
const normalizedJson = JSON.stringify(condition, Object.keys(condition).sort()); | ||
|
||
return createHash('sha256').update(normalizedJson).digest('hex'); | ||
} |
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.
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.
validate if we ca use rooted $or