File tree Expand file tree Collapse file tree 6 files changed +62
-0
lines changed
apps/script_executor_worker/src
libs/data-access-layer/src Expand file tree Collapse file tree 6 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ import {
1717 syncRemoveOrganization ,
1818} from './activities/cleanup/organization'
1919import {
20+ countOrphanMembersSegmentsAgg ,
21+ countOrphanOrganizationSegmentsAgg ,
2022 deleteOrphanMembersSegmentsAgg ,
2123 deleteOrphanOrganizationSegmentsAgg ,
2224 getOrphanMembersSegmentsAgg ,
@@ -100,6 +102,8 @@ export {
100102 calculateMemberAffiliations ,
101103 startOrphanCleanupRun ,
102104 updateOrphanCleanupRun ,
105+ countOrphanMembersSegmentsAgg ,
106+ countOrphanOrganizationSegmentsAgg ,
103107 getOrphanMembersSegmentsAgg ,
104108 deleteOrphanMembersSegmentsAgg ,
105109 getOrphanOrganizationSegmentsAgg ,
Original file line number Diff line number Diff line change 11import {
2+ countOrphanMemberSegmentsAgg as countMemberSegmentsAgg ,
23 deleteOrphanMemberSegmentsAgg as deleteMemberSegmentsAgg ,
34 getOrphanMemberSegmentsAgg as getMemberSegmentsAgg ,
45} from '@crowd/data-access-layer/src/members/segmentsAgg'
56import {
7+ countOrphanOrganizationSegmentsAgg as countOrganizationSegmentsAgg ,
68 deleteOrphanOrganizationSegmentsAgg as deleteOrganizationSegmentsAgg ,
79 getOrphanOrganizationSegmentsAgg as getOrganizationSegmentsAgg ,
810} from '@crowd/data-access-layer/src/organizations/segmentsAgg'
@@ -72,3 +74,21 @@ export async function deleteOrphanOrganizationSegmentsAgg(organizationId: string
7274 throw error
7375 }
7476}
77+
78+ export async function countOrphanMembersSegmentsAgg ( ) : Promise < number > {
79+ try {
80+ return countMemberSegmentsAgg ( dbStoreQx ( svc . postgres . reader ) )
81+ } catch ( error ) {
82+ svc . log . error ( error , 'Error counting orphan memberSegmentsAgg records!' )
83+ throw error
84+ }
85+ }
86+
87+ export async function countOrphanOrganizationSegmentsAgg ( ) : Promise < number > {
88+ try {
89+ return countOrganizationSegmentsAgg ( dbStoreQx ( svc . postgres . reader ) )
90+ } catch ( error ) {
91+ svc . log . error ( error , 'Error counting orphan organizationSegmentsAgg records!' )
92+ throw error
93+ }
94+ }
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { chunkArray } from '../../utils/common'
77const {
88 startOrphanCleanupRun,
99 updateOrphanCleanupRun,
10+ countOrphanMembersSegmentsAgg,
1011 getOrphanMembersSegmentsAgg,
1112 deleteOrphanMembersSegmentsAgg,
1213} = proxyActivities < typeof activities > ( {
@@ -28,6 +29,10 @@ export async function cleanupMemberSegmentsAgg(args: IScriptBatchTestArgs): Prom
2829 // Initialize the cleanup run only on the first iteration
2930 if ( ! args . cleanupRunId ) {
3031 runId = await startOrphanCleanupRun ( AGGREGATE_NAME )
32+
33+ // Get and log total count of orphans on first run
34+ const totalCount = await countOrphanMembersSegmentsAgg ( )
35+ console . log ( `Total orphan ${ AGGREGATE_NAME } records to delete: ${ totalCount } ` )
3136 } else {
3237 runId = args . cleanupRunId
3338 }
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { chunkArray } from '../../utils/common'
77const {
88 startOrphanCleanupRun,
99 updateOrphanCleanupRun,
10+ countOrphanOrganizationSegmentsAgg,
1011 getOrphanOrganizationSegmentsAgg,
1112 deleteOrphanOrganizationSegmentsAgg,
1213} = proxyActivities < typeof activities > ( {
@@ -28,6 +29,10 @@ export async function cleanupOrganizationSegmentAgg(args: IScriptBatchTestArgs):
2829 // Initialize the cleanup run only on the first iteration
2930 if ( ! args . cleanupRunId ) {
3031 runId = await startOrphanCleanupRun ( AGGREGATE_NAME )
32+
33+ // Get and log total count of orphans on first run
34+ const totalCount = await countOrphanOrganizationSegmentsAgg ( )
35+ console . log ( `Total orphan ${ AGGREGATE_NAME } records to delete: ${ totalCount } ` )
3136 } else {
3237 runId = args . cleanupRunId
3338 }
Original file line number Diff line number Diff line change 11import { QueryExecutor } from '../queryExecutor'
22
3+ export async function countOrphanMemberSegmentsAgg ( qx : QueryExecutor ) : Promise < number > {
4+ const result = await qx . selectOneOrNone (
5+ `
6+ SELECT COUNT(*) as count
7+ FROM "memberSegmentsAgg" msa
8+ LEFT JOIN members m ON msa."memberId" = m.id
9+ WHERE m.id IS NULL
10+ ` ,
11+ { } ,
12+ )
13+
14+ return parseInt ( result ?. count || '0' , 10 )
15+ }
16+
317export async function getOrphanMemberSegmentsAgg (
418 qx : QueryExecutor ,
519 batchSize : number ,
Original file line number Diff line number Diff line change 11import { QueryExecutor } from '../queryExecutor'
22
3+ export async function countOrphanOrganizationSegmentsAgg ( qx : QueryExecutor ) : Promise < number > {
4+ const result = await qx . selectOneOrNone (
5+ `
6+ SELECT COUNT(*) as count
7+ FROM "organizationSegmentsAgg" osa
8+ LEFT JOIN organizations o ON osa."organizationId" = o.id
9+ WHERE o.id IS NULL
10+ ` ,
11+ { } ,
12+ )
13+
14+ return parseInt ( result ?. count || '0' , 10 )
15+ }
16+
317export async function getOrphanOrganizationSegmentsAgg (
418 qx : QueryExecutor ,
519 batchSize : number ,
You can’t perform that action at this time.
0 commit comments