From 4a6fdfd54970559fb4b8c20bcc2ad5ba08e7d1c7 Mon Sep 17 00:00:00 2001 From: limkhl Date: Thu, 4 Apr 2024 01:59:23 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=86=B5=EA=B3=84=EC=97=90=20=ED=8F=89?= =?UTF-8?q?=EA=B7=A0=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/stats/candidate/page.js | 2 +- src/app/stats/city/page.js | 49 ++++++++++++++++++++++++--------- src/app/stats/party/page.js | 46 +++++++++++++++++++++++-------- 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/src/app/stats/candidate/page.js b/src/app/stats/candidate/page.js index bd3f473..d6445dd 100644 --- a/src/app/stats/candidate/page.js +++ b/src/app/stats/candidate/page.js @@ -32,7 +32,7 @@ export default function StatsByCandidate() { return (
-

가장 질문이 많은 후보

+

가장 많은 질문을 받은 후보

{loading &&

Loading...

} {hasData &&

{candidateWithMaxRequest.name}({candidateWithMaxRequest.party}) : {candidateWithMaxRequest.requests}

}
diff --git a/src/app/stats/city/page.js b/src/app/stats/city/page.js index b5cc550..0fc156a 100644 --- a/src/app/stats/city/page.js +++ b/src/app/stats/city/page.js @@ -20,8 +20,10 @@ export default function StatsByCity() { })) const sum = requests.reduce((acc, counter) => acc + counter, 0) - return await acc + sum - }, 0) + const length = candidatesByRegions.length + const {requests: accRequests, candidates: accCandidates} = await acc + return await { requests: accRequests + sum, candidates: accCandidates + length } + }, ({requests: 0, candidates: 0})) return candidatesRequests } @@ -31,7 +33,7 @@ export default function StatsByCity() { const cities = await response.json() const citiesRequests = await Promise.all(cities.map(async ({name}) => { const requests = await fetchCandidatesRequestsByCity(name) - return { city: name, requests } + return { city: name, ...requests} })) setRequestsByCities(citiesRequests) setLoading(false) @@ -42,34 +44,55 @@ export default function StatsByCity() { }, []) const hasData = !loading && requestsByCities.length > 0 - const cityWithMaxRequest = requestsByCities.reduce((acc, {city, requests}) => { - return requests > acc.requests ? { city, requests } : acc - }, { city: '', requests: 0 }) - const cityWithMinRequest = requestsByCities.reduce((acc, {city, requests}) => { - return requests < acc.requests ?{ city, requests } : acc - }, { city: '', requests: Number.MAX_SAFE_INTEGER }) + const cityWithMaxRequest = requestsByCities.reduce((acc, cur) => { + return cur.requests > acc.requests ? cur : acc + }, { city: '', requests: Number.MIN_SAFE_INTEGER, candidates: 0 }) + const cityWithMinRequest = requestsByCities.reduce((acc, cur) => { + return cur.requests < acc.requests ? cur : acc + }, { city: '', requests: Number.MAX_SAFE_INTEGER, candidates: 0 }) + + const cityWithMaxAverage = requestsByCities.reduce((acc, cur) => { + return (cur.requests / cur.candidates) > (acc.requests / acc.candidates) ? cur : acc + }, { city: '', requests: Number.MIN_SAFE_INTEGER, candidates: 0 }) + const cityWithMinAverage = requestsByCities.reduce((acc, cur) => { + return (cur.requests / cur.candidates) < (acc.requests / acc.candidates) ? cur : acc + }, { city: '', requests: Number.MAX_SAFE_INTEGER, candidates: 0 }) + + function getAverage(requests, candidates) { + return (requests / candidates).toFixed() + } return (

지역별 질문 수

{loading &&

Loading...

} - {hasData && requestsByCities.map(({city, requests}) => ( + {hasData && requestsByCities.map(({city, requests, candidates}) => (
-

{city} : {requests}

+

{city} : {requests} / {candidates} => 후보 한 명당 평균 {getAverage(requests, candidates)}개

))}
-

가장 질문이 많은 지역

+

가장 많은 질문을 받은 지역

{loading &&

Loading...

} {hasData &&

{cityWithMaxRequest.city} : {cityWithMaxRequest.requests}

}
-

가장 질문이 적은 지역

+

가장 적은 질문을 받은 지역

{loading &&

Loading...

} {hasData &&

{cityWithMinRequest.city} : {cityWithMinRequest.requests}

}
+
+

후보 한 명당 가장 많은 질문을 받은 지역

+ {loading &&

Loading...

} + {hasData &&

{cityWithMaxAverage.city} : {getAverage(cityWithMaxAverage.requests, cityWithMaxAverage.candidates)}

} +
+
+

후보 한 명당 가장 적은 질문을 받은 지역

+ {loading &&

Loading...

} + {hasData &&

{cityWithMinAverage.city} : {getAverage(cityWithMinAverage.requests, cityWithMinAverage.candidates)}

} +
) } \ No newline at end of file diff --git a/src/app/stats/party/page.js b/src/app/stats/party/page.js index 5e6e358..f4b2cb1 100644 --- a/src/app/stats/party/page.js +++ b/src/app/stats/party/page.js @@ -16,7 +16,8 @@ export default function StatsByParty() { return data.requests })) const sum = requests.reduce((acc, counter) => acc + counter, 0) - return sum + const length = candidatesByParties.length + return {requests: sum, candidates: length} } const getRequestsByParties = async () => { @@ -25,7 +26,7 @@ export default function StatsByParty() { const parties = await response.json() const partiesRequests = await Promise.all(parties.map(async ({name}) => { const requests = await fetchCandidatesRequestsByParty(name) - return { party: name, requests } + return { party: name, ...requests } })) setRequestsByParties(partiesRequests) setLoading(false) @@ -36,34 +37,55 @@ export default function StatsByParty() { }, []) const hasData = !loading && requestsByParties.length > 0 - const partyWithMaxRequest = requestsByParties.reduce((acc, {party, requests}) => { - return requests > acc.requests ? { party, requests } : acc - }, { party: '', requests: 0 }) - const partyWithMinRequest = requestsByParties.reduce((acc, {party, requests}) => { - return requests < acc.requests ?{ party, requests } : acc - }, { party: '', requests: Number.MAX_SAFE_INTEGER }) + const partyWithMaxRequest = requestsByParties.reduce((acc, cur) => { + return cur.requests > acc.requests ? cur : acc + }, { party: '', requests: Number.MIN_SAFE_INTEGER, candidates: 0 }) + const partyWithMinRequest = requestsByParties.reduce((acc, cur) => { + return cur.requests < acc.requests ? cur : acc + }, { party: '', requests: Number.MAX_SAFE_INTEGER, candidates: 0 }) + + const partyWithMaxAverage = requestsByParties.reduce((acc, cur) => { + return (cur.requests / cur.candidates) > (acc.requests / acc.candidates) ? cur : acc + }, { party: '', requests: Number.MIN_SAFE_INTEGER, candidates: 0 }) + const partyWithMinAverage = requestsByParties.reduce((acc, cur) => { + return (cur.requests / cur.candidates) < (acc.requests / acc.candidates) ? cur : acc + }, { party: '', requests: Number.MAX_SAFE_INTEGER, candidates: 0 }) + + function getAverage(requests, candidates) { + return (requests / candidates).toFixed() + } return (

정당별 질문 수

{loading &&

Loading...

} - {hasData && requestsByParties.map(({party, requests}) => ( + {hasData && requestsByParties.map(({party, requests, candidates}) => (
-

{party} : {requests}

+

{party} : {requests} / {candidates} => 후보 한 명당 평균 {getAverage(requests, candidates)}개

))}
-

가장 질문이 많은 정당

+

가장 많은 질문을 받은 정당

{loading &&

Loading...

} {hasData &&

{partyWithMaxRequest.party} : {partyWithMaxRequest.requests}

}
-

가장 질문이 적은 정당

+

가장 적은 질문을 받은 정당

{loading &&

Loading...

} {hasData &&

{partyWithMinRequest.party} : {partyWithMinRequest.requests}

}
+
+

후보 한 명당 가장 많은 질문을 받은 정당

+ {loading &&

Loading...

} + {hasData &&

{partyWithMaxAverage.party} : {getAverage(partyWithMaxAverage.requests, partyWithMaxAverage.candidates)}

} +
+
+

후보 한 명당 가장 적은 질문을 받은 정당

+ {loading &&

Loading...

} + {hasData &&

{partyWithMinAverage.party} : {getAverage(partyWithMinAverage.requests, partyWithMinAverage.candidates)}

} +
) } \ No newline at end of file