sibi-dst 2025.1.5__tar.gz → 2025.1.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/PKG-INFO +3 -1
  2. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/pyproject.toml +3 -1
  3. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/__init__.py +2 -1
  4. sibi_dst-2025.1.7/sibi_dst/df_helper/_artifact_updater_multi_wrapper.py +420 -0
  5. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/_parquet_artifact.py +10 -32
  6. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/_parquet_reader.py +6 -21
  7. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/parquet/_parquet_options.py +2 -1
  8. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/base.py +23 -3
  9. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/clickhouse_writer.py +2 -1
  10. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/data_wrapper.py +9 -19
  11. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/update_planner.py +5 -5
  12. sibi_dst-2025.1.5/sibi_dst/df_helper/_artifact_updater_multi_wrapper.py +0 -431
  13. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/README.md +0 -0
  14. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/__init__.py +0 -0
  15. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/_df_helper.py +0 -0
  16. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/__init__.py +0 -0
  17. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/http/__init__.py +0 -0
  18. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/http/_http_config.py +0 -0
  19. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/parquet/__init__.py +0 -0
  20. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/parquet/_filter_handler.py +0 -0
  21. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/sqlalchemy/__init__.py +0 -0
  22. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/sqlalchemy/_db_connection.py +0 -0
  23. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/sqlalchemy/_io_dask.py +0 -0
  24. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/sqlalchemy/_load_from_db.py +0 -0
  25. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/backends/sqlalchemy/_sql_model_builder.py +0 -0
  26. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/core/__init__.py +0 -0
  27. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/core/_defaults.py +0 -0
  28. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/core/_filter_handler.py +0 -0
  29. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/core/_params_config.py +0 -0
  30. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/core/_query_config.py +0 -0
  31. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/df_helper/data_cleaner.py +0 -0
  32. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/geopy_helper/__init__.py +0 -0
  33. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/geopy_helper/geo_location_service.py +0 -0
  34. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/geopy_helper/utils.py +0 -0
  35. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/osmnx_helper/__init__.py +0 -0
  36. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/osmnx_helper/base_osm_map.py +0 -0
  37. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/osmnx_helper/basemaps/__init__.py +0 -0
  38. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/osmnx_helper/basemaps/calendar_html.py +0 -0
  39. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/osmnx_helper/basemaps/router_plotter.py +0 -0
  40. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/osmnx_helper/utils.py +0 -0
  41. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/tests/__init__.py +0 -0
  42. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/tests/test_data_wrapper_class.py +0 -0
  43. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/__init__.py +0 -0
  44. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/credentials.py +0 -0
  45. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/data_from_http_source.py +0 -0
  46. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/data_utils.py +0 -0
  47. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/date_utils.py +0 -0
  48. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/df_utils.py +0 -0
  49. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/file_utils.py +0 -0
  50. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/filepath_generator.py +0 -0
  51. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/log_utils.py +0 -0
  52. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/manifest_manager.py +0 -0
  53. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/parquet_saver.py +0 -0
  54. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/phone_formatter.py +0 -0
  55. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/storage_config.py +0 -0
  56. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/storage_manager.py +0 -0
  57. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/utils/webdav_client.py +0 -0
  58. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/__init__.py +0 -0
  59. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/__init__.py +0 -0
  60. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/_df_helper.py +0 -0
  61. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/__init__.py +0 -0
  62. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlalchemy/__init__.py +0 -0
  63. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlalchemy/_db_connection.py +0 -0
  64. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlalchemy/_io_dask.py +0 -0
  65. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlalchemy/_load_from_db.py +0 -0
  66. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlalchemy/_model_builder.py +0 -0
  67. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlmodel/__init__.py +0 -0
  68. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlmodel/_db_connection.py +0 -0
  69. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlmodel/_io_dask.py +0 -0
  70. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlmodel/_load_from_db.py +0 -0
  71. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/backends/sqlmodel/_model_builder.py +0 -0
  72. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/core/__init__.py +0 -0
  73. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/core/_filter_handler.py +0 -0
  74. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/core/_params_config.py +0 -0
  75. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/df_helper/core/_query_config.py +0 -0
  76. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/utils/__init__.py +0 -0
  77. {sibi_dst-2025.1.5 → sibi_dst-2025.1.7}/sibi_dst/v2/utils/log_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sibi-dst
