1010from macro_agents .defs .domains .sec .config import MAX_ERROR_DETAILS
1111from macro_agents .defs .domains .sec .filing_downloader import FilingDownloader
1212from macro_agents .defs .domains .sec .metadata import sec_filing_metadata
13- from macro_agents .defs .resources .gcs import GCSResource
1413from macro_agents .defs .resources .bigquery_warehouse import BigQueryWarehouseResource
14+ from macro_agents .defs .resources .gcs import GCSResource
1515from macro_agents .defs .resources .sec_edgar import SECEdgarResource
1616
1717sec_filing_documents_partitions = dg .DynamicPartitionsDefinition (
@@ -38,65 +38,57 @@ def sec_filing_documents(
3838 Partition key is the filing_id. Uses FilingDownloader for download logic.
3939 """
4040 filing_id = context .partition_key
41- downloader = FilingDownloader (sec_edgar , gcs , context .log )
42-
43- conn = None
44- try :
45- conn = bq .get_connection ()
46-
47- filing_df = downloader .query_filing_by_id (conn , filing_id )
48-
49- if filing_df .is_empty ():
50- context .log .warning (f"No filing found for filing_id={ filing_id } " )
51- return dg .MaterializeResult (
52- metadata = {
53- "status" : "not_found" ,
54- "filing_id" : filing_id ,
55- "documents_downloaded" : 0 ,
56- }
57- )
41+ downloader = FilingDownloader (sec_edgar , gcs , bq , context .log )
42+
43+ filing_df = downloader .query_filing_by_id (filing_id )
44+
45+ if filing_df .is_empty ():
46+ context .log .warning (f"No filing found for filing_id={ filing_id } " )
47+ return dg .MaterializeResult (
48+ metadata = {
49+ "status" : "not_found" ,
50+ "filing_id" : filing_id ,
51+ "documents_downloaded" : 0 ,
52+ }
53+ )
5854
59- # Process all rows for this filing_id (may span multiple symbols
60- # if a CIK/accession-derived ID maps to more than one symbol)
61- rows = filing_df .to_dicts ()
62- total_downloaded = 0
63- last_result = None
64-
65- for row in rows :
66- if downloader .is_filing_processed (conn , filing_id , row ["symbol" ]):
67- context .log .debug (
68- f"Filing { filing_id } already processed for { row ['symbol' ]} "
69- )
70- continue
71-
72- last_result = downloader .download_filing (conn , row )
73- if last_result .status == "downloaded" :
74- total_downloaded += 1
75-
76- if last_result is None :
77- return dg .MaterializeResult (
78- metadata = {
79- "status" : "already_processed" ,
80- "filing_id" : filing_id ,
81- "symbol" : rows [0 ]["symbol" ],
82- "documents_downloaded" : 0 ,
83- }
55+ # Process all rows for this filing_id (may span multiple symbols
56+ # if a CIK/accession-derived ID maps to more than one symbol)
57+ rows = filing_df .to_dicts ()
58+ total_downloaded = 0
59+ last_result = None
60+
61+ for row in rows :
62+ if downloader .is_filing_processed (filing_id , row ["symbol" ]):
63+ context .log .debug (
64+ f"Filing { filing_id } already processed for { row ['symbol' ]} "
8465 )
66+ continue
67+
68+ last_result = downloader .download_filing (row )
69+ if last_result .status == "downloaded" :
70+ total_downloaded += 1
8571
72+ if last_result is None :
8673 return dg .MaterializeResult (
8774 metadata = {
88- "status" : last_result .status ,
89- "filing_id" : last_result .filing_id ,
90- "symbol" : last_result .symbol ,
91- "gcs_path" : last_result .gcs_path ,
92- "documents_downloaded" : total_downloaded ,
93- "error" : last_result .error ,
75+ "status" : "already_processed" ,
76+ "filing_id" : filing_id ,
77+ "symbol" : rows [0 ]["symbol" ],
78+ "documents_downloaded" : 0 ,
9479 }
9580 )
9681
97- finally :
98- if conn :
99- conn .close ()
82+ return dg .MaterializeResult (
83+ metadata = {
84+ "status" : last_result .status ,
85+ "filing_id" : last_result .filing_id ,
86+ "symbol" : last_result .symbol ,
87+ "gcs_path" : last_result .gcs_path ,
88+ "documents_downloaded" : total_downloaded ,
89+ "error" : last_result .error ,
90+ }
91+ )
10092
10193
10294@dg .asset (
@@ -117,38 +109,28 @@ def sec_filing_documents_batch(
117109 Queries sec_filings for up to 25 unprocessed filings across all companies,
118110 downloads each primary document to GCS, and marks them processed.
119111 """
120- downloader = FilingDownloader (sec_edgar , gcs , context .log )
121-
122- conn = None
123- try :
124- conn = bq .get_connection ()
112+ downloader = FilingDownloader (sec_edgar , gcs , bq , context .log )
125113
126- filings_to_process = downloader .query_unprocessed_filings (conn )
127-
128- if filings_to_process .is_empty ():
129- context .log .debug ("No unprocessed filings to download" )
130- return dg .MaterializeResult (
131- metadata = {"status" : "no_filings" , "documents_downloaded" : 0 }
132- )
133-
134- context .log .info (f"Downloading documents for { len (filings_to_process )} filings" )
135-
136- result = downloader .download_batch (conn , filings_to_process )
114+ filings_to_process = downloader .query_unprocessed_filings ()
137115
116+ if filings_to_process .is_empty ():
117+ context .log .debug ("No unprocessed filings to download" )
138118 return dg .MaterializeResult (
139- metadata = {
140- "status" : "completed" ,
141- "documents_downloaded" : result .total_downloaded ,
142- "errors" : result .total_errors ,
143- "remaining_to_process" : result .remaining ,
144- "error_details" : (
145- result .error_details [:MAX_ERROR_DETAILS ]
146- if result .error_details
147- else []
148- ),
149- }
119+ metadata = {"status" : "no_filings" , "documents_downloaded" : 0 }
150120 )
151121
152- finally :
153- if conn :
154- conn .close ()
122+ context .log .info (f"Downloading documents for { len (filings_to_process )} filings" )
123+
124+ result = downloader .download_batch (filings_to_process )
125+
126+ return dg .MaterializeResult (
127+ metadata = {
128+ "status" : "completed" ,
129+ "documents_downloaded" : result .total_downloaded ,
130+ "errors" : result .total_errors ,
131+ "remaining_to_process" : result .remaining ,
132+ "error_details" : (
133+ result .error_details [:MAX_ERROR_DETAILS ] if result .error_details else []
134+ ),
135+ }
136+ )
0 commit comments