Skip to content
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

Add optimism data queries #1146

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/repositories/donationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const findDonationById = async (
export const donationsTotalAmountPerDateRange = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(`COALESCE(SUM(donation."valueUsd"), 0)`, 'sum')
Expand All @@ -127,6 +128,11 @@ export const donationsTotalAmountPerDateRange = async (
if (toDate) {
query.andWhere(`donation."createdAt" <= '${toDate}'`);
}

if (fromOptimism) {
query.andWhere(`donation."transactionNetworkId" = 10`);
}

const donationsUsdAmount = await query.getRawOne();

query.cache(
Expand All @@ -140,6 +146,7 @@ export const donationsTotalAmountPerDateRange = async (
export const donationsTotalAmountPerDateRangeByMonth = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -156,6 +163,10 @@ export const donationsTotalAmountPerDateRangeByMonth = async (
query.andWhere(`donation."createdAt" <= '${toDate}'`);
}

if (fromOptimism) {
query.andWhere(`donation."transactionNetworkId" = 10`);
}

query.groupBy('year, month');
query.orderBy('year', 'ASC');
query.addOrderBy('month', 'ASC');
Expand All @@ -171,6 +182,7 @@ export const donationsTotalAmountPerDateRangeByMonth = async (
export const donationsNumberPerDateRange = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(`COALESCE(COUNT(donation."valueUsd"), 0)`, 'count')
Expand All @@ -183,6 +195,11 @@ export const donationsNumberPerDateRange = async (
if (toDate) {
query.andWhere(`donation."createdAt" <= '${toDate}'`);
}

if (fromOptimism) {
query.andWhere(`donation."transactionNetworkId" = 10`);
}

const donationsUsdAmount = await query.getRawOne();

query.cache(
Expand All @@ -196,6 +213,7 @@ export const donationsNumberPerDateRange = async (
export const donationsTotalNumberPerDateRangeByMonth = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -212,6 +230,10 @@ export const donationsTotalNumberPerDateRangeByMonth = async (
query.andWhere(`donation."createdAt" <= '${toDate}'`);
}

if (fromOptimism) {
query.andWhere(`donation."transactionNetworkId" = 10`);
}

query.groupBy('year, month');
query.orderBy('year', 'ASC');
query.addOrderBy('month', 'ASC');
Expand All @@ -227,6 +249,7 @@ export const donationsTotalNumberPerDateRangeByMonth = async (
export const donorsCountPerDate = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<number> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -243,6 +266,10 @@ export const donorsCountPerDate = async (
query.andWhere(`donation."createdAt" <= '${toDate}'`);
}

if (fromOptimism) {
query.andWhere(`donation."transactionNetworkId" = 10`);
}

query.cache(`donorsCountPerDate-${fromDate || ''}-${toDate || ''}`, 300000);

const queryResult = await query.getRawOne();
Expand All @@ -252,6 +279,7 @@ export const donorsCountPerDate = async (
export const donorsCountPerDateByMonthAndYear = async (
fromDate?: string,
toDate?: string,
fromOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Donation.createQueryBuilder('donation')
.select(
Expand All @@ -267,6 +295,10 @@ export const donorsCountPerDateByMonthAndYear = async (
query.andWhere(`donation."createdAt" <= '${toDate}'`);
}

if (fromOptimism) {
query.andWhere(`donation."transactionNetworkId" = 10`);
}

query.groupBy('year, month');
query.orderBy('year', 'ASC');
query.addOrderBy('month', 'ASC');
Expand Down
32 changes: 32 additions & 0 deletions src/repositories/projectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ export const userIsOwnerOfProject = async (
export const totalProjectsPerDate = async (
fromDate?: string,
toDate?: string,
verified?: boolean,
includesOptimism?: boolean,
): Promise<number> => {
const query = Project.createQueryBuilder('project');

Expand All @@ -338,6 +340,20 @@ export const totalProjectsPerDate = async (
query.andWhere(`project."creationDate" <= '${toDate}'`);
}

if (verified) {
query.andWhere(
`project."verified" = true AND project."reviewStatus" = 'Listed'`,
);
}

if (includesOptimism) {
query.innerJoin(
`project.addresses`,
'addresses',
'addresses."networkId" = 10',
);
}

query.cache(`totalProjectPerDate-${fromDate || ''}-${toDate || ''}`, 300000);

return await query.getCount();
Expand All @@ -346,6 +362,8 @@ export const totalProjectsPerDate = async (
export const totalProjectsPerDateByMonthAndYear = async (
fromDate?: string,
toDate?: string,
verified?: boolean,
includesOptimism?: boolean,
): Promise<ResourcesTotalPerMonthAndYear[]> => {
const query = Project.createQueryBuilder('project').select(
`COUNT(project.id) as total, EXTRACT(YEAR from project."creationDate") as year, EXTRACT(MONTH from project."creationDate") as month, CONCAT(CAST(EXTRACT(YEAR from project."creationDate") as VARCHAR), '/', CAST(EXTRACT(MONTH from project."creationDate") as VARCHAR)) as date`,
Expand All @@ -359,6 +377,20 @@ export const totalProjectsPerDateByMonthAndYear = async (
query.andWhere(`project."creationDate" <= '${toDate}'`);
}

if (verified) {
query.andWhere(
`project."verified" = true AND project."reviewStatus" = 'Listed'`,
);
}

if (includesOptimism) {
query.innerJoin(
`project.addresses`,
'addresses',
'addresses."networkId" = 10',
);
}

query.groupBy('year, month');
query.orderBy('year', 'ASC');
query.addOrderBy('month', 'ASC');
Expand Down
30 changes: 25 additions & 5 deletions src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,24 @@ export class DonationResolver {
// fromDate and toDate should be in this format YYYYMMDD HH:mm:ss
@Arg('fromDate', { nullable: true }) fromDate?: string,
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('fromOptimism', { nullable: true }) fromOptimism?: boolean,
): Promise<ResourcePerDateRange> {
try {
validateWithJoiSchema(
{ fromDate, toDate },
resourcePerDateReportValidator,
);
const total = await donationsTotalAmountPerDateRange(fromDate, toDate);
const total = await donationsTotalAmountPerDateRange(
fromDate,
toDate,
fromOptimism,
);
const totalPerMonthAndYear =
await donationsTotalAmountPerDateRangeByMonth(fromDate, toDate);
await donationsTotalAmountPerDateRangeByMonth(
fromDate,
toDate,
fromOptimism,
);

return {
total,
Expand All @@ -317,15 +326,24 @@ export class DonationResolver {
// fromDate and toDate should be in this format YYYYMMDD HH:mm:ss
@Arg('fromDate', { nullable: true }) fromDate?: string,
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('fromOptimism', { nullable: true }) fromOptimism?: boolean,
): Promise<ResourcePerDateRange> {
try {
validateWithJoiSchema(
{ fromDate, toDate },
resourcePerDateReportValidator,
);
const total = await donationsNumberPerDateRange(fromDate, toDate);
const total = await donationsNumberPerDateRange(
fromDate,
toDate,
fromOptimism,
);
const totalPerMonthAndYear =
await donationsTotalNumberPerDateRangeByMonth(fromDate, toDate);
await donationsTotalNumberPerDateRangeByMonth(
fromDate,
toDate,
fromOptimism,
);

return {
total,
Expand Down Expand Up @@ -354,16 +372,18 @@ export class DonationResolver {
// fromDate and toDate should be in this format YYYYMMDD HH:mm:ss
@Arg('fromDate', { nullable: true }) fromDate?: string,
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('fromOptimism', { nullable: true }) fromOptimism?: boolean,
): Promise<ResourcePerDateRange> {
try {
validateWithJoiSchema(
{ fromDate, toDate },
resourcePerDateReportValidator,
);
const total = await donorsCountPerDate(fromDate, toDate);
const total = await donorsCountPerDate(fromDate, toDate, fromOptimism);
const totalPerMonthAndYear = await donorsCountPerDateByMonthAndYear(
fromDate,
toDate,
fromOptimism,
);
return {
total,
Expand Down
11 changes: 10 additions & 1 deletion src/resolvers/projectResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,16 +1197,25 @@ export class ProjectResolver {
// fromDate and toDate should be in this format YYYYMMDD HH:mm:ss
@Arg('fromDate', { nullable: true }) fromDate?: string,
@Arg('toDate', { nullable: true }) toDate?: string,
@Arg('verified', { nullable: true }) verified?: boolean,
@Arg('includesOptimism', { nullable: true }) includesOptimism?: boolean,
): Promise<ResourcePerDateRange> {
try {
validateWithJoiSchema(
{ fromDate, toDate },
resourcePerDateReportValidator,
);
const total = await totalProjectsPerDate(fromDate, toDate);
const total = await totalProjectsPerDate(
fromDate,
toDate,
verified,
includesOptimism,
);
const totalPerMonthAndYear = await totalProjectsPerDateByMonthAndYear(
fromDate,
toDate,
verified,
includesOptimism,
);

return {
Expand Down
10 changes: 10 additions & 0 deletions test/graphqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,14 @@ export const fetchNewProjectsPerDate = `
query (
$fromDate: String
$toDate: String
$verified: Boolean
$includesOptimism: Boolean
) {
projectsPerDate(
fromDate: $fromDate
toDate: $toDate
verified: $verified
includesOptimism: $includesOptimism
) {
total
totalPerMonthAndYear {
Expand Down Expand Up @@ -399,10 +403,12 @@ export const fetchTotalDonors = `
query (
$fromDate: String
$toDate: String
$fromOptimism: Boolean
) {
totalDonorsCountPerDate(
fromDate: $fromDate
toDate: $toDate
fromOptimism: $fromOptimism
) {
total
totalPerMonthAndYear {
Expand All @@ -417,10 +423,12 @@ export const fetchTotalDonationsUsdAmount = `
query (
$fromDate: String
$toDate: String
$fromOptimism: Boolean
) {
donationsTotalUsdPerDate (
fromDate: $fromDate
toDate: $toDate
fromOptimism: $fromOptimism
) {
total
totalPerMonthAndYear {
Expand All @@ -435,10 +443,12 @@ export const fetchTotalDonationsNumberPerDateRange = `
query (
$fromDate: String
$toDate: String
$fromOptimism: Boolean
) {
totalDonationsNumberPerDate (
fromDate: $fromDate
toDate: $toDate
fromOptimism: $fromOptimism
) {
total
totalPerMonthAndYear {
Expand Down