3
- Version: 2025.1.5
3
+ Version: 2025.1.7
4
4
  Summary: Data Science Toolkit
5
5
  Author: Luis Valverde
6
6
  Author-email: lvalverdeb@gmail.com
@@ -13,6 +13,8 @@ Requires-Dist: clickhouse-connect (>=0.8.18,<0.9.0)
13
13
  Requires-Dist: clickhouse-driver (>=0.2.9,<0.3.0)
14
14
  Requires-Dist: dask[complete] (>=2025.5.1,<2026.0.0)
15
15
  Requires-Dist: mysqlclient (>=2.2.7,<3.0.0)
16
+ Requires-Dist: opentelemetry-exporter-otlp (>=1.35.0,<2.0.0)
17
+ Requires-Dist: opentelemetry-sdk (>=1.35.0,<2.0.0)
16
18
  Requires-Dist: pandas (>=2.3.1,<3.0.0)
17
19
  Requires-Dist: psycopg2 (>=2.9.10,<3.0.0)
18
20
  Requires-Dist: pyarrow (>=20.0.0,<21.0.0)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "sibi-dst"
3
- version = "2025.1.5"
3
+ version = "2025.1.7"
4
4
  description = "Data Science Toolkit"
5
5
  authors = ["Luis Valverde <lvalverdeb@gmail.com>"]
6
6
  readme = "README.md"
@@ -22,6 +22,8 @@ sqlalchemy = "^2.0.41"
22
22
  pymysql = "^1.1.1"
23
23
  pyarrow = "^20.0.0"
24
24
  rich = "^14.0.0"
25
+ opentelemetry-exporter-otlp = "^1.35.0"
26
+ opentelemetry-sdk = "^1.35.0"
25
27
 
26
28
  [tool.poetry.group.dev]
27
29
  optional = true
@@ -3,11 +3,12 @@ from __future__ import annotations
3
3
  from ._df_helper import DfHelper
4
4
  from ._parquet_artifact import ParquetArtifact
5
5
  from ._parquet_reader import ParquetReader
6
- from ._artifact_updater_multi_wrapper import ArtifactUpdaterMultiWrapperThreaded
6
+ from ._artifact_updater_multi_wrapper import ArtifactUpdaterMultiWrapperThreaded, ArtifactUpdaterMultiWrapperAsync
7
7
 
8
8
  __all__ = [
9
9
  'DfHelper',
10
10
  'ParquetArtifact',
11
11
  'ParquetReader',
12
12
  'ArtifactUpdaterMultiWrapperThreaded',
13
+ 'ArtifactUpdaterMultiWrapperAsync',
13
14
  ]
