Skip to content

Commit

Permalink
Add: Diversity
Browse files Browse the repository at this point in the history
  • Loading branch information
happycastle114 authored and Re-st committed Nov 13, 2023
1 parent e8e82a6 commit 4850072
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/controllers/scrapResult.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 });
};

0 comments on commit 4850072

Please sign in to comment.