-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindicator_models.py
More file actions
344 lines (274 loc) · 8.98 KB
/
Copy pathindicator_models.py
File metadata and controls
344 lines (274 loc) · 8.98 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
"""
指标血缘数据模型
定义指标血缘链路的所有数据结构,包括指标定义、算法配置、依赖关系、血缘图和查询结果
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Literal
MEASURE_LABELS: dict[str, str] = {
"001": "原始统计值(期末余额)",
"002": "月日均",
"003": "季日均",
"004": "年日均",
"005": "月初余额",
"006": "季初余额",
"007": "年初余额",
"008": "上月末余额",
"009": "上季末余额",
"010": "上年末余额",
"011": "月累计发生额",
"012": "季累计发生额",
"013": "月日均年化",
"014": "季日均年化",
"015": "年日均年化",
"016": "月发生额",
"017": "季发生额",
"018": "年发生额",
"019": "月环比增减",
"020": "季环比增减",
"021": "年环比增减",
"022": "月同比增减",
"023": "季同比增减",
"024": "年同比增减",
"025": "比月初增减",
"026": "比季初增减",
"027": "比年初增减",
"028": "月日均年化增量",
"029": "季累计",
}
INDEX_TYPE_LABELS: dict[str, str] = {
"1": "基础指标",
"2": "衍生指标",
"3": "特殊指标",
"6": "绩效指标",
}
ALGO_TYPE_LABELS: dict[str, str] = {
"1": "通用算法",
"2": "自定义算法",
"3": "年化通用算法",
"4": "临时指标转标准指标",
}
@dataclass
class IndicatorDef:
"""指标定义"""
index_no: str = ""
index_name: str = ""
index_bclass: str = ""
index_level1_class: str = ""
index_level2_class: str = ""
index_level3_class: str = ""
biz_cali: str = ""
tech_cali: str = ""
stat_period: str = ""
@dataclass
class IndicatorMeasureDef:
"""指标度量定义"""
index_measure: str = ""
measure_type: str = ""
subindex_measure: str = ""
index_no: str = ""
@dataclass
class IndicatorCalcBase:
"""基础指标算法配置,来源于 Excel 配置表 fdl_idx_para_index_calc"""
index_no: str = ""
index_measure: str = ""
algo_type: str = ""
call_level: str = ""
trg_table_name: str = ""
src_table_name: str = ""
measure_sql: str = ""
condition_sql: str = ""
sqlcc: str = ""
index_flag: str = ""
start_dt: str = ""
end_dt: str = ""
@dataclass
class IndicatorCalcGL:
"""总账指标算法配置,来源于 fdl_idx_para_index_calc_gl"""
index_no: str = ""
index_measure: str = ""
sign_no: int = 0
subj_no: str = ""
length_val: int = 0
amt_val: str = ""
start_dt: str = ""
end_dt: str = ""
@dataclass
class IndicatorRel:
"""指标依赖关系"""
index_no: str = ""
depend_index_nos: list[str] = field(default_factory=list)
@dataclass
class ProcedureIndicatorInfo:
"""存储过程指标加工元数据"""
proc_name: str = ""
step_order: int = 0
description: str = ""
index_type: str = ""
config_table: str = ""
target_table: str = ""
source_tables: list[str] = field(default_factory=list)
@dataclass
class IndicatorLineageNode:
"""指标血缘图节点
node_type 取值: "indicator" | "measure" | "table" | "field" | "procedure"
"""
node_id: str = ""
node_type: Literal["indicator", "measure", "table", "field", "procedure"] = "indicator"
index_no: str = ""
index_measure: str = ""
index_type: str = ""
algo_type: str = ""
label: str = ""
layer: str = ""
brch_type: str = ""
detail: dict = field(default_factory=dict)
@property
def display_label(self) -> str:
if self.label:
return self.label
if self.index_no and self.index_measure:
return f"{self.index_no}[{self.index_measure}]"
return self.node_id
@dataclass
class IndicatorLineageEdge:
"""指标血缘图边
edge_type 取值: "data_flow" | "calc_dependency" | "procedure_step" | "gl_mapping"
"""
edge_id: str = ""
source_id: str = ""
target_id: str = ""
edge_type: Literal["data_flow", "calc_dependency", "procedure_step", "gl_mapping"] = "data_flow"
procedure: str = ""
transform_logic: str = ""
algo_type: str = ""
condition_sql: str = ""
measure_sql: str = ""
@dataclass
class IndicatorLineageGraph:
"""指标血缘图容器"""
nodes: list[IndicatorLineageNode] = field(default_factory=list)
edges: list[IndicatorLineageEdge] = field(default_factory=list)
stats: dict = field(default_factory=dict)
@property
def node_count(self) -> int:
return len(self.nodes)
@property
def edge_count(self) -> int:
return len(self.edges)
def get_node(self, node_id: str) -> IndicatorLineageNode | None:
for node in self.nodes:
if node.node_id == node_id:
return node
return None
def get_upstream_nodes(self, node_id: str) -> list[IndicatorLineageNode]:
upstream_ids = {e.source_id for e in self.edges if e.target_id == node_id}
return [n for n in self.nodes if n.node_id in upstream_ids]
def get_downstream_nodes(self, node_id: str) -> list[IndicatorLineageNode]:
downstream_ids = {e.target_id for e in self.edges if e.source_id == node_id}
return [n for n in self.nodes if n.node_id in downstream_ids]
@dataclass
class IndicatorChainStep:
"""指标加工链路中的单个步骤"""
step_num: int = 0
index_no: str = ""
index_measure: str = ""
index_type: str = ""
algo_type: str = ""
procedure: str = ""
source_tables: list[str] = field(default_factory=list)
target_table: str = ""
transform_logic: str = ""
condition_sql: str = ""
measure_sql: str = ""
brch_type: str = ""
gl_subj_no: str = ""
gl_amt_val: str = ""
gl_sign_no: int = 0
@property
def is_gl_step(self) -> bool:
return self.algo_type == "2"
@property
def algo_label(self) -> str:
return ALGO_TYPE_LABELS.get(self.algo_type, self.algo_type)
@property
def measure_label(self) -> str:
return MEASURE_LABELS.get(self.index_measure, self.index_measure)
@property
def index_type_label(self) -> str:
return INDEX_TYPE_LABELS.get(self.index_type, self.index_type)
@dataclass
class IndicatorChain:
"""指标完整加工链路,从源头到目标"""
target_index_no: str = ""
target_measure: str = ""
steps: list[IndicatorChainStep] = field(default_factory=list)
depth: int = 0
@property
def step_count(self) -> int:
return len(self.steps)
@property
def procedures_involved(self) -> list[str]:
seen: list[str] = []
for s in self.steps:
if s.procedure and s.procedure not in seen:
seen.append(s.procedure)
return seen
@property
def tables_involved(self) -> list[str]:
seen: list[str] = []
for s in self.steps:
for t in s.source_tables:
if t and t not in seen:
seen.append(t)
if s.target_table and s.target_table not in seen:
seen.append(s.target_table)
return seen
@property
def has_gl_step(self) -> bool:
return any(s.is_gl_step for s in self.steps)
@dataclass
class IndicatorLineageResult:
"""指标血缘查询结果"""
target_index_no: str = ""
target_measure: str = ""
graph: IndicatorLineageGraph = field(default_factory=IndicatorLineageGraph)
chains: list[IndicatorChain] = field(default_factory=list)
query_time_ms: float = 0.0
@property
def chain_count(self) -> int:
return len(self.chains)
@property
def max_depth(self) -> int:
if not self.chains:
return 0
return max(c.depth for c in self.chains)
@property
def measure_label(self) -> str:
return MEASURE_LABELS.get(self.target_measure, self.target_measure)
@dataclass
class IndicatorConfigResult:
"""解析后的指标配置容器"""
base_calcs: list[IndicatorCalcBase] = field(default_factory=list)
gl_calcs: list[IndicatorCalcGL] = field(default_factory=list)
relations: list[IndicatorRel] = field(default_factory=list)
procedures: dict[str, ProcedureIndicatorInfo] = field(default_factory=dict)
parse_time_sec: float = 0.0
@property
def base_calc_count(self) -> int:
return len(self.base_calcs)
@property
def gl_calc_count(self) -> int:
return len(self.gl_calcs)
@property
def relation_count(self) -> int:
return len(self.relations)
@property
def procedure_count(self) -> int:
return len(self.procedures)
def get_base_calcs_by_index(self, index_no: str) -> list[IndicatorCalcBase]:
return [c for c in self.base_calcs if c.index_no == index_no]
def get_gl_calcs_by_index(self, index_no: str) -> list[IndicatorCalcGL]:
return [c for c in self.gl_calcs if c.index_no == index_no]
def get_dependents(self, index_no: str) -> list[str]:
return [r.index_no for r in self.relations if index_no in r.depend_index_nos]