@@ -0,0 +1,420 @@
1
+ import time
2
+ from concurrent.futures import ThreadPoolExecutor, as_completed
3
+ from typing import Any, Callable, Dict, List, Optional, Type
4
+
5
+ from sibi_dst.utils import ManagedResource
6
+
7
+ class ArtifactUpdaterMultiWrapperThreaded(ManagedResource):
8
+ """
9
+ Updates artifacts concurrently using a ThreadPoolExecutor.
10
+
11
+ This version is refactored for a pure multi-threaded environment, aligning
12
+ the orchestration model with the underlying threaded workers (DataWrapper).
13
+ """
14
+ wrapped_classes: Dict[str, List[Type]]
15
+ def __init__(
16
+ self,
17
+ wrapped_classes: Dict[str, List[Type]],
18
+ *,
19
+ max_workers: int = 4,
20
+ retry_attempts: int = 3,
21
+ backoff_base: int = 2,
22
+ backoff_max: int = 60,
23
+ backoff_jitter: float = 0.1,
24
+ priority_fn: Optional[Callable[[Type], int]] = None,
25
+ artifact_class_kwargs: Optional[Dict[str, Any]] = None,
26
+ **kwargs: Dict[str, Any]
27
+ ) -> None:
28
+ super().__init__(**kwargs)
29
+ self.wrapped_classes = wrapped_classes
30
+ self.max_workers = max_workers
31
+ self.retry_attempts = retry_attempts
32
+ self.backoff_base = backoff_base
33
+ self.backoff_max = backoff_max
34
+ self.backoff_jitter = backoff_jitter
35
+ self.priority_fn = priority_fn
36
+ # Default artifact init kwargs
37
+ today = datetime.datetime.today() + datetime.timedelta(days=1)
38
+ default_kwargs = {
39
+ 'parquet_start_date': today.strftime('%Y-%m-%d'),
40
+ 'parquet_end_date': today.strftime('%Y-%m-%d'),
41
+ 'logger': self.logger,
42
+ 'debug': self.debug,
43
+ 'fs': self.fs,
44
+ 'verbose': self.verbose,
45
+ }
46
+ self.artifact_class_kwargs = artifact_class_kwargs or default_kwargs.copy()
47
+
48
+ # State tracking
49
+ self.completion_times: Dict[str, float] = {}
50
+ self.failed: List[str] = []
51
+ self.original_classes: List[Type] = []
52
+
53
+ def get_artifact_classes(self, data_type: str) -> List[Type]:
54
+ """Retrieve artifact classes by data type."""
55
+ self.logger.info(f"Fetching artifact classes for '{data_type}'")
56
+ classes = self.wrapped_classes.get(data_type)
57
+ if not classes:
58
+ raise ValueError(f"Unsupported data type: {data_type}")
59
+ self.logger.info(f"Found {len(classes)} artifact classes for '{data_type}'")
60
+ return classes
61
+
62
+ def estimate_priority(self, artifact_cls: Type) -> int:
63
+ """
64
+ Determines task priority. Lower values run first.
65
+ Note: This is a blocking call and will run sequentially before updates start.
66
+ """
67
+ name = artifact_cls.__name__
68
+ # Custom priority function takes precedence
69
+ if self.priority_fn:
70
+ try:
71
+ return self.priority_fn(artifact_cls)
72
+ except Exception as e:
73
+ self.logger.warning(f"priority_fn error for {name}: {e}")
74
+
75
+ # # Fallback to size estimate if available
76
+ # if hasattr(artifact_cls, 'get_size_estimate'):
77
+ # try:
78
+ # # This performs blocking I/O
79
+ # return artifact_cls(**self.artifact_class_kwargs).get_size_estimate()
80
+ #
81
+ # except Exception as e:
82
+ # self.logger.warning(f"get_size_estimate failed for {name}: {e}")
83
+
84
+ # Default priority
85
+ return 999
86
+
87
+ def _update_artifact_with_retry(self, artifact_cls: Type, update_kwargs: Dict[str, Any]) -> str:
88
+ """
89
+ A blocking worker function that handles instantiation, update, and retries for a single artifact.
90
+ This function is designed to be run in a ThreadPoolExecutor.
91
+ """
92
+ name = artifact_cls.__name__
93
+ self.logger.debug(f"Worker thread starting update for {name}")
94
+
95
+ for attempt in range(1, self.retry_attempts + 1):
96
+ try:
97
+ # Instantiate and update directly within the worker thread
98
+ artifact_instance = artifact_cls(**self.artifact_class_kwargs)
99
+ artifact_instance.update_parquet(**update_kwargs)
100
+
101
+ self.logger.info(f"✅ {name} updated successfully on attempt {attempt}")
102
+ return name # Return the name on success
103
+
104
+ except Exception as e:
105
+ self.logger.error(f"Error on {name} attempt {attempt}/{self.retry_attempts}: {e}", exc_info=self.debug)
106
+ if attempt < self.retry_attempts:
107
+ delay = min(self.backoff_base ** (attempt - 1), self.backoff_max)
108
+ delay *= 1 + random.uniform(0, self.backoff_jitter)
109
+ self.logger.info(f"Sleeping {delay:.1f}s before retrying {name}")
110
+ time.sleep(delay)
111
+
112
+ # If all retries fail, raise an exception to be caught by the main loop
113
+ raise RuntimeError(f"{name} failed after {self.retry_attempts} attempts.")
114
+
115
+ async def update_data(self, data_type: str, **kwargs: Any) -> None:
116
+ """
117
+ Entry point to update all artifacts of a given type using a ThreadPoolExecutor.
118
+ """
119
+ self.logger.debug(f"Starting multi-threaded update for '{data_type}' with kwargs={kwargs}")
120
+
121
+ # Reset state for this run
122
+ self.completion_times.clear()
123
+ self.failed.clear()
124
+ self.original_classes = self.get_artifact_classes(data_type)
125
+
126
+ # Sequentially estimate priorities and sort classes before execution
127
+ self.logger.debug("Estimating priorities to order tasks...")
128
+ ordered_classes = sorted(self.original_classes, key=self.estimate_priority)
129
+ self.logger.debug("Priority estimation complete. Submitting tasks to thread pool.")
130
+
131
+ start_time = time.monotonic()
132
+
133
+ with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
134
+ future_to_class_name = {
135
+ executor.submit(self._update_artifact_with_retry, cls, kwargs): cls.__name__
136
+ for cls in ordered_classes
137
+ }
138
+
139
+ for future in as_completed(future_to_class_name):
140
+ name = future_to_class_name[future]
141
+ try:
142
+ # result() will re-raise the exception from the worker if one occurred
143
+ future.result()
144
+ # If no exception, the task succeeded
145
+ self.completion_times[name] = time.monotonic() - start_time
146
+ except Exception as e:
147
+ self.logger.error(f"✖️ {name} permanently failed. See error log above.")
148
+ self.failed.append(name)
149
+
150
+ # Log final status
151
+ total = len(self.original_classes)
152
+ completed = len(self.completion_times)
153
+ failed_count = len(self.failed)
154
+ self.logger.info(f"All artifacts processed: total={total}, completed={completed}, failed={failed_count}")
155
+
156
+ def get_update_status(self) -> Dict[str, Any]:
157
+ """Returns a summary status including completion times."""
158
+ completed_set = set(self.completion_times.keys())
159
+ failed_set = set(self.failed)
160
+ pending_set = {cls.__name__ for cls in self.original_classes} - completed_set - failed_set
161
+
162
+ return {
163
+ 'total': len(self.original_classes),
164
+ 'completed': list(completed_set),
165
+ 'failed': list(failed_set),
166
+ 'pending': list(pending_set),
167
+ 'completion_times': self.completion_times,
168
+ }
169
+
170
+ @staticmethod
171
+ def format_status_table(status: Dict[str, Any]) -> str:
172
+ """Formats the status dictionary into a readable table."""
173
+ lines = [
174
+ f"Total: {status['total']}",
175
+ f"Completed: {len(status['completed'])}",
176
+ f"Failed: {len(status['failed'])}",
177
+ f"Pending: {len(status['pending'])}",
178
+ "\nPer-artifact completion times (seconds):"
179
+ ]
180
+ sorted_times = sorted(status['completion_times'].items(), key=lambda item: item[1], reverse=True)
181
+ for name, duration in sorted_times:
182
+ lines.append(f" - {name:<30}: {duration:.2f}s")
183
+ if status['failed']:
184
+ lines.append("\nFailed artifacts:")
185
+ for name in status['failed']:
186
+ lines.append(f" - {name}")
187
+ return "\n".join(lines)
188
+
189
+
190
+ import asyncio
191
+ import datetime
192
+ import random
193
+ from typing import Any, Callable, Dict, List, Optional, Type
194
+
195
+ class ArtifactUpdaterMultiWrapperAsync(ManagedResource):
196
+ """
197
+ Simplified wrapper that updates artifacts concurrently using an asyncio.Semaphore.
198
+
199
+ Features:
200
+ - Caps concurrency at max_workers via semaphore
201
+ - Optionally prioritises tasks via a priority function or static method on artifact classes
202
+ - Tracks per-artifact completion times
203
+ - Configurable retry/backoff strategy
204
+ - Optional metrics integration
205
+ - Thread-safe within a single asyncio loop
206
+
207
+ Usage:
208
+ wrapper = ArtifactUpdaterMultiWrapper(
209
+ wrapped_classes={
210
+ 'mydata': [DataArtifactA, DataArtifactB],
211
+ },
212
+ max_workers=4,
213
+ retry_attempts=3,
214
+ update_timeout_seconds=600,
215
+ backoff_base=2,
216
+ backoff_max=60,
217
+ backoff_jitter=0.1,
218
+ priority_fn=None, # or custom
219
+ metrics_client=None,
220
+ debug=True,
221
+ logger=None,
222
+ artifact_class_kwargs={
223
+ 'fs': my_fs,
224
+ 'parquet_storage_path': 's3://bucket/data',
225
+ 'logger': my_logger,
226
+ 'debug': True,
227
+ }
228
+ )
229
+ await wrapper.update_data('mydata', period='ytd', overwrite=True)
230
+ """
231
+ def __init__(
232
+ self,
233
+ wrapped_classes: Dict[str, List[Type]],
234
+ *,
235
+ max_workers: int = 3,
236
+ retry_attempts: int = 3,
237
+ update_timeout_seconds: int = 600,
238
+ backoff_base: int = 2,
239
+ backoff_max: Optional[int] = 60,
240
+ backoff_jitter: float = 0.1,
241
+ priority_fn: Optional[Callable[[Type], int]] = None,
242
+ metrics_client: Any = None,
243
+ artifact_class_kwargs: Optional[Dict[str, Any]] = None,
244
+ **kwargs: Dict[str, Any]
245
+ ) -> None:
246
+ super().__init__(**kwargs)
247
+ self.wrapped_classes = wrapped_classes
248
+ self.max_workers = max_workers
249
+ self.retry_attempts = retry_attempts
250
+ self.update_timeout_seconds = update_timeout_seconds
251
+ self.backoff_base = backoff_base
252
+ self.backoff_max = backoff_max
253
+ self.backoff_jitter = backoff_jitter
254
+ self.priority_fn = priority_fn
255
+ self.metrics_client = metrics_client
256
+
257
+ # Default artifact init kwargs
258
+ today = datetime.datetime.today() + datetime.timedelta(days=1)
259
+ default_kwargs = {
260
+ 'parquet_start_date': today.strftime('%Y-%m-%d'),
261
+ 'parquet_end_date': today.strftime('%Y-%m-%d'),
262
+ 'logger': self.logger,
263
+ 'debug': self.debug,
264
+ 'fs': self.fs,
265
+ 'verbose': self.verbose,
266
+ }
267
+ self.artifact_class_kwargs = artifact_class_kwargs or default_kwargs.copy()
268
+
269
+ # State
270
+ self.completion_times: Dict[str, float] = {}
271
+ self.failed: List[str] = []
272
+ self.original_classes: List[Type] = []
273
+
274
+ def get_artifact_classes(self, data_type: str) -> List[Type]:
275
+ """
276
+ Retrieve artifact classes by data type.
277
+ """
278
+ self.logger.info(f"Fetching artifact classes for '{data_type}'")
279
+ if data_type not in self.wrapped_classes:
280
+ raise ValueError(f"Unsupported data type: {data_type}")
281
+ classes = self.wrapped_classes[data_type]
282
+ self.logger.info(f"Found {len(classes)} artifact classes for '{data_type}'")
283
+ return classes
284
+
285
+ def estimate_priority(self, artifact_cls: Type) -> int:
286
+ """
287
+ Determine task priority for ordering. Lower values run first.
288
+ """
289
+ name = artifact_cls.__name__
290
+ if self.priority_fn:
291
+ try:
292
+ pr = self.priority_fn(artifact_cls)
293
+ self.logger.debug(f"priority_fn for {name}: {pr}")
294
+ return pr
295
+ except Exception as e:
296
+ self.logger.warning(f"priority_fn error for {name}: {e}")
297
+ try:
298
+ fs = self.artifact_class_kwargs.get('fs')
299
+ path = self.artifact_class_kwargs.get('parquet_storage_path')
300
+ pr=1
301
+ if hasattr(artifact_cls, 'get_size_estimate'):
302
+ pr = artifact_cls.get_size_estimate(fs, path)
303
+ self.logger.debug(f"Estimated priority for {name}: {pr}")
304
+ return pr
305
+ except Exception:
306
+ return 1
307
+
308
+ async def _bounded_update(self, artifact_cls: Type, sem: asyncio.Semaphore, **update_kwargs) -> None:
309
+ """
310
+ Wrap update_artifact in a semaphore slot to limit concurrency.
311
+ """
312
+ async with sem:
313
+ name = artifact_cls.__name__
314
+ start = asyncio.get_event_loop().time()
315
+ self.logger.info(f"Starting update for {name}")
316
+ try:
317
+ for attempt in range(1, self.retry_attempts + 1):
318
+ try:
319
+ artifact = await asyncio.to_thread(
320
+ artifact_cls, **self.artifact_class_kwargs
321
+ )
322
+ await asyncio.wait_for(
323
+ asyncio.to_thread(
324
+ artifact.update_parquet, **update_kwargs
325
+ ),
326
+ timeout=self.update_timeout_seconds
327
+ )
328
+ duration = asyncio.get_event_loop().time() - start
329
+ self.completion_times[name] = duration
330
+ self.logger.info(f"✅ {name} updated in {duration:.2f}s (attempt {attempt})")
331
+ if self.metrics_client:
332
+ self.metrics_client.increment('task_succeeded')
333
+ return
334
+ except asyncio.TimeoutError:
335
+ self.logger.warning(f"Timeout on {name}, attempt {attempt}")
336
+ except Exception as e:
337
+ self.logger.error(f"Error on {name} attempt {attempt}: {e}")
338
+
339
+ delay = min(self.backoff_base ** (attempt - 1), self.backoff_max)
340
+ delay *= 1 + random.uniform(0, self.backoff_jitter)
341
+ self.logger.info(f"Sleeping {delay:.1f}s before retrying {name}")
342
+ await asyncio.sleep(delay)
343
+
344
+ except asyncio.CancelledError:
345
+ self.logger.warning(f"{name} update cancelled")
346
+ raise
347
+
348
+ # permanent failure
349
+ self.logger.error(f"✖️ {name} permanently failed after {self.retry_attempts} attempts")
350
+ if self.metrics_client:
351
+ self.metrics_client.increment('task_failed')
352
+ self.failed.append(name)
353
+
354
+ async def update_data(self, data_type: str, **kwargs: Any) -> None:
355
+ """
356
+ Entry point to update all artifacts of a given type concurrently.
357
+ """
358
+ self.logger.info(f"Starting update_data for '{data_type}' with kwargs={kwargs}")
359
+
360
+ # RESET STATE
361
+ self.completion_times.clear()
362
+ self.failed.clear()
363
+ self.original_classes = self.get_artifact_classes(data_type)
364
+
365
+ # NON-DESTRUCTIVE SORTING
366
+ ordered = sorted(self.original_classes, key=self.estimate_priority)
367
+
368
+ sem = asyncio.Semaphore(self.max_workers)
369
+ tasks = [
370
+ asyncio.create_task(self._bounded_update(cls, sem, **kwargs))
371
+ for cls in ordered
372
+ ]
373
+
374
+ try:
375
+ for coro in asyncio.as_completed(tasks):
376
+ await coro
377
+ except asyncio.CancelledError:
378
+ self.logger.warning("update_data was cancelled—aborting remaining retries")
379
+ for t in tasks:
380
+ t.cancel()
381
+ raise
382
+ finally:
383
+ total = len(self.original_classes)
384
+ completed = len(self.completion_times)
385
+ failed = len(self.failed)
386
+ self.logger.info(f"All artifacts processed: total={total}, completed={completed}, failed={failed}")
387
+
388
+ def get_update_status(self) -> Dict[str, Any]:
389
+ """
390
+ Returns summary status including completion times.
391
+ """
392
+ total = len(self.original_classes)
393
+ completed = set(self.completion_times.keys())
394
+ failed = set(self.failed)
395
+ pending = {cls.__name__ for cls in self.original_classes} - completed - failed
396
+
397
+ return {
398
+ 'total': total,
399
+ 'completed': list(completed),
400
+ 'failed': list(failed),
401
+ 'pending': list(pending),
402
+ 'completion_times': self.completion_times,
403
+ }
404
+
405
+ @staticmethod
406
+ def format_status_table(status: Dict[str, Any]) -> str:
407
+ """
408
+ Formats the status dict into a readable table.
409
+ """
410
+ lines = [
411
+ f"Total: {status['total']}",
412
+ f"Completed: {len(status['completed'])} {status['completed']}",
413
+ f"Failed: {len(status['failed'])} {status['failed']}",
414
+ f"Pending: {len(status['pending'])} {status['pending']}",
415
+ "",
416
+ "Per-artifact timings:"
417
+ ]
418
+ for name, dur in status['completion_times'].items():
419
+ lines.append(f" {name}: {dur:.2f}s")
420
+ return "\n".join(lines)
@@ -1,8 +1,7 @@
1
1
  from __future__ import annotations
