|
|
export interface QueryStatsParams { |
|
|
date?: string; |
|
|
start_date?: string; |
|
|
days?: number; |
|
|
max?: number; |
|
|
num?: number; |
|
|
period?: string; |
|
|
summarize?: number; |
|
|
filter_by_country?: string; |
|
|
} |
|
|
|
|
|
const getDaysOfMonthFromDate = ( date: string ): number => { |
|
|
const dateObj = new Date( date ); |
|
|
const year = dateObj.getFullYear(); |
|
|
const month = dateObj.getMonth() + 1; |
|
|
|
|
|
return new Date( year, month, 0 ).getDate(); |
|
|
}; |
|
|
|
|
|
const daysInYearFromDate = ( date: string ) => { |
|
|
const dateObj = new Date( date ); |
|
|
const year = dateObj.getFullYear(); |
|
|
|
|
|
return ( year % 4 === 0 && year % 100 > 0 ) || year % 400 === 0 ? 366 : 365; |
|
|
}; |
|
|
|
|
|
export const processQueryParams = ( query: QueryStatsParams ) => { |
|
|
|
|
|
const num = query.num || 1; |
|
|
|
|
|
const max = query.max ?? 10; |
|
|
const date = query.date || new Date().toISOString().split( 'T' )[ 0 ]; |
|
|
|
|
|
|
|
|
let days = num; |
|
|
switch ( query.period ) { |
|
|
case 'week': |
|
|
days = 7; |
|
|
break; |
|
|
case 'month': |
|
|
days = getDaysOfMonthFromDate( date ); |
|
|
break; |
|
|
case 'year': |
|
|
days = daysInYearFromDate( date ); |
|
|
break; |
|
|
} |
|
|
|
|
|
return { |
|
|
...query, |
|
|
start_date: query.start_date || '', |
|
|
num, |
|
|
max, |
|
|
date, |
|
|
days, |
|
|
}; |
|
|
}; |
|
|
|