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

[DO NOT MERGE] first pass combined metrics #173

Open
wants to merge 2 commits into
base: impact-metrics
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions src/server/api/routers/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,46 @@ export const projectsRouter = createTRPCRouter({
projectIds: approvedIds,
});

const combinedMetrics: Record<string, Partial<OSOMetricsCSV>> = {};
approvedApplications.map((a) => {

const dupes = approvedApplications.filter((b) => b.recipient === a.recipient);
dupes.map((dupe) => {
if (metricsByProjectId[dupe.id]) {
//project has metrics

const metrics = metricsByProjectId[dupe.id]!;
let k: keyof OSOMetricsCSV;
for (k in metrics) {
// for each metric, if it exists in combined then add to it else just put it there.
// also, only if it's a number. if it's a string
if (typeof metrics[k] === "number") {
if (!combinedMetrics[dupe.id]) {
// first add
combinedMetrics[dupe.id] = metrics;
} else {
// nth add
if (combinedMetrics[dupe.id]![k]) {
let existing = combinedMetrics[dupe.id]![k] as number;
let toAdd = metrics[k] as number;
let combined = combinedMetrics[dupe.id];
if (combined![k]) {
combined![k] = existing + toAdd;
}
}
}

}
}
}
});







const projectsResult: Array<
Attestation & {
metrics?: Partial<OSOMetricsCSV>;
Expand All @@ -289,14 +329,21 @@ export const projectsRouter = createTRPCRouter({
const { id: projectId } = project;
const metrics = metricsByProjectId[projectId];
const metadata = metadataByProjectId[projectId];
const combined = combinedMetrics[projectId];

return {
...project,
metadata,
metrics,
combinedMetrics: combined,
nextPage: cursor + 1,
};
});





return projectsResult;
} catch (error) {
throw new TRPCError({
Expand Down