File size: 9,224 Bytes
e00e744
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Agent result models for mapping JSON responses from lineage agents.

This module contains classes for representing the structured results returned
by lineage analysis agents in a type-safe manner.
"""

from typing import Dict, Any, List, Optional


class SchemaField:
    """Schema field configuration for agent results"""
    
    def __init__(self, name: str, type: str, description: str):
        self.name = name
        self.type = type
        self.description = description
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'SchemaField':
        """Create SchemaField from dictionary"""
        return cls(
            name=data.get('name', ''),
            type=data.get('type', ''),
            description=data.get('description', '')
        )
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            'name': self.name,
            'type': self.type,
            'description': self.description
        }


class Schema:
    """Schema configuration for agent results"""
    
    def __init__(self, fields: List[SchemaField]):
        self.fields = fields
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Schema':
        """Create Schema from dictionary"""
        fields = [SchemaField.from_dict(field) for field in data.get('fields', [])]
        return cls(fields=fields)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            'fields': [field.to_dict() for field in self.fields]
        }


class Transformation:
    """Transformation configuration for column lineage"""
    
    def __init__(self, type: str, subtype: str, description: str, masking: bool = False):
        self.type = type
        self.subtype = subtype
        self.description = description
        self.masking = masking
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Transformation':
        """Create Transformation from dictionary"""
        return cls(
            type=data.get('type', ''),
            subtype=data.get('subtype', ''),
            description=data.get('description', ''),
            masking=data.get('masking', False)
        )
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            'type': self.type,
            'subtype': self.subtype,
            'description': self.description,
            'masking': self.masking
        }


class InputField:
    """Input field configuration for column lineage"""
    
    def __init__(self, namespace: str, name: str, field: str, 
                 transformations: List[Transformation]):
        self.namespace = namespace
        self.name = name
        self.field = field
        self.transformations = transformations
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'InputField':
        """Create InputField from dictionary"""
        transformations = [Transformation.from_dict(t) for t in data.get('transformations', [])]
        return cls(
            namespace=data.get('namespace', ''),
            name=data.get('name', ''),
            field=data.get('field', ''),
            transformations=transformations
        )
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            'namespace': self.namespace,
            'name': self.name,
            'field': self.field,
            'transformations': [t.to_dict() for t in self.transformations]
        }


class ColumnLineageField:
    """Column lineage field configuration"""
    
    def __init__(self, input_fields: List[InputField]):
        self.input_fields = input_fields
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'ColumnLineageField':
        """Create ColumnLineageField from dictionary"""
        input_fields = [InputField.from_dict(field) for field in data.get('inputFields', [])]
        return cls(input_fields=input_fields)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            'inputFields': [field.to_dict() for field in self.input_fields]
        }


class ColumnLineage:
    """Column lineage configuration"""
    
    def __init__(self, fields: Dict[str, ColumnLineageField]):
        self.fields = fields
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'ColumnLineage':
        """Create ColumnLineage from dictionary"""
        fields = {
            field_name: ColumnLineageField.from_dict(field_data)
            for field_name, field_data in data.get('fields', {}).items()
        }
        return cls(fields=fields)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            'fields': {
                field_name: field_data.to_dict()
                for field_name, field_data in self.fields.items()
            }
        }


class InputFacets:
    """Input facets configuration for agent results"""
    
    def __init__(self, schema: Optional[Schema] = None):
        self.schema = schema
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'InputFacets':
        """Create InputFacets from dictionary"""
        schema = Schema.from_dict(data.get('schema', {})) if data.get('schema') else None
        return cls(schema=schema)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        result = {}
        if self.schema:
            result['schema'] = self.schema.to_dict()
        return result


class Input:
    """Input configuration for agent results"""
    
    def __init__(self, namespace: str, name: str, facets: Optional[InputFacets] = None):
        self.namespace = namespace
        self.name = name
        self.facets = facets
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Input':
        """Create Input from dictionary"""
        facets = InputFacets.from_dict(data.get('facets', {})) if data.get('facets') else None
        return cls(
            namespace=data.get('namespace', ''),
            name=data.get('name', ''),
            facets=facets
        )
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        result = {
            'namespace': self.namespace,
            'name': self.name
        }
        if self.facets:
            result['facets'] = self.facets.to_dict()
        return result


class OutputFacets:
    """Output facets configuration for agent results"""
    
    def __init__(self, column_lineage: Optional[ColumnLineage] = None):
        self.column_lineage = column_lineage
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'OutputFacets':
        """Create OutputFacets from dictionary"""
        column_lineage = ColumnLineage.from_dict(data.get('columnLineage', {})) if data.get('columnLineage') else None
        return cls(column_lineage=column_lineage)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        result = {}
        if self.column_lineage:
            result['columnLineage'] = self.column_lineage.to_dict()
        return result


class Output:
    """Output configuration for agent results"""
    
    def __init__(self, namespace: str, name: str, facets: Optional[OutputFacets] = None):
        self.namespace = namespace
        self.name = name
        self.facets = facets
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Output':
        """Create Output from dictionary"""
        facets = OutputFacets.from_dict(data.get('facets', {})) if data.get('facets') else None
        return cls(
            namespace=data.get('namespace', ''),
            name=data.get('name', ''),
            facets=facets
        )
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        result = {
            'namespace': self.namespace,
            'name': self.name
        }
        if self.facets:
            result['facets'] = self.facets.to_dict()
        return result


class AgentResult:
    """Main result class for agent lineage analysis"""
    
    def __init__(self, inputs: List[Input], outputs: List[Output]):
        self.inputs = inputs
        self.outputs = outputs
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'AgentResult':
        """Create AgentResult from dictionary"""
        inputs = [Input.from_dict(input_data) for input_data in data.get('inputs', [])]
        outputs = [Output.from_dict(output_data) for output_data in data.get('outputs', [])]
        return cls(inputs=inputs, outputs=outputs)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            'inputs': [input_obj.to_dict() for input_obj in self.inputs],
            'outputs': [output_obj.to_dict() for output_obj in self.outputs]
        }
    
    def __str__(self) -> str:
        """String representation"""
        return f"AgentResult(inputs={len(self.inputs)}, outputs={len(self.outputs)})"
    
    def __repr__(self) -> str:
        """Detailed string representation"""
        return f"AgentResult(inputs={self.inputs}, outputs={self.outputs})"