|
11 | 11 | CognitionIntegrationType, |
12 | 12 | ) |
13 | 13 | from ..util import prevent_sql_injection |
| 14 | +from submodules.model import enums |
14 | 15 |
|
15 | 16 | FINISHED_STATES = [ |
16 | 17 | CognitionMarkdownFileState.FINISHED.value, |
@@ -329,3 +330,150 @@ def get_distinct_item_ids_for_all_permissions( |
329 | 330 | return [] |
330 | 331 |
|
331 | 332 | return [row[0] for row in results if row and row[0]] |
| 333 | + |
| 334 | + |
| 335 | +def get_last_integrations_tasks() -> List[Dict[str, Any]]: |
| 336 | + query = f""" |
| 337 | + WITH embedding_agg AS ( |
| 338 | + SELECT |
| 339 | + project_id, |
| 340 | + jsonb_object_agg( |
| 341 | + state, |
| 342 | + jsonb_build_object( |
| 343 | + 'count', count, |
| 344 | + 'embeddings', embeddings |
| 345 | + ) |
| 346 | + ) AS embeddings_by_state |
| 347 | + FROM ( |
| 348 | + SELECT |
| 349 | + e.project_id, |
| 350 | + e.state, |
| 351 | + COUNT(*) AS count, |
| 352 | + jsonb_agg( |
| 353 | + jsonb_build_object( |
| 354 | + 'createdBy', e.created_by, |
| 355 | + 'finishedAt', e.finished_at, |
| 356 | + 'id', e.id, |
| 357 | + 'name', e.name, |
| 358 | + 'startedAt', e.started_at, |
| 359 | + 'state', e.state |
| 360 | + ) ORDER BY e.started_at DESC |
| 361 | + ) AS embeddings |
| 362 | + FROM embedding e |
| 363 | + GROUP BY e.project_id, e.state |
| 364 | + ) AS x |
| 365 | + GROUP BY project_id |
| 366 | + ), |
| 367 | +
|
| 368 | + attribute_agg AS ( |
| 369 | + SELECT |
| 370 | + project_id, |
| 371 | + jsonb_object_agg( |
| 372 | + state, |
| 373 | + jsonb_build_object( |
| 374 | + 'count', count, |
| 375 | + 'attributes', attributes |
| 376 | + ) |
| 377 | + ) AS attributes_by_state |
| 378 | + FROM ( |
| 379 | + SELECT |
| 380 | + a.project_id, |
| 381 | + a.state, |
| 382 | + COUNT(*) AS count, |
| 383 | + jsonb_agg( |
| 384 | + jsonb_build_object( |
| 385 | + 'dataType', a.data_type, |
| 386 | + 'finishedAt', a.finished_at, |
| 387 | + 'id', a.id, |
| 388 | + 'name', a.name, |
| 389 | + 'startedAt', a.started_at, |
| 390 | + 'state', a.state |
| 391 | + ) ORDER BY a.started_at DESC |
| 392 | + ) AS attributes |
| 393 | + FROM attribute a |
| 394 | + WHERE a.state NOT IN ('UPLOADED','AUTOMATICALLY_CREATED') |
| 395 | + GROUP BY a.project_id, a.state |
| 396 | + ) AS x |
| 397 | + GROUP BY project_id |
| 398 | + ), |
| 399 | +
|
| 400 | + record_tokenization_task_agg AS ( |
| 401 | + SELECT |
| 402 | + project_id, |
| 403 | + jsonb_object_agg( |
| 404 | + state, |
| 405 | + jsonb_build_object( |
| 406 | + 'count', count, |
| 407 | + 'record_tokenization_tasks', record_tokenization_tasks |
| 408 | + ) |
| 409 | + ) AS record_tokenization_tasks_by_state |
| 410 | + FROM ( |
| 411 | + SELECT |
| 412 | + rtt.project_id, |
| 413 | + rtt.state, |
| 414 | + COUNT(*) AS count, |
| 415 | + jsonb_agg( |
| 416 | + jsonb_build_object( |
| 417 | + 'finishedAt', rtt.finished_at, |
| 418 | + 'id', rtt.id, |
| 419 | + 'startedAt', rtt.started_at, |
| 420 | + 'state', rtt.state, |
| 421 | + 'type', rtt.type |
| 422 | + ) ORDER BY rtt.started_at DESC |
| 423 | + ) AS record_tokenization_tasks |
| 424 | + FROM record_tokenization_task rtt |
| 425 | + GROUP BY rtt.project_id, rtt.state |
| 426 | + ) AS x |
| 427 | + GROUP BY project_id |
| 428 | + ), |
| 429 | +
|
| 430 | + integration_data AS ( |
| 431 | + SELECT |
| 432 | + i.id AS integration_id, |
| 433 | + i.name AS integration_name, |
| 434 | + i.error_message, |
| 435 | + i.started_at, |
| 436 | + i.finished_at, |
| 437 | + i.state, |
| 438 | + i.organization_id, |
| 439 | + i.project_id, |
| 440 | + i.created_by, |
| 441 | + i.type, |
| 442 | + o.name AS organization_name, |
| 443 | + p.name AS project_name, |
| 444 | + jsonb_build_object( |
| 445 | + 'embeddingsByState', coalesce(ea.embeddings_by_state, '[]'::jsonb), |
| 446 | + 'attributesByState', coalesce(aa.attributes_by_state, '[]'::jsonb), |
| 447 | + 'recordTokenizationTasksByState', coalesce(rtt.record_tokenization_tasks_by_state, '[]'::jsonb) |
| 448 | + ) AS full_data |
| 449 | + FROM cognition.integration i |
| 450 | + LEFT JOIN embedding_agg ea |
| 451 | + ON ea.project_id = i.project_id |
| 452 | + LEFT JOIN attribute_agg aa |
| 453 | + ON aa.project_id = i.project_id |
| 454 | + LEFT JOIN record_tokenization_task_agg rtt |
| 455 | + ON rtt.project_id = i.project_id |
| 456 | + JOIN organization o |
| 457 | + ON o.id = i.organization_id |
| 458 | + JOIN project p |
| 459 | + ON p.id = i.project_id |
| 460 | + ) |
| 461 | +
|
| 462 | + SELECT |
| 463 | + int_data.organization_id as organization_id, |
| 464 | + int_data.organization_name as organization_name, |
| 465 | + int_data.integration_id, |
| 466 | + int_data.integration_name, |
| 467 | + int_data.error_message, |
| 468 | + int_data.started_at, |
| 469 | + int_data.finished_at, |
| 470 | + int_data.state, |
| 471 | + int_data.full_data, |
| 472 | + int_data.created_by, |
| 473 | + int_data.type, |
| 474 | + int_data.project_name |
| 475 | + FROM integration_data int_data |
| 476 | + ORDER BY int_data.organization_id, int_data.started_at DESC |
| 477 | + """ |
| 478 | + |
| 479 | + return general.execute_all(query) |
0 commit comments