@dataif/cli 0.1.0
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.
- package/README.md +16 -0
- package/bin/dataif.js +623 -0
- package/package.json +26 -0
- package/scripts/build-template.mjs +72 -0
- package/templates/dataif/README.md +157 -0
- package/templates/dataif/infra/.env.example +119 -0
- package/templates/dataif/infra/.env.stg.example +119 -0
- package/templates/dataif/infra/airflow/Dockerfile +11 -0
- package/templates/dataif/infra/airflow/Dockerfile.release +17 -0
- package/templates/dataif/infra/airflow/requirements.txt +3 -0
- package/templates/dataif/infra/docker-compose.yml +306 -0
- package/templates/dataif/infra/init-db/01-init-dataif.sh +129 -0
- package/templates/dataif/infra/init-db/pnp-curated-views.sqlinc +444 -0
- package/templates/dataif/infra/init-db/pnp-raw-staging-curated.sqlinc +701 -0
- package/templates/dataif/infra/keycloak/Dockerfile +4 -0
- package/templates/dataif/infra/keycloak/realm-dataif.json +73 -0
- package/templates/dataif/infra/ollama/Dockerfile +9 -0
- package/templates/dataif/infra/ollama/bootstrap-model.sh +100 -0
- package/templates/dataif/infra/ollama/sabia-7b.Modelfile +14 -0
- package/templates/dataif/infra/postgres/Dockerfile +4 -0
- package/templates/dataif/pipelines/airflow/dags/generated/.gitkeep +1 -0
- package/templates/dataif/pipelines/airflow/dags/generated/2020_financeiro_fcc6f1f3_sync.py +9 -0
- package/templates/dataif/pipelines/dataif_pipelines/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/airflow/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/airflow/pnp_pipeline_factory.py +167 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/base/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/base/connector.py +28 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/base/types.py +14 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/config.py +19 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/connector.py +558 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/powerbi_microdados.py +728 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/transform.py +296 -0
- package/templates/dataif/pipelines/dataif_pipelines/jobs/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/jobs/nilo_pipeline.py +112 -0
- package/templates/dataif/pipelines/dataif_pipelines/orchestration/__init__.py +21 -0
- package/templates/dataif/pipelines/dataif_pipelines/orchestration/pnp_workflow.py +783 -0
- package/templates/dataif/pipelines/dataif_pipelines/repositories/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/repositories/pnp_raw_repository.py +860 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/__init__.py +19 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_curated_service.py +66 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_download_service.py +534 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_quality_service.py +9 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_raw_ingestion_service.py +124 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_staging_service.py +271 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/powerbi_catalog_service.py +159 -0
- package/templates/dataif/pipelines/sql/staging/020_pnp_matriculas.sql +112 -0
- package/templates/dataif/pipelines/sql/staging/030_pnp_eficiencia_academica.sql +83 -0
- package/templates/dataif/pipelines/sql/staging/040_pnp_servidores.sql +90 -0
- package/templates/dataif/pipelines/sql/staging/050_pnp_financeiro.sql +72 -0
- package/templates/dataif/pipelines/sql/views_curated/004_mv_pnp_dashboard_fast.sql +204 -0
- package/templates/dataif/pipelines/sql/views_curated/010_vw_pnp_admin_ingestao.sql +51 -0
- package/templates/dataif/pipelines/sql/views_curated/020_vw_pnp_qualidade_dados.sql +114 -0
- package/templates/dataif/pipelines/sql/views_curated/030_vw_pnp_matriculas.sql +67 -0
- package/templates/dataif/pipelines/sql/views_curated/040_vw_pnp_eficiencia.sql +33 -0
- package/templates/dataif/pipelines/sql/views_curated/050_vw_pnp_servidores.sql +30 -0
- package/templates/dataif/pipelines/sql/views_curated/060_vw_pnp_financeiro.sql +22 -0
- package/templates/dataif/pipelines/sql/views_curated/070_vw_pnp_vanna.sql +115 -0
- package/templates/dataif/scripts/configure-env.sh +149 -0
- package/templates/dataif/scripts/create_metabase_pnp_dashboard.py +943 -0
- package/templates/dataif/scripts/create_metabase_pnp_matriculas_dashboard.py +580 -0
- package/templates/dataif/scripts/deploy.sh +79 -0
- package/templates/dataif/scripts/fix_metabase_template_tag_ids.py +91 -0
- package/templates/dataif/scripts/pnp_powerbi_microdados_probe.py +14 -0
- package/templates/dataif/scripts/pnp_validate_raw_run.py +330 -0
- package/templates/dataif/scripts/publish-images.sh +31 -0
- package/templates/dataif/scripts/sync_metabase_dashboard_field_filters.py +241 -0
- package/templates/dataif/scripts/use-vanna-ollama.sh +139 -0
- package/templates/dataif/services/api/.dockerignore +18 -0
- package/templates/dataif/services/api/Dockerfile +12 -0
- package/templates/dataif/services/api/app/__init__.py +1 -0
- package/templates/dataif/services/api/app/auth.py +48 -0
- package/templates/dataif/services/api/app/config.py +59 -0
- package/templates/dataif/services/api/app/keycloak_admin.py +215 -0
- package/templates/dataif/services/api/app/main.py +2432 -0
- package/templates/dataif/services/api/app/metabase_admin.py +191 -0
- package/templates/dataif/services/api/app/metabase_bootstrap.py +44 -0
- package/templates/dataif/services/api/app/metabase_embed.py +15 -0
- package/templates/dataif/services/api/app/pnp_dag_provisioner.py +113 -0
- package/templates/dataif/services/api/app/pnp_instance_repository.py +951 -0
- package/templates/dataif/services/api/app/pnp_powerbi.py +438 -0
- package/templates/dataif/services/api/app/vanna_client.py +32 -0
- package/templates/dataif/services/api/requirements.txt +9 -0
- package/templates/dataif/services/vanna/.dockerignore +18 -0
- package/templates/dataif/services/vanna/Dockerfile +12 -0
- package/templates/dataif/services/vanna/app/config.py +57 -0
- package/templates/dataif/services/vanna/app/main.py +108 -0
- package/templates/dataif/services/vanna/app/runtime_config.py +114 -0
- package/templates/dataif/services/vanna/app/sql_guard.py +123 -0
- package/templates/dataif/services/vanna/app/vanna_engine.py +382 -0
- package/templates/dataif/services/vanna/requirements.txt +8 -0
- package/templates/dataif/services/web/.dockerignore +13 -0
- package/templates/dataif/services/web/Dockerfile +16 -0
- package/templates/dataif/services/web/index.html +12 -0
- package/templates/dataif/services/web/nginx.conf +74 -0
- package/templates/dataif/services/web/package-lock.json +4397 -0
- package/templates/dataif/services/web/package.json +32 -0
- package/templates/dataif/services/web/postcss.config.mjs +5 -0
- package/templates/dataif/services/web/src/App.jsx +2817 -0
- package/templates/dataif/services/web/src/adminAuth.js +245 -0
- package/templates/dataif/services/web/src/assets/avatar_placeholder.png +0 -0
- package/templates/dataif/services/web/src/assets/github_logo_icon_229278.svg +1 -0
- package/templates/dataif/services/web/src/assets/if-logo.png +0 -0
- package/templates/dataif/services/web/src/assets/if.svg +0 -0
- package/templates/dataif/services/web/src/assets/pnp-horizontal.svg +1 -0
- package/templates/dataif/services/web/src/components/AppHeader.jsx +233 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/mobile-header.tsx +56 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-account-card.tsx +209 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-item-button.tsx +67 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-item.tsx +108 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-list.tsx +83 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/config.ts +23 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/header-navigation.tsx +240 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination-base.tsx +376 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination-dot.tsx +52 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination-line.tsx +48 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination.tsx +328 -0
- package/templates/dataif/services/web/src/components/application/tabs/tabs.tsx +223 -0
- package/templates/dataif/services/web/src/components/base/avatar/avatar-label-group.tsx +28 -0
- package/templates/dataif/services/web/src/components/base/avatar/avatar.tsx +129 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/avatar-add-button.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/avatar-company-icon.tsx +24 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/avatar-online-indicator.tsx +29 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/index.tsx +4 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/verified-tick.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/badges/badge-types.ts +264 -0
- package/templates/dataif/services/web/src/components/base/badges/badges.tsx +415 -0
- package/templates/dataif/services/web/src/components/base/button-group/button-group.tsx +104 -0
- package/templates/dataif/services/web/src/components/base/buttons/button.tsx +267 -0
- package/templates/dataif/services/web/src/components/base/input/hint-text.tsx +31 -0
- package/templates/dataif/services/web/src/components/base/input/input.tsx +269 -0
- package/templates/dataif/services/web/src/components/base/input/label.tsx +48 -0
- package/templates/dataif/services/web/src/components/base/radio-buttons/radio-buttons.tsx +127 -0
- package/templates/dataif/services/web/src/components/base/select/combobox.tsx +150 -0
- package/templates/dataif/services/web/src/components/base/select/multi-select.tsx +361 -0
- package/templates/dataif/services/web/src/components/base/select/popover.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/select/select-item.tsx +95 -0
- package/templates/dataif/services/web/src/components/base/select/select-native.tsx +67 -0
- package/templates/dataif/services/web/src/components/base/select/select.tsx +144 -0
- package/templates/dataif/services/web/src/components/base/tags/base-components/tag-close-x.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/tooltip/tooltip.tsx +107 -0
- package/templates/dataif/services/web/src/components/foundations/dot-icon.tsx +22 -0
- package/templates/dataif/services/web/src/components/foundations/logo/untitledui-logo-minimal.tsx +170 -0
- package/templates/dataif/services/web/src/components/foundations/logo/untitledui-logo.tsx +58 -0
- package/templates/dataif/services/web/src/hooks/use-breakpoint.ts +34 -0
- package/templates/dataif/services/web/src/hooks/use-resize-observer.ts +67 -0
- package/templates/dataif/services/web/src/main.jsx +14 -0
- package/templates/dataif/services/web/src/providers/theme-provider.jsx +62 -0
- package/templates/dataif/services/web/src/styles/globals.css +60 -0
- package/templates/dataif/services/web/src/styles/theme.css +1326 -0
- package/templates/dataif/services/web/src/styles/typography.css +430 -0
- package/templates/dataif/services/web/src/styles.css +1287 -0
- package/templates/dataif/services/web/src/utils/cx.ts +24 -0
- package/templates/dataif/services/web/src/utils/is-react-component.ts +33 -0
- package/templates/dataif/services/web/vite.config.js +14 -0
- package/templates/dataif/sql/ddl/001_schemas.sql +6 -0
- package/templates/dataif/sql/ddl/003_pnp_raw_staging_curated.sql +699 -0
- package/templates/dataif/sql/migrations/001_pnp_phase1_backfill.sql +3 -0
- package/templates/dataif/sql/migrations/002_pnp_phase2_admin_config_backfill.sql +184 -0
- package/templates/dataif/sql/migrations/003_pnp_phase3_raw_tabular_backfill.sql +3 -0
- package/templates/dataif/sql/migrations/004_pnp_phase3_raw_backfill_support_index.sql +3 -0
- package/templates/dataif/sql/migrations/005_pnp_phase7_staging_support_indexes.sql +2 -0
- package/templates/dataif/sql/migrations/006_pnp_phase7_staging_autovacuum_tuning.sql +2 -0
- package/templates/dataif/sql/migrations/007_pnp_phase7b_run_packages.sql +20 -0
- package/templates/dataif/sql/migrations/008_pnp_phase7a_pipeline_endpoints.sql +169 -0
- package/templates/dataif/sql/migrations/009_pnp_phase8_curated.sql +35 -0
- package/templates/dataif/sql/migrations/010_pnp_phase10_staging_incremental_upsert.sql +3 -0
- package/templates/dataif/sql/migrations/010_pnp_pipeline_uuid.sql +51 -0
- package/templates/dataif/sql/migrations/011_app_settings.sql +7 -0
- package/templates/dataif/sql/staging/020_pnp_matriculas.sql +112 -0
- package/templates/dataif/sql/staging/030_pnp_eficiencia_academica.sql +83 -0
- package/templates/dataif/sql/staging/040_pnp_servidores.sql +90 -0
- package/templates/dataif/sql/staging/050_pnp_financeiro.sql +72 -0
- package/templates/dataif/sql/views_curated/003_vw_pnp_microdados_admin.sql +160 -0
- package/templates/dataif/sql/views_curated/004_mv_pnp_dashboard_fast.sql +204 -0
- package/templates/dataif/sql/views_curated/010_vw_pnp_admin_ingestao.sql +51 -0
- package/templates/dataif/sql/views_curated/020_vw_pnp_qualidade_dados.sql +114 -0
- package/templates/dataif/sql/views_curated/030_vw_pnp_matriculas.sql +67 -0
- package/templates/dataif/sql/views_curated/040_vw_pnp_eficiencia.sql +33 -0
- package/templates/dataif/sql/views_curated/050_vw_pnp_servidores.sql +30 -0
- package/templates/dataif/sql/views_curated/060_vw_pnp_financeiro.sql +22 -0
- package/templates/dataif/sql/views_curated/070_vw_pnp_vanna.sql +115 -0
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import psycopg2
|
|
9
|
+
from psycopg2.extras import RealDictCursor
|
|
10
|
+
|
|
11
|
+
from dataif_pipelines.connectors.base.connector import BaseConnector
|
|
12
|
+
from dataif_pipelines.connectors.base.types import NormalizedRecord, RawRecord, RunContext
|
|
13
|
+
from dataif_pipelines.connectors.nilo_pecanha.config import NiloConfig, load_config
|
|
14
|
+
from dataif_pipelines.services import (
|
|
15
|
+
pnp_download_service,
|
|
16
|
+
pnp_quality_service,
|
|
17
|
+
pnp_raw_ingestion_service,
|
|
18
|
+
powerbi_catalog_service,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
logger = logging.getLogger(__name__)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class EndpointDefinition:
|
|
26
|
+
id: int
|
|
27
|
+
endpoint_key: str
|
|
28
|
+
description: str | None
|
|
29
|
+
page_url: str | None
|
|
30
|
+
api_endpoint_url: str | None
|
|
31
|
+
csv_url: str | None
|
|
32
|
+
dictionary_url: str | None
|
|
33
|
+
request_params: dict[str, Any]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class NiloPecanhaConnector(BaseConnector):
|
|
37
|
+
_STREAM_BATCH_SIZE = 250
|
|
38
|
+
|
|
39
|
+
def __init__(self, dsn: str | None = None, config: NiloConfig | None = None) -> None:
|
|
40
|
+
self.dsn = dsn or os.environ["WAREHOUSE_DSN"]
|
|
41
|
+
self.config = config or load_config()
|
|
42
|
+
self._pending_assets: list[dict[str, Any]] = []
|
|
43
|
+
self._pending_catalog_entries: list[dict[str, Any]] = []
|
|
44
|
+
self._pending_run_selection: list[dict[str, Any]] = []
|
|
45
|
+
self._pending_downloads: list[dict[str, Any]] = []
|
|
46
|
+
self._pending_quarantine: list[dict[str, Any]] = []
|
|
47
|
+
self._last_asset_count = 0
|
|
48
|
+
self._last_endpoint_count = 0
|
|
49
|
+
self._download_count = 0
|
|
50
|
+
self._last_download_column_count = 0
|
|
51
|
+
self._last_quarantine_count = 0
|
|
52
|
+
self._last_raw_domain_count = 0
|
|
53
|
+
|
|
54
|
+
def connector_id(self) -> str:
|
|
55
|
+
return "nilo_pecanha"
|
|
56
|
+
|
|
57
|
+
def runtime_stats(self) -> dict[str, int]:
|
|
58
|
+
return {
|
|
59
|
+
"endpoint_count": self._last_endpoint_count,
|
|
60
|
+
"asset_count": self._last_asset_count,
|
|
61
|
+
"download_count": self._download_count,
|
|
62
|
+
"download_column_count": self._last_download_column_count,
|
|
63
|
+
"quarantine_count": self._last_quarantine_count,
|
|
64
|
+
"raw_domain_count": self._last_raw_domain_count,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
def fetch(self, run_context: RunContext, instance_key: str | None = None) -> list[RawRecord]:
|
|
68
|
+
self._reset_pending_state()
|
|
69
|
+
|
|
70
|
+
endpoints = self._load_active_endpoints(instance_key=instance_key)
|
|
71
|
+
self._last_endpoint_count = len(endpoints)
|
|
72
|
+
|
|
73
|
+
wrapped_records: list[RawRecord] = []
|
|
74
|
+
for endpoint in endpoints:
|
|
75
|
+
mode = str((endpoint.request_params or {}).get("mode") or "").strip().lower()
|
|
76
|
+
if mode != "powerbi_microdados":
|
|
77
|
+
logger.warning("Skipping unsupported nilo_pecanha endpoint mode=%s endpoint_key=%s", mode, endpoint.endpoint_key)
|
|
78
|
+
continue
|
|
79
|
+
|
|
80
|
+
payloads = self._fetch_powerbi_microdados_records(endpoint, run_context.run_id)
|
|
81
|
+
wrapped_records.extend(
|
|
82
|
+
self._wrap_payloads(
|
|
83
|
+
payloads=payloads,
|
|
84
|
+
endpoint=endpoint,
|
|
85
|
+
source_kind="powerbi_microdados",
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
logger.info(
|
|
90
|
+
"Fetched connector=%s endpoints=%s records=%s assets=%s downloads=%s",
|
|
91
|
+
self.connector_id(),
|
|
92
|
+
self._last_endpoint_count,
|
|
93
|
+
len(wrapped_records),
|
|
94
|
+
len(self._pending_assets),
|
|
95
|
+
len(self._pending_downloads),
|
|
96
|
+
)
|
|
97
|
+
return wrapped_records
|
|
98
|
+
|
|
99
|
+
def extract_and_load_raw(self, run_context: RunContext, instance_key: str | None = None) -> int:
|
|
100
|
+
self._reset_pending_state()
|
|
101
|
+
endpoints = self._load_active_endpoints(instance_key=instance_key)
|
|
102
|
+
self._last_endpoint_count = len(endpoints)
|
|
103
|
+
logger.info(
|
|
104
|
+
"Starting streaming raw extraction connector=%s instance_key=%s endpoints=%s run_id=%s",
|
|
105
|
+
self.connector_id(),
|
|
106
|
+
instance_key,
|
|
107
|
+
self._last_endpoint_count,
|
|
108
|
+
run_context.run_id,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
total_loaded = 0
|
|
112
|
+
total_assets = 0
|
|
113
|
+
total_download_columns = 0
|
|
114
|
+
total_quarantine = 0
|
|
115
|
+
|
|
116
|
+
for endpoint in endpoints:
|
|
117
|
+
mode = str((endpoint.request_params or {}).get("mode") or "").strip().lower()
|
|
118
|
+
if mode != "powerbi_microdados":
|
|
119
|
+
logger.warning("Skipping unsupported nilo_pecanha endpoint mode=%s endpoint_key=%s", mode, endpoint.endpoint_key)
|
|
120
|
+
continue
|
|
121
|
+
|
|
122
|
+
logger.info(
|
|
123
|
+
"Starting endpoint extraction endpoint_key=%s description=%s",
|
|
124
|
+
endpoint.endpoint_key,
|
|
125
|
+
endpoint.description,
|
|
126
|
+
)
|
|
127
|
+
endpoint_stats = self._extract_powerbi_microdados_streaming(endpoint, run_context)
|
|
128
|
+
total_loaded += int(endpoint_stats.get("raw_record_count") or 0)
|
|
129
|
+
total_assets += int(endpoint_stats.get("asset_count") or 0)
|
|
130
|
+
total_download_columns += int(endpoint_stats.get("download_column_count") or 0)
|
|
131
|
+
total_quarantine += int(endpoint_stats.get("quarantine_count") or 0)
|
|
132
|
+
logger.info(
|
|
133
|
+
"Finished endpoint extraction endpoint_key=%s raw_records=%s quarantine=%s downloads=%s",
|
|
134
|
+
endpoint.endpoint_key,
|
|
135
|
+
int(endpoint_stats.get("raw_record_count") or 0),
|
|
136
|
+
int(endpoint_stats.get("quarantine_count") or 0),
|
|
137
|
+
self._download_count,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
self._last_asset_count = total_assets
|
|
141
|
+
self._last_download_column_count = total_download_columns
|
|
142
|
+
self._last_quarantine_count = total_quarantine
|
|
143
|
+
self._last_raw_domain_count = total_loaded
|
|
144
|
+
logger.info(
|
|
145
|
+
"Buffered raw load completed connector=%s endpoints=%s downloads=%s raw_records=%s",
|
|
146
|
+
self.connector_id(),
|
|
147
|
+
self._last_endpoint_count,
|
|
148
|
+
self._download_count,
|
|
149
|
+
total_loaded,
|
|
150
|
+
)
|
|
151
|
+
return total_loaded
|
|
152
|
+
|
|
153
|
+
def normalize(self, raw_records: list[RawRecord], run_context: RunContext) -> list[NormalizedRecord]:
|
|
154
|
+
result = pnp_raw_ingestion_service.normalize_raw_records(raw_records, run_context)
|
|
155
|
+
self._pending_quarantine.extend(result.quarantine_rows)
|
|
156
|
+
return result.normalized_records
|
|
157
|
+
|
|
158
|
+
def load_raw(self, normalized_records: list[NormalizedRecord], run_context: RunContext) -> int:
|
|
159
|
+
stats = pnp_raw_ingestion_service.load_raw_batch(
|
|
160
|
+
self.dsn,
|
|
161
|
+
normalized_records=normalized_records,
|
|
162
|
+
pending_assets=self._pending_assets,
|
|
163
|
+
pending_catalog_entries=self._pending_catalog_entries,
|
|
164
|
+
pending_run_selection=self._pending_run_selection,
|
|
165
|
+
pending_downloads=self._pending_downloads,
|
|
166
|
+
pending_quarantine=self._pending_quarantine,
|
|
167
|
+
write_legacy=False,
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
self._last_asset_count = int(stats.get("asset_count") or 0)
|
|
171
|
+
self._last_download_column_count = int(stats.get("download_column_count") or 0)
|
|
172
|
+
self._last_quarantine_count = int(stats.get("quarantine_count") or 0)
|
|
173
|
+
self._last_raw_domain_count = int(stats.get("raw_record_count") or 0)
|
|
174
|
+
return self._last_raw_domain_count
|
|
175
|
+
|
|
176
|
+
def post_load_checks(self, run_id: str) -> dict[str, object]:
|
|
177
|
+
return pnp_quality_service.collect_run_checks(self.dsn, run_id)
|
|
178
|
+
|
|
179
|
+
def _reset_pending_state(self) -> None:
|
|
180
|
+
self._pending_assets = []
|
|
181
|
+
self._pending_catalog_entries = []
|
|
182
|
+
self._pending_run_selection = []
|
|
183
|
+
self._pending_downloads = []
|
|
184
|
+
self._pending_quarantine = []
|
|
185
|
+
self._download_count = 0
|
|
186
|
+
self._last_asset_count = 0
|
|
187
|
+
self._last_download_column_count = 0
|
|
188
|
+
self._last_quarantine_count = 0
|
|
189
|
+
self._last_raw_domain_count = 0
|
|
190
|
+
|
|
191
|
+
def _load_active_endpoints(self, instance_key: str | None = None) -> list[EndpointDefinition]:
|
|
192
|
+
query = """
|
|
193
|
+
SELECT
|
|
194
|
+
pe.pipeline_endpoint_id AS id,
|
|
195
|
+
pe.endpoint_key,
|
|
196
|
+
CONCAT(i.instance_name, ' - ', et.endpoint_name) AS description,
|
|
197
|
+
i.page_url,
|
|
198
|
+
NULL::TEXT AS api_endpoint_url,
|
|
199
|
+
NULL::TEXT AS csv_url,
|
|
200
|
+
NULL::TEXT AS dictionary_url,
|
|
201
|
+
jsonb_build_object(
|
|
202
|
+
'mode', 'powerbi_microdados',
|
|
203
|
+
'entity_type', 'pipeline',
|
|
204
|
+
'instance_key', i.instance_key,
|
|
205
|
+
'instance_name', i.instance_name,
|
|
206
|
+
'pipeline_key', i.instance_key,
|
|
207
|
+
'pipeline_name', i.instance_name,
|
|
208
|
+
'connection_key', i.connection_key,
|
|
209
|
+
'connection_name', i.connection_name,
|
|
210
|
+
'endpoint_key', pe.endpoint_key,
|
|
211
|
+
'endpoint_name', et.endpoint_name,
|
|
212
|
+
'selected_years', COALESCE(sel.selected_years, '[]'::jsonb),
|
|
213
|
+
'selected_microdados_types', jsonb_build_array(et.tipo_microdados),
|
|
214
|
+
'selected_downloads', COALESCE(sel.selected_downloads, '[]'::jsonb),
|
|
215
|
+
'selected_source_label', 'Catalogo publico de microdados via Power BI',
|
|
216
|
+
'selected_source_group', 'Microdados Publicos',
|
|
217
|
+
'source_path', 'powerbi_microdados'
|
|
218
|
+
) AS request_params
|
|
219
|
+
FROM raw.pnp_pipeline_endpoints pe
|
|
220
|
+
JOIN raw.pnp_instances i
|
|
221
|
+
ON i.instance_key = pe.instance_key
|
|
222
|
+
JOIN raw.pnp_endpoint_tables et
|
|
223
|
+
ON et.endpoint_key = pe.endpoint_key
|
|
224
|
+
LEFT JOIN LATERAL (
|
|
225
|
+
SELECT
|
|
226
|
+
to_jsonb(ARRAY_AGG(DISTINCT s.ano_base ORDER BY s.ano_base DESC)) AS selected_years,
|
|
227
|
+
to_jsonb(
|
|
228
|
+
ARRAY_AGG(
|
|
229
|
+
jsonb_build_object(
|
|
230
|
+
'ano_base', s.ano_base,
|
|
231
|
+
'tipo_microdados', s.tipo_microdados,
|
|
232
|
+
'microdados_url', s.configured_microdados_url
|
|
233
|
+
)
|
|
234
|
+
ORDER BY COALESCE(s.selection_rank, 2147483647), s.ano_base DESC
|
|
235
|
+
) FILTER (WHERE s.configured_microdados_url IS NOT NULL)
|
|
236
|
+
) AS selected_downloads
|
|
237
|
+
FROM raw.pnp_instance_selection s
|
|
238
|
+
WHERE s.instance_key = i.instance_key
|
|
239
|
+
AND s.is_active = TRUE
|
|
240
|
+
AND s.tipo_microdados = et.tipo_microdados
|
|
241
|
+
) sel ON TRUE
|
|
242
|
+
WHERE i.is_active = TRUE
|
|
243
|
+
AND i.deleted_at IS NULL
|
|
244
|
+
AND pe.is_active = TRUE
|
|
245
|
+
AND et.is_active = TRUE
|
|
246
|
+
"""
|
|
247
|
+
params: list[Any] = []
|
|
248
|
+
if instance_key:
|
|
249
|
+
query += " AND i.instance_key = %s"
|
|
250
|
+
params.append(instance_key)
|
|
251
|
+
query += " ORDER BY i.instance_key, pe.pipeline_endpoint_id"
|
|
252
|
+
|
|
253
|
+
with psycopg2.connect(self.dsn, cursor_factory=RealDictCursor) as conn, conn.cursor() as cur:
|
|
254
|
+
cur.execute(query, tuple(params))
|
|
255
|
+
rows = cur.fetchall()
|
|
256
|
+
|
|
257
|
+
return [
|
|
258
|
+
EndpointDefinition(
|
|
259
|
+
id=int(row["id"]),
|
|
260
|
+
endpoint_key=str(row["endpoint_key"]),
|
|
261
|
+
description=row.get("description"),
|
|
262
|
+
page_url=row.get("page_url"),
|
|
263
|
+
api_endpoint_url=row.get("api_endpoint_url"),
|
|
264
|
+
csv_url=row.get("csv_url"),
|
|
265
|
+
dictionary_url=row.get("dictionary_url"),
|
|
266
|
+
request_params=dict(row.get("request_params") or {}),
|
|
267
|
+
)
|
|
268
|
+
for row in rows
|
|
269
|
+
]
|
|
270
|
+
|
|
271
|
+
def _fetch_powerbi_microdados_records(self, endpoint: EndpointDefinition, run_id: str) -> list[dict[str, Any]]:
|
|
272
|
+
if not endpoint.page_url:
|
|
273
|
+
raise RuntimeError("powerbi_microdados mode requires page_url")
|
|
274
|
+
|
|
275
|
+
request_params = dict(endpoint.request_params or {})
|
|
276
|
+
client = powerbi_catalog_service.create_powerbi_client(
|
|
277
|
+
page_url=endpoint.page_url,
|
|
278
|
+
timeout_seconds=self.config.timeout_seconds,
|
|
279
|
+
)
|
|
280
|
+
selection = powerbi_catalog_service.resolve_catalog_selection(
|
|
281
|
+
client=client,
|
|
282
|
+
request_params=request_params,
|
|
283
|
+
)
|
|
284
|
+
self._pending_catalog_entries.extend(
|
|
285
|
+
powerbi_catalog_service.build_catalog_entry_rows(run_id=run_id, selection=selection)
|
|
286
|
+
)
|
|
287
|
+
self._pending_run_selection.extend(
|
|
288
|
+
powerbi_catalog_service.build_run_selection_rows(run_id=run_id, selection=selection)
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
batch = pnp_download_service.build_download_batch(
|
|
292
|
+
client=client,
|
|
293
|
+
run_id=run_id,
|
|
294
|
+
endpoint_id=endpoint.id,
|
|
295
|
+
endpoint_key=endpoint.endpoint_key,
|
|
296
|
+
selection=selection,
|
|
297
|
+
)
|
|
298
|
+
self._pending_assets.extend(batch.assets)
|
|
299
|
+
self._pending_downloads.extend(batch.downloads)
|
|
300
|
+
self._pending_quarantine.extend(batch.quarantine_rows)
|
|
301
|
+
self._download_count += len(batch.downloads)
|
|
302
|
+
return batch.records
|
|
303
|
+
|
|
304
|
+
def _extract_powerbi_microdados_streaming(self, endpoint: EndpointDefinition, run_context: RunContext) -> dict[str, int]:
|
|
305
|
+
if not endpoint.page_url:
|
|
306
|
+
raise RuntimeError("powerbi_microdados mode requires page_url")
|
|
307
|
+
|
|
308
|
+
request_params = dict(endpoint.request_params or {})
|
|
309
|
+
client = powerbi_catalog_service.create_powerbi_client(
|
|
310
|
+
page_url=endpoint.page_url,
|
|
311
|
+
timeout_seconds=self.config.timeout_seconds,
|
|
312
|
+
)
|
|
313
|
+
selection = powerbi_catalog_service.resolve_catalog_selection(
|
|
314
|
+
client=client,
|
|
315
|
+
request_params=request_params,
|
|
316
|
+
)
|
|
317
|
+
catalog_entries = powerbi_catalog_service.build_catalog_entry_rows(run_id=run_context.run_id, selection=selection)
|
|
318
|
+
run_selection_rows = powerbi_catalog_service.build_run_selection_rows(run_id=run_context.run_id, selection=selection)
|
|
319
|
+
run_selection_by_url = {str(row["microdados_url"]): row for row in run_selection_rows}
|
|
320
|
+
|
|
321
|
+
stats = pnp_raw_ingestion_service.upsert_raw_metadata(
|
|
322
|
+
self.dsn,
|
|
323
|
+
pending_assets=[],
|
|
324
|
+
pending_catalog_entries=catalog_entries,
|
|
325
|
+
pending_run_selection=[],
|
|
326
|
+
pending_downloads=[],
|
|
327
|
+
write_legacy=False,
|
|
328
|
+
)
|
|
329
|
+
total_asset_count = int(stats.get("asset_count") or 0)
|
|
330
|
+
total_download_columns = int(stats.get("download_column_count") or 0)
|
|
331
|
+
total_quarantine = 0
|
|
332
|
+
total_raw_records = 0
|
|
333
|
+
manifest_downloads: list[dict[str, Any]] = []
|
|
334
|
+
|
|
335
|
+
for selection_rank, entry in enumerate(selection.selected_entries, start=1):
|
|
336
|
+
source_file_name = pnp_download_service.resolve_source_file_name(entry)
|
|
337
|
+
logger.info(
|
|
338
|
+
"Starting download endpoint_key=%s selection_rank=%s ano=%s tipo=%s file=%s url=%s",
|
|
339
|
+
endpoint.endpoint_key,
|
|
340
|
+
selection_rank,
|
|
341
|
+
entry.ano_base,
|
|
342
|
+
entry.tipo_microdados,
|
|
343
|
+
source_file_name,
|
|
344
|
+
entry.microdados_url,
|
|
345
|
+
)
|
|
346
|
+
with client.open_entry_content_stream(entry) as content_stream:
|
|
347
|
+
streamed = pnp_download_service.stream_csv_binary_stream(content_stream.raw_stream, entry.microdados_url)
|
|
348
|
+
running_download = pnp_download_service.build_download_row(
|
|
349
|
+
run_id=run_context.run_id,
|
|
350
|
+
instance_key=selection.instance_key,
|
|
351
|
+
entry=entry,
|
|
352
|
+
source_file_name=source_file_name,
|
|
353
|
+
source_file_sha256="",
|
|
354
|
+
content_type=content_stream.content_type,
|
|
355
|
+
size_bytes=0,
|
|
356
|
+
row_count_raw=0,
|
|
357
|
+
delimiter=streamed.delimiter,
|
|
358
|
+
selection_source=selection.selection_source,
|
|
359
|
+
selection_rank=selection_rank,
|
|
360
|
+
headers=streamed.headers,
|
|
361
|
+
status="running",
|
|
362
|
+
error_message=None,
|
|
363
|
+
)
|
|
364
|
+
metadata_stats = pnp_raw_ingestion_service.upsert_raw_metadata(
|
|
365
|
+
self.dsn,
|
|
366
|
+
pending_assets=[],
|
|
367
|
+
pending_catalog_entries=[],
|
|
368
|
+
pending_run_selection=[run_selection_by_url[entry.microdados_url]],
|
|
369
|
+
pending_downloads=[running_download],
|
|
370
|
+
write_legacy=False,
|
|
371
|
+
include_download_columns=True,
|
|
372
|
+
)
|
|
373
|
+
download_id_by_url = dict(metadata_stats.get("download_id_by_url") or {})
|
|
374
|
+
total_asset_count += int(metadata_stats.get("asset_count") or 0)
|
|
375
|
+
total_download_columns += int(metadata_stats.get("download_column_count") or 0)
|
|
376
|
+
|
|
377
|
+
raw_chunk: list[RawRecord] = []
|
|
378
|
+
quarantine_chunk: list[dict[str, Any]] = []
|
|
379
|
+
row_count = 0
|
|
380
|
+
invalid_row_count = 0
|
|
381
|
+
flushed_batches = 0
|
|
382
|
+
|
|
383
|
+
for row_result in streamed.row_results:
|
|
384
|
+
if row_result.invalid_row:
|
|
385
|
+
quarantine_chunk.append(
|
|
386
|
+
pnp_download_service.build_quarantine_row(
|
|
387
|
+
run_id=run_context.run_id,
|
|
388
|
+
instance_key=selection.instance_key,
|
|
389
|
+
entry=entry,
|
|
390
|
+
invalid_row=row_result.invalid_row,
|
|
391
|
+
)
|
|
392
|
+
)
|
|
393
|
+
invalid_row_count += 1
|
|
394
|
+
|
|
395
|
+
if row_result.row:
|
|
396
|
+
row_count += 1
|
|
397
|
+
raw_chunk.append(
|
|
398
|
+
{
|
|
399
|
+
"payload": pnp_download_service.build_record_payload(
|
|
400
|
+
row=row_result.row,
|
|
401
|
+
row_number=row_result.source_row_number,
|
|
402
|
+
entry=entry,
|
|
403
|
+
selection=selection,
|
|
404
|
+
source_file_name=source_file_name,
|
|
405
|
+
source_file_sha256="",
|
|
406
|
+
),
|
|
407
|
+
"source_url": entry.microdados_url,
|
|
408
|
+
"endpoint_id": endpoint.id,
|
|
409
|
+
"endpoint_key": endpoint.endpoint_key,
|
|
410
|
+
"source_kind": "powerbi_microdados",
|
|
411
|
+
}
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
if len(raw_chunk) >= self._STREAM_BATCH_SIZE or len(quarantine_chunk) >= self._STREAM_BATCH_SIZE:
|
|
415
|
+
chunk_stats = self._flush_streaming_chunk(raw_chunk, quarantine_chunk, run_context, download_id_by_url)
|
|
416
|
+
total_raw_records += int(chunk_stats.get("raw_record_count") or 0)
|
|
417
|
+
total_quarantine += int(chunk_stats.get("quarantine_count") or 0)
|
|
418
|
+
total_asset_count += int(chunk_stats.get("asset_count") or 0)
|
|
419
|
+
flushed_batches += 1
|
|
420
|
+
if flushed_batches == 1 or flushed_batches % 100 == 0:
|
|
421
|
+
logger.info(
|
|
422
|
+
"Streaming progress endpoint_key=%s file=%s rows=%s invalid_rows=%s flushed_batches=%s raw_records_total=%s",
|
|
423
|
+
endpoint.endpoint_key,
|
|
424
|
+
source_file_name,
|
|
425
|
+
row_count,
|
|
426
|
+
invalid_row_count,
|
|
427
|
+
flushed_batches,
|
|
428
|
+
total_raw_records,
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
if raw_chunk or quarantine_chunk:
|
|
432
|
+
chunk_stats = self._flush_streaming_chunk(raw_chunk, quarantine_chunk, run_context, download_id_by_url)
|
|
433
|
+
total_raw_records += int(chunk_stats.get("raw_record_count") or 0)
|
|
434
|
+
total_quarantine += int(chunk_stats.get("quarantine_count") or 0)
|
|
435
|
+
total_asset_count += int(chunk_stats.get("asset_count") or 0)
|
|
436
|
+
flushed_batches += 1
|
|
437
|
+
|
|
438
|
+
final_download = pnp_download_service.build_download_row(
|
|
439
|
+
run_id=run_context.run_id,
|
|
440
|
+
instance_key=selection.instance_key,
|
|
441
|
+
entry=entry,
|
|
442
|
+
source_file_name=source_file_name,
|
|
443
|
+
source_file_sha256=content_stream.sha256 or "",
|
|
444
|
+
content_type=content_stream.content_type,
|
|
445
|
+
size_bytes=content_stream.size_bytes,
|
|
446
|
+
row_count_raw=row_count,
|
|
447
|
+
delimiter=streamed.delimiter,
|
|
448
|
+
selection_source=selection.selection_source,
|
|
449
|
+
selection_rank=selection_rank,
|
|
450
|
+
headers=streamed.headers,
|
|
451
|
+
status="success",
|
|
452
|
+
error_message=None,
|
|
453
|
+
)
|
|
454
|
+
manifest_downloads.append(final_download)
|
|
455
|
+
download_asset = pnp_download_service.build_download_asset_row(
|
|
456
|
+
run_id=run_context.run_id,
|
|
457
|
+
endpoint_id=endpoint.id,
|
|
458
|
+
endpoint_key=endpoint.endpoint_key,
|
|
459
|
+
entry=entry,
|
|
460
|
+
source_file_name=source_file_name,
|
|
461
|
+
content_type=content_stream.content_type,
|
|
462
|
+
size_bytes=content_stream.size_bytes,
|
|
463
|
+
sha256=content_stream.sha256 or "",
|
|
464
|
+
row_count=row_count,
|
|
465
|
+
headers=streamed.headers,
|
|
466
|
+
)
|
|
467
|
+
final_stats = pnp_raw_ingestion_service.upsert_raw_metadata(
|
|
468
|
+
self.dsn,
|
|
469
|
+
pending_assets=[download_asset],
|
|
470
|
+
pending_catalog_entries=[],
|
|
471
|
+
pending_run_selection=[],
|
|
472
|
+
pending_downloads=[final_download],
|
|
473
|
+
write_legacy=False,
|
|
474
|
+
include_download_columns=False,
|
|
475
|
+
)
|
|
476
|
+
total_asset_count += int(final_stats.get("asset_count") or 0)
|
|
477
|
+
self._download_count += 1
|
|
478
|
+
logger.info(
|
|
479
|
+
"Buffered download loaded endpoint_key=%s file=%s rows=%s invalid_rows=%s flushed_batches=%s size_bytes=%s",
|
|
480
|
+
endpoint.endpoint_key,
|
|
481
|
+
source_file_name,
|
|
482
|
+
row_count,
|
|
483
|
+
invalid_row_count,
|
|
484
|
+
flushed_batches,
|
|
485
|
+
content_stream.size_bytes,
|
|
486
|
+
)
|
|
487
|
+
|
|
488
|
+
manifest_asset = pnp_download_service.build_manifest_asset_row(
|
|
489
|
+
run_id=run_context.run_id,
|
|
490
|
+
endpoint_id=endpoint.id,
|
|
491
|
+
endpoint_key=endpoint.endpoint_key,
|
|
492
|
+
selection=selection,
|
|
493
|
+
downloads=manifest_downloads,
|
|
494
|
+
raw_record_count=sum(int(item.get("row_count_raw") or 0) for item in manifest_downloads),
|
|
495
|
+
)
|
|
496
|
+
manifest_stats = pnp_raw_ingestion_service.upsert_raw_metadata(
|
|
497
|
+
self.dsn,
|
|
498
|
+
pending_assets=[manifest_asset],
|
|
499
|
+
pending_catalog_entries=[],
|
|
500
|
+
pending_run_selection=[],
|
|
501
|
+
pending_downloads=[],
|
|
502
|
+
write_legacy=False,
|
|
503
|
+
)
|
|
504
|
+
total_asset_count += int(manifest_stats.get("asset_count") or 0)
|
|
505
|
+
|
|
506
|
+
return {
|
|
507
|
+
"asset_count": total_asset_count,
|
|
508
|
+
"download_column_count": total_download_columns,
|
|
509
|
+
"quarantine_count": total_quarantine,
|
|
510
|
+
"raw_record_count": total_raw_records,
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
def _flush_streaming_chunk(
|
|
514
|
+
self,
|
|
515
|
+
raw_chunk: list[RawRecord],
|
|
516
|
+
quarantine_chunk: list[dict[str, Any]],
|
|
517
|
+
run_context: RunContext,
|
|
518
|
+
download_id_by_url: dict[str, int],
|
|
519
|
+
) -> dict[str, int]:
|
|
520
|
+
if not raw_chunk and not quarantine_chunk:
|
|
521
|
+
return {"raw_record_count": 0, "quarantine_count": 0, "asset_count": 0}
|
|
522
|
+
|
|
523
|
+
normalization_result = pnp_raw_ingestion_service.normalize_raw_records(raw_chunk, run_context)
|
|
524
|
+
pending_quarantine = [*quarantine_chunk, *normalization_result.quarantine_rows]
|
|
525
|
+
stats = pnp_raw_ingestion_service.load_raw_record_chunk(
|
|
526
|
+
self.dsn,
|
|
527
|
+
normalized_records=normalization_result.normalized_records,
|
|
528
|
+
pending_quarantine=pending_quarantine,
|
|
529
|
+
download_id_by_url=download_id_by_url,
|
|
530
|
+
pending_assets=[],
|
|
531
|
+
write_legacy=False,
|
|
532
|
+
)
|
|
533
|
+
raw_chunk.clear()
|
|
534
|
+
quarantine_chunk.clear()
|
|
535
|
+
return stats
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
@staticmethod
|
|
539
|
+
def _parse_csv(content: str) -> pnp_download_service.ParsedCsvContent:
|
|
540
|
+
return pnp_download_service.parse_csv_content(content)
|
|
541
|
+
|
|
542
|
+
@staticmethod
|
|
543
|
+
def _wrap_payloads(
|
|
544
|
+
payloads: list[dict[str, Any]],
|
|
545
|
+
endpoint: EndpointDefinition,
|
|
546
|
+
source_kind: str,
|
|
547
|
+
) -> list[RawRecord]:
|
|
548
|
+
return [
|
|
549
|
+
{
|
|
550
|
+
"payload": payload,
|
|
551
|
+
"source_url": str(payload.get("microdados_url") or endpoint.page_url or ""),
|
|
552
|
+
"endpoint_id": endpoint.id,
|
|
553
|
+
"endpoint_key": endpoint.endpoint_key,
|
|
554
|
+
"source_kind": source_kind,
|
|
555
|
+
}
|
|
556
|
+
for payload in payloads
|
|
557
|
+
if isinstance(payload, dict)
|
|
558
|
+
]
|