-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild_runner.py
More file actions
249 lines (213 loc) · 8.77 KB
/
build_runner.py
File metadata and controls
249 lines (213 loc) · 8.77 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
import logging
from databao_context_engine.build_sources.build_service import BuildService
from databao_context_engine.build_sources.export_results import (
append_result_to_all_results,
export_build_result,
reset_all_results,
)
from databao_context_engine.build_sources.types import (
BuildDatasourceResult,
DatasourceExecutionStatus,
IndexDatasourceResult,
)
from databao_context_engine.datasources.datasource_context import (
DatasourceContext,
read_datasource_type_from_context,
)
from databao_context_engine.datasources.datasource_discovery import discover_datasources, prepare_source
from databao_context_engine.pluginlib.build_plugin import DatasourceType
from databao_context_engine.plugins.plugin_loader import load_plugins
from databao_context_engine.progress.progress import DatasourceStatus, ProgressCallback, ProgressEmitter
from databao_context_engine.project.layout import ProjectLayout
logger = logging.getLogger(__name__)
def build(
project_layout: ProjectLayout,
*,
build_service: BuildService,
generate_embeddings: bool = True,
progress: ProgressCallback | None = None,
) -> list[BuildDatasourceResult]:
"""Build the context for all datasources in the project.
Unless you already have access to BuildService, this should not be called directly.
Instead, internal callers should go through the build_wiring module or directly use DatabaoContextProjectManager.build_context().
1) Load available plugins
2) Discover sources
3) For each source, call process_source
Returns:
A list of per-datasource build results.
"""
plugins = load_plugins()
datasource_ids = discover_datasources(project_layout)
emitter = ProgressEmitter(progress)
if not datasource_ids:
logger.info("No sources discovered under %s", project_layout.src_dir)
emitter.task_started(total_datasources=0)
emitter.task_finished(ok=0, failed=0, skipped=0)
return []
emitter.task_started(total_datasources=len(datasource_ids))
results: list[BuildDatasourceResult] = []
failed = 0
skipped = 0
reset_all_results(project_layout.output_dir)
for datasource_index, datasource_id in enumerate(datasource_ids, start=1):
try:
prepared_source = prepare_source(project_layout, datasource_id)
logger.info(
f'Found datasource of type "{prepared_source.datasource_type.full_type}" with name {prepared_source.datasource_id.datasource_path}'
)
emitter.datasource_started(
datasource_id=str(datasource_id),
index=datasource_index,
total=len(datasource_ids),
)
plugin = plugins.get(prepared_source.datasource_type)
if plugin is None:
logger.warning(
"No plugin for '%s' (datasource=%s) — skipping.",
prepared_source.datasource_type.full_type,
prepared_source.datasource_id.relative_path_to_config_file(),
)
emitter.datasource_finished(
datasource_id=str(datasource_id),
index=datasource_index,
total=len(datasource_ids),
status=DatasourceStatus.SKIPPED,
)
results.append(
BuildDatasourceResult(datasource_id=datasource_id, status=DatasourceExecutionStatus.SKIPPED)
)
skipped += 1
continue
result = build_service.process_prepared_source(
prepared_source=prepared_source,
plugin=plugin,
generate_embeddings=generate_embeddings,
progress=progress,
)
output_dir = project_layout.output_dir
context_file_path = export_build_result(output_dir, result)
append_result_to_all_results(output_dir, result)
results.append(
BuildDatasourceResult(
datasource_id=datasource_id,
status=DatasourceExecutionStatus.OK,
datasource_type=DatasourceType(full_type=result.datasource_type),
context_built_at=result.context_built_at,
context_file_path=context_file_path,
)
)
emitter.datasource_finished(
datasource_id=str(datasource_id),
index=datasource_index,
total=len(datasource_ids),
status=DatasourceStatus.OK,
)
except Exception as e:
logger.debug(str(e), exc_info=True, stack_info=True)
logger.info(f"Failed to build source at ({datasource_id.relative_path_to_config_file()}): {str(e)}")
emitter.datasource_finished(
datasource_id=str(datasource_id),
index=datasource_index,
total=len(datasource_ids),
status=DatasourceStatus.FAILED,
error=str(e),
)
failed += 1
results.append(
BuildDatasourceResult(
datasource_id=datasource_id, status=DatasourceExecutionStatus.FAILED, error=str(e)
)
)
ok = sum(1 for result in results if result.status == DatasourceExecutionStatus.OK)
logger.debug(
"Successfully built %d datasources. %s %s",
ok,
f"Skipped {skipped}." if skipped > 0 else "",
f"Failed to build {failed}." if failed > 0 else "",
)
emitter.task_finished(
ok=ok,
failed=failed,
skipped=skipped,
)
return results
def run_indexing(
*,
project_layout: ProjectLayout,
build_service: BuildService,
contexts: list[DatasourceContext],
progress: ProgressCallback | None = None,
) -> list[IndexDatasourceResult]:
"""Index a list of built datasource contexts.
1) Load available plugins
2) Infer datasource type from context file
3) For each context, call index_built_context
Returns:
A list of per-context indexing results.
"""
plugins = load_plugins()
results: list[IndexDatasourceResult] = []
ok = 0
skipped = 0
failed = 0
emitter = ProgressEmitter(progress)
emitter.task_started(total_datasources=len(contexts))
for datasource_index, context in enumerate(contexts, start=1):
try:
logger.info(f"Indexing datasource {context.datasource_id}")
emitter.datasource_started(
datasource_id=str(context.datasource_id),
index=datasource_index,
total=len(contexts),
)
datasource_type = read_datasource_type_from_context(context)
plugin = plugins.get(datasource_type)
if plugin is None:
logger.warning(
"No plugin for datasource type '%s' — skipping indexing for %s.",
getattr(datasource_type, "full_type", datasource_type),
context.datasource_id,
)
skipped += 1
emitter.datasource_finished(
datasource_id=str(context.datasource_id),
index=datasource_index,
total=len(contexts),
status=DatasourceStatus.SKIPPED,
)
continue
build_service.index_built_context(context=context, plugin=plugin, progress=progress)
ok += 1
emitter.datasource_finished(
datasource_id=str(context.datasource_id),
index=datasource_index,
total=len(contexts),
status=DatasourceStatus.OK,
)
results.append(
IndexDatasourceResult(datasource_id=context.datasource_id, status=DatasourceExecutionStatus.OK)
)
except Exception as e:
logger.debug(str(e), exc_info=True, stack_info=True)
logger.info(f"Failed to build source at ({context.datasource_id}): {str(e)}")
failed += 1
results.append(
IndexDatasourceResult(
datasource_id=context.datasource_id, status=DatasourceExecutionStatus.FAILED, error=str(e)
)
)
emitter.datasource_finished(
datasource_id=str(context.datasource_id),
index=datasource_index,
total=len(contexts),
status=DatasourceStatus.FAILED,
error=str(e),
)
logger.debug(
"Successfully indexed %d/%d datasource(s). %s",
ok,
len(contexts),
f"Skipped {skipped}. Failed {failed}." if (skipped or failed) else "",
)
emitter.task_finished(ok=ok, failed=failed, skipped=skipped)
return results