get plus summaries backend with percentages

This commit is contained in:
Kalle
2021-02-18 15:34:07 +02:00
parent a30dcc394f
commit 94bd94b8a1
2 changed files with 84 additions and 18 deletions

View File

@@ -95,20 +95,20 @@ export const navIcons: {
],
},
// { code: "tournaments", displayName: t`Tournaments`, menuItems: [] },
// {
// code: "plus",
// displayName: t`Plus Server`,
// menuItems: [
// {
// code: "plus/voting",
// displayName: t`Voting`,
// },
// { code: "plus", displayName: t`Suggested and vouched players` },
// { code: "plus/history", displayName: t`Voting history` },
// { code: "draft", displayName: t`Draft Cup` },
// { code: "plus/faq", displayName: t`FAQ` },
// ],
// },
{
code: "plus",
displayName: t`Plus Server`,
menuItems: [
// {
// code: "plus/voting",
// displayName: t`Voting`,
// },
// { code: "plus", displayName: t`Suggested and vouched players` },
{ code: "plus/history", displayName: t`Voting history` },
// { code: "draft", displayName: t`Draft Cup` },
// { code: "plus/faq", displayName: t`FAQ` },
],
},
{ code: "links", displayName: t`External links`, menuItems: [] },
];

View File

@@ -1,4 +1,4 @@
import { Prisma } from "@prisma/client";
import { PlusRegion, Prisma } from "@prisma/client";
import prisma from "prisma/client";
import { userBasicSelection } from "utils/prisma";
@@ -6,7 +6,30 @@ export type GetVotingSummariesByMonthAndTierData = Prisma.PromiseReturnType<
typeof getVotingSummariesByMonthAndTier
>;
const getVotingSummariesByMonthAndTier = ({
const getPercentageFromCounts = (
countsNA: number[],
countsEU: number[],
votedUserRegion: PlusRegion
) => {
const sameRegionArr = votedUserRegion === "NA" ? countsNA : countsEU;
const otherRegionArr = votedUserRegion === "NA" ? countsEU : countsNA;
const sameSum =
sameRegionArr[0] * 0 +
sameRegionArr[1] * 1 +
sameRegionArr[2] * 2 +
sameRegionArr[3] * 3;
const sameVoterCount = sameRegionArr.reduce((acc, cur) => acc + cur, 0);
const sameMax = sameVoterCount * 3;
const otherSum = otherRegionArr[1] * 0 + otherRegionArr[2] * 1;
const otherVoterCount = otherRegionArr.reduce((acc, cur) => acc + cur, 0);
const otherMax = otherVoterCount * 1;
return ((sameSum + otherSum) / (sameMax + otherMax)) * 100;
};
const getVotingSummariesByMonthAndTier = async ({
tier,
year,
month,
@@ -15,7 +38,7 @@ const getVotingSummariesByMonthAndTier = ({
year: number;
month: number;
}) => {
return prisma.plusVotingSummary.findMany({
const summaries = await prisma.plusVotingSummary.findMany({
where: { tier, year, month },
select: {
countsEU: true,
@@ -23,10 +46,53 @@ const getVotingSummariesByMonthAndTier = ({
wasVouched: true,
countsNA: true,
user: {
select: userBasicSelection,
select: {
...userBasicSelection,
plusStatus: {
select: {
region: true,
},
},
},
},
},
});
return summaries
.map((summary) => {
// sometimes user can change their region. This flips the user region if it is noticed that
// they received -2 or +2 from the opposite region. Sometimes user can still have wrong
// region after this func has ran but this is ok.
const fixUserRegionIfNeeded = () => {
if (
summary.user.plusStatus!.region === "NA" &&
(summary.countsEU[0] !== 0 || summary.countsEU[3] !== 0)
)
return "EU";
if (
summary.user.plusStatus!.region === "EU" &&
(summary.countsNA[0] !== 0 || summary.countsNA[3] !== 0)
)
return "NA";
return summary.user.plusStatus!.region;
};
return {
...summary,
regionForVoting: fixUserRegionIfNeeded(),
percentage: getPercentageFromCounts(
summary.countsNA,
summary.countsEU,
fixUserRegionIfNeeded()
),
};
})
.sort((a, b) => b.percentage - a.percentage)
.map((summary) => ({
...summary,
percentage: parseFloat(summary.percentage.toFixed(1)),
}));
};
const getMostRecentVotingWithResultsMonth = async () => {