From 1553b52094199791c0fb92f893d1d52399d195b2 Mon Sep 17 00:00:00 2001 From: gnomadic Date: Mon, 23 Sep 2024 17:12:45 -0400 Subject: [PATCH 1/2] first pass combined metrics --- src/server/api/routers/projects.ts | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/server/api/routers/projects.ts b/src/server/api/routers/projects.ts index 1698afb4..6fa59c7c 100644 --- a/src/server/api/routers/projects.ts +++ b/src/server/api/routers/projects.ts @@ -273,6 +273,48 @@ export const projectsRouter = createTRPCRouter({ projectIds: approvedIds, }); + const combinedMetrics: Record> = {}; + 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; @@ -289,14 +331,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({ From e75a1420b0900518ee5f7640aa61ae4524a194a5 Mon Sep 17 00:00:00 2001 From: gnomadic Date: Tue, 24 Sep 2024 08:40:15 -0400 Subject: [PATCH 2/2] iterating --- src/server/api/routers/projects.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/server/api/routers/projects.ts b/src/server/api/routers/projects.ts index 6fa59c7c..321c8bcc 100644 --- a/src/server/api/routers/projects.ts +++ b/src/server/api/routers/projects.ts @@ -299,14 +299,12 @@ export const projectsRouter = createTRPCRouter({ if (combined![k]) { combined![k] = existing + toAdd; } - } } } } } - });