2
2
  import datetime
3
- import logging
4
3
  import threading
5
- from typing import Optional, Any, Dict
4
+ from typing import Optional, Any, Dict, ClassVar
6
5
 
7
6
  import dask.dataframe as dd
8
7
  import fsspec
@@ -55,7 +54,7 @@ class ParquetArtifact(DfHelper):
55
54
  :ivar fs: Filesystem object used for storage operations.
56
55
  :type fs: fsspec.AbstractFileSystem
57
56
  """
58
- DEFAULT_CONFIG = {
57
+ DEFAULT_CONFIG: ClassVar[Dict[str, str]] = {
59
58
  'backend': 'parquet'
60
59
  }
61
60
 
@@ -91,8 +90,6 @@ class ParquetArtifact(DfHelper):
91
90
  }
92
91
  self.df: Optional[dd.DataFrame] = None
93
92
  super().__init__(**self.config)
94
- #self._own_logger = False
95
- #self._setup_logging()
96
93
  self.data_wrapper_class = data_wrapper_class
97
94
 
98
95
  self.date_field = self._validate_required('date_field')
@@ -101,16 +98,6 @@ class ParquetArtifact(DfHelper):
101
98
  self.parquet_start_date = self._validate_required('parquet_start_date')
102
99
  self.parquet_end_date = self._validate_required('parquet_end_date')
103
100
 
104
- # Filesystem setup
105
- #self.filesystem_type = self.config.setdefault('filesystem_type', 'file')
106
- #self.filesystem_options = self.config.setdefault('filesystem_options', {})
107
- #self.fs = self.config.setdefault('fs', None)
108
- #self._own_fs = self.fs is None
109
- #if self.fs is None:
110
- # self.fs = fsspec.filesystem(self.filesystem_type, **self.filesystem_options)
111
- # self._own_fs = True
112
- #self.config.setdefault('fs', self.fs)
113
- ## Populate to parameters to pass to data_wrapper_class
114
101
  self.class_params = self.config.pop('class_params', {
115
102
  'debug': self.debug,
116
103
  'logger': self.logger,
@@ -125,15 +112,6 @@ class ParquetArtifact(DfHelper):
125
112
  self.update_planner_params = {}
126
113
  self.datawrapper_params = {}
127
114
 
128
- #def _setup_logging(self):
129
- # """Initialize logger and debug settings."""
130
- # self.debug = self.config.get('debug', False)
131
- # logger = self.config.get('logger', None)
132
- # self._own_logger = logger is None
133
- # self.logger = logger or Logger.default_logger(
134
- # logger_name=f'Parquet_Artifact_InstanceOf_{self.__class__.__name__}')
135
- # self.logger.set_level(Logger.DEBUG if self.debug else Logger.INFO)
136
-
137
115
  def _validate_required(self, key: str) -> Any:
138
116
  """Validate required configuration fields."""
139
117
  value = self.config.setdefault(key, None)
@@ -211,28 +189,28 @@ class ParquetArtifact(DfHelper):
211
189
  """
