FitTrackee/fittrackee_client/src/utils/stats.js

111 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-01-04 10:07:24 +01:00
import {
addDays,
addMonths,
2019-08-28 15:35:22 +02:00
addYears,
format,
startOfMonth,
2019-01-04 10:07:24 +01:00
startOfWeek,
2019-08-28 15:35:22 +02:00
startOfYear,
2019-01-04 10:07:24 +01:00
} from 'date-fns'
const xAxisFormats = [
{ duration: 'week', dateFormat: 'yyyy-MM-dd', xAxis: 'dd/MM' },
{ duration: 'month', dateFormat: 'yyyy-MM', xAxis: 'MM/yyyy' },
{ duration: 'year', dateFormat: 'yyyy', xAxis: 'yyyy' },
2019-01-04 10:07:24 +01:00
]
export const formatDuration = (totalSeconds, formatWithDay = false) => {
let days = '0'
if (formatWithDay) {
days = String(Math.floor(totalSeconds / 86400))
totalSeconds %= 86400
}
const hours = String(Math.floor(totalSeconds / 3600)).padStart(2, '0')
totalSeconds %= 3600
const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, '0')
const seconds = String(totalSeconds % 60).padStart(2, '0')
if (formatWithDay) {
2019-08-28 15:35:22 +02:00
return `${days === '0' ? '' : `${days}d:`}${
2019-01-04 10:07:24 +01:00
hours === '00' ? '' : `${hours}h:`
}${minutes}m:${seconds}s`
}
return `${hours === '00' ? '' : `${hours}:`}${minutes}:${seconds}`
}
2019-08-28 15:35:22 +02:00
export const formatValue = (displayedData, value) =>
value === 0
2019-01-04 11:10:07 +01:00
? ''
: displayedData === 'distance'
2019-08-28 15:35:22 +02:00
? `${value.toFixed(2)} km`
: displayedData === 'duration'
? formatDuration(value)
: value
2019-01-04 11:10:07 +01:00
2019-01-04 10:07:24 +01:00
const dateIncrement = (duration, day) => {
switch (duration) {
case 'week':
return addDays(day, 7)
case 'year':
return addYears(day, 1)
case 'month':
default:
return addMonths(day, 1)
}
}
const startDate = (duration, day, weekm) => {
2019-01-04 10:07:24 +01:00
switch (duration) {
case 'week':
return startOfWeek(day, { weekStartsOn: weekm ? 1 : 0 })
2019-01-04 10:07:24 +01:00
case 'year':
return startOfYear(day)
case 'month':
default:
return startOfMonth(day)
}
}
export const formatStats = (stats, sports, params, displayedSports, weekm) => {
const nbWorkoutsStats = []
2019-01-04 10:07:24 +01:00
const distanceStats = []
const durationStats = []
2019-08-28 15:35:22 +02:00
for (
let day = startDate(params.duration, params.start, weekm);
2019-08-28 15:35:22 +02:00
day <= params.end;
day = dateIncrement(params.duration, day)
2019-01-04 10:07:24 +01:00
) {
const [xAxisFormat] = xAxisFormats.filter(
x => x.duration === params.duration
)
const date = format(day, xAxisFormat.dateFormat)
const xAxis = format(day, xAxisFormat.xAxis)
const dataNbWorkouts = { date: xAxis }
2019-01-04 10:07:24 +01:00
const dataDistance = { date: xAxis }
const dataDuration = { date: xAxis }
if (stats[date]) {
2019-08-28 15:35:22 +02:00
Object.keys(stats[date])
.filter(sportId =>
displayedSports ? displayedSports.includes(+sportId) : true
)
.map(sportId => {
const sportLabel = sports.filter(s => s.id === +sportId)[0].label
dataNbWorkouts[sportLabel] = stats[date][sportId].nb_workouts
2019-08-28 15:35:22 +02:00
dataDistance[sportLabel] = stats[date][sportId].total_distance
dataDuration[sportLabel] = stats[date][sportId].total_duration
return null
})
2019-01-04 10:07:24 +01:00
}
nbWorkoutsStats.push(dataNbWorkouts)
2019-01-04 10:07:24 +01:00
distanceStats.push(dataDistance)
durationStats.push(dataDuration)
}
return {
workouts: nbWorkoutsStats,
2019-01-04 10:07:24 +01:00
distance: distanceStats,
2019-08-28 15:35:22 +02:00
duration: durationStats,
2019-01-04 10:07:24 +01:00
}
}