1717
1818from src .config import Settings , get_settings
1919from src .models import Sekolah
20- from src .core .gsheet import fetch_csv_data
20+ from src .core .gsheet import fetch_csv_data , _extract_file_version
2121from src .core .s3 import (_upload_to_s3 , _latest_csv_from_s3 , _read_csv_from_s3 )
2222from src .models .sekolah import SekolahStatus
2323from src .pipeline .status_sync import sync_entiti_statuses
@@ -91,22 +91,22 @@ def _read_csv(path: str) -> Iterable[Dict[str, Any]]:
9191def _read_google_sheet (sheet_id : str , gid : str ) -> Iterable [Dict [str , Any ]]:
9292 logger .info ("Scraping Google Sheet directly (sheet_id=%s, gid=%s)" , sheet_id , gid ,)
9393
94- csv_bytes = fetch_csv_data (sheet_id , gid )
94+ csv_bytes , _ = fetch_csv_data (sheet_id , gid )
9595 df = pd .read_csv (io .BytesIO (csv_bytes ), dtype = str ).fillna ("" )
9696
9797 logger .info ("Google Sheet loaded: %d rows, %d columns" , df .shape [0 ], df .shape [1 ])
9898 return df .to_dict (orient = "records" )
9999
100100
101- def _load_rows (settings : Settings ) -> Iterable [Dict [str , Any ]]:
102- csv_bytes = fetch_csv_data (settings .gsheet_id , settings .gsheet_gid )
101+ def _load_rows (settings : Settings ) -> tuple [ Iterable [Dict [str , Any ]], str | None ]:
102+ csv_bytes , file_name = fetch_csv_data (settings .gsheet_id , settings .gsheet_gid )
103103 logger .info ("Uploading CSV data to S3 bucket %s" , settings .s3_bucket_dataproc )
104- s3_key = _upload_to_s3 (csv_bytes , settings .s3_bucket_dataproc , settings .s3_prefix_sekolah )
104+ s3_key = _upload_to_s3 (csv_bytes , settings .s3_bucket_dataproc , settings .s3_prefix_sekolah , file_name )
105105 logger .info ("CSV uploaded to S3 at key: %s" , s3_key )
106106
107107 df = _read_csv_from_s3 (settings .s3_bucket_dataproc , s3_key )
108108 logger .info ("CSV loaded from S3: %d rows, %d columns" , df .shape [0 ], df .shape [1 ])
109- return df .to_dict (orient = "records" )
109+ return df .to_dict (orient = "records" ), file_name
110110
111111
112112def _chunked (rows : Iterable [Dict [str , Any ]], size : int ) -> Iterator [list [Dict [str , Any ]]]:
@@ -142,13 +142,14 @@ def _format_validation_messages(exc: ValidationError) -> list[str]:
142142def _collect_documents (
143143 settings : Settings ,
144144 kodSekolah_madani : Set [str ],
145- ) -> tuple [list [dict [str , Any ]], list [dict [str , Any ]], int , set [Any ]]:
145+ ) -> tuple [list [dict [str , Any ]], list [dict [str , Any ]], int , set [Any ], str | None ]:
146146 documents : list [dict [str , Any ]] = []
147147 errors : list [dict [str , Any ]] = []
148148 total = 0
149149 present_identifiers : set [Any ] = set ()
150150
151- for index , row in enumerate (_load_rows (settings ), start = 1 ):
151+ rows , file_name = _load_rows (settings )
152+ for index , row in enumerate (rows , start = 1 ):
152153 total += 1
153154
154155 raw_kod = str (row .get ("KODSEKOLAH" , "" )).strip ()
@@ -169,7 +170,7 @@ def _collect_documents(
169170 document ["isSekolahAngkatMADANI" ] = canonical_kod in kodSekolah_madani
170171 documents .append (document )
171172
172- return documents , errors , total , present_identifiers
173+ return documents , errors , total , present_identifiers , file_name
173174
174175
175176def _load_kodSekolah_madani (database , settings : Settings ) -> Set [str ]:
@@ -326,7 +327,7 @@ def run(settings: Settings) -> dict[str, Any]:
326327 sekolah_collection = database [Sekolah .collection_name ]
327328 entiti_collection = database [settings .entiti_sekolah_collection ]
328329 kodSekolah_madani = _load_kodSekolah_madani (database , settings )
329- documents , errors , total , present_identifiers = _collect_documents (settings , kodSekolah_madani )
330+ documents , errors , total , present_identifiers , file_name = _collect_documents (settings , kodSekolah_madani )
330331
331332 for document in documents :
332333 # All schools present in raw file are ACTIVE
@@ -378,7 +379,13 @@ def run(settings: Settings) -> dict[str, Any]:
378379 "inactivated" : inactivated ,
379380 "entiti_synced" : entiti_synced ,
380381 }
381- upsert_dataset_status ("sekolah" , settings )
382+
383+ file_version = _extract_file_version (file_name )
384+
385+ if not file_version :
386+ logger .warning ("File name not found or failed to extract fileVersion. filename=%s" , file_name )
387+
388+ upsert_dataset_status ("sekolah" , settings , file_version )
382389 return summary
383390
384391def run_with_overrides (** overrides : Any ) -> dict [str , Any ]:
0 commit comments