Added year option.

This commit is contained in:
Mike Cao 2020-07-30 23:08:33 -07:00
parent 38abd673f3
commit 5b45301178
4 changed files with 18 additions and 2 deletions

View File

@ -10,6 +10,7 @@ const filterOptions = [
{ label: 'Today', value: '1day' }, { label: 'Today', value: '1day' },
{ label: 'This week', value: '1week' }, { label: 'This week', value: '1week' },
{ label: 'This month', value: '1month' }, { label: 'This month', value: '1month' },
{ label: 'This year', value: '1year' },
]; ];
export default function DateFilter({ value, onChange }) { export default function DateFilter({ value, onChange }) {

View File

@ -12,12 +12,15 @@ export default function PageviewsChart({ data, unit, children }) {
(label, index, values) => { (label, index, values) => {
const d = new Date(values[index].value); const d = new Date(values[index].value);
const n = data.pageviews.length; const n = data.pageviews.length;
switch (unit) { switch (unit) {
case 'day': case 'day':
if (n >= 15) { if (n >= 15) {
return index % ~~(n / 15) === 0 ? format(d, 'MMM d') : ''; return index % ~~(n / 15) === 0 ? format(d, 'MMM d') : '';
} }
return format(d, 'EEE M/d'); return format(d, 'EEE M/d');
case 'month':
return format(d, 'MMMM');
default: default:
return label; return label;
} }

View File

@ -3,18 +3,22 @@ import {
addMinutes, addMinutes,
addHours, addHours,
addDays, addDays,
addMonths,
subHours, subHours,
subDays, subDays,
startOfHour, startOfHour,
startOfDay, startOfDay,
startOfWeek, startOfWeek,
startOfMonth, startOfMonth,
startOfYear,
endOfHour, endOfHour,
endOfDay, endOfDay,
endOfWeek, endOfWeek,
endOfMonth, endOfMonth,
endOfYear,
differenceInHours, differenceInHours,
differenceInCalendarDays, differenceInCalendarDays,
differenceInMonths,
} from 'date-fns'; } from 'date-fns';
export function getTimezone() { export function getTimezone() {
@ -28,7 +32,7 @@ export function getLocalTime(t) {
export function getDateRange(value) { export function getDateRange(value) {
const now = new Date(); const now = new Date();
const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month)$/).groups; const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month|year)$/).groups;
if (+num === 1) { if (+num === 1) {
switch (unit) { switch (unit) {
@ -53,6 +57,13 @@ export function getDateRange(value) {
unit: 'day', unit: 'day',
value, value,
}; };
case 'year':
return {
startDate: startOfYear(now),
endDate: endOfYear(now),
unit: 'month',
value,
};
} }
} }
@ -77,6 +88,7 @@ export function getDateRange(value) {
const dateFuncs = { const dateFuncs = {
hour: [differenceInHours, addHours, startOfHour], hour: [differenceInHours, addHours, startOfHour],
day: [differenceInCalendarDays, addDays, startOfDay], day: [differenceInCalendarDays, addDays, startOfDay],
month: [differenceInMonths, addMonths, startOfMonth],
}; };
export function getDateArray(data, startDate, endDate, unit) { export function getDateArray(data, startDate, endDate, unit) {

View File

@ -7,7 +7,7 @@ export default async (req, res) => {
const { id, start_at, end_at, unit, tz } = req.query; const { id, start_at, end_at, unit, tz } = req.query;
if (!moment.tz.zone(tz) || !['hour', 'day'].includes(unit)) { if (!moment.tz.zone(tz) || !['month', 'hour', 'day'].includes(unit)) {
return res.status(400).end(); return res.status(400).end();
} }