""" BIGQUERY CLEANED LAYER ETL Kimball Data Warehouse Architecture Kimball ETL Flow yang dijalankan file ini: Input : STAGING layer (Silver) — staging_integrated (fs_asean_silver) Output : STAGING layer (Silver) — cleaned_integrated (fs_asean_silver) Audit : AUDIT layer — etl_logs, etl_metadata (fs_asean_audit) Classes: CleanedDataLoader — Cleaning, enrichment, & load ke Silver layer Usage: python bigquery_cleaned_layer.py """ import re import json import logging from datetime import datetime from typing import Dict, Optional import pandas as pd from scripts.bigquery_config import get_bigquery_client from scripts.bigquery_helpers import ( log_update, load_to_bigquery, read_from_bigquery, setup_logging, save_etl_metadata, ) from google.cloud import bigquery # ============================================================================= # LOAD STAGING DATA # ============================================================================= REQUIRED_INPUT_COLUMNS = [ 'source', 'indicator_original', 'indicator_standardized', 'country', 'year', 'year_range', 'value', 'unit', ] def load_staging_data(client: bigquery.Client) -> pd.DataFrame: """Load data dari staging_integrated (STAGING/Silver layer).""" print("\nLoading data from staging_integrated (fs_asean_silver)...") df_staging = read_from_bigquery(client, 'staging_integrated', layer='silver') print(f" ✓ Loaded : {len(df_staging):,} rows") print(f" Columns : {len(df_staging.columns)}") if df_staging.empty: print(" ⚠ staging_integrated kosong.") return df_staging missing_cols = [c for c in REQUIRED_INPUT_COLUMNS if c not in df_staging.columns] if missing_cols: raise ValueError(f"Kolom wajib tidak ada di staging_integrated: {missing_cols}") print(f" Sources : {df_staging['source'].nunique()}") print(f" Indicators : {df_staging['indicator_standardized'].nunique()}") print(f" Countries : {df_staging['country'].nunique()}") years = pd.to_numeric(df_staging['year'], errors='coerce').dropna() if not years.empty: print(f" Year range : {int(years.min())}-{int(years.max())}") else: print(" Year range : - (kolom year kosong)") return df_staging # ============================================================================= # COLUMN CONSTRAINT HELPERS # ============================================================================= # Schema constraints — semua varchar max lengths COLUMN_CONSTRAINTS = { 'source' : 20, 'indicator_original' : 255, 'indicator_standardized': 255, 'country' : 100, 'year_range' : 20, 'unit' : 20, 'pillar' : 20, 'direction' : 15, # 'higher_better'=13, 'lower_better'=12 } def truncate_string(value, max_length: int): """Truncate string ke max_length, return as-is jika None/NaN.""" if pd.isna(value): return value value_str = str(value) return value_str[:max_length] if len(value_str) > max_length else value_str def apply_column_constraints(df: pd.DataFrame) -> pd.DataFrame: """ Apply column length constraints sesuai schema tabel. Melaporkan kolom mana yang dipotong dan contohnya. """ df_constrained = df.copy() truncation_report = {} for column, max_length in COLUMN_CONSTRAINTS.items(): if column not in df_constrained.columns: continue mask = ( df_constrained[column].notna() & (df_constrained[column].astype(str).str.len() > max_length) ) truncated_count = mask.sum() if truncated_count > 0: truncation_report[column] = { 'count' : int(truncated_count), 'max_length': max_length, 'examples' : df_constrained.loc[mask, column].head(3).tolist() } df_constrained[column] = df_constrained[column].apply( lambda x: truncate_string(x, max_length) ) if truncation_report: print("\n ⚠ Column Truncations Applied:") for column, info in truncation_report.items(): print(f" - {column}: {info['count']} values truncated to {info['max_length']} chars") else: print("\n ✓ No truncations needed — all values within constraints") return df_constrained # ============================================================================= # COUNTRY NAME STANDARDIZATION # ============================================================================= ASEAN_MAPPING = { 'BRN' : 'Brunei Darussalam', 'BRUNEI' : 'Brunei Darussalam', 'BRUNEI DARUSSALAM' : 'Brunei Darussalam', 'KHM' : 'Cambodia', 'CAMBODIA' : 'Cambodia', 'IDN' : 'Indonesia', 'INDONESIA' : 'Indonesia', 'LAO' : 'Laos', 'LAOS' : 'Laos', "LAO PEOPLE'S DEMOCRATIC REPUBLIC" : 'Laos', 'LAO PDR' : 'Laos', 'MYS' : 'Malaysia', 'MALAYSIA' : 'Malaysia', 'MMR' : 'Myanmar', 'MYANMAR' : 'Myanmar', 'BURMA' : 'Myanmar', 'PHL' : 'Philippines', 'PHILIPPINES' : 'Philippines', 'SGP' : 'Singapore', 'SINGAPORE' : 'Singapore', 'THA' : 'Thailand', 'THAILAND' : 'Thailand', 'VNM' : 'Vietnam', 'VIETNAM' : 'Vietnam', 'VIET NAM' : 'Vietnam', } ASEAN_COUNTRIES = set(ASEAN_MAPPING.values()) def standardize_country_names_asean(df: pd.DataFrame, country_column: str = 'country') -> tuple: """ Standardize country names untuk ASEAN. Ensures country names within varchar(100) constraint. Returns: tuple: (df_clean, report_dict) """ df_clean = df.copy() def map_country(country): if pd.isna(country): return country # rapikan spasi & apostrof melengkung sebelum lookup s = re.sub(r'\s+', ' ', str(country).strip().replace('’', "'")) mapped = ASEAN_MAPPING.get(s.upper(), s) return mapped[:100] if len(mapped) > 100 else mapped original = df_clean[country_column].copy() df_clean[country_column] = original.apply(map_country) # NaN != NaN bernilai True, jadi baris NaN harus dikecualikan secara eksplisit changed = original.notna() & (original != df_clean[country_column]) changes = dict(zip(original[changed], df_clean.loc[changed, country_column])) unmapped = sorted( set(df_clean[country_column].dropna().unique()) - ASEAN_COUNTRIES ) return df_clean, { 'countries_mapped': len(changes), 'changes' : changes, 'non_asean_values': unmapped, } # ============================================================================= # PILLAR CLASSIFICATION # ============================================================================= def _match_pillar(indicator_name) -> Optional[str]: """Return pillar jika ada keyword yang cocok, None jika tidak ada.""" ind = str(indicator_name).lower() for kw in ['requirement', 'coefficient', 'losses', 'fat supply']: if kw in ind: return 'Sustainability' if any(kw in ind for kw in [ 'adequacy', 'protein supply', 'supply of protein', 'dietary energy supply', 'share of dietary energy', 'derived from cereals' ]): return 'Availability' if any(kw in ind for kw in [ 'variability', 'cereal import dependency', 'arable land equipped', 'political stability', 'value of food imports in total' ]): return 'Stability' if any(kw in ind for kw in [ 'gdp', 'gross domestic product', 'rail lines', 'road density', 'number of moderately', 'number of severely', 'number of people undernourished', 'prevalence of moderate', 'prevalence of severe', 'prevalence of undernourishment', 'food insecure' ]): return 'Access' if any(kw in ind for kw in [ 'wasting', 'wasted', 'stunted', 'overweight', 'obese', 'obesity', 'anemia', 'birthweight', 'breastfeeding', 'drinking water', 'sanitation', 'children under 5', 'newborns with low', 'women of reproductive' ]): return 'Utilization' return None def assign_pillar(indicator_name: str) -> str: """ Assign pillar berdasarkan keyword indikator. Return values: 'Availability', 'Access', 'Utilization', 'Stability', 'Sustainability' All ≤ 20 chars (varchar(20) constraint). Indikator tanpa keyword yang cocok jatuh ke 'Sustainability' (default). """ if pd.isna(indicator_name): return 'Sustainability' return _match_pillar(indicator_name) or 'Sustainability' # ============================================================================= # DIRECTION CLASSIFICATION # ============================================================================= _HIGHER_BETTER_EXCEPTIONS = [ 'exclusive breastfeeding', 'dietary energy supply', 'dietary energy supply adequacy', 'average fat supply', 'average protein supply', 'supply of protein of animal origin', ] _LOWER_BETTER_KEYWORDS = [ 'prevalence of undernourishment', 'prevalence of severe food insecurity', 'prevalence of moderate or severe food insecurity', 'prevalence of moderate food insecurity', 'prevalence of wasting', 'prevalence of stunting', 'prevalence of overweight', 'prevalence of obesity', 'prevalence of anemia', 'prevalence of low birthweight', 'number of people undernourished', 'number of severely food insecure', 'number of moderately or severely food insecure', 'number of children under 5 years affected by wasting', 'number of children under 5 years of age who are overweight', 'number of children under 5 years of age who are stunted', 'number of newborns with low birthweight', 'number of obese adults', 'number of women of reproductive age', 'percentage of children under 5 years affected by wasting', 'percentage of children under 5 years of age who are overweight', 'percentage of children under 5 years of age who are stunted', 'cereal import dependency', 'import dependency', 'value of food imports in total merchandise exports', 'value of food imports', 'variability of food production', 'variability of food supply', 'per capita food production variability', 'per capita food supply variability', 'coefficient of variation', 'incidence of caloric losses', 'food losses', ] def _match_direction(indicator_name) -> Optional[str]: """Return direction jika ada keyword yang cocok, None jika tidak ada.""" ind = str(indicator_name).lower() # Spesifik lower_better if 'share of dietary energy supply derived from cereals' in ind: return 'lower_better' # Higher_better exceptions — cek sebelum lower_better keywords if any(kw in ind for kw in _HIGHER_BETTER_EXCEPTIONS): return 'higher_better' # Lower_better — masalah yang harus diminimalkan if any(kw in ind for kw in _LOWER_BETTER_KEYWORDS): return 'lower_better' return None def assign_direction(indicator_name: str) -> str: """ Assign direction berdasarkan indikator. Return values: 'higher_better' (13 chars) atau 'lower_better' (12 chars) Both ≤ 15 chars (varchar(15) constraint). Indikator tanpa keyword yang cocok jatuh ke 'higher_better' (default). """ if pd.isna(indicator_name): return 'higher_better' return _match_direction(indicator_name) or 'higher_better' # ============================================================================= # CLEANED DATA LOADER # ============================================================================= class CleanedDataLoader: """ Loader untuk cleaned integrated data ke STAGING layer (Silver). Kimball context: Input : staging_integrated → STAGING (Silver) — fs_asean_silver Output : cleaned_integrated → STAGING (Silver) — fs_asean_silver Audit : etl_logs, etl_metadata → AUDIT — fs_asean_audit Pipeline steps: 1. Standardize country names (ASEAN) 2. Remove missing values (hanya kolom wajib) 3. Remove duplicates 4. Add pillar & direction classification 5. Apply column constraints & cast tipe data 6. Validate 7. Load ke BigQuery 8. Log ke Audit layer """ SCHEMA = [ bigquery.SchemaField("source", "STRING", mode="REQUIRED"), bigquery.SchemaField("indicator_original", "STRING", mode="REQUIRED"), bigquery.SchemaField("indicator_standardized", "STRING", mode="REQUIRED"), bigquery.SchemaField("country", "STRING", mode="REQUIRED"), bigquery.SchemaField("year", "INTEGER", mode="NULLABLE"), bigquery.SchemaField("year_range", "STRING", mode="NULLABLE"), bigquery.SchemaField("value", "FLOAT", mode="NULLABLE"), bigquery.SchemaField("unit", "STRING", mode="NULLABLE"), bigquery.SchemaField("pillar", "STRING", mode="REQUIRED"), bigquery.SchemaField("direction", "STRING", mode="REQUIRED"), ] # Kolom yang benar-benar wajib ada (sesuai mode REQUIRED di schema + value # karena baris tanpa nilai tidak berguna untuk analisis). # year, year_range, unit bersifat NULLABLE sehingga TIDAK boleh membuat baris dibuang. REQUIRED_NOT_NULL = [ 'source', 'indicator_original', 'indicator_standardized', 'country', 'value' ] # Kunci unik satu observasi. source & year_range ikut agar data dari sumber # berbeda / periode berbeda tidak saling menimpa. DEDUP_KEY = ['source', 'indicator_standardized', 'country', 'year', 'year_range'] OUTPUT_COLUMNS = [f.name for f in SCHEMA] def __init__(self, client: bigquery.Client, load_mode: str = 'full_refresh', filter_non_asean: bool = False): self.client = client self.load_mode = load_mode self.filter_non_asean = filter_non_asean self.logger = logging.getLogger(self.__class__.__name__) self.logger.propagate = False self.table_name = 'cleaned_integrated' self.target_layer = 'silver' self.metadata = { 'source_class' : self.__class__.__name__, 'table_name' : self.table_name, 'start_time' : None, 'end_time' : None, 'duration_seconds' : None, 'rows_fetched' : 0, 'rows_transformed' : 0, 'rows_loaded' : 0, 'load_mode' : load_mode, 'validation_metrics': {} } # ------------------------------------------------------------------ # STEP METHODS # ------------------------------------------------------------------ def _step_standardize_countries(self, df: pd.DataFrame) -> pd.DataFrame: print("\n [Step 1/5] Standardize country names...") df, report = standardize_country_names_asean(df, country_column='country') print(f" ✓ Country name variants mapped : {report['countries_mapped']}") if report['non_asean_values']: print(f" ⚠ Non-ASEAN values found : {report['non_asean_values']}") if self.filter_non_asean: before = len(df) df = df[df['country'].isin(ASEAN_COUNTRIES)] print(f" ✓ Non-ASEAN rows removed : {before - len(df):,}") unique_countries = sorted(df['country'].dropna().unique()) print(f" Countries ({len(unique_countries)}) : {', '.join(unique_countries)}") # Log ke tabel TARGET (bukan staging_integrated) agar audit trail benar log_update(self.client, 'STAGING', self.table_name, 'standardize_asean', report['countries_mapped']) return df def _step_remove_missing(self, df: pd.DataFrame) -> pd.DataFrame: print("\n [Step 2/5] Remove missing values...") rows_before = len(df) df_clean = df.copy() # String kosong / whitespace dianggap missing for col in df_clean.select_dtypes(include='object').columns: df_clean[col] = df_clean[col].replace(r'^\s*$', pd.NA, regex=True) # Hanya kolom wajib — year_range / unit NULLABLE dan sering kosong df_clean = df_clean.dropna(subset=self.REQUIRED_NOT_NULL) # Observasi harus punya periode: minimal year ATAU year_range has_period = df_clean['year'].notna() | df_clean['year_range'].notna() df_clean = df_clean[has_period] rows_after = len(df_clean) removed = rows_before - rows_after print(f" Rows before : {rows_before:,}") print(f" Rows after : {rows_after:,}") if rows_before: print(f" Rows removed : {removed:,} ({removed/rows_before*100:.1f}%)") print(f" Retention : {rows_after/rows_before*100:.1f}%") return df_clean def _step_remove_duplicates(self, df: pd.DataFrame) -> pd.DataFrame: print("\n [Step 3/5] Remove duplicates...") if df.empty: return df key = self.DEDUP_KEY exact_dups = df.duplicated().sum() key_dups = df.duplicated(subset=key).sum() # duplikat kunci dengan nilai berbeda = konflik data, perlu perhatian conflicts = ( df.groupby(key, dropna=False)['value'].nunique().gt(1).sum() ) print(f" Exact duplicates : {exact_dups:,}") print(f" Key duplicates : {key_dups:,}") if conflicts: print(f" ⚠ Key dengan nilai berbeda (konflik): {conflicts:,} " f"— baris pertama (urut deterministik) dipertahankan") rows_before = len(df) # Urutkan dulu agar keep='first' deterministik antar-run df_sorted = df.sort_values( key + ['indicator_original', 'value'], na_position='last', kind='mergesort' ) df_clean = df_sorted.drop_duplicates(subset=key, keep='first') removed = rows_before - len(df_clean) print(f" Rows removed : {removed:,} ({removed/rows_before*100:.1f}%)") return df_clean def _step_add_classifications(self, df: pd.DataFrame) -> pd.DataFrame: print("\n [Step 4/5] Add pillar & direction classification...") df = df.copy() df['pillar'] = df['indicator_standardized'].apply(assign_pillar) df['direction'] = df['indicator_standardized'].apply(assign_direction) # Laporkan indikator yang jatuh ke nilai default (tidak ada keyword cocok) indicators = df['indicator_standardized'].drop_duplicates() no_pillar = [i for i in indicators if _match_pillar(i) is None] no_dir = [i for i in indicators if _match_direction(i) is None] if no_pillar: print(f" ⚠ {len(no_pillar)} indikator tanpa rule pillar (default 'Sustainability'):") for ind in no_pillar[:10]: print(f" - {ind}") if no_dir: print(f" ℹ {len(no_dir)} indikator tanpa rule direction (default 'higher_better'):") for ind in no_dir[:10]: print(f" - {ind}") print(" ✓ Pillar distribution:") for pillar, count in df['pillar'].value_counts().items(): print(f" - {pillar}: {count:,}") print(" ✓ Direction distribution:") for direction, count in df['direction'].value_counts().items(): print(f" - {direction}: {count:,} ({count / len(df) * 100:.1f}%)") return df def _step_apply_constraints(self, df: pd.DataFrame) -> pd.DataFrame: print("\n [Step 5/5] Apply column constraints & cast types...") df = apply_column_constraints(df) # Cast sesuai schema BigQuery: year INTEGER (nullable), value FLOAT. # Tanpa ini year bisa menjadi float64 dan gagal/berubah saat load. df['year'] = pd.to_numeric(df['year'], errors='coerce').astype('Int64') df['value'] = pd.to_numeric(df['value'], errors='coerce').astype('float64') # Baris yang valuenya tidak bisa di-cast tidak boleh lolos bad_value = df['value'].isna().sum() if bad_value: print(f" ⚠ {bad_value:,} baris dibuang: value bukan angka") df = df.dropna(subset=['value']) return df[self.OUTPUT_COLUMNS].reset_index(drop=True) # ------------------------------------------------------------------ # VALIDATION # ------------------------------------------------------------------ def validate_data(self, df: pd.DataFrame) -> Dict: validation = { 'total_rows' : int(len(df)), 'total_columns' : int(len(df.columns)), 'duplicate_count' : int(df.duplicated().sum()), 'completeness_pct': float(round((1 - df.isnull().sum().sum() / df.size) * 100, 2)), 'memory_usage_mb' : float(round(df.memory_usage(deep=True).sum() / 1024**2, 2)) } if 'year' in df.columns: validation['year_range'] = { 'min' : int(df['year'].min()) if not df['year'].isnull().all() else None, 'max' : int(df['year'].max()) if not df['year'].isnull().all() else None, 'unique_years': int(df['year'].nunique()) } for col in ('pillar', 'direction', 'source'): if col in df.columns: validation[f'{col}_breakdown'] = { str(k): int(v) for k, v in df[col].value_counts().to_dict().items() } if 'indicator_standardized' in df.columns: validation['unique_indicators'] = int(df['indicator_standardized'].nunique()) if 'country' in df.columns: validation['unique_countries'] = int(df['country'].nunique()) # Column length check column_length_check = {} for col, max_len in COLUMN_CONSTRAINTS.items(): if col in df.columns: max_actual = df[col].dropna().astype(str).str.len().max() max_actual = 0 if pd.isna(max_actual) else int(max_actual) column_length_check[col] = { 'max_length_constraint': max_len, 'max_actual_length' : max_actual, 'within_limit' : bool(max_actual <= max_len) } validation['column_length_check'] = column_length_check return validation # ------------------------------------------------------------------ # RUN # ------------------------------------------------------------------ def run(self, df: pd.DataFrame) -> int: """ Execute full cleaning pipeline → load ke STAGING (Silver). Returns: int: Rows loaded """ self.metadata['start_time'] = datetime.now() self.metadata['rows_fetched'] = len(df) if df.empty: print(" ERROR: DataFrame is empty, nothing to process.") return 0 # Pipeline steps df = self._step_standardize_countries(df) df = self._step_remove_missing(df) df = self._step_remove_duplicates(df) df = self._step_add_classifications(df) if not df.empty else df df = self._step_apply_constraints(df) if not df.empty else df # Pengaman: WRITE_TRUNCATE dengan DataFrame kosong akan MENGHAPUS tabel target. if df.empty: print("\n ERROR: Hasil cleaning kosong — load dibatalkan, " "tabel cleaned_integrated tidak diubah.") return 0 self.metadata['rows_transformed'] = len(df) # Validate validation = self.validate_data(df) self.metadata['validation_metrics'] = validation all_within_limits = all( info['within_limit'] for info in validation.get('column_length_check', {}).values() ) if not all_within_limits: print("\n ⚠ WARNING: Some columns still exceed length constraints!") for col, info in validation['column_length_check'].items(): if not info['within_limit']: print(f" - {col}: {info['max_actual_length']} > {info['max_length_constraint']}") # Load ke Silver print(f"\n Loading to [STAGING/Silver] {self.table_name} → fs_asean_silver...") rows_loaded = load_to_bigquery( self.client, df, self.table_name, layer='silver', write_disposition="WRITE_TRUNCATE", schema=self.SCHEMA ) self.metadata['rows_loaded'] = rows_loaded # Audit logs log_update(self.client, 'STAGING', self.table_name, 'full_refresh', rows_loaded) # ETL metadata self.metadata['end_time'] = datetime.now() self.metadata['duration_seconds'] = ( self.metadata['end_time'] - self.metadata['start_time'] ).total_seconds() self.metadata['execution_timestamp'] = self.metadata['start_time'] self.metadata['completeness_pct'] = validation.get('completeness_pct', 0) self.metadata['config_snapshot'] = json.dumps({ 'load_mode' : self.load_mode, 'filter_non_asean': self.filter_non_asean, 'dedup_key' : self.DEDUP_KEY, }) self.metadata['validation_metrics'] = json.dumps(validation) save_etl_metadata(self.client, self.metadata) # Summary print(f"\n ✓ Cleaned Integration completed: {rows_loaded:,} rows") print(f" Duration : {self.metadata['duration_seconds']:.2f}s") print(f" Completeness : {validation['completeness_pct']:.2f}%") if 'year_range' in validation: yr = validation['year_range'] if yr['min'] and yr['max']: print(f" Year range : {yr['min']}–{yr['max']}") print(f" Indicators : {validation.get('unique_indicators', '-')}") print(f" Countries : {validation.get('unique_countries', '-')}") print("\n Schema Validation:") for col, info in validation.get('column_length_check', {}).items(): status = "✓" if info['within_limit'] else "✗" print(f" {status} {col}: {info['max_actual_length']}/{info['max_length_constraint']}") print("\n Metadata → [AUDIT] etl_metadata") return rows_loaded # ============================================================================= # AIRFLOW TASK FUNCTIONS ← sama polanya dengan raw layer # ============================================================================= def run_cleaned_integration(): """ Airflow task: Load cleaned_integrated dari staging_integrated. Dipanggil oleh DAG setelah task staging_integration_to_silver selesai. """ client = get_bigquery_client() df_staging = load_staging_data(client) loader = CleanedDataLoader(client, load_mode='full_refresh') rows = loader.run(df_staging) print(f"Cleaned layer loaded: {rows:,} rows") if rows == 0: # Gagalkan task Airflow agar tidak lanjut ke layer berikutnya dengan data lama/kosong raise ValueError("Cleaned layer menghasilkan 0 rows — periksa staging_integrated.") # ============================================================================= # MAIN EXECUTION # ============================================================================= if __name__ == "__main__": print("=" * 60) print("BIGQUERY CLEANED LAYER ETL") print("Kimball DW Architecture") print(" Input : STAGING (Silver) → staging_integrated") print(" Output : STAGING (Silver) → cleaned_integrated") print(" Audit : AUDIT → etl_logs, etl_metadata") print("=" * 60) setup_logging() client = get_bigquery_client() df_staging = load_staging_data(client) print("\n[1/1] Cleaned Integration → STAGING (Silver)...") loader = CleanedDataLoader(client, load_mode='full_refresh') final_count = loader.run(df_staging) print("\n" + "=" * 60) print("✓ CLEANED LAYER ETL COMPLETED") print(f" 🥈 STAGING (Silver) : cleaned_integrated ({final_count:,} rows)") print(" 📋 AUDIT : etl_logs, etl_metadata") print("=" * 60)