-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmonitoring_utils.py
More file actions
296 lines (252 loc) · 8.13 KB
/
Copy pathmonitoring_utils.py
File metadata and controls
296 lines (252 loc) · 8.13 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
"""Shared constants and helpers for monitoring across all offline store backends.
Every backend needs the same table names, column lists, primary keys,
empty-metric templates, and result-row normalization. Centralizing them
here avoids ~8x duplication and prevents column-list drift.
"""
import json
import math
from datetime import date, datetime
from typing import Any, Dict, List, Optional, Tuple
# ------------------------------------------------------------------ #
# Table / file names
# ------------------------------------------------------------------ #
MON_TABLE_FEATURE = "feast_monitoring_feature_metrics"
MON_TABLE_FEATURE_VIEW = "feast_monitoring_feature_view_metrics"
MON_TABLE_FEATURE_SERVICE = "feast_monitoring_feature_service_metrics"
MON_TABLE_JOB = "feast_monitoring_jobs"
MONITORING_DIR = "feast_monitoring"
MONITORING_PARQUET_FILES: Dict[str, str] = {
"feature": "feast_monitoring_feature_metrics.parquet",
"feature_view": "feast_monitoring_feature_view_metrics.parquet",
"feature_service": "feast_monitoring_feature_service_metrics.parquet",
"job": "feast_monitoring_jobs.parquet",
}
# ------------------------------------------------------------------ #
# Column definitions — (ordered, used by INSERT / SELECT / Parquet)
# ------------------------------------------------------------------ #
FEATURE_METRICS_COLUMNS: List[str] = [
"project_id",
"feature_view_name",
"feature_name",
"metric_date",
"granularity",
"data_source_type",
"computed_at",
"is_baseline",
"feature_type",
"row_count",
"null_count",
"null_rate",
"mean",
"stddev",
"min_val",
"max_val",
"p50",
"p75",
"p90",
"p95",
"p99",
"histogram",
]
FEATURE_METRICS_PK: List[str] = [
"project_id",
"feature_view_name",
"feature_name",
"metric_date",
"granularity",
"data_source_type",
]
FEATURE_VIEW_METRICS_COLUMNS: List[str] = [
"project_id",
"feature_view_name",
"metric_date",
"granularity",
"data_source_type",
"computed_at",
"is_baseline",
"total_row_count",
"total_features",
"features_with_nulls",
"avg_null_rate",
"max_null_rate",
]
FEATURE_VIEW_METRICS_PK: List[str] = [
"project_id",
"feature_view_name",
"metric_date",
"granularity",
"data_source_type",
]
FEATURE_SERVICE_METRICS_COLUMNS: List[str] = [
"project_id",
"feature_service_name",
"metric_date",
"granularity",
"data_source_type",
"computed_at",
"is_baseline",
"total_feature_views",
"total_features",
"avg_null_rate",
"max_null_rate",
]
FEATURE_SERVICE_METRICS_PK: List[str] = [
"project_id",
"feature_service_name",
"metric_date",
"granularity",
"data_source_type",
]
JOB_COLUMNS: List[str] = [
"job_id",
"project_id",
"feature_view_name",
"job_type",
"status",
"parameters",
"metric_date",
"started_at",
"completed_at",
"error_message",
"result_summary",
]
JOB_PK: List[str] = [
"job_id",
]
def monitoring_table_meta(
metric_type: str,
) -> Tuple[str, List[str], List[str]]:
"""Return (table_name, columns, pk_columns) for a metric type.
Raises ValueError for unknown metric types.
"""
if metric_type == "feature":
return MON_TABLE_FEATURE, FEATURE_METRICS_COLUMNS, FEATURE_METRICS_PK
if metric_type == "feature_view":
return (
MON_TABLE_FEATURE_VIEW,
FEATURE_VIEW_METRICS_COLUMNS,
FEATURE_VIEW_METRICS_PK,
)
if metric_type == "feature_service":
return (
MON_TABLE_FEATURE_SERVICE,
FEATURE_SERVICE_METRICS_COLUMNS,
FEATURE_SERVICE_METRICS_PK,
)
if metric_type == "job":
return MON_TABLE_JOB, JOB_COLUMNS, JOB_PK
raise ValueError(f"Unknown monitoring metric_type: '{metric_type}'")
def monitoring_parquet_meta(
metric_type: str,
) -> Tuple[str, List[str], List[str]]:
"""Return (parquet_filename, columns, pk_columns) for file-based backends.
File names match the SQL table names with a ``.parquet`` extension so
the mapping between backends is consistent.
"""
fname = MONITORING_PARQUET_FILES.get(metric_type)
if fname is None:
raise ValueError(f"Unknown monitoring metric_type: '{metric_type}'")
_, columns, pk = monitoring_table_meta(metric_type)
return fname, columns, pk
# ------------------------------------------------------------------ #
# Tiny helpers duplicated across backends
# ------------------------------------------------------------------ #
def opt_float(val: Any) -> Optional[float]:
"""Safely cast a value to float, returning None for None/NaN/Inf."""
if val is None:
return None
f = float(val)
if math.isnan(f) or math.isinf(f):
return None
return f
def empty_numeric_metric(feature_name: str) -> Dict[str, Any]:
"""Return a metric dict with all-None stats for a numeric feature."""
return {
"feature_name": feature_name,
"feature_type": "numeric",
"row_count": 0,
"null_count": 0,
"null_rate": 0.0,
"mean": None,
"stddev": None,
"min_val": None,
"max_val": None,
"p50": None,
"p75": None,
"p90": None,
"p95": None,
"p99": None,
"histogram": None,
}
def empty_categorical_metric(feature_name: str) -> Dict[str, Any]:
"""Return a metric dict with all-None stats for a categorical feature."""
return {
"feature_name": feature_name,
"feature_type": "categorical",
"row_count": 0,
"null_count": 0,
"null_rate": 0.0,
"mean": None,
"stddev": None,
"min_val": None,
"max_val": None,
"p50": None,
"p75": None,
"p90": None,
"p95": None,
"p99": None,
"histogram": None,
}
# ------------------------------------------------------------------ #
# Result-row normalization (used after SQL fetch or Parquet read)
# ------------------------------------------------------------------ #
def normalize_monitoring_row(record: Dict[str, Any]) -> Dict[str, Any]:
"""Normalize a monitoring metric dict for JSON serialization.
- Replaces float NaN / Inf with None (not JSON-serializable).
- Parses ``histogram`` from JSON string if needed.
- Converts ``metric_date`` / ``computed_at`` to ISO strings.
- Normalizes ``is_baseline`` to Python bool.
"""
import math
for key, val in record.items():
if isinstance(val, float) and (math.isnan(val) or math.isinf(val)):
record[key] = None
hist = record.get("histogram")
if isinstance(hist, str):
try:
record["histogram"] = json.loads(hist)
except (json.JSONDecodeError, TypeError):
pass
for key in ("metric_date", "computed_at"):
val = record.get(key)
if isinstance(val, (date, datetime)):
record[key] = val.isoformat()
baseline = record.get("is_baseline")
if baseline is not None:
record["is_baseline"] = bool(baseline)
return record
# ------------------------------------------------------------------ #
# View-level aggregate builder (shared by batch + log save paths)
# ------------------------------------------------------------------ #
def build_view_aggregate(
metrics_list: List[Dict[str, Any]],
) -> Dict[str, Any]:
"""Compute view-level aggregate stats from per-feature metrics.
Returns a dict with keys: total_row_count, total_features,
features_with_nulls, avg_null_rate, max_null_rate.
"""
null_rates = [
m["null_rate"] for m in metrics_list if m.get("null_rate") is not None
]
return {
"total_row_count": max(
(m["row_count"] for m in metrics_list if m.get("row_count") is not None),
default=0,
),
"total_features": len(metrics_list),
"features_with_nulls": sum(
1 for m in metrics_list if (m.get("null_count") or 0) > 0
),
"avg_null_rate": sum(null_rates) / len(null_rates) if null_rates else 0.0,
"max_null_rate": max(null_rates) if null_rates else 0.0,
}