-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuseFCOExploreSuggestions.ts
More file actions
133 lines (112 loc) · 3.52 KB
/
useFCOExploreSuggestions.ts
File metadata and controls
133 lines (112 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { encodeSearchQueryString } from "./encodeSearchQueryString";
import { FEAST_FCO_TYPES } from "../parsers/types";
import { useParams } from "react-router-dom";
import { useFeatureViewTagsAggregation } from "./useTagsAggregation";
import { feast } from "../protos";
interface ExplorationSuggestionItem {
name: string;
link: string;
label: string;
count: number;
}
interface ExplorationSuggestion {
title: string;
items: ExplorationSuggestionItem[];
}
const FCO_TO_URL_NAME_MAP: Record<FEAST_FCO_TYPES, string> = {
dataSource: "/data-source",
entity: "/entity",
featureView: "/feature-view",
featureService: "/feature-service",
};
const createSearchLink = (
FCOType: FEAST_FCO_TYPES,
key: string,
value: string,
) => {
const URL = FCO_TO_URL_NAME_MAP[FCOType];
return URL + "?" + encodeSearchQueryString(`${key}:${value}`);
};
const NUMBER_OF_SUGGESTION_GROUPS = 2;
const NUMBER_OF_VALUES_PER_GROUP = 4;
const sortTagByUniqueValues = <T>(
tagAggregation: Record<string, Record<string, T[]>>,
) => {
return Object.entries(tagAggregation).sort(
([a, valuesOfA], [b, valuesOfB]) => {
return Object.keys(valuesOfB).length - Object.keys(valuesOfA).length;
},
);
};
const sortTagsByTotalUsage = <T>(
tagAggregation: Record<string, Record<string, T[]>>,
) => {
return Object.entries(tagAggregation).sort(
([a, valuesOfA], [b, valuesOfB]) => {
const countOfA = Object.values(valuesOfA).reduce((memo, current) => {
return memo + current.length;
}, 0);
const countOfB = Object.values(valuesOfB).reduce((memo, current) => {
return memo + current.length;
}, 0);
return countOfB - countOfA;
},
);
};
const generateExplorationSuggestions = (
tagAggregation: Record<string, Record<string, feast.core.IFeatureView[]>>,
projectName: string,
) => {
const suggestions: ExplorationSuggestion[] = [];
if (tagAggregation) {
const SortedCandidates =
sortTagByUniqueValues<feast.core.IFeatureView>(tagAggregation);
SortedCandidates.slice(0, NUMBER_OF_SUGGESTION_GROUPS).forEach(
([selectedTag, selectedTagValuesMap]) => {
suggestions.push({
title: `Feature Views by "${selectedTag}"`,
items: Object.entries(selectedTagValuesMap)
.sort(([a, entriesOfA], [b, entriesOfB]) => {
return entriesOfB.length - entriesOfA.length;
})
.slice(0, NUMBER_OF_VALUES_PER_GROUP)
.map(([tagValue, fvEntries]) => {
return {
name: tagValue,
link:
`/p/${projectName}` +
createSearchLink(
FEAST_FCO_TYPES["featureView"],
selectedTag,
tagValue,
),
label: `Feature Services where ${selectedTag} is '${tagValue}'`,
count: fvEntries.length,
};
}),
});
},
);
}
return suggestions;
};
const useFCOExploreSuggestions = () => {
const query = useFeatureViewTagsAggregation();
const tagAggregation = query.data;
const { projectName } = useParams();
let data: ExplorationSuggestion[] | undefined = undefined;
if (query.isSuccess && tagAggregation && projectName) {
data = generateExplorationSuggestions(tagAggregation, projectName);
}
return {
...query,
data,
};
};
export default useFCOExploreSuggestions;
export {
generateExplorationSuggestions,
sortTagByUniqueValues,
sortTagsByTotalUsage,
};
export type { ExplorationSuggestion };