-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1726 from Giveth/release-optimize-donation-box-me…
…trics Release optimize donation box metrics
- Loading branch information
Showing
11 changed files
with
170 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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
22 changes: 22 additions & 0 deletions
22
migration/1721260881923-addDonationPercentageToDonation.ts
This file contains 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,22 @@ | ||
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm'; | ||
|
||
export class AddDonationPercentageToDonation1721260881923 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.addColumn( | ||
'donation', | ||
new TableColumn({ | ||
name: 'donationPercentage', | ||
type: 'decimal', | ||
precision: 5, | ||
scale: 2, | ||
isNullable: true, | ||
}), | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropColumn('donation', 'donationPercentage'); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
migration/1721261762331-fillDonationPercentageInDonation.ts
This file contains 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,72 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import config from '../src/config'; | ||
|
||
interface DonationUpdate { | ||
id: number; | ||
donationPercentage: number; | ||
} | ||
|
||
export class FillDonationPercentageInDonation1721261762331 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const environment = config.get('ENVIRONMENT') as string; | ||
|
||
if (environment === 'test') { | ||
return; | ||
} | ||
|
||
// Load all donations with useDonationBox set to true | ||
const donations = await queryRunner.query( | ||
`SELECT id, "userId", "projectId", "createdAt", "transactionId", amount, "relevantDonationTxHash" | ||
FROM donation WHERE "useDonationBox" = true`, | ||
); | ||
|
||
// Calculate the donation percentages | ||
const updates: DonationUpdate[] = []; | ||
|
||
for (const donation of donations) { | ||
if (donation.projectId === 1 && donation.relevantDonationTxHash) { | ||
const relevantDonation = donations.find( | ||
d => d.transactionId === donation.relevantDonationTxHash, | ||
); | ||
|
||
if (relevantDonation) { | ||
const totalAmount = donation.amount + relevantDonation.amount; | ||
const donationPercentage = (donation.amount / totalAmount) * 100; | ||
|
||
updates.push({ | ||
id: donation.id, | ||
donationPercentage, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// Perform batch update using a single query | ||
if (updates.length > 0) { | ||
const updateQuery = updates | ||
.map(update => `(${update.id}, ${update.donationPercentage})`) | ||
.join(', '); | ||
|
||
await queryRunner.query( | ||
` | ||
UPDATE donation AS d | ||
SET "donationPercentage" = u."donationPercentage" FROM (VALUES ${updateQuery}) AS u(id | ||
, "donationPercentage") | ||
WHERE d.id = u.id; | ||
`, | ||
); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
` | ||
UPDATE donation | ||
SET "donationPercentage" = NULL | ||
WHERE "donationPercentage" IS NOT NULL; | ||
`, | ||
); | ||
} | ||
} |
This file contains 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,19 @@ | ||
import { MigrationInterface, QueryRunner, TableIndex } from 'typeorm'; | ||
|
||
export class AddIndexToUseDonationBox1721841071921 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.createIndex( | ||
'donation', | ||
new TableIndex({ | ||
name: 'IDX_USE_DONATION_BOX', | ||
columnNames: ['useDonationBox'], | ||
}), | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropIndex('donation', 'IDX_USE_DONATION_BOX'); | ||
} | ||
} |
This file contains 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 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 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 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 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.