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

Add date filter #747

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.s.

the next task is from .. to it makes sense to use '>=' over hasSame
using >= as the added benefit of using standards, JS Date is standard while luxon is implementing none standard date formats

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date range filter will use a different matcher but I do plan to use luxon there as well.
IMHO this is the standard approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do plan to use luxon there as well.

that is actually a "task completion definition", i explicitly requested to implement the filter without using external libraries, unless it's very hard to implement using vanilla type script, I explained that I would like to remove the requirement if possible.
the rule of thumb was that if implementing the filter will take a lot of code more using vanilla type script, then it is ok to use a library. In this case it's possible to create a method that can be reusable between all the date-time watchers and will actually use less lines of code and be more readable when using vanilla typescript, see examples above, please drop the requirement on the external library in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code you proposed is interesting but cannot compete with an established library in terms of reliability and readability. I understand there are some other benefits of this approach. Let us discuss this later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code you proposed is interesting but cannot compete with an established library in terms of reliability and readability.

hmm, Date is a library that is part of the core of java script, it's reliable and readable ( depending on the writer :-) )

Let us discuss this later on.

Discussion is always appreciated, for this PR, please don't use the library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Let's put in on hold until we clear this issue.

};

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
@@ -1,6 +1,6 @@
import React from 'react';

import { ResourceLinkProps } from '@openshift-console/dynamic-plugin-sdk';
import { ResourceLinkProps, TimestampProps } from '@openshift-console/dynamic-plugin-sdk';

export const ResourceLink = ({
name,
Expand All @@ -23,3 +23,7 @@ export const BlueInfoCircleIcon = () => <div data-test-element-name="BlueInfoCir
export const useModal = (props) => <div data-test-element-name="useModal" {...props} />;
export const ActionService = () => <div data-test-element-name="ActionService" />;
export const ActionServiceProvider = () => <div data-test-element-name="ActionServiceProvider" />;

export const Timestamp = ({ timestamp }: TimestampProps) => (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

<div data-test-element-name="Timestamp">{timestamp}</div>
);
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,16 @@ exports[`Plan rows plantest-01 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -224,6 +234,14 @@ exports[`Plan rows plantest-02 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
/>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -408,6 +426,16 @@ exports[`Plan rows plantest-03 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -609,6 +637,16 @@ exports[`Plan rows plantest-04 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -810,6 +848,16 @@ exports[`Plan rows plantest-05 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -968,6 +1016,14 @@ exports[`Plan rows plantest-06 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
/>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1152,6 +1208,16 @@ exports[`Plan rows plantest-07 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1308,6 +1374,16 @@ exports[`Plan rows plantest-08 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1465,6 +1541,16 @@ exports[`Plan rows plantest-09 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1646,6 +1732,16 @@ exports[`Plan rows plantest-10 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down Expand Up @@ -1847,6 +1943,16 @@ exports[`Plan rows plantest-11 1`] = `
name: openshift-migration, gvk: ~~, ns: undefined
</div>
</td>
<td
class=""
data-label="Migration started"
>
<div
data-test-element-name="Timestamp"
>
2020-10-10T14:04:10.000Z
</div>
</td>
<td
class=""
data-label="Source provider"
Expand Down
Loading