2021-08-22 20:16:04 +02:00
|
|
|
import { format } from 'date-fns'
|
|
|
|
|
2021-09-25 11:28:40 +02:00
|
|
|
import { IChartDataset } from '@/types/chart'
|
2021-08-22 21:10:47 +02:00
|
|
|
import { ISport } from '@/types/sports'
|
2021-08-22 20:16:04 +02:00
|
|
|
import {
|
|
|
|
IStatisticsChartData,
|
|
|
|
IStatisticsDateParams,
|
2021-09-26 08:59:17 +02:00
|
|
|
TStatisticsDatasetKeys,
|
2021-08-22 20:16:04 +02:00
|
|
|
TStatisticsDatasets,
|
|
|
|
TStatisticsFromApi,
|
|
|
|
} from '@/types/statistics'
|
|
|
|
import { incrementDate, startDate } from '@/utils/dates'
|
2021-09-20 12:18:40 +02:00
|
|
|
import { sportColors } from '@/utils/sports'
|
2021-08-22 20:16:04 +02:00
|
|
|
|
|
|
|
// date format from api
|
2021-09-25 11:28:40 +02:00
|
|
|
const dateFormats: Record<string, string> = {
|
2021-08-22 20:16:04 +02:00
|
|
|
week: 'yyyy-MM-dd',
|
|
|
|
month: 'yyyy-MM',
|
|
|
|
year: 'yyyy',
|
|
|
|
}
|
|
|
|
|
2021-09-26 08:59:17 +02:00
|
|
|
export const datasetKeys: TStatisticsDatasetKeys[] = [
|
2021-09-04 21:36:59 +02:00
|
|
|
'nb_workouts',
|
|
|
|
'total_duration',
|
|
|
|
'total_distance',
|
|
|
|
]
|
2021-08-22 20:16:04 +02:00
|
|
|
|
|
|
|
export const getDateKeys = (
|
|
|
|
params: IStatisticsDateParams,
|
|
|
|
weekStartingMonday: boolean
|
|
|
|
): Date[] => {
|
|
|
|
const days = []
|
|
|
|
for (
|
|
|
|
let day = startDate(params.duration, params.start, weekStartingMonday);
|
|
|
|
day <= params.end;
|
|
|
|
day = incrementDate(params.duration, day)
|
|
|
|
) {
|
|
|
|
days.push(day)
|
|
|
|
}
|
|
|
|
return days
|
|
|
|
}
|
|
|
|
|
|
|
|
const getStatisticsChartDataset = (
|
2021-09-04 19:40:27 +02:00
|
|
|
sportLabel: string,
|
|
|
|
color: string
|
2021-09-25 11:28:40 +02:00
|
|
|
): IChartDataset => {
|
2021-09-21 14:49:49 +02:00
|
|
|
return {
|
|
|
|
label: sportLabel,
|
|
|
|
backgroundColor: [color],
|
|
|
|
data: [],
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 20:16:04 +02:00
|
|
|
|
|
|
|
export const getDatasets = (displayedSports: ISport[]): TStatisticsDatasets => {
|
|
|
|
const datasets: TStatisticsDatasets = {
|
|
|
|
nb_workouts: [],
|
|
|
|
total_distance: [],
|
|
|
|
total_duration: [],
|
|
|
|
}
|
|
|
|
displayedSports.map((sport) => {
|
2021-09-04 19:40:27 +02:00
|
|
|
const color = sportColors[sport.label]
|
|
|
|
datasets.nb_workouts.push(getStatisticsChartDataset(sport.label, color))
|
|
|
|
datasets.total_distance.push(getStatisticsChartDataset(sport.label, color))
|
|
|
|
datasets.total_duration.push(getStatisticsChartDataset(sport.label, color))
|
2021-08-22 20:16:04 +02:00
|
|
|
})
|
|
|
|
return datasets
|
|
|
|
}
|
|
|
|
|
|
|
|
export const formatStats = (
|
|
|
|
params: IStatisticsDateParams,
|
|
|
|
weekStartingMonday: boolean,
|
|
|
|
sports: ISport[],
|
|
|
|
displayedSportsId: number[],
|
|
|
|
apiStats: TStatisticsFromApi
|
|
|
|
): IStatisticsChartData => {
|
|
|
|
const dayKeys = getDateKeys(params, weekStartingMonday)
|
|
|
|
const dateFormat = dateFormats[params.duration]
|
|
|
|
const displayedSports = sports.filter((sport) =>
|
2021-10-03 19:23:17 +02:00
|
|
|
displayedSportsId.includes(sport.id)
|
2021-08-22 20:16:04 +02:00
|
|
|
)
|
|
|
|
const labels: string[] = []
|
|
|
|
const datasets = getDatasets(displayedSports)
|
|
|
|
const sportsId: Record<string, number> = {}
|
|
|
|
displayedSports.map(
|
|
|
|
(displayedSport) => (sportsId[displayedSport.label] = displayedSport.id)
|
|
|
|
)
|
|
|
|
|
|
|
|
dayKeys.map((key) => {
|
|
|
|
const date: string = format(key, dateFormat)
|
|
|
|
labels.push(date)
|
2021-09-04 21:36:59 +02:00
|
|
|
datasetKeys.map((datasetKey) => {
|
2021-09-04 19:40:27 +02:00
|
|
|
datasets[datasetKey].map((dataset) => {
|
2021-08-22 20:16:04 +02:00
|
|
|
dataset.data.push(
|
|
|
|
apiStats !== {} &&
|
|
|
|
date in apiStats &&
|
|
|
|
sportsId[dataset.label] in apiStats[date]
|
|
|
|
? apiStats[date][sportsId[dataset.label]][datasetKey]
|
2021-09-04 19:40:27 +02:00
|
|
|
: 0
|
2021-08-22 20:16:04 +02:00
|
|
|
)
|
2021-09-04 19:40:27 +02:00
|
|
|
})
|
2021-08-22 20:16:04 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
labels,
|
|
|
|
datasets,
|
|
|
|
}
|
|
|
|
}
|