Skip to content

Commit

Permalink
Merge pull request #134 from Giveth/add-endpoint-for-analytics-dashboard
Browse files Browse the repository at this point in the history
Add endpoint for analytics dashboard
  • Loading branch information
MohammadPCh authored Oct 16, 2024
2 parents 72f9e10 + 7c59fa0 commit d7849fd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/server-extension/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ export const getProjectSortBy = (sortBy: EProjectSort) => {
return { sortBy: "vouch", order: "DESC" };
}
};

export function isValidDate(dateStr: string): boolean {
if (typeof dateStr !== "string" || dateStr.trim() === "") {
return false;
}
const date = new Date(dateStr);
return !Number.isNaN(date.getTime());
}
74 changes: 74 additions & 0 deletions src/server-extension/organization-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import "reflect-metadata";
import { Arg, Query, Resolver } from "type-graphql";
import type { EntityManager } from "typeorm";
import { VouchCountPerMonth, VouchCountResult } from "./types";
import { isValidDate } from "./helper";

@Resolver()
export class OrganisationResolver {
constructor(private tx: () => Promise<EntityManager>) {}

@Query(() => VouchCountResult)
async getOrganisationVouchCountByDate(
@Arg("organisationId", () => String) organisationId: string,
@Arg("fromDate", () => String) fromDate: string,
@Arg("toDate", () => String) toDate: string
): Promise<VouchCountResult> {
if (!isValidDate(fromDate) || !isValidDate(toDate)) {
throw new Error(
"Invalid date format. Dates must be in YYYY-MM-DD format."
);
}

const from = new Date(fromDate);
const to = new Date(toDate);
if (from > to) {
throw new Error("`fromDate` cannot be later than `toDate`.");
}
try {
const manager = await this.tx();

const queryPerMonth = `
SELECT
to_char(project_attestation.attest_timestamp, 'YYYY-MM') AS date,
COUNT(*) AS total_count,
SUM(CASE WHEN project_attestation.comment IS NOT NULL AND project_attestation.comment != '' THEN 1 ELSE 0 END) AS count_with_comments
FROM
project_attestation
LEFT JOIN attestor_organisation
ON project_attestation.attestor_organisation_id = attestor_organisation.id
WHERE
attestor_organisation.organisation_id = $1
AND project_attestation.vouch = true
AND project_attestation.attest_timestamp BETWEEN $2 AND $3
GROUP BY
to_char(project_attestation.attest_timestamp, 'YYYY-MM')
ORDER BY
date;
`;

const resultPerMonth = await manager.query(queryPerMonth, [
organisationId,
fromDate,
toDate,
]);

const totalPerMonth: VouchCountPerMonth[] = resultPerMonth.map(
(row: any) => ({
date: row.date,
totalCount: row.total_count,
countWithComments: row.count_with_comments,
countWithoutComments: row.total_count - row.count_with_comments,
})
);

return {
total: totalPerMonth.reduce((acc, row) => acc + row.totalCount, 0),
totalPerMonth,
};
} catch (error: any) {
console.error("Error fetching vouch count by date:", error);
throw new Error(`Failed to fetch vouch count by date: ${error.message}`);
}
}
}
File renamed without changes.
3 changes: 2 additions & 1 deletion src/server-extension/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { ProjectResolver } from "../resolver";
export { ProjectResolver } from "../project-resolver";
export { OrganisationResolver } from "../organization-resolver";
24 changes: 24 additions & 0 deletions src/server-extension/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,27 @@ export class ProjectsSortedByVouchOrFlagType {
@Field(() => ID)
id!: string;
}

@ObjectType()
export class VouchCountResult {
@Field()
total: number = 0;

@Field(() => [VouchCountPerMonth])
totalPerMonth: VouchCountPerMonth[] = [];
}

@ObjectType()
export class VouchCountPerMonth {
@Field()
date: string = "";

@Field()
totalCount: number = 0;

@Field()
countWithComments: number = 0;

@Field()
countWithoutComments: number = 0;
}

0 comments on commit d7849fd

Please sign in to comment.