-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathregistry_lineage.py
More file actions
459 lines (409 loc) · 19.2 KB
/
registry_lineage.py
File metadata and controls
459 lines (409 loc) · 19.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
"""
Registry lineage generation for Feast objects.
This module provides functionality to generate relationship graphs between
Feast objects (entities, feature views, data sources, feature services)
for lineage visualization.
"""
from dataclasses import dataclass
from enum import Enum
from typing import Dict, List, Tuple
from feast.protos.feast.core.Registry_pb2 import Registry
class FeastObjectType(Enum):
DATA_SOURCE = "dataSource"
ENTITY = "entity"
FEATURE_VIEW = "featureView"
FEATURE_SERVICE = "featureService"
FEATURE = "feature"
@dataclass
class EntityReference:
type: FeastObjectType
name: str
def to_proto(self):
try:
from feast.protos.feast.registry.RegistryServer_pb2 import (
EntityReference as EntityReferenceProto,
)
return EntityReferenceProto(type=self.type.value, name=self.name)
except ImportError:
return {"type": self.type.value, "name": self.name}
@dataclass
class EntityRelation:
source: EntityReference
target: EntityReference
def to_proto(self):
try:
from feast.protos.feast.registry.RegistryServer_pb2 import (
EntityRelation as EntityRelationProto,
)
return EntityRelationProto(
source=self.source.to_proto(), target=self.target.to_proto()
)
except ImportError:
# Fallback to dict if protobuf not generated yet
return {"source": self.source.to_proto(), "target": self.target.to_proto()}
class RegistryLineageGenerator:
"""
Generates lineage relationships between Feast objects.
"""
def generate_lineage(
self, registry: Registry
) -> Tuple[List[EntityRelation], List[EntityRelation]]:
"""
Generate both direct and indirect relationships from registry objects.
Args:
registry: The registry protobuf containing all objects
Returns:
Tuple of (direct_relationships, indirect_relationships)
"""
direct_relationships = self._parse_direct_relationships(registry)
indirect_relationships = self._parse_indirect_relationships(
direct_relationships, registry
)
return direct_relationships, indirect_relationships
def _parse_direct_relationships(self, registry: Registry) -> List[EntityRelation]:
"""Parse direct relationships between objects."""
relationships = []
# FeatureService -> FeatureView relationships
for feature_service in registry.feature_services:
if (
hasattr(feature_service, "spec")
and feature_service.spec
and feature_service.spec.features
):
for feature in feature_service.spec.features:
rel = EntityRelation(
source=EntityReference(
FeastObjectType.FEATURE_VIEW, feature.feature_view_name
),
target=EntityReference(
FeastObjectType.FEATURE_SERVICE,
feature_service.spec.name,
),
)
relationships.append(rel)
# Entity -> FeatureView and DataSource -> FeatureView relationships
for feature_view in registry.feature_views:
if hasattr(feature_view, "spec") and feature_view.spec:
# Entity relationships
if hasattr(feature_view.spec, "entities"):
for entity_name in feature_view.spec.entities:
rel = EntityRelation(
source=EntityReference(FeastObjectType.ENTITY, entity_name),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, feature_view.spec.name
),
)
relationships.append(rel)
# Feature -> FeatureView relationships
if hasattr(feature_view.spec, "features"):
for feature in feature_view.spec.features:
rel = EntityRelation(
source=EntityReference(
FeastObjectType.FEATURE, feature.name
),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, feature_view.spec.name
),
)
relationships.append(rel)
# Batch source relationship
if (
hasattr(feature_view.spec, "batch_source")
and feature_view.spec.batch_source
):
# Try to get the data source name
data_source_name = None
if (
hasattr(feature_view.spec.batch_source, "name")
and feature_view.spec.batch_source.name
):
data_source_name = feature_view.spec.batch_source.name
elif (
hasattr(feature_view.spec.batch_source, "table")
and feature_view.spec.batch_source.table
):
# Fallback to table name for unnamed data sources
data_source_name = (
f"table:{feature_view.spec.batch_source.table}"
)
elif (
hasattr(feature_view.spec.batch_source, "path")
and feature_view.spec.batch_source.path
):
# Fallback to path for file-based sources
data_source_name = f"path:{feature_view.spec.batch_source.path}"
else:
# Use a generic identifier
data_source_name = f"unnamed_source_{hash(str(feature_view.spec.batch_source))}"
if data_source_name:
relationships.append(
EntityRelation(
source=EntityReference(
FeastObjectType.DATA_SOURCE, data_source_name
),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, feature_view.spec.name
),
)
)
# OnDemand FeatureView: Feature -> OnDemandFeatureView relationships
for odfv in registry.on_demand_feature_views:
if hasattr(odfv, "spec") and odfv.spec:
# Entity relationships
if hasattr(odfv.spec, "entities"):
for entity_name in odfv.spec.entities:
rel = EntityRelation(
source=EntityReference(FeastObjectType.ENTITY, entity_name),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, odfv.spec.name
),
)
relationships.append(rel)
# Feature -> OnDemandFeatureView relationships
if hasattr(odfv.spec, "features"):
for feature in odfv.spec.features:
relationships.append(
EntityRelation(
source=EntityReference(
FeastObjectType.FEATURE, feature.name
),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, odfv.spec.name
),
)
)
# OnDemand FeatureView relationships
for odfv in registry.on_demand_feature_views:
if (
hasattr(odfv, "spec")
and odfv.spec
and hasattr(odfv.spec, "sources")
and odfv.spec.sources
):
# Handle protobuf map structure
if hasattr(odfv.spec.sources, "items"):
source_items = odfv.spec.sources.items()
else:
# Fallback for different protobuf representations
source_items = [(k, v) for k, v in enumerate(odfv.spec.sources)]
for source_name, source in source_items:
if (
hasattr(source, "request_data_source")
and source.request_data_source
):
if hasattr(source.request_data_source, "name"):
relationships.append(
EntityRelation(
source=EntityReference(
FeastObjectType.DATA_SOURCE,
source.request_data_source.name,
),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, odfv.spec.name
),
)
)
elif (
hasattr(source, "feature_view_projection")
and source.feature_view_projection
):
# Find the source feature view's batch source
if hasattr(source.feature_view_projection, "feature_view_name"):
source_fv = next(
(
fv
for fv in registry.feature_views
if hasattr(fv, "spec")
and fv.spec
and hasattr(fv.spec, "name")
and fv.spec.name
== source.feature_view_projection.feature_view_name
),
None,
)
if (
source_fv
and hasattr(source_fv, "spec")
and source_fv.spec
and hasattr(source_fv.spec, "batch_source")
and source_fv.spec.batch_source
and hasattr(source_fv.spec.batch_source, "name")
):
relationships.append(
EntityRelation(
source=EntityReference(
FeastObjectType.DATA_SOURCE,
source_fv.spec.batch_source.name,
),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, odfv.spec.name
),
)
)
# Stream FeatureView relationships
for sfv in registry.stream_feature_views:
if hasattr(sfv, "spec") and sfv.spec:
# Stream source
if (
hasattr(sfv.spec, "stream_source")
and sfv.spec.stream_source
and hasattr(sfv.spec.stream_source, "name")
and sfv.spec.stream_source.name
):
relationships.append(
EntityRelation(
source=EntityReference(
FeastObjectType.DATA_SOURCE, sfv.spec.stream_source.name
),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, sfv.spec.name
),
)
)
# Batch source
if (
hasattr(sfv.spec, "batch_source")
and sfv.spec.batch_source
and hasattr(sfv.spec.batch_source, "name")
and sfv.spec.batch_source.name
):
relationships.append(
EntityRelation(
source=EntityReference(
FeastObjectType.DATA_SOURCE, sfv.spec.batch_source.name
),
target=EntityReference(
FeastObjectType.FEATURE_VIEW, sfv.spec.name
),
)
)
return relationships
def _parse_indirect_relationships(
self, direct_relationships: List[EntityRelation], registry: Registry
) -> List[EntityRelation]:
"""Parse indirect relationships (transitive relationships through feature views)."""
indirect_relationships = []
# Create Entity -> FeatureService and DataSource -> FeatureService relationships
for feature_service in registry.feature_services:
if (
hasattr(feature_service, "spec")
and feature_service.spec
and hasattr(feature_service.spec, "features")
and feature_service.spec.features
):
for feature in feature_service.spec.features:
if hasattr(feature, "feature_view_name"):
# Find all relationships that target this feature view
related_sources = [
rel.source
for rel in direct_relationships
if rel.target.name == feature.feature_view_name
and rel.target.type == FeastObjectType.FEATURE_VIEW
]
# Create indirect relationships to the feature service
for source in related_sources:
indirect_relationships.append(
EntityRelation(
source=source,
target=EntityReference(
FeastObjectType.FEATURE_SERVICE,
feature_service.spec.name,
),
)
)
# Create Entity -> DataSource relationships (through feature views)
# Build a map of feature view -> data sources
feature_view_to_data_sources: Dict[str, List[str]] = {}
for rel in direct_relationships:
if (
rel.source.type == FeastObjectType.DATA_SOURCE
and rel.target.type == FeastObjectType.FEATURE_VIEW
):
if rel.target.name not in feature_view_to_data_sources:
feature_view_to_data_sources[rel.target.name] = []
feature_view_to_data_sources[rel.target.name].append(rel.source.name)
# For each Entity -> FeatureView relationship, create Entity -> DataSource relationships
for rel in direct_relationships:
if (
rel.source.type == FeastObjectType.ENTITY
and rel.target.type == FeastObjectType.FEATURE_VIEW
):
# Find data sources that this feature view uses
if rel.target.name in feature_view_to_data_sources:
for data_source_name in feature_view_to_data_sources[
rel.target.name
]:
indirect_relationships.append(
EntityRelation(
source=rel.source, # The entity
target=EntityReference(
FeastObjectType.DATA_SOURCE,
data_source_name,
),
)
)
return indirect_relationships
def get_object_relationships(
self,
registry: Registry,
object_type: str,
object_name: str,
include_indirect: bool = False,
) -> List[EntityRelation]:
"""
Get all relationships for a specific object.
Args:
registry: The registry protobuf
object_type: Type of the object (dataSource, entity, featureView, featureService)
object_name: Name of the object
include_indirect: Whether to include indirect relationships
Returns:
List of relationships involving the specified object
"""
direct_relationships, indirect_relationships = self.generate_lineage(registry)
all_relationships = direct_relationships[:]
if include_indirect:
all_relationships.extend(indirect_relationships)
# Filter relationships involving the specified object
filtered_relationships = []
target_type = FeastObjectType(object_type)
for rel in all_relationships:
if (rel.source.type == target_type and rel.source.name == object_name) or (
rel.target.type == target_type and rel.target.name == object_name
):
filtered_relationships.append(rel)
return filtered_relationships
def get_object_lineage_graph(
self, registry: Registry, object_type: str, object_name: str, depth: int = 2
) -> Dict:
"""
Get a complete lineage graph for an object up to specified depth.
This can be used for more complex lineage queries and visualization.
"""
direct_relationships, indirect_relationships = self.generate_lineage(registry)
all_relationships = direct_relationships + indirect_relationships
# Build adjacency graph
graph: Dict[str, List[str]] = {}
for rel in all_relationships:
source_key = f"{rel.source.type.value}:{rel.source.name}"
target_key = f"{rel.target.type.value}:{rel.target.name}"
if source_key not in graph:
graph[source_key] = []
graph[source_key].append(target_key)
# Perform BFS to get subgraph up to specified depth
start_key = f"{object_type}:{object_name}"
visited = set()
result_nodes = set()
result_edges = []
def bfs(current_key, current_depth):
if current_depth > depth or current_key in visited:
return
visited.add(current_key)
result_nodes.add(current_key)
if current_key in graph:
for neighbor in graph[current_key]:
result_edges.append((current_key, neighbor))
result_nodes.add(neighbor)
bfs(neighbor, current_depth + 1)
bfs(start_key, 0)
return {"nodes": list(result_nodes), "edges": result_edges}