Skip to content

Commit

Permalink
Add Date filter
Browse files Browse the repository at this point in the history
Allow to filter events at given date.

Key points:
1. multiple dates can be choosen
2. ISO date format used (YYYY-MM-DD) regardless of the locale
3. date comparison is done in UTC zone
4. Migration started column (timestamp) added to Plans table

Reference-Url: https://github.com/oVirt/ovirt-web-ui/blob/ea9a10e75e7dc965ddc6a1a7f2d36f6301117bbc/src/components/Toolbar/DatePickerFilter.js
Signed-off-by: Radoslaw Szwajkowski <[email protected]>
  • Loading branch information
rszwajko committed Oct 10, 2023
1 parent b85d662 commit 82453ba
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 4 deletions.
74 changes: 74 additions & 0 deletions packages/common/src/components/Filter/DateFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { FormEvent, useState } from 'react';
import { DateTime } from 'luxon';

import { DatePicker, InputGroup, ToolbarFilter } from '@patternfly/react-core';

import { FilterTypeProps } from './types';

/**
* This Filter type enables selecting a single date (a day).
*
* **FilterTypeProps are interpreted as follows**:<br>
* 1) selectedFilters - dates in YYYY-MM-DD format (ISO date format).<br>
* 2) onFilterUpdate - accepts the list of dates.<br>
*
* [<img src="static/media/src/components-stories/assets/github-logo.svg"><i class="fi fi-brands-github">
* <font color="green">View component source on GitHub</font>](https://github.com/kubev2v/forklift-console-plugin/blob/main/packages/common/src/components/Filter/DateFilter.tsx)
*/
export const DateFilter = ({
selectedFilters = [],
onFilterUpdate,
title,
filterId,
placeholderLabel,
showFilter = true,
}: FilterTypeProps) => {
const validFilters =
selectedFilters
?.map((str) => DateTime.fromISO(str))
?.filter((dt: DateTime) => dt.isValid)
?.map((dt: DateTime) => dt.toISODate()) ?? [];

// internal state - stored as ISO date string (no time)
const [date, setDate] = useState(DateTime.now().toISODate());

const clearSingleDate = (option) => {
onFilterUpdate([...validFilters.filter((d) => d !== option)]);
};

const onDateChange = (even: FormEvent<HTMLInputElement>, value: string) => {
// require full format "YYYY-MM-DD" although partial date is also accepted
// i.e. YYYY-MM gets parsed as YYYY-MM-01 and results in auto completing the date
// unfortunately due to auto-complete user cannot delete the date char after char
if (value?.length === 10 && DateTime.fromISO(value).isValid) {
const targetDate = DateTime.fromISO(value).toISODate();
setDate(targetDate);
onFilterUpdate([...validFilters.filter((d) => d !== targetDate), targetDate]);
}
};

return (
<ToolbarFilter
key={filterId}
chips={validFilters}
deleteChip={(category, option) => clearSingleDate(option)}
deleteChipGroup={() => onFilterUpdate([])}
categoryName={title}
showToolbarItem={showFilter}
>
<InputGroup>
<DatePicker
value={DateTime.fromISO(date).toISODate()}
dateFormat={(date) => DateTime.fromJSDate(date).toISODate()}
dateParse={(str) => DateTime.fromISO(str).toJSDate()}
onChange={onDateChange}
aria-label={title}
placeholder={placeholderLabel}
invalidFormatText={placeholderLabel}
// default value ("parent") creates collision with sticky table header
appendTo={document.body}
/>
</InputGroup>
</ToolbarFilter>
);
};
1 change: 1 addition & 0 deletions packages/common/src/components/Filter/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @index(['./*', /__/g], f => `export * from '${f.path}';`)
export * from './DateFilter';
export * from './EnumFilter';
export * from './FreetextFilter';
export * from './GroupedEnumFilter';
Expand Down
11 changes: 10 additions & 1 deletion packages/common/src/components/FilterGroup/matchers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import jsonpath from 'jsonpath';
import { DateTime } from 'luxon';

import { ResourceField } from '../../utils';
import { EnumFilter, FreetextFilter, GroupedEnumFilter, SwitchFilter } from '../Filter';
import { DateFilter, EnumFilter, FreetextFilter, GroupedEnumFilter, SwitchFilter } from '../Filter';

import { FilterRenderer, ValueMatcher } from './types';

Expand Down Expand Up @@ -96,6 +97,12 @@ const groupedEnumMatcher = {
matchValue: enumMatcher.matchValue,
};

const dateMatcher = {
filterType: 'date',
matchValue: (value: string) => (filter: string) =>
DateTime.fromISO(value).toUTC().hasSame(DateTime.fromISO(filter).toUTC(), 'day'),
};

