-
Notifications
You must be signed in to change notification settings - Fork 732
chore: add duplicate organization pruning script [CM-766] #3597
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
116cba6
chore: add duplicate organization pruning script
skwowet ca67823
Merge branch 'main' into script/clean-duplicate-orgs
skwowet bd1a315
fix: resolve pr comments
skwowet fd852e4
chore: add script for pruning and refreshing member organizations
skwowet 241df9f
Merge branch 'main' into script/clean-duplicate-orgs
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
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
41 changes: 0 additions & 41 deletions
41
services/apps/script_executor_worker/src/activities/cleanup/duplicate-members.ts
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
services/apps/script_executor_worker/src/activities/prune-duplicate-organizations.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,63 @@ | ||
| import { refreshMemberOrganizationAffiliations } from '@crowd/data-access-layer/src/member-organization-affiliation' | ||
| import { deleteMemberOrganizations } from '@crowd/data-access-layer/src/members' | ||
| import OrganizationRepository from '@crowd/data-access-layer/src/old/apps/script_executor_worker/organization.repo' | ||
| import { pgpQx } from '@crowd/data-access-layer/src/queryExecutor' | ||
|
|
||
| import { svc } from '../main' | ||
|
|
||
| export async function pruneOrganization(orgId: string): Promise<void> { | ||
| try { | ||
| const orgRepo = new OrganizationRepository(svc.postgres.writer.connection(), svc.log) | ||
| await orgRepo.pruneOrganization(orgId) | ||
| } catch (error) { | ||
| svc.log.error(error, 'Error pruning organization in database!') | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export async function getOrganizationsToPrune( | ||
| batchSize: number, | ||
| ): Promise<{ id: string; displayName: string }[]> { | ||
| try { | ||
| const orgRepo = new OrganizationRepository(svc.postgres.reader.connection(), svc.log) | ||
| return orgRepo.getOrganizationsToPrune(batchSize) | ||
| } catch (error) { | ||
| svc.log.error(error, 'Error getting organizations to prune!') | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export async function getMemberOrganizationsToPrune( | ||
| batchSize: number, | ||
| ): Promise<{ id: string; memberId: string }[]> { | ||
| try { | ||
| const orgRepo = new OrganizationRepository(svc.postgres.reader.connection(), svc.log) | ||
| return orgRepo.getMemberOrganizationsToPrune(batchSize) | ||
| } catch (error) { | ||
| svc.log.error(error, 'Error getting member organizations to prune!') | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export async function pruneMemberOrganization( | ||
| memberOrganizationId: string, | ||
| memberId: string, | ||
| ): Promise<void> { | ||
| try { | ||
| const qx = pgpQx(svc.postgres.writer.connection()) | ||
| await deleteMemberOrganizations(qx, memberId, [memberOrganizationId], false) | ||
| } catch (error) { | ||
| svc.log.error(error, 'Error pruning member organization!') | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export async function refreshMemberAffiliations(memberId: string): Promise<void> { | ||
| try { | ||
| const qx = pgpQx(svc.postgres.writer.connection()) | ||
| await refreshMemberOrganizationAffiliations(qx, memberId) | ||
| } catch (error) { | ||
| svc.log.error(error, 'Error refreshing member affiliations!') | ||
| throw error | ||
| } | ||
| } |
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
64 changes: 0 additions & 64 deletions
64
services/apps/script_executor_worker/src/workflows/cleanup/duplicate-members.ts
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
services/apps/script_executor_worker/src/workflows/prune-duplicate-organizations.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,37 @@ | ||
| import { proxyActivities } from '@temporalio/workflow' | ||
|
|
||
| import * as activities from '../activities' | ||
| import { IScriptBatchTestArgs } from '../types' | ||
| import { chunkArray } from '../utils/common' | ||
|
|
||
| const { getOrganizationsToPrune, pruneOrganization, syncRemoveOrganization } = proxyActivities< | ||
| typeof activities | ||
| >({ | ||
| startToCloseTimeout: '30 minutes', | ||
| retry: { maximumAttempts: 3, backoffCoefficient: 3 }, | ||
| }) | ||
|
|
||
| export async function pruneDuplicateOrganizations(args: IScriptBatchTestArgs): Promise<void> { | ||
| const BATCH_SIZE = args.batchSize ?? 100 | ||
|
|
||
| const organizationsToPrune = await getOrganizationsToPrune(BATCH_SIZE) | ||
|
|
||
| if (organizationsToPrune.length === 0) { | ||
| console.log('No more organizations to prune!') | ||
| return | ||
| } | ||
|
|
||
| const CHUNK_SIZE = 25 | ||
|
|
||
| for (const chunk of chunkArray(organizationsToPrune, CHUNK_SIZE)) { | ||
| const cleanupTasks = chunk.map(async (o) => { | ||
| console.log('Pruning organization', o.displayName) | ||
| await syncRemoveOrganization(o.id) | ||
| return pruneOrganization(o.id) | ||
| }) | ||
|
|
||
| await Promise.all(cleanupTasks).catch((err) => { | ||
| console.error('Error pruning organizations!', err) | ||
| }) | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
services/apps/script_executor_worker/src/workflows/prune-incorrect-member-organizations.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,42 @@ | ||
| import { proxyActivities } from '@temporalio/workflow' | ||
|
|
||
| import * as activities from '../activities' | ||
| import { IScriptBatchTestArgs } from '../types' | ||
| import { chunkArray } from '../utils/common' | ||
|
|
||
| const { getMemberOrganizationsToPrune, pruneMemberOrganization, refreshMemberAffiliations } = | ||
| proxyActivities<typeof activities>({ | ||
| startToCloseTimeout: '30 minutes', | ||
| retry: { maximumAttempts: 3, backoffCoefficient: 3 }, | ||
| }) | ||
|
|
||
| export async function pruneIncorrectMemberOrganizations(args: IScriptBatchTestArgs): Promise<void> { | ||
| const BATCH_SIZE = args.batchSize ?? 100 | ||
|
|
||
| const memberOrganizationsToPrune = await getMemberOrganizationsToPrune(BATCH_SIZE) | ||
|
|
||
| if (memberOrganizationsToPrune.length === 0) { | ||
| console.log('No more member organizations to prune!') | ||
| return | ||
| } | ||
|
|
||
| const memberIdsToRefresh = new Set<string>() | ||
| const CHUNK_SIZE = 25 | ||
|
|
||
| for (const chunk of chunkArray(memberOrganizationsToPrune, CHUNK_SIZE)) { | ||
| const cleanupTasks = chunk.map(async (mo) => { | ||
| console.log('Pruning member organization', mo.id) | ||
| await pruneMemberOrganization(mo.id, mo.memberId) | ||
| memberIdsToRefresh.add(mo.memberId) | ||
| }) | ||
|
|
||
| await Promise.all(cleanupTasks).catch((err) => { | ||
| console.error('Error pruning member organizations!', err) | ||
| }) | ||
| } | ||
|
|
||
| for (const memberId of memberIdsToRefresh) { | ||
| console.log('Refreshing member affiliations', memberId) | ||
| await refreshMemberAffiliations(memberId) | ||
| } | ||
| } |
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.