Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Add week and year to presets in DateTimeRangePicker #4303

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ interface UnitsMap {
[key: string]: string;
}

const unitLong: UnitsMap = {m: UNITS.MINUTE, h: UNITS.HOUR, d: UNITS.DAY};
const unitShort: UnitsMap = {[UNITS.MINUTE]: 'm', [UNITS.HOUR]: 'h', [UNITS.DAY]: 'd'};
const unitLong: UnitsMap = {
m: UNITS.MINUTE,
h: UNITS.HOUR,
d: UNITS.DAY,
w: UNITS.WEEK,
y: UNITS.YEAR,
};
const unitShort: UnitsMap = {
[UNITS.MINUTE]: 'm',
[UNITS.HOUR]: 'h',
[UNITS.DAY]: 'd',
[UNITS.WEEK]: 'w',
[UNITS.YEAR]: 'y',
};

const presetRanges = [
{value: 1, unit: UNITS.MINUTE},
Expand All @@ -47,6 +59,14 @@ const presetRanges = [
{value: 12, unit: UNITS.HOUR},
{value: 1, unit: UNITS.DAY},
{value: 2, unit: UNITS.DAY},
{value: 1, unit: UNITS.WEEK},
{value: 2, unit: UNITS.WEEK},
{value: 4, unit: UNITS.WEEK},
{value: 8, unit: UNITS.WEEK},
{value: 16, unit: UNITS.WEEK},
{value: 26, unit: UNITS.WEEK},
{value: 1, unit: UNITS.YEAR},
{value: 2, unit: UNITS.YEAR},
];

const NOW = new RelativeDate(UNITS.MINUTE, 0);
Expand Down Expand Up @@ -94,7 +114,11 @@ const RelativeDatePicker = ({
case UNITS.HOUR:
return 60;
case UNITS.DAY:
return 1440;
return 24 * 60;
case UNITS.WEEK:
return 7 * 24 * 60;
case UNITS.YEAR:
return 365 * 24 * 60;
case UNITS.MINUTE:
default:
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const UNITS = {
MINUTE: 'minute',
HOUR: 'hour',
DAY: 'day',
WEEK: 'week',
YEAR: 'year',
};

export const POSITIONS = {
Expand Down Expand Up @@ -264,6 +266,10 @@ const getRelativeDateMs = (date: RelativeDate): number => {
return now - value * 60 * 60 * 1000;
case UNITS.DAY:
return now - value * 24 * 60 * 60 * 1000;
case UNITS.WEEK:
return now - value * 7 * 24 * 60 * 60 * 1000;
case UNITS.YEAR:
return now - value * 365 * 24 * 60 * 60 * 1000;
default:
return now;
}
Expand Down
2 changes: 1 addition & 1 deletion ui/packages/shared/profile/src/ProfileSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ProfileSelection {
Type: () => string;
}

export const timeFormat = "MMM d, 'at' HH:mm:ss '(UTC)'";
export const timeFormat = "yyyy-MM-dd HH:mm:ss '(UTC)'";

export function ParamsString(params: {[key: string]: string}): string {
return Object.keys(params)
Expand Down
16 changes: 10 additions & 6 deletions ui/packages/shared/utilities/src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const TimeUnits = {
Minutes: 'minutes',
Hours: 'hours',
Days: 'days',
Weeks: 'weeks',
Years: 'years',
} as const;

export type TimeUnit = (typeof TimeUnits)[keyof typeof TimeUnits];
Expand All @@ -47,7 +49,9 @@ export const unitsInTime = {
[TimeUnits.Seconds]: {multiplier: 1e9, symbol: 's'},
[TimeUnits.Minutes]: {multiplier: 6 * 1e10, symbol: 'm'},
[TimeUnits.Hours]: {multiplier: 60 * 60 * 1e9, symbol: 'h'},
[TimeUnits.Days]: {multiplier: 60 * 60 * 24 * 1e9, symbol: 'd'},
[TimeUnits.Days]: {multiplier: 24 * 60 * 60 * 1e9, symbol: 'd'},
[TimeUnits.Weeks]: {multiplier: 7 * 24 * 60 * 60 * 1e9, symbol: 'w'},
[TimeUnits.Years]: {multiplier: 365 * 24 * 60 * 60 * 1e9, symbol: 'y'},
};

export const convertTime = (value: number, from: TimeUnit, to: TimeUnit): number => {
Expand Down Expand Up @@ -120,19 +124,22 @@ export const formatForTimespan = (from: number, to: number): string => {
hours,
days,
weeks,
years,
}: {
seconds?: number;
minutes?: number;
hours?: number;
days?: number;
weeks?: number;
years?: number;
}): number => {
return (
(seconds ?? 0) +
(minutes ?? 0) * 60 +
(hours ?? 0) * 60 * 60 +
(days ?? 0) * 24 * 60 * 60 +
(weeks ?? 0) * 7 * 24 * 60 * 60
(weeks ?? 0) * 7 * 24 * 60 * 60 +
(years ?? 0) * 365 * 24 * 60 * 60
);
};

Expand All @@ -144,10 +151,7 @@ export const formatForTimespan = (from: number, to: number): string => {
if (durationInSeconds <= getTotalSeconds({hours: 13})) {
return 'HH:mm';
}
if (durationInSeconds <= getTotalSeconds({hours: 25})) {
return 'HH:mm M/d';
}
return 'M/d';
return 'yyyy-MM-dd HH:mm';
};

export const getStepDuration = (start: number, end: number, stepCount = 1000): Duration => {
Expand Down
Loading