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

feat(viz-gallery): add 'feature' tag and fuzzy search weighting #18662

Merged
merged 19 commits into from
Mar 11, 2022
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
Prev Previous commit
Next Next commit
use chartLabel enum to unify
  • Loading branch information
stephenLYZ committed Feb 24, 2022
commit 778b59ac05afcbd7d06bae8de1bb440feb88ccf6
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { Behavior } from '../types/Base';
import { Behavior, ChartLabel } from '../types/Base';

interface LookupTable {
[key: string]: boolean;
Expand All @@ -40,15 +40,13 @@ export interface ChartMetadataConfig {
thumbnail: string;
useLegacyApi?: boolean;
behaviors?: Behavior[];
deprecated?: boolean;
exampleGallery?: ExampleImage[];
tags?: string[];
category?: string | null;
label?: {
name: string;
description: string;
};
searchWeight?: number;
name?: ChartLabel;
description?: string;
} | null;
}

export default class ChartMetadata {
Expand Down Expand Up @@ -76,20 +74,16 @@ export default class ChartMetadata {

enableNoResults: boolean;

deprecated: boolean;

exampleGallery: ExampleImage[];

tags: string[];

category: string | null;

label?: {
name: string;
description: string;
};

searchWeight: number;
name?: ChartLabel;
description?: string;
} | null;

constructor(config: ChartMetadataConfig) {
const {
Expand All @@ -104,12 +98,10 @@ export default class ChartMetadata {
behaviors = [],
datasourceCount = 1,
enableNoResults = true,
deprecated = false,
exampleGallery = [],
tags = [],
category = null,
label,
searchWeight = 0,
label = null,
} = config;

this.name = name;
Expand All @@ -132,12 +124,10 @@ export default class ChartMetadata {
this.behaviors = behaviors;
this.datasourceCount = datasourceCount;
this.enableNoResults = enableNoResults;
this.deprecated = deprecated;
this.exampleGallery = exampleGallery;
this.tags = tags;
this.category = category;
this.label = label;
this.searchWeight = searchWeight;
}

canBeAnnotationType(type: string): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ export interface PlainObject {
[key: string]: any;
}

export enum ChartLabel {
VERIFIED = 'VERIFIED',
DEPRECATED = 'DEPRECATED',
}

export default {};
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
ChartMetadata,
SupersetTheme,
useTheme,
ChartLabel,
} from '@superset-ui/core';
import { Collapse, Input, Tooltip } from 'src/common/components';
import Label from 'src/components/Label';
Expand Down Expand Up @@ -111,6 +112,8 @@ const DEFAULT_ORDER = [
'country_map',
];

const CHART_LABEL_ORDER = [ChartLabel.DEPRECATED, ChartLabel.VERIFIED];

const typesWithDefaultOrder = new Set(DEFAULT_ORDER);

const THUMBNAIL_GRID_UNITS = 24;
Expand Down Expand Up @@ -410,10 +413,10 @@ const Thumbnail: React.FC<ThumbnailProps> = ({
>
{type.name}
</div>
{type.label && (
{type.label?.name && (
<ThumbnailLabelWrapper>
<HighlightLabel>
<div>{type.label.name}</div>
<div>{t(type.label?.name)}</div>
</HighlightLabel>
</ThumbnailLabelWrapper>
)}
Expand Down Expand Up @@ -499,7 +502,8 @@ export default function VizTypeGallery(props: VizTypeGalleryProps) {
.map(([key, value]) => ({ key, value }))
.filter(
({ value }) =>
nativeFilterGate(value.behaviors || []) && !value.deprecated,
nativeFilterGate(value.behaviors || []) &&
value.label?.name !== ChartLabel.DEPRECATED,
);
result.sort((a, b) => vizSortFactor(a) - vizSortFactor(b));
return result;
Expand Down Expand Up @@ -587,9 +591,15 @@ export default function VizTypeGallery(props: VizTypeGalleryProps) {
return fuse
.search(searchInputValue)
.map(result => result.item)
.sort(
(a, b) => (b.value?.searchWeight || 0) - (a.value?.searchWeight || 0),
);
.sort((a, b) => {
Copy link
Member

Choose a reason for hiding this comment

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

This sort is new. Before we were using the numeric weighting. Does the weighting still work, or does this search "win" in terms of ranking things?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, now we use CHART_LABEL_ORDER array to determine the search weight, and the larger the index, the larger the weight

Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of using this sort based on the label's place in the array order, we can define the labels with some basic (flexible/configurable) weighting, like this

const chartLabels = {
    Featured: {
        weight: 0.3
    },
    Depracated: {
        weight: -0.3
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

This is better (and I think is sufficient for the feature), but I wish there was a way to avoid using sort and instead have this be just one more factor in the search result, like the weight for keys that fuse supports. This might be a bit heavy-handed in terms of search weighting, but... the feature works!

const aOrder = a.value?.label?.name
? CHART_LABEL_ORDER.indexOf(a.value?.label?.name)
: 0;
const bOrder = b.value?.label?.name
? CHART_LABEL_ORDER.indexOf(b.value?.label?.name)
: 0;
return bOrder - aOrder;
});
}, [searchInputValue, fuse]);

const focusSearch = useCallback(() => {
Expand Down Expand Up @@ -787,15 +797,15 @@ export default function VizTypeGallery(props: VizTypeGalleryProps) {
`}
>
{selectedVizMetadata?.name}
{selectedVizMetadata?.label && (
{selectedVizMetadata?.label?.name && (
<Tooltip
id="viz-badge-tooltip"
placement="top"
title={selectedVizMetadata?.label.description}
title={selectedVizMetadata.label?.description}
>
<TitleLabelWrapper>
<HighlightLabel>
<div>{selectedVizMetadata.label.name}</div>
<div>{t(selectedVizMetadata.label?.name)}</div>
</HighlightLabel>
</TitleLabelWrapper>
</Tooltip>
Expand Down