-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuseTagsAggregation.ts
More file actions
84 lines (73 loc) · 2.29 KB
/
useTagsAggregation.ts
File metadata and controls
84 lines (73 loc) · 2.29 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
import { useContext, useMemo } from "react";
import RegistryPathContext from "../contexts/RegistryPathContext";
import useLoadRegistry from "../queries/useLoadRegistry";
import { feast } from "../protos";
// Usage of generic type parameter T
// https://stackoverflow.com/questions/53203409/how-to-tell-typescript-that-im-returning-an-array-of-arrays-of-the-input-type
const buildTagCollection = <T>(
array: T[],
recordExtractor: (unknownFCO: T) => Record<string, string> | undefined, // Assumes that tags are always a Record<string, string>
): Record<string, Record<string, T[]>> => {
const tagCollection = array.reduce(
(memo: Record<string, Record<string, T[]>>, fco: T) => {
const tags = recordExtractor(fco);
if (tags) {
Object.entries(tags).forEach(([tagKey, tagValue]) => {
if (!memo[tagKey]) {
memo[tagKey] = {
[tagValue]: [fco],
};
} else {
if (!memo[tagKey][tagValue]) {
memo[tagKey][tagValue] = [fco];
} else {
memo[tagKey][tagValue].push(fco);
}
}
});
}
return memo;
},
{},
);
return tagCollection;
};
const useFeatureViewTagsAggregation = () => {
const registryUrl = useContext(RegistryPathContext);
const query = useLoadRegistry(registryUrl);
const data = useMemo(() => {
return query.data && query.data.objects && query.data.objects.featureViews
? buildTagCollection<feast.core.IFeatureView>(
query.data.objects.featureViews!,
(fv) => {
return fv.spec?.tags!;
},
)
: undefined;
}, [query.data]);
return {
...query,
data,
};
};
const useFeatureServiceTagsAggregation = () => {
const registryUrl = useContext(RegistryPathContext);
const query = useLoadRegistry(registryUrl);
const data = useMemo(() => {
return query.data &&
query.data.objects &&
query.data.objects.featureServices
? buildTagCollection<feast.core.IFeatureService>(
query.data.objects.featureServices,
(fs) => {
return fs.spec?.tags!;
},
)
: undefined;
}, [query.data]);
return {
...query,
data,
};
};
export { useFeatureViewTagsAggregation, useFeatureServiceTagsAggregation };