-
Notifications
You must be signed in to change notification settings - Fork 728
fix misattributed activities based on username + memberId #2715
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
25 commits
Select commit
Hold shift + click to select a range
f971344
fix miattributed activities based on username + memberId
skwowet 4ae5d0c
fix
skwowet 696ed05
increase startToClose
skwowet e883261
include tenantId
skwowet 02c0dbd
update query
skwowet b4d3120
update query
skwowet 5e9f3ee
Merge branch 'main' into script/LFX-1846
skwowet dfc6a56
improve script
skwowet b06c544
make linter happy
skwowet 0c7382c
fix
skwowet d5e21e9
Merge branch 'main' into script/LFX-1846
skwowet e217fe0
update in questdb
skwowet 56c4ee6
batch
skwowet 3644f91
update script
skwowet 1bc2a48
fix script worker
skwowet 6566479
update script
skwowet cfea6fa
increase timeout
skwowet 4804b58
donot use updateActivities
skwowet 47611cf
fix script worker
skwowet 6c520c2
fix rest of the members
skwowet 092621f
fix
skwowet 21de66b
fix
skwowet 6fa6ea8
update updatedAt for fixed activities
skwowet ee4156e
update script
skwowet 861f177
fix
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
96 changes: 96 additions & 0 deletions
96
services/apps/script_executor_worker/src/activities/fix-misattributed-activities/index.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,96 @@ | ||
| import { parse } from 'csv-parse/sync' | ||
| import * as fs from 'fs' | ||
| import * as path from 'path' | ||
|
|
||
| import { ActivityRepository } from '@crowd/data-access-layer/src/old/apps/script_executor_worker/activities.repo' | ||
| import MemberRepository from '@crowd/data-access-layer/src/old/apps/script_executor_worker/member.repo' | ||
| import { IMemberIdentity } from '@crowd/types' | ||
|
|
||
| import { svc } from '../../main' | ||
|
|
||
| // export async function findActivitiesWithWrongMembers(tenantId: string, limit: number) { | ||
| // let activitiesWithWrongMember = [] | ||
|
|
||
| // try { | ||
| // const activityRepo = new ActivityRepository( | ||
| // svc.postgres.reader.connection(), | ||
| // svc.log, | ||
| // svc.questdbSQL, | ||
| // ) | ||
| // activitiesWithWrongMember = await activityRepo.getActivitiesWithWrongMembers(tenantId, limit) | ||
| // } catch (err) { | ||
| // throw new Error(err) | ||
| // } | ||
|
|
||
| // return activitiesWithWrongMember | ||
| // } | ||
|
|
||
| export async function findMemberIdentity(username: string, platform: string, tenantId: string) { | ||
| let memberIdentity: IMemberIdentity | ||
|
|
||
| try { | ||
| const memberRepo = new MemberRepository(svc.postgres.reader.connection(), svc.log) | ||
| memberIdentity = await memberRepo.findMemberIdentity(username, platform, tenantId) | ||
| } catch (err) { | ||
| throw new Error(err) | ||
| } | ||
|
|
||
| return memberIdentity | ||
| } | ||
|
|
||
| export async function updateActivityWithWrongMember(activityId: string, correctMemberId: string) { | ||
| try { | ||
| const activityRepo = new ActivityRepository( | ||
| svc.postgres.writer.connection(), | ||
| svc.log, | ||
| svc.questdbSQL, | ||
| ) | ||
| await activityRepo.updateActivityWithWrongMember(activityId, correctMemberId) | ||
| } catch (err) { | ||
| throw new Error(err) | ||
| } | ||
| } | ||
|
|
||
| export async function batchUpdateActivitiesWithWrongMember( | ||
| wrongMemberId: string, | ||
| correctMemberId: string, | ||
| ) { | ||
| try { | ||
| const activityRepo = new ActivityRepository( | ||
| svc.postgres.writer.connection(), | ||
| svc.log, | ||
| svc.questdbSQL, | ||
| ) | ||
| await activityRepo.batchUpdateActivitiesWithWrongMember(wrongMemberId, correctMemberId) | ||
| } catch (err) { | ||
| throw new Error(err) | ||
| } | ||
| } | ||
|
|
||
| export async function updateMemberActivitiesUpdatedAt(memberId: string) { | ||
| try { | ||
| const activityRepo = new ActivityRepository( | ||
| svc.postgres.writer.connection(), | ||
| svc.log, | ||
| svc.questdbSQL, | ||
| ) | ||
| await activityRepo.updateMemberActivitiesUpdatedAt(memberId) | ||
| } catch (err) { | ||
| throw new Error(err) | ||
| } | ||
| } | ||
|
|
||
| export async function findActivitiesWithWrongMembers(): Promise< | ||
| Array<{ | ||
| wrongMemberId: string | ||
| correctMemberId: string | ||
| activitiesCount: number | ||
| }> | ||
| > { | ||
| const csvFilePath = path.join(process.cwd(), 'misattributed_activities.csv') | ||
| const fileContent = fs.readFileSync(csvFilePath, 'utf-8') | ||
| return parse(fileContent, { | ||
| columns: true, | ||
| skip_empty_lines: true, | ||
| }) | ||
| } | ||
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 |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import { dissectMember } from './workflows/dissectMember' | ||
| import { findAndMergeMembersWithSamePlatformIdentitiesDifferentCapitalization } from './workflows/findAndMergeMembersWithSamePlatformIdentitiesDifferentCapitalization' | ||
| import { findAndMergeMembersWithSameVerifiedEmailsInDifferentPlatforms } from './workflows/findAndMergeMembersWithSameVerifiedEmailsInDifferentPlatforms' | ||
| import { fixMisattributedActivities } from './workflows/fixMisattributedActivities' | ||
| import { fixOrgIdentitiesWithWrongUrls } from './workflows/fixOrgIdentitiesWithWrongUrls' | ||
|
|
||
| export { | ||
| findAndMergeMembersWithSameVerifiedEmailsInDifferentPlatforms, | ||
| findAndMergeMembersWithSamePlatformIdentitiesDifferentCapitalization, | ||
| dissectMember, | ||
| fixOrgIdentitiesWithWrongUrls, | ||
| fixMisattributedActivities, | ||
| } |
58 changes: 58 additions & 0 deletions
58
services/apps/script_executor_worker/src/workflows/fixMisattributedActivities.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,58 @@ | ||
| import { proxyActivities } from '@temporalio/workflow' | ||
|
|
||
| import * as activities from '../activities' | ||
| import { IFixMisattributedActivitiesArgs } from '../types' | ||
|
|
||
| const activity = proxyActivities<typeof activities>({ | ||
| startToCloseTimeout: '60 minutes', | ||
| }) | ||
|
|
||
| export async function fixMisattributedActivities( | ||
| args: IFixMisattributedActivitiesArgs, | ||
| ): Promise<void> { | ||
| // Read CSV file | ||
| const records = await activity.findActivitiesWithWrongMembers() | ||
|
|
||
| if (!records.length) { | ||
| console.log(`No activities found in the CSV file!`) | ||
| return | ||
| } | ||
|
|
||
| const startIndex = Number(args.startIndex) | ||
|
|
||
| if (!startIndex) { | ||
| console.log('something wrong with startIndex') | ||
| return | ||
| } | ||
|
|
||
| console.log(`Starting at record ${startIndex}`) | ||
|
|
||
| // Skip records that were already processed | ||
| const remainingRecords = records.slice(startIndex) | ||
|
|
||
| if (!remainingRecords.length) { | ||
| console.log(`No remaining records to process after skipping ${startIndex} records!`) | ||
| return | ||
| } | ||
|
|
||
| let processedMemberCount = 0 | ||
| const totalRecords = remainingRecords.length | ||
|
|
||
| // Process each record from CSV | ||
| for (const record of remainingRecords) { | ||
| console.log(`Updating ${record.correctMemberId} member activities updatedAt`) | ||
|
|
||
| await activity.updateMemberActivitiesUpdatedAt(record.correctMemberId) | ||
|
|
||
| processedMemberCount++ | ||
|
|
||
| if (args.testRun && processedMemberCount >= 10) { | ||
| console.log('Test run complete!') | ||
| break | ||
| } | ||
|
|
||
| console.log(`Processed ${processedMemberCount}/${totalRecords} members in the CSV file!`) | ||
| } | ||
|
|
||
| console.log('Completed processing all members!') | ||
| } |
106 changes: 106 additions & 0 deletions
106
services/libs/data-access-layer/src/old/apps/script_executor_worker/activities.repo.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,106 @@ | ||
| import { DbConnOrTx, DbConnection, DbTransaction } from '@crowd/database' | ||
| import { Logger } from '@crowd/logging' | ||
|
|
||
| import { updateActivities } from '../../../activities/update' | ||
|
|
||
| export class ActivityRepository { | ||
| constructor( | ||
| private readonly connection: DbConnection | DbTransaction, | ||
| private readonly log: Logger, | ||
| private readonly questdbSQL: DbConnOrTx, | ||
| ) {} | ||
|
|
||
| async getActivitiesWithWrongMembers( | ||
| tenantId: string, | ||
| limit = 100, | ||
| ): Promise<{ wrongMemberId: string; correctMemberId: string }[]> { | ||
| try { | ||
| return await this.connection.query( | ||
| ` | ||
| SELECT | ||
| a."memberId" as "wrongMemberId", | ||
| mi."memberId" as "correctMemberId" | ||
| FROM activities a | ||
| JOIN "memberIdentities" mi ON a.username = mi.value | ||
| AND a.platform = mi.platform | ||
| AND mi.type = 'username' | ||
| AND mi."verified" = true | ||
| AND a."tenantId" = mi."tenantId" | ||
| WHERE a."memberId" <> mi."memberId" | ||
| AND a."tenantId" = $(tenantId) | ||
| GROUP BY a."memberId", mi."memberId" | ||
| LIMIT $(limit) | ||
| `, | ||
| { | ||
| tenantId, | ||
| limit, | ||
| }, | ||
| ) | ||
| } catch (err) { | ||
| this.log.error('Error while finding activities!', err) | ||
| throw new Error(err) | ||
| } | ||
|
skwowet marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async updateActivityWithWrongMember(activityId: string, correctMemberId: string): Promise<void> { | ||
| try { | ||
| // Update the activity in pgsql to persist progress | ||
| await this.connection.none( | ||
| ` | ||
| UPDATE activities | ||
| SET "memberId" = $(correctMemberId) | ||
| WHERE id = $(activityId) | ||
| `, | ||
| { | ||
| correctMemberId, | ||
| activityId, | ||
| }, | ||
| ) | ||
|
|
||
| // Update the activity in QuestDB | ||
| await updateActivities( | ||
| this.questdbSQL, | ||
| async () => ({ memberId: correctMemberId }), | ||
| 'id = $(activityId)', | ||
| { | ||
| activityId, | ||
| }, | ||
| ) | ||
| } catch (err) { | ||
| this.log.error('Error while updating activities!', err) | ||
| throw new Error(err) | ||
| } | ||
| } | ||
|
skwowet marked this conversation as resolved.
|
||
|
|
||
| async batchUpdateActivitiesWithWrongMember( | ||
| wrongMemberId: string, | ||
| correctMemberId: string, | ||
| ): Promise<void> { | ||
| try { | ||
| await this.questdbSQL.none( | ||
| 'UPDATE "activities" SET "memberId" = $(correctMemberId) WHERE "memberId" = $(wrongMemberId)', | ||
| { | ||
| wrongMemberId, | ||
| correctMemberId, | ||
| }, | ||
| ) | ||
| } catch (err) { | ||
| this.log.error('Error while batch updating activities!', err) | ||
| throw new Error(err) | ||
| } | ||
| } | ||
|
|
||
| async updateMemberActivitiesUpdatedAt(memberId: string): Promise<void> { | ||
| try { | ||
| await this.questdbSQL.none( | ||
| 'UPDATE "activities" SET "updatedAt" = now() WHERE "memberId" = $(memberId)', | ||
| { | ||
| memberId, | ||
| }, | ||
| ) | ||
| } catch (err) { | ||
| this.log.error('Error while updating activities!', err) | ||
| throw new Error(err) | ||
| } | ||
| } | ||
| } | ||
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.