From 4850072bf889a1e6e5ee2ab75af273ee63fa32b9 Mon Sep 17 00:00:00 2001 From: happycastle <41810556+happycastle114@users.noreply.github.com> Date: Wed, 8 Nov 2023 21:14:19 +0900 Subject: [PATCH] Add: Diversity --- src/controllers/scrapResult.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/controllers/scrapResult.ts b/src/controllers/scrapResult.ts index 9567028..a41f64f 100644 --- a/src/controllers/scrapResult.ts +++ b/src/controllers/scrapResult.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import { ScrapResultModel } from "@/db/model"; +import { gini_simpson, shannon } from "@/utils/diversity"; export const getScrapResult = async (req: Request, res: Response) => { try { @@ -12,17 +13,20 @@ export const getScrapResult = async (req: Request, res: Response) => { }; export const getDiversity = async (req: Request, res: Response) => { - try { - const scrapResults = await ScrapResultModel.find({}); - const diversity = scrapResults.map(scrapResult => { - return { - name: scrapResult.name, - diversity: scrapResult.diversity, - }; - }); - res.status(200).send(diversity); - } catch (err) { - console.log(err); - res.status(500).send("scrapResult/getDiversity: Internal Server Error"); + const { type, id } = req.params; + + const scrapResults = await ScrapResultModel.find({ council_id: id }); + + if (scrapResults.length === 0) { + res.status(404).send("scrapResult/getDiversity: Not Found"); + return; } + + const type_list = scrapResults[0].councilors.reduce((acc: string[], cur) => { + return acc.concat([cur.party]); + }, []); + + const diversity = gini_simpson(type_list); + + res.status(200).send({ diversity }); };