212
190
  Synchronously estimates artifact size for use in multi-threaded environments.
213
191
 
214
- This method uses the filesystem's own .sync() method to safely execute
215
- asynchronous I/O operations from a synchronous context, preventing
216
- event loop conflicts.
192
+ This method safely executes asynchronous I/O operations from a synchronous
193
+ context, handling variations in fsspec filesystem implementations.
217
194
  """
218
195
 
219
196
  async def _get_total_bytes_async():
220
197
  """A helper async coroutine to perform the I/O."""
221
198
  import asyncio
222
199
 
223
- # Use the async versions of fsspec methods (e.g., _glob, _size)
224
200
  files = await self.fs._glob(f"{self.parquet_storage_path}/*.parquet")
225
201
  if not files:
226
202
  return 0
227
203
 
228
- # Concurrently gather the size of all files for performance
229
204
  size_tasks = [self.fs._size(f) for f in files]
230
205
  sizes = await asyncio.gather(*size_tasks)
231
206
  return sum(s for s in sizes if s is not None)
232
207
 
233
- # Use the filesystem's own built-in sync method. This is the most
234
- # reliable way to bridge the sync/async gap for fsspec.
235
- total_bytes = self.fs.sync(_get_total_bytes_async())
208
+ try:
209
+ # Attempt the standard fsspec method first
210
+ total_bytes = self.fs.sync(_get_total_bytes_async())
211
+ except AttributeError:
212
+ # fallback for filesystems like s3fs that lack .sync()
213
+ total_bytes = self.fs.loop.run_until_complete(_get_total_bytes_async())
236
214
 
237
215
  # Convert to megabytes, ensuring a minimum of 1
238
216
  return max(1, int(total_bytes / (1024 ** 2)))
@@ -1,11 +1,9 @@
1
- import logging
2
- from typing import Optional
1
+ from typing import Optional, ClassVar, Dict
3
2
 
4
3
  import dask.dataframe as dd
5
4
  import fsspec
6
5
 
7
6
  from sibi_dst.df_helper import DfHelper
8
- from sibi_dst.utils import Logger
9
7
 
10
8
  class ParquetReader(DfHelper):
11
9
  """
