-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Index project type #2285
Merged
Merged
Index project type #2285
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
39cc28b
Index project type frontend changes
hlbmtc d58f052
Index project type backend changes
hlbmtc e5c77b9
- Added `ProjectIndexQuestion` model
hlbmtc 3553566
Index project changes FE
hlbmtc 328622e
Merge remote-tracking branch 'origin/feat/2080-index-project-type' in…
hlbmtc 0752322
Adjusted link generation
hlbmtc e349d4a
Small admin fix
hlbmtc b484e6e
Merge branch 'main' into feat/2080-index-project-type
hlbmtc b72cec9
Conflicts resolution
hlbmtc 8722ab3
- Added `/index/:slug` page url
hlbmtc e9a8af4
Merge branch 'main' into feat/2080-index-project-type
hlbmtc 064784b
Merge branch 'main' into feat/2080-index-project-type
hlbmtc 4af825e
Translations
hlbmtc e40194c
Merge branch 'main' into feat/2080-index-project-type
hlbmtc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
front_end/src/app/(main)/(tournaments)/tournament/components/index/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { fromUnixTime, subWeeks } from "date-fns"; | ||
import { useTranslations } from "next-intl"; | ||
import React, { FC } from "react"; | ||
|
||
import WeeklyMovement from "@/components/weekly_movement"; | ||
import { ProjectIndexWeights } from "@/types/projects"; | ||
import { QuestionType } from "@/types/question"; | ||
import { scaleInternalLocation } from "@/utils/charts"; | ||
|
||
import IndexQuestionsTable from "./index_questions_table"; | ||
|
||
import "./styles.css"; | ||
|
||
type Props = { | ||
indexWeights: ProjectIndexWeights[]; | ||
}; | ||
|
||
const IndexSection: FC<Props> = ({ indexWeights }) => { | ||
const t = useTranslations(); | ||
|
||
const { index: indexValue, indexWeekAgo } = calculateIndex(indexWeights); | ||
const indexWeeklyMovement = Number((indexValue - indexWeekAgo).toFixed(1)); | ||
|
||
return ( | ||
<IndexQuestionsTable | ||
indexWeights={indexWeights} | ||
HeadingSection={ | ||
<div className="flex flex-col items-center border-b border-gray-300 bg-blue-100 px-4 py-4 text-center leading-4 dark:border-gray-300-dark dark:bg-blue-100-dark"> | ||
<p className="m-0 mb-2 text-3xl capitalize leading-9"> | ||
{t.rich("indexScore", { | ||
value: Number(indexValue.toFixed(1)), | ||
bold: (chunks) => <b>{chunks}</b>, | ||
})} | ||
</p> | ||
<WeeklyMovement | ||
weeklyMovement={indexWeeklyMovement} | ||
message={t("weeklyMovementChange", { | ||
value: | ||
indexWeeklyMovement === 0 | ||
? t("noChange") | ||
: Math.abs(indexWeeklyMovement), | ||
})} | ||
className="text-base" | ||
iconClassName="text-base" | ||
/> | ||
</div> | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
function calculateIndex(posts: ProjectIndexWeights[]): { | ||
index: number; | ||
indexWeekAgo: number; | ||
} { | ||
const weightSum = posts.reduce((acc, post) => acc + post.weight, 0); | ||
if (weightSum === 0) { | ||
return { index: 0, indexWeekAgo: 0 }; | ||
} | ||
const dateNow = new Date(); | ||
const weekAgoDate = subWeeks(dateNow, 1); | ||
|
||
const { scoreSum, weeklyScoreSum } = posts.reduce( | ||
(acc, obj) => { | ||
const question = | ||
obj.post.question || | ||
obj.post.group_of_questions?.questions?.find( | ||
(q) => obj.question_id === q.id | ||
); | ||
|
||
if (!question) { | ||
return acc; | ||
} | ||
|
||
const latestAggregation = question.aggregations.recency_weighted.latest; | ||
const historyAggregation = question.aggregations.recency_weighted.history; | ||
if (!latestAggregation) { | ||
return acc; | ||
} | ||
|
||
let postValue = 0; | ||
let postValueWeekAgo = 0; | ||
const cp = latestAggregation.centers?.at(-1); | ||
const weekAgoCP = | ||
historyAggregation.find( | ||
(el) => | ||
el.end_time && | ||
fromUnixTime(el.end_time) >= | ||
fromUnixTime(weekAgoDate.getTime() / 1000) | ||
)?.centers?.[0] ?? null; | ||
|
||
switch (question.type) { | ||
case QuestionType.Binary: { | ||
if (!cp) { | ||
break; | ||
} | ||
|
||
const median = scaleInternalLocation(cp, { | ||
range_min: 0, | ||
range_max: 100, | ||
zero_point: null, | ||
}); | ||
postValue = 2 * median - 1; | ||
|
||
const medianWeekAgo = scaleInternalLocation(weekAgoCP ?? cp, { | ||
range_min: 0, | ||
range_max: 100, | ||
zero_point: null, | ||
}); | ||
postValueWeekAgo = 2 * medianWeekAgo - 1; | ||
break; | ||
} | ||
case QuestionType.Numeric: { | ||
const scaling = question.scaling; | ||
const min = scaling.range_min; | ||
const max = scaling.range_max; | ||
if (!min || !max) { | ||
break; | ||
} | ||
|
||
if (!cp) { | ||
break; | ||
} | ||
const median = scaleInternalLocation(cp, scaling); | ||
postValue = (2 * median - max - min) / (max - min); | ||
|
||
const medianWeekAgo = scaleInternalLocation(weekAgoCP ?? cp, scaling); | ||
postValueWeekAgo = (2 * medianWeekAgo - max - min) / (max - min); | ||
break; | ||
} | ||
} | ||
|
||
return { | ||
scoreSum: acc.scoreSum + obj.weight * postValue, | ||
weeklyScoreSum: acc.weeklyScoreSum + obj.weight * postValueWeekAgo, | ||
}; | ||
}, | ||
{ scoreSum: 0, weeklyScoreSum: 0 } | ||
); | ||
|
||
return { | ||
index: scoreSum / weightSum, | ||
indexWeekAgo: weeklyScoreSum / weightSum, | ||
}; | ||
} | ||
|
||
export default IndexSection; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add translation for other locales as well