const sliderMatcher = {
filterType: 'slider',
matchValue: (value: string) => (filter: string) => Boolean(value).toString() === filter || !value,
Expand All @@ -106,9 +113,11 @@ export const defaultValueMatchers: ValueMatcher[] = [
enumMatcher,
groupedEnumMatcher,
sliderMatcher,
dateMatcher,
];

export const defaultSupportedFilters: Record<string, FilterRenderer> = {
date: DateFilter,
enum: EnumFilter,
freetext: FreetextFilter,
groupedEnum: GroupedEnumFilter,
Expand Down
12 changes: 12 additions & 0 deletions packages/common/src/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DateTime } from 'luxon';

/**
* Converts a given ISO date string in a known format and timezone to a UTC ISO string.
*
* @param {string} isoDateString - The ISO date string in a known format and timezone.
* @returns {string} The equivalent UTC ISO string if date is valid or undefined otherwise.
*/
export function convertToUTC(isoDateString: string): string | undefined {
const date = DateTime.fromISO(isoDateString);
return date.isValid ? date.toUTC().toISO() : undefined;
}
1 change: 1 addition & 0 deletions packages/common/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @index(['./*', /__/g], f => `export * from '${f.path}';`)
export * from './constants';
export * from './dates';
export * from './localCompare';
export * from './localStorage';
export * from './types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
"Migration networks maps are used to map network interfaces between source and target workloads.": "Migration networks maps are used to map network interfaces between source and target workloads.",
"Migration plans are used to plan migration or virtualization workloads from source providers to target providers.": "Migration plans are used to plan migration or virtualization workloads from source providers to target providers.",
"Migration plans are used to plan migration or virtualization workloads from source providers to target providers. At least one source and one target provider must be available in order to create a migration plan, <2>Learn more</2>.": "Migration plans are used to plan migration or virtualization workloads from source providers to target providers. At least one source and one target provider must be available in order to create a migration plan, <2>Learn more</2>.",
"Migration started": "Migration started",
"Migration storage maps are used to map storage interfaces between source and target workloads, at least one source and one target provider must be available in order to create a migration plan, <2>Learn more</2>.": "Migration storage maps are used to map storage interfaces between source and target workloads, at least one source and one target provider must be available in order to create a migration plan, <2>Learn more</2>.",
"Migration storage maps are used to map storage interfaces between source and target workloads.": "Migration storage maps are used to map storage interfaces between source and target workloads.",
"Migration Toolkit for Virtualization": "Migration Toolkit for Virtualization",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import { MigrateOrCutoverButton } from '@kubev2v/legacy/Plans/components/Migrate
import { PlanNameNavLink as Link } from '@kubev2v/legacy/Plans/components/PlanStatusNavLink';
import { ScheduledCutoverTime } from '@kubev2v/legacy/Plans/components/ScheduledCutoverTime';
import { StatusIcon } from '@migtools/lib-ui';
import { K8sGroupVersionKind, ResourceLink } from '@openshift-console/dynamic-plugin-sdk';
import {
K8sGroupVersionKind,
ResourceLink,
Timestamp,
} from '@openshift-console/dynamic-plugin-sdk';
import {
Flex,
FlexItem,
Expand Down Expand Up @@ -192,6 +196,7 @@ const cellCreator: Record<string, (props: CellProps) => JSX.Element> = {
<VirtualMachineIcon /> {value}
</RouterLink>
),
[C.MIGRATION_STARTED]: ({ value }: CellProps) => <Timestamp timestamp={value} />,
};

const PlanRow = ({
Expand Down
10 changes: 10 additions & 0 deletions packages/forklift-console-plugin/src/modules/Plans/PlansPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ export const fieldsMetadataFactory: ResourceFieldFactory = (t) => [
},
sortable: true,
},
{
resourceFieldId: C.MIGRATION_STARTED,
label: t('Migration started'),
isVisible: true,
filter: {
type: 'date',
placeholderLabel: 'YYYY-MM-DD',
},
sortable: true,
},
{
resourceFieldId: C.SOURCE,
label: t('Source provider'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ exports[`Plan rows plantest-01 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -224,6 +230,10 @@ exports[`Plan rows plantest-02 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
/>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -408,6 +418,12 @@ exports[`Plan rows plantest-03 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -609,6 +625,12 @@ exports[`Plan rows plantest-04 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -810,6 +832,12 @@ exports[`Plan rows plantest-05 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -968,6 +996,10 @@ exports[`Plan rows plantest-06 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
/>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1152,6 +1184,12 @@ exports[`Plan rows plantest-07 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1308,6 +1346,12 @@ exports[`Plan rows plantest-08 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1465,6 +1509,12 @@ exports[`Plan rows plantest-09 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1646,6 +1696,12 @@ exports[`Plan rows plantest-10 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1847,6 +1903,12 @@ exports[`Plan rows plantest-11 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
2020-10-10T14:04:10Z
</td>
<td
class=""
data-label="Source provider"
Expand Down
5 changes: 3 additions & 2 deletions packages/forklift-console-plugin/src/modules/Plans/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useMigrations, usePlans, useProviders } from 'src/utils/fetch';
import { groupVersionKindForObj, resolveProviderRef } from 'src/utils/resources';
import { MigrationResource, PlanResource, ProviderRef } from 'src/utils/types';

import { convertToUTC } from '@kubev2v/common';
import { PlanState } from '@kubev2v/legacy/common/constants';
import { getPlanState } from '@kubev2v/legacy/Plans/components/helpers';
import { findLatestMigration } from '@kubev2v/legacy/queries';
Expand Down Expand Up @@ -110,8 +111,8 @@ export const mergeData = (
!vm.error &&
!vm.conditions?.find((condition) => condition.type === 'Canceled'),
).length || 0,
migrationCompleted: migration?.completed,
migrationStarted: migration?.started,
migrationCompleted: convertToUTC(migration?.completed),
migrationStarted: convertToUTC(migration?.started),
latestMigration,
object: plan,
}),
Expand Down

0 comments on commit 82453ba

Please sign in to comment.