@@ -44,19 +42,17 @@ class ParquetReader(DfHelper):
44
42
  Parquet storage.
45
43
  :type fs: fsspec.AbstractFileSystem
46
44
  """
47
- DEFAULT_CONFIG = {
45
+ DEFAULT_CONFIG: ClassVar[Dict[str, int]] = {
48
46
  'backend': 'parquet'
49
47
  }
50
48
 
51
- def __init__(self, filesystem_type="file", filesystem_options=None, **kwargs):
49
+ def __init__(self, **kwargs):
52
50
  self.config = {
53
51
  **self.DEFAULT_CONFIG,
54
52
  **kwargs,
55
53
  }
56
- self.df: Optional[dd.DataFrame] = None
57
- #self.debug = self.config.setdefault('debug', False)
58
- #self.logger = self.config.setdefault('logger', Logger.default_logger(logger_name=self.__class__.__name__))
59
- #self.logger.set_level(logging.DEBUG if self.debug else logging.INFO)
54
+ super().__init__(**self.config)
55
+
60
56
  self.parquet_storage_path = self.config.setdefault('parquet_storage_path', None)
61
57
  if self.parquet_storage_path is None:
62
58
  raise ValueError('parquet_storage_path must be set')
@@ -68,19 +64,9 @@ class ParquetReader(DfHelper):
68
64
  if self.parquet_end_date is None:
69
65
  raise ValueError('parquet_end_date must be set')
70
66
 
71
- # Filesystem setup
72
- #self.filesystem_type = filesystem_type
73
- #self.filesystem_options = filesystem_options or {}
74
- #self.fs = self.config.setdefault('fs', None)
75
- #if self.fs is None:
76
- # self.fs = fsspec.filesystem(self.filesystem_type, **self.filesystem_options)
77
- #self.config.setdefault('fs', self.fs)
78
-
79
67
  if not self.directory_exists():
80
68
  raise ValueError(f"{self.parquet_storage_path} does not exist")
81
69
 
82
- super().__init__(**self.config)
83
-
84
70
  def load(self, **kwargs):
85
71
  self.df = super().load(**kwargs)
86
72
  return self.df
@@ -90,5 +76,4 @@ class ParquetReader(DfHelper):
90
76
  info = self.fs.info(self.parquet_storage_path)
91
77
  return info['type'] == 'directory'
92
78
  except FileNotFoundError:
93
- return False
94
-
79
+ return False
@@ -85,7 +85,8 @@ class ParquetConfig(BaseModel):
85
85
  if self.logger is None:
86
86
  self.logger = Logger.default_logger(logger_name=self.__class__.__name__)
87
87
  self.logger.set_level(Logger.DEBUG if self.debug else Logger.INFO)
88
-
88
+ if self.fs is None:
89
+ raise ValueError('Parquet Options: File system (fs) must be specified')
89
90
 
90
91
  if self.parquet_storage_path is None:
91
92
  raise ValueError('Parquet storage path must be specified')