@@ -214,13 +214,21 @@ def titles_title_i_summary_search():
214
214
min_year , max_year = cfg .TITLE_I_START_YEAR , cfg .TITLE_I_END_YEAR
215
215
start_year = request .args .get ('start_year' , type = int , default = min_year )
216
216
end_year = request .args .get ('end_year' , type = int , default = max_year )
217
- title_id = 100
217
+
218
+ title_id = get_title_id (TITLE_I_NAME )
219
+ if title_id is None :
220
+ msg = {
221
+ "reason" : "No record for the given title name " + TITLE_I_NAME ,
222
+ "error" : "Not found: " + request .url ,
223
+ }
224
+ logging .error ("Title I: " + json .dumps (msg ))
225
+ return rs_handlers .not_found (msg )
218
226
219
227
if start_year and end_year and start_year > end_year :
220
228
start_year , end_year = min_year , max_year # Return all data if invalid range
221
229
222
230
if start_year is None :
223
- start_year = min_year # Default to earliest available year
231
+ start_year = min_year # Default to the earliest available year
224
232
225
233
if end_year is None :
226
234
end_year = max_year # Default to latest available year
@@ -234,13 +242,21 @@ def titles_title_i_state_distribution_search():
234
242
min_year , max_year = cfg .TITLE_I_START_YEAR , cfg .TITLE_I_END_YEAR
235
243
start_year = request .args .get ('start_year' , type = int , default = min_year )
236
244
end_year = request .args .get ('end_year' , type = int , default = max_year )
237
- title_id = 100
245
+
246
+ title_id = get_title_id (TITLE_I_NAME )
247
+ if title_id is None :
248
+ msg = {
249
+ "reason" : "No record for the given title name " + TITLE_I_NAME ,
250
+ "error" : "Not found: " + request .url ,
251
+ }
252
+ logging .error ("Title I: " + json .dumps (msg ))
253
+ return rs_handlers .not_found (msg )
238
254
239
255
if start_year and end_year and start_year > end_year :
240
256
start_year , end_year = min_year , max_year # Reset to full range if invalid
241
257
242
258
if start_year is None :
243
- start_year = min_year # Default to earliest available year
259
+ start_year = min_year # Default to the earliest available year
244
260
245
261
if end_year is None :
246
262
end_year = max_year # Default to latest available year
@@ -283,7 +299,7 @@ def titles_title_i_subtitles_subtitle_a_state_distribution_search():
283
299
start_year , end_year = min_year , max_year # Reset to full range if invalid
284
300
285
301
if start_year is None :
286
- start_year = min_year # Default to earliest available year
302
+ start_year = min_year # Default to the earliest available year
287
303
288
304
if end_year is None :
289
305
end_year = max_year # Default to latest available year
@@ -311,7 +327,7 @@ def titles_title_i_subtitles_subtitle_a_summary_search():
311
327
start_year , end_year = min_year , max_year # Reset to full range if invalid
312
328
313
329
if start_year is None :
314
- start_year = min_year # Default to earliest available year
330
+ start_year = min_year # Default to the earliest available year
315
331
316
332
if end_year is None :
317
333
end_year = max_year # Default to latest available year
@@ -339,7 +355,7 @@ def titles_title_i_subtitles_subtitle_d_state_distribution_search():
339
355
start_year , end_year = min_year , max_year # Reset to full range if invalid
340
356
341
357
if start_year is None :
342
- start_year = min_year # Default to earliest available year
358
+ start_year = min_year # Default to the earliest available year
343
359
344
360
if end_year is None :
345
361
end_year = max_year # Default to latest available year
@@ -367,7 +383,7 @@ def titles_title_i_subtitles_subtitle_d_summary_search():
367
383
start_year , end_year = min_year , max_year # Reset to full range if invalid
368
384
369
385
if start_year is None :
370
- start_year = min_year # Default to earliest available year
386
+ start_year = min_year # Default to the earliest available year
371
387
372
388
if end_year is None :
373
389
end_year = max_year # Default to latest available year
@@ -395,7 +411,7 @@ def titles_title_i_subtitles_subtitle_e_state_distribution_search():
395
411
start_year , end_year = min_year , max_year # Reset to full range if invalid
396
412
397
413
if start_year is None :
398
- start_year = min_year # Default to earliest available year
414
+ start_year = min_year # Default to the earliest available year
399
415
400
416
if end_year is None :
401
417
end_year = max_year # Default to latest available year
@@ -423,7 +439,7 @@ def titles_title_i_subtitles_subtitle_e_summary_search():
423
439
start_year , end_year = min_year , max_year # Reset to full range if invalid
424
440
425
441
if start_year is None :
426
- start_year = min_year # Default to earliest available year
442
+ start_year = min_year # Default to the earliest available year
427
443
428
444
if end_year is None :
429
445
end_year = max_year # Default to latest available year
@@ -1441,7 +1457,7 @@ def generate_title_iv_state_distribution_response(program_id, start_year, end_ye
1441
1457
Program .name .label ('programName' ),
1442
1458
Payment .year .label ('year' ),
1443
1459
Payment .payment .label ('totalPaymentInDollars' ),
1444
- Payment .recipient_count .label ('totalCounts ' )
1460
+ Payment .recipient_count .label ('totalRecipientCount ' )
1445
1461
).join (
1446
1462
Program , Payment .program_id == Program .id
1447
1463
).filter (
@@ -1575,7 +1591,7 @@ def generate_title_i_total_state_distribution_response(title_id, start_year, end
1575
1591
Subtitle .name .label ('subtitleName' ),
1576
1592
Payment .year .label ('year' ),
1577
1593
Payment .payment .label ('totalPaymentInDollars' ),
1578
- Payment .recipient_count .label ('totalCounts ' )).join (
1594
+ Payment .recipient_count .label ('totalRecipientCount ' )).join (
1579
1595
Subtitle , Payment .subtitle_id == Subtitle .id ).filter (
1580
1596
Payment .title_id == title_id ,
1581
1597
Payment .year .between (start_year , end_year )
@@ -1586,21 +1602,21 @@ def generate_title_i_total_state_distribution_response(title_id, start_year, end
1586
1602
1587
1603
# create a nested dictionary to store data by year and state
1588
1604
data_by_year_and_state = defaultdict (
1589
- lambda : defaultdict (lambda : {'totalPaymentInDollars' : 0 , 'totalRecipients ' : 0 }))
1590
- all_years_summary = defaultdict (lambda : {'totalPaymentInDollars' : 0 , 'totalRecipients ' : 0 })
1605
+ lambda : defaultdict (lambda : {'totalPaymentInDollars' : 0 , 'totalRecipientCount ' : 0 }))
1606
+ all_years_summary = defaultdict (lambda : {'totalPaymentInDollars' : 0 , 'totalRecipientCount ' : 0 })
1591
1607
1592
1608
for record in result :
1593
1609
state , title_name , year , payments , recipients = record
1594
1610
entry = data_by_year_and_state [year ][state ]
1595
1611
entry ['state' ] = state
1596
1612
entry ['totalPaymentInDollars' ] += payments
1597
- entry ['totalRecipients ' ] += recipients
1613
+ entry ['totalRecipientCount ' ] += recipients
1598
1614
1599
1615
# add to all years summary
1600
1616
summary = all_years_summary [state ]
1601
1617
summary ['state' ] = state
1602
1618
summary ['totalPaymentInDollars' ] += payments
1603
- summary ['totalRecipients ' ] += recipients
1619
+ summary ['totalRecipientCount ' ] += recipients
1604
1620
1605
1621
# sort by total payment
1606
1622
sorted_data_by_year = {}
@@ -1701,7 +1717,7 @@ def generate_title_i_total_summary_response(title_id, start_year, end_year):
1701
1717
Title .name .label ('titleName' ),
1702
1718
Payment .year .label ('year' ),
1703
1719
Payment .payment .label ('totalPaymentInDollars' ),
1704
- Payment .recipient_count .label ('totalCounts ' ),
1720
+ Payment .recipient_count .label ('totalRecipientCount ' ),
1705
1721
Subtitle .name .label ('subtitleName' )
1706
1722
).join (
1707
1723
Title , Payment .title_id == Title .id
@@ -1718,25 +1734,25 @@ def generate_title_i_total_summary_response(title_id, start_year, end_year):
1718
1734
# Initialize dictionaries to store aggregated data
1719
1735
title_summary = defaultdict (lambda : {
1720
1736
'totalPaymentInDollars' : 0 ,
1721
- 'totalCounts ' : 0 ,
1737
+ 'totalRecipientCount ' : 0 ,
1722
1738
'recipients' : [],
1723
1739
'subtitles' : defaultdict (lambda : {
1724
1740
'totalPaymentInDollars' : 0 ,
1725
- 'totalCounts ' : 0 ,
1741
+ 'totalRecipientCount ' : 0 ,
1726
1742
'recipients' : []
1727
1743
})
1728
1744
})
1729
1745
1730
1746
# Process each record in the data
1731
1747
for state , title_name , year , payment , recipients , subtitle in results :
1732
1748
title_summary [title_name ]['totalPaymentInDollars' ] += payment
1733
- title_summary [title_name ]['totalCounts ' ] += recipients
1749
+ title_summary [title_name ]['totalRecipientCount ' ] += recipients
1734
1750
title_summary [title_name ]['recipients' ].append (recipients )
1735
1751
1736
1752
# Aggregate subtitle data
1737
1753
subtitle_dict = title_summary [title_name ]['subtitles' ][subtitle ]
1738
1754
subtitle_dict ['totalPaymentInDollars' ] += payment
1739
- subtitle_dict ['totalCounts ' ] += recipients
1755
+ subtitle_dict ['totalRecipientCount ' ] += recipients
1740
1756
subtitle_dict ['recipients' ].append (recipients )
1741
1757
1742
1758
# Prepare the final summary
@@ -1754,16 +1770,18 @@ def generate_title_i_total_summary_response(title_id, start_year, end_year):
1754
1770
subtitle_list .append ({
1755
1771
'programName' : subtitle_name ,
1756
1772
'totalPaymentInDollars' : round (subtitle_info ['totalPaymentInDollars' ], 2 ),
1757
- 'totalCounts ' : subtitle_info ['totalCounts ' ],
1773
+ 'totalRecipientCount ' : subtitle_info ['totalRecipientCount ' ],
1758
1774
'averageRecipientCount' : subtitle_avg_recipients ,
1759
1775
'totalPaymentInPercentage' : round (payment_percentage , 2 )
1760
1776
})
1761
1777
1762
1778
title_entry = {
1763
1779
'titleName' : title ,
1764
1780
'totalPaymentInDollars' : round (info ['totalPaymentInDollars' ], 2 ),
1765
- 'totalCounts ' : info ['totalCounts ' ],
1781
+ 'totalRecipientCount ' : info ['totalRecipientCount ' ],
1766
1782
'averageRecipientCount' : round (average_recipient_count , 2 ),
1783
+ 'startYear' : start_year ,
1784
+ 'endYear' : end_year ,
1767
1785
'subtitles' : subtitle_list
1768
1786
}
1769
1787
final_summary .append (title_entry )
@@ -1847,7 +1865,7 @@ def generate_title_i_state_distribution_response(subtitle_id, start_year, end_ye
1847
1865
func .sum (Payment .payment ).label ('totalPaymentInDollars' ),
1848
1866
func .round (func .avg (Payment .base_acres ), 2 ).label ('averageAreaInAcres' ),
1849
1867
func .cast (func .avg (Payment .recipient_count ), BigInteger ).label ('averageRecipientCount' ),
1850
- func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalCounts ' ),
1868
+ func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalRecipientCount ' ),
1851
1869
(func .cast (func .sum (Payment .recipient_count ) / subtitle_subquery_recipient_count * 100 , Numeric (5 , 2 ))).label (
1852
1870
'averageRecipientCountInPercentageNationwide' )
1853
1871
).join (
@@ -1872,7 +1890,7 @@ def generate_title_i_state_distribution_response(subtitle_id, start_year, end_ye
1872
1890
response_dict = dict (zip (column_names , row ))
1873
1891
1874
1892
# Cleanup / renaming attributes
1875
- response_dict ["totalCountsInPercentageNationwide " ] = response_dict ["averageRecipientCountInPercentageNationwide" ]
1893
+ response_dict ["totalRecipientCountInPercentageNationwide " ] = response_dict ["averageRecipientCountInPercentageNationwide" ]
1876
1894
if response_dict ['averageAreaInAcres' ] is None :
1877
1895
response_dict ['averageAreaInAcres' ] = 0.0
1878
1896
subtitle_response_dict [response_dict ['state' ]] = response_dict
@@ -1908,7 +1926,7 @@ def generate_title_i_state_distribution_response(subtitle_id, start_year, end_ye
1908
1926
Payment .state_code .label ('state' ),
1909
1927
Program .name .label ('programName' ),
1910
1928
func .sum (Payment .payment ).label ('totalPaymentInDollars' ),
1911
- func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalCounts ' ),
1929
+ func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalRecipientCount ' ),
1912
1930
func .round (func .avg (Payment .base_acres ), 2 ).label ('averageAreaInAcres' ),
1913
1931
func .cast (func .sum (Payment .recipient_count ) / total_years , BigInteger ).label ('averageRecipientCount' ),
1914
1932
(func .cast (func .sum (Payment .payment ) / program_subquery_total_payment * 100 , Numeric (5 , 2 ))).label (
@@ -1938,7 +1956,7 @@ def generate_title_i_state_distribution_response(subtitle_id, start_year, end_ye
1938
1956
state = response_dict ['state' ]
1939
1957
1940
1958
# Cleanup / renaming attributes
1941
- response_dict ["totalCountsInPercentageNationwide " ] = response_dict [
1959
+ response_dict ["totalRecipientCountInPercentageNationwide " ] = response_dict [
1942
1960
"averageRecipientCountInPercentageNationwide" ]
1943
1961
response_dict ['subPrograms' ] = []
1944
1962
if response_dict ['averageAreaInAcres' ] is None :
@@ -2020,14 +2038,14 @@ def generate_title_i_state_distribution_response(subtitle_id, start_year, end_ye
2020
2038
else :
2021
2039
program ["totalPaymentInPercentageWithinState" ] = 0.0
2022
2040
2023
- if subtitle_response_dict [state ]["totalCounts " ] != 0 :
2024
- program ["totalCountsInPercentageWithinState " ] = (
2025
- round (program ["totalCounts " ] / subtitle_response_dict [state ]["totalCounts " ] * 100 , 2 ))
2041
+ if subtitle_response_dict [state ]["totalRecipientCount " ] != 0 :
2042
+ program ["totalRecipientCountInPercentageWithinState " ] = (
2043
+ round (program ["totalRecipientCount " ] / subtitle_response_dict [state ]["totalRecipientCount " ] * 100 , 2 ))
2026
2044
# TODO: Temporary fix. The below attribute may need to be calculated based on the average recipient count or removed if not needed.
2027
- program ["averageRecipientCountInPercentageWithinState" ] = program ["totalCountsInPercentageWithinState " ]
2045
+ program ["averageRecipientCountInPercentageWithinState" ] = program ["totalRecipientCountInPercentageWithinState " ]
2028
2046
else :
2029
2047
program ["averageRecipientCountInPercentageWithinState" ] = 0.0
2030
- program ["totalCountsInPercentageWithinState " ] = 0.0
2048
+ program ["totalRecipientCountInPercentageWithinState " ] = 0.0
2031
2049
2032
2050
for subprogram in program ["subPrograms" ]:
2033
2051
if subtitle_response_dict [state ]["totalPaymentInDollars" ] != 0.0 :
@@ -2060,7 +2078,7 @@ def generate_title_i_summary_response(subtitle_id, start_year, end_year):
2060
2078
subtitle_query = (session .query (
2061
2079
Subtitle .name .label ('subtitleName' ),
2062
2080
func .sum (Payment .payment ).label ('totalPaymentInDollars' ),
2063
- func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalCounts ' ),
2081
+ func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalRecipientCount ' ),
2064
2082
func .cast (subtitle_avg_recipient_count , BigInteger ).label ('averageRecipientCount' )
2065
2083
).join (
2066
2084
Subtitle , Payment .subtitle_id == Subtitle .id
@@ -2080,6 +2098,8 @@ def generate_title_i_summary_response(subtitle_id, start_year, end_year):
2080
2098
subtitle_response_dict = dict ()
2081
2099
for row in subtitle_result :
2082
2100
subtitle_response_dict = dict (zip (column_names , row ))
2101
+ subtitle_response_dict ["startYear" ] = start_year
2102
+ subtitle_response_dict ["endYear" ] = end_year
2083
2103
subtitle_response_dict ["programs" ] = []
2084
2104
2085
2105
# Find all programs under the subtitle
@@ -2111,7 +2131,7 @@ def generate_title_i_summary_response(subtitle_id, start_year, end_year):
2111
2131
program_query = (session .query (
2112
2132
Program .name .label ('programName' ),
2113
2133
func .sum (Payment .payment ).label ('totalPaymentInDollars' ),
2114
- func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalCounts ' ),
2134
+ func .cast (func .sum (Payment .recipient_count ), Integer ).label ('totalRecipientCount ' ),
2115
2135
func .cast (program_avg_recipient_count , BigInteger ).label ('averageRecipientCount' ),
2116
2136
(func .cast (func .sum (Payment .payment ) / subtitle_subquery * 100 , Numeric (5 , 2 ))).label (
2117
2137
'totalPaymentInPercentage' )
0 commit comments