-
Notifications
You must be signed in to change notification settings - Fork 731
feat: infer memberOrganization stint dates from work-email activities (CM-1105) #4054
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d997511
feat: infer memberOrganization stint dates from work-email activities
skwowet 057464d
fix: resolve pr comments from ai bots
skwowet 0af601c
fix: update redis member ID retrieval method and cleanup logic
skwowet 34354a5
Update services/apps/data_sink_worker/src/service/member.service.ts
skwowet f74aef1
fix: resolve pr comments from ai bots
skwowet 68a79fc
refactor: extract source rank logic into a separate func for clarity …
skwowet 9553e2c
refactor: streamline email domain extraction logic in ActivityService
skwowet 8a9f500
chore: enable debugger logs in prod
skwowet d7a18df
chore: add logging for member organization stint inference job start
skwowet 15bd7ec
fix: debugger and info logs
skwowet 8914f41
chore: remove unused constant and add TODO for applying stint changes…
skwowet 498f0c1
feat: apply overrides in member organization stint changes job
skwowet 4fead74
fix: resolve pr review comments
skwowet 0f3d3a2
fix: redis key pruning script
skwowet 21e42bf
chore: rm redis key clean up script
skwowet 0adfab9
fix: lua script edge case
skwowet bdce7d1
fix: set organization source to UI for manual edits in member organiz…
skwowet c2f76de
Merge branch 'main' into improve/CM-1105
skwowet afc5734
chore: rm debugger env
skwowet 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
3 changes: 3 additions & 0 deletions
3
.../src/database/migrations/V1776931245__member-organizations-email-domain-partial-index.sql
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,3 @@ | ||
| CREATE INDEX CONCURRENTLY IF NOT EXISTS "ix_memberOrganizations_memberId_emailDomain" | ||
| ON "memberOrganizations" ("memberId") | ||
| WHERE "source" = 'email-domain' AND "deletedAt" IS NULL; |
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
141 changes: 141 additions & 0 deletions
141
services/apps/cron_service/src/jobs/inferMemberOrganizationStintChanges.job.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,141 @@ | ||
| import CronTime from 'cron-time-generator' | ||
|
|
||
| import { | ||
| MEMBER_ORG_STINT_CHANGES_DATES_PREFIX, | ||
| MEMBER_ORG_STINT_CHANGES_QUEUE, | ||
| inferMemberOrganizationStintChanges, | ||
| } from '@crowd/common_services' | ||
| import { | ||
| QueryExecutor, | ||
| changeMemberOrganizationAffiliationOverrides, | ||
| checkOrganizationAffiliationPolicy, | ||
| createMemberOrganization, | ||
| fetchMemberOrganizationsBySource, | ||
| updateMemberOrganization, | ||
| } from '@crowd/data-access-layer' | ||
| import { WRITE_DB_CONFIG, getDbConnection } from '@crowd/data-access-layer/src/database' | ||
| import { pgpQx } from '@crowd/data-access-layer/src/queryExecutor' | ||
| import { REDIS_CONFIG, RedisCache, getRedisClient } from '@crowd/redis' | ||
| import { MemberOrgDate, MemberOrgStintChange, OrganizationSource } from '@crowd/types' | ||
|
|
||
| import { IJobDefinition } from '../types' | ||
|
|
||
| const job: IJobDefinition = { | ||
| name: 'infer-member-organization-stint-changes', | ||
| cronTime: CronTime.every(5).minutes(), | ||
| timeout: 10 * 60, | ||
| process: async (ctx) => { | ||
| const redis = await getRedisClient(REDIS_CONFIG()) | ||
| const db = await getDbConnection(WRITE_DB_CONFIG()) | ||
| const qx = pgpQx(db) | ||
|
|
||
| ctx.log.info('Starting member organization stint inference job.') | ||
|
|
||
| const memberIds = await redis.sRandMemberCount(MEMBER_ORG_STINT_CHANGES_QUEUE, 500) | ||
| if (!memberIds?.length) return | ||
|
|
||
| ctx.log.info({ count: memberIds.length }, 'Processing members from queue.') | ||
|
|
||
| let processed = 0 | ||
|
|
||
| for (const memberId of memberIds) { | ||
| try { | ||
| const datesKey = `${MEMBER_ORG_STINT_CHANGES_DATES_PREFIX}:${memberId}` | ||
| const rawMembers = await redis.sMembers(datesKey) | ||
|
|
||
| if (!rawMembers?.length) { | ||
| await redis.sRem(MEMBER_ORG_STINT_CHANGES_QUEUE, memberId) | ||
| continue | ||
| } | ||
|
|
||
| const orgDates = parseSetMembers(rawMembers) | ||
|
|
||
| if (orgDates.length > 0) { | ||
| const existingOrgs = await fetchMemberOrganizationsBySource( | ||
| qx, | ||
| memberId, | ||
| OrganizationSource.EMAIL_DOMAIN, | ||
| ) | ||
|
|
||
| const changes = inferMemberOrganizationStintChanges(memberId, existingOrgs, orgDates) | ||
|
|
||
| if (changes.length > 0) { | ||
| ctx.log.debug({ memberId, changes }, 'Stint changes identified.') | ||
| await qx.tx((tx) => applyStintChanges(tx, changes)) | ||
| } | ||
| } | ||
|
skwowet marked this conversation as resolved.
|
||
|
|
||
| // Atomically remove only the values we read. | ||
| // If no new values were added, remove the member from the queue. | ||
| await RedisCache.ackSetMembers( | ||
| redis, | ||
| datesKey, | ||
| MEMBER_ORG_STINT_CHANGES_QUEUE, | ||
| memberId, | ||
| rawMembers, | ||
| ) | ||
|
|
||
| processed++ | ||
| } catch (err) { | ||
| ctx.log.error(err, { memberId }, 'Failed to process member stint inference.') | ||
| throw err | ||
|
skwowet marked this conversation as resolved.
|
||
| } | ||
|
skwowet marked this conversation as resolved.
|
||
| } | ||
|
|
||
| ctx.log.info({ processed }, 'Batch complete.') | ||
| }, | ||
| } | ||
|
|
||
| /** | ||
| * Parses set members of the form "orgId|date" into typed activity dates. | ||
| */ | ||
| function parseSetMembers(members: string[]): MemberOrgDate[] { | ||
| const results: MemberOrgDate[] = [] | ||
|
|
||
| for (const m of members) { | ||
| const idx = m.indexOf('|') | ||
| if (idx > 0) { | ||
| results.push({ organizationId: m.slice(0, idx), date: m.slice(idx + 1) }) | ||
| } | ||
| } | ||
|
|
||
| return results | ||
| } | ||
|
|
||
| /** | ||
| * Applies the stint changes to the database. | ||
| */ | ||
| async function applyStintChanges(qx: QueryExecutor, changes: MemberOrgStintChange[]) { | ||
| for (const change of changes) { | ||
| if (change.type === 'insert') { | ||
| const memberOrganizationId = await createMemberOrganization(qx, change.memberId, { | ||
| organizationId: change.organizationId, | ||
| dateStart: change.dateStart, | ||
| dateEnd: change.dateEnd, | ||
| source: OrganizationSource.EMAIL_DOMAIN, | ||
| }) | ||
|
|
||
| const isAffiliationBlocked = await checkOrganizationAffiliationPolicy( | ||
| qx, | ||
| change.organizationId, | ||
| ) | ||
|
|
||
| if (memberOrganizationId && isAffiliationBlocked) { | ||
| await changeMemberOrganizationAffiliationOverrides(qx, [ | ||
| { | ||
| memberId: change.memberId, | ||
| memberOrganizationId, | ||
| allowAffiliation: false, | ||
| }, | ||
| ]) | ||
| } | ||
| } else { | ||
| await updateMemberOrganization(qx, change.memberId, change.id, { | ||
| dateStart: change.dateStart, | ||
| dateEnd: change.dateEnd, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default job | ||
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
97 changes: 0 additions & 97 deletions
97
services/apps/data_sink_worker/src/bin/map-member-to-org.ts
This file was deleted.
Oops, something went wrong.
98 changes: 0 additions & 98 deletions
98
services/apps/data_sink_worker/src/bin/map-tenant-members-to-org.ts
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.