Add support for intervals (#1574)
Co-authored-by: Vini Brasil <vini@hey.com> Co-authored-by: Vignesh Joglekar <hey@vigneshjoglekar.com>pull/2463/head r2022.11.22-20
parent
ed9e03ae14
commit
497a52c10a
@ -0,0 +1,127 @@
|
||||
import {parseUTCDate, formatMonthYYYY, formatDay, formatDayShort} from '../../util/date'
|
||||
|
||||
const browserDateFormat = Intl.DateTimeFormat(navigator.language, { hour: 'numeric' })
|
||||
|
||||
const is12HourClock = function() {
|
||||
return browserDateFormat.resolvedOptions().hour12
|
||||
}
|
||||
|
||||
const parseISODate = function(isoDate) {
|
||||
const date = parseUTCDate(isoDate)
|
||||
const minutes = date.getMinutes();
|
||||
return { date, minutes }
|
||||
}
|
||||
|
||||
const formatHours = function(isoDate) {
|
||||
const monthIndex = 1
|
||||
const dateParts = isoDate.split(/[^0-9]/);
|
||||
dateParts[monthIndex] = dateParts[monthIndex] - 1
|
||||
|
||||
const localDate = new Date(...dateParts)
|
||||
return browserDateFormat.format(localDate)
|
||||
}
|
||||
|
||||
const monthIntervalFormatter = {
|
||||
long(isoDate, options) {
|
||||
const formatted = this.short(isoDate, options)
|
||||
return options.isBucketPartial ? `Partial of ${formatted}` : formatted
|
||||
},
|
||||
short(isoDate, _options) {
|
||||
const { date } = parseISODate(isoDate)
|
||||
return formatMonthYYYY(date)
|
||||
}
|
||||
}
|
||||
|
||||
const weekIntervalFormatter = {
|
||||
long(isoDate, options) {
|
||||
const formatted = this.short(isoDate, options)
|
||||
return options.isBucketPartial ? `Partial week of ${formatted}` : `Week of ${formatted}`
|
||||
},
|
||||
short(isoDate, _options) {
|
||||
const { date } = parseISODate(isoDate)
|
||||
return formatDayShort(date)
|
||||
}
|
||||
}
|
||||
|
||||
const dateIntervalFormatter = {
|
||||
long(isoDate, _options) {
|
||||
const { date } = parseISODate(isoDate)
|
||||
return formatDay(date)
|
||||
},
|
||||
short(isoDate, _options) {
|
||||
const { date } = parseISODate(isoDate)
|
||||
return formatDayShort(date)
|
||||
}
|
||||
}
|
||||
|
||||
const hourIntervalFormatter = {
|
||||
long(isoDate, options) {
|
||||
return this.short(isoDate, options)
|
||||
},
|
||||
short(isoDate, _options) {
|
||||
const formatted = formatHours(isoDate)
|
||||
|
||||
if (is12HourClock()) {
|
||||
return formatted.replace(' ', '').toLowerCase()
|
||||
} else {
|
||||
return formatted.replace(/[^0-9]/g, '').concat(":00")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const minuteIntervalFormatter = {
|
||||
long(isoDate, options) {
|
||||
if (options.period == 'realtime') {
|
||||
const minutesAgo = Math.abs(isoDate)
|
||||
return minutesAgo === 1 ? '1 minute ago' : minutesAgo + ' minutes ago'
|
||||
} else {
|
||||
return this.short(isoDate, options)
|
||||
}
|
||||
},
|
||||
short(isoDate, options) {
|
||||
if (options.period === 'realtime') return isoDate + 'm'
|
||||
|
||||
const { minutes } = parseISODate(isoDate)
|
||||
const formatted = formatHours(isoDate)
|
||||
if (is12HourClock()) {
|
||||
return formatted.replace(' ', ':' + (minutes < 10 ? `0${minutes}` : minutes)).toLowerCase()
|
||||
} else {
|
||||
return formatted.replace(/[^0-9]/g, '').concat(":" + (minutes < 10 ? `0${minutes}` : minutes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Each interval has a different date and time format. This object maps each
|
||||
// interval with two functions: `long` and `short`, that formats date and time
|
||||
// accordingly.
|
||||
const factory = {
|
||||
month: monthIntervalFormatter,
|
||||
week: weekIntervalFormatter,
|
||||
date: dateIntervalFormatter,
|
||||
hour: hourIntervalFormatter,
|
||||
minute: minuteIntervalFormatter
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a function that formats a ISO 8601 timestamp based on the given
|
||||
* arguments.
|
||||
*
|
||||
* The preferred date and time format in the dashboard depends on the selected
|
||||
* interval and period. For example, in real-time view only the time is necessary,
|
||||
* while other intervals require dates to be displayed.
|
||||
*
|
||||
* @param {string} interval - The interval of the query, e.g. `minute`, `hour`
|
||||
* @param {boolean} longForm - Whether the formatted result should be in long or
|
||||
* short form.
|
||||
* @param {string} period - The period of the query, e.g. `12mo`, `day`
|
||||
* @param {boolean} isPeriodFull - Indicates whether the interval has been cut
|
||||
* off by the requested date range or not. If false, the returned formatted date
|
||||
* indicates this cut off, e.g. `Partial week of November 8`.
|
||||
*/
|
||||
export default function dateFormatter(interval, longForm, period, isPeriodFull) {
|
||||
const displayMode = longForm ? 'long' : 'short'
|
||||
const options = { period: period, interval: interval, isBucketPartial: !isPeriodFull }
|
||||
return function(isoDate, _index, _ticks) {
|
||||
return factory[interval][displayMode](isoDate, options)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
import { Menu, Transition } from '@headlessui/react';
|
||||
import { ChevronDownIcon } from '@heroicons/react/20/solid'
|
||||
import React, { Fragment } from 'react';
|
||||
import classNames from 'classnames'
|
||||
import * as storage from '../../util/storage'
|
||||
|
||||
export const INTERVAL_LABELS = {
|
||||
'minute': 'Minutes',
|
||||
'hour': 'Hours',
|
||||
'date': 'Days',
|
||||
'week': 'Weeks',
|
||||
'month': 'Months'
|
||||
}
|
||||
|
||||
export const getStoredInterval = function(period, domain) {
|
||||
return storage.getItem(`interval__${period}__${domain}`)
|
||||
}
|
||||
|
||||
export const storeInterval = function(period, domain, interval) {
|
||||
storage.setItem(`interval__${period}__${domain}`, interval)
|
||||
}
|
||||
|
||||
function DropdownItem({ option, currentInterval, updateInterval }) {
|
||||
return (
|
||||
<Menu.Item onClick={() => updateInterval(option)} key={option} disabled={option == currentInterval}>
|
||||
{({ active }) => (
|
||||
<span className={classNames({
|
||||
'bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-200 cursor-pointer': active,
|
||||
'text-gray-700 dark:text-gray-200': !active,
|
||||
'font-bold cursor-none select-none': option == currentInterval,
|
||||
}, 'block px-4 py-2 text-sm')}>
|
||||
{ INTERVAL_LABELS[option] }
|
||||
</span>
|
||||
)}
|
||||
</Menu.Item>
|
||||
)
|
||||
}
|
||||
|
||||
export function IntervalPicker({ graphData, query, site, updateInterval }) {
|
||||
if (query.period == 'realtime') return null
|
||||
|
||||
const currentInterval = graphData?.interval
|
||||
const options = site.validIntervalsByPeriod[query.period]
|
||||
|
||||
return (
|
||||
<Menu as="div" className="relative inline-block pl-2">
|
||||
<Menu.Button className="text-sm inline-flex focus:outline-none text-gray-700 dark:text-gray-300 hover:text-indigo-600 dark:hover:text-indigo-600 items-center">
|
||||
{ INTERVAL_LABELS[currentInterval] }
|
||||
<ChevronDownIcon className="w-4 h-4" aria-hidden="true" />
|
||||
</Menu.Button>
|
||||
|
||||
<Transition
|
||||
as={Fragment}
|
||||
enter="transition ease-out duration-100"
|
||||
enterFrom="transform opacity-0 scale-95"
|
||||
enterTo="transform opacity-100 scale-100"
|
||||
leave="transition ease-in duration-75"
|
||||
leaveFrom="transform opacity-100 scale-100"
|
||||
leaveTo="transform opacity-0 scale-95"
|
||||
>
|
||||
<Menu.Items className="py-1 text-left origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 focus:outline-none z-10">
|
||||
{ options.map((option) => DropdownItem({ option, currentInterval, updateInterval })) }
|
||||
</Menu.Items>
|
||||
</Transition>
|
||||
</Menu>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in New Issue