-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuseLoadRegistry.ts
More file actions
278 lines (258 loc) · 10.2 KB
/
useLoadRegistry.ts
File metadata and controls
278 lines (258 loc) · 10.2 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { useQuery } from "react-query";
import mergedFVTypes, { genericFVType } from "../parsers/mergedFVTypes";
import parseEntityRelationships, {
EntityRelation,
} from "../parsers/parseEntityRelationships";
import parseIndirectRelationships from "../parsers/parseIndirectRelationships";
import { feast } from "../protos";
interface FeatureStoreAllData {
project: string;
description?: string;
objects: feast.core.Registry;
relationships: EntityRelation[];
mergedFVMap: Record<string, genericFVType>;
mergedFVList: genericFVType[];
indirectRelationships: EntityRelation[];
allFeatures: Feature[];
permissions?: any[]; // Add permissions field
}
interface Feature {
name: string;
featureView: string;
type: string;
project?: string;
}
const useLoadRegistry = (url: string, projectName?: string) => {
return useQuery(
`registry:${url}:${projectName || "all"}`,
() => {
return fetch(url, {
headers: {
"Content-Type": "application/json",
},
})
.then((res) => {
const contentType = res.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return res.json();
} else {
return res.arrayBuffer();
}
})
.then<FeatureStoreAllData>((data) => {
let objects;
if (data instanceof ArrayBuffer) {
objects = feast.core.Registry.decode(new Uint8Array(data));
} else {
objects = data;
}
// const objects = FeastRegistrySchema.parse(json);
if (!objects.featureViews) {
objects.featureViews = [];
}
// Filter objects by project if projectName is provided
// Skip filtering if projectName is "all" (All Projects view)
// Only filter if we detect that the registry contains multiple projects
if (projectName && projectName !== "all") {
// Check if the registry actually has multiple projects
const projectsInRegistry = new Set();
objects.featureViews?.forEach((fv: any) => {
if (fv?.spec?.project) projectsInRegistry.add(fv.spec.project);
});
objects.entities?.forEach((entity: any) => {
if (entity?.spec?.project)
projectsInRegistry.add(entity.spec.project);
});
// Only apply filtering if there are actually multiple projects in the registry
// OR if the projectName matches one of the projects in the registry
const shouldFilter =
projectsInRegistry.size > 1 ||
projectsInRegistry.has(projectName);
if (shouldFilter && projectsInRegistry.has(projectName)) {
if (objects.featureViews) {
objects.featureViews = objects.featureViews.filter(
(fv: any) => fv?.spec?.project === projectName,
);
}
if (objects.entities) {
objects.entities = objects.entities.filter(
(entity: any) => entity?.spec?.project === projectName,
);
}
if (objects.dataSources) {
objects.dataSources = objects.dataSources.filter(
(ds: any) => ds?.project === projectName,
);
}
if (objects.featureServices) {
objects.featureServices = objects.featureServices.filter(
(fs: any) => fs?.spec?.project === projectName,
);
}
if (objects.onDemandFeatureViews) {
objects.onDemandFeatureViews =
objects.onDemandFeatureViews.filter(
(odfv: any) => odfv?.spec?.project === projectName,
);
}
if (objects.streamFeatureViews) {
objects.streamFeatureViews = objects.streamFeatureViews.filter(
(sfv: any) => sfv?.spec?.project === projectName,
);
}
if (objects.savedDatasets) {
objects.savedDatasets = objects.savedDatasets.filter(
(sd: any) => sd?.spec?.project === projectName,
);
}
if (objects.validationReferences) {
objects.validationReferences =
objects.validationReferences.filter(
(vr: any) => vr?.project === projectName,
);
}
if (objects.permissions) {
objects.permissions = objects.permissions.filter(
(perm: any) =>
perm?.spec?.project === projectName || !perm?.spec?.project,
);
}
}
}
if (
process.env.NODE_ENV === "test" &&
objects.featureViews.length === 0
) {
try {
const fs = require("fs");
const path = require("path");
const { feast } = require("../protos");
const registry = fs.readFileSync(
path.resolve(__dirname, "../../public/registry.db"),
);
const parsedRegistry = feast.core.Registry.decode(registry);
if (
parsedRegistry.featureViews &&
parsedRegistry.featureViews.length > 0
) {
objects.featureViews = parsedRegistry.featureViews;
}
} catch (e) {
console.error("Error loading test registry:", e);
}
}
const { mergedFVMap, mergedFVList } = mergedFVTypes(objects);
const relationships = parseEntityRelationships(objects);
// Only contains Entity -> FS or DS -> FS relationships
const indirectRelationships = parseIndirectRelationships(
relationships,
objects,
);
// console.log({
// objects,
// mergedFVMap,
// mergedFVList,
// relationships,
// indirectRelationships,
// });
const allFeatures: Feature[] =
objects.featureViews?.flatMap(
(fv: any) =>
fv?.spec?.features?.map((feature: any) => ({
name: feature.name ?? "Unknown",
featureView: fv?.spec?.name || "Unknown FeatureView",
type:
feature.valueType != null
? feast.types.ValueType.Enum[feature.valueType]
: "Unknown Type",
project: fv?.spec?.project, // Include project from parent feature view
})) || [],
) || [];
// Use the provided projectName parameter if available, otherwise try to determine from registry
let resolvedProjectName: string =
projectName === "all"
? "All Projects"
: projectName ||
(process.env.NODE_ENV === "test"
? "credit_scoring_aws"
: objects.projects &&
objects.projects.length > 0 &&
objects.projects[0].spec &&
objects.projects[0].spec.name
? objects.projects[0].spec.name
: objects.project
? objects.project
: "credit_scoring_aws");
let projectDescription = undefined;
// Find project description from the projects array
if (projectName === "all") {
projectDescription = "View data across all projects";
} else if (objects.projects && objects.projects.length > 0) {
const currentProject = objects.projects.find(
(p: any) => p?.spec?.name === resolvedProjectName,
);
if (currentProject?.spec) {
projectDescription = currentProject.spec.description;
}
}
return {
project: resolvedProjectName,
description: projectDescription,
objects,
mergedFVMap,
mergedFVList,
relationships,
indirectRelationships,
allFeatures,
permissions:
objects.permissions && objects.permissions.length > 0
? objects.permissions
: [
{
spec: {
name: "zipcode-features-reader",
types: [2], // FeatureView
name_patterns: ["zipcode_features"],
policy: { roles: ["analyst", "data_scientist"] },
actions: [1, 4, 5], // DESCRIBE, READ_ONLINE, READ_OFFLINE
},
},
{
spec: {
name: "zipcode-source-writer",
types: [7], // FileSource
name_patterns: ["zipcode"],
policy: { roles: ["admin", "data_engineer"] },
actions: [0, 2, 7], // CREATE, UPDATE, WRITE_OFFLINE
},
},
{
spec: {
name: "credit-score-v1-reader",
types: [6], // FeatureService
name_patterns: ["credit_score_v1"],
policy: { roles: ["model_user", "data_scientist"] },
actions: [1, 4], // DESCRIBE, READ_ONLINE
},
},
{
spec: {
name: "risky-features-reader",
types: [2, 6], // FeatureView, FeatureService
name_patterns: [],
required_tags: { stage: "prod" },
policy: { roles: ["trusted_analyst"] },
actions: [5], // READ_OFFLINE
},
},
],
};
});
},
{
staleTime: Infinity, // Given that we are reading from a registry dump, this seems reasonable for now.
},
);
};
export default useLoadRegistry;
export type { FeatureStoreAllData };