Skip to content

Commit 08bb571

Browse files
committed
.
1 parent ad3da35 commit 08bb571

7 files changed

Lines changed: 240 additions & 115 deletions

File tree

devtracker.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require 'csv'
2222
require "sinatra/cookies"
2323
require "cgi"
24+
require 'set'
2425

2526
#helpers path
2627
require_relative 'helpers/formatters.rb'
@@ -59,12 +60,12 @@
5960
# Developer Machine: set global settings
6061
#set :oipa_api_url, 'https://fcdo-direct-indexing.iati.cloud/search/'#'https://fcdo.iati.cloud/search/'#'https://fcdo-direct-indexing.iati.cloud/search/'#'https://devtracker.fcdo.gov.uk/api/'
6162
# set :oipa_api_url, 'https://devtracker-entry.oipa.nl/api/'
62-
# set :oipa_api_url, 'https://fcdo.iati.cloud/search/'
63+
set :oipa_api_url, 'https://fcdo.iati.cloud/search/'
6364
# set :oipa_api_url, 'https://fcdo-direct-indexing.iati.cloud/search/'
64-
#set :bind, '0.0.0.0' # Allows for vagrant pass-through whilst debugging
65+
set :bind, '0.0.0.0' # Allows for vagrant pass-through whilst debugging
6566

6667
# Server Machine: set global settings to use varnish cache
67-
set :oipa_api_url, 'http://127.0.0.1:6081/search/'
68+
#set :oipa_api_url, 'http://127.0.0.1:6081/search/'
6869
set :prod_api_url, 'https://fcdo.iati.cloud'
6970
set :dev_api_url, 'https://fcdo-staging.iati.cloud'
7071

@@ -80,8 +81,8 @@
8081

8182
set :current_first_day_of_financial_year, first_day_of_financial_year(DateTime.now)
8283
set :current_last_day_of_financial_year, last_day_of_financial_year(DateTime.now)
83-
set :goverment_department_ids, 'GB-GOV-25,GB-GOV-26,GB-GOV-15,GB-GOV-9,GB-GOV-6,GB-GOV-2,GB-GOV-1,GB-1,GB-GOV-3,GB-GOV-13,GB-GOV-7,GB-6,GB-10,GB-GOV-10,GB-9,GB-GOV-8,GB-GOV-5,GB-GOV-12,GB-COH-RC000346,GB-COH-03877777,GB-GOV-24'
84-
#set :goverment_department_ids, 'GB-GOV-1,GB-1'
84+
#set :goverment_department_ids, 'GB-GOV-25,GB-GOV-26,GB-GOV-15,GB-GOV-9,GB-GOV-6,GB-GOV-2,GB-GOV-1,GB-1,GB-GOV-3,GB-GOV-13,GB-GOV-7,GB-6,GB-10,GB-GOV-10,GB-9,GB-GOV-8,GB-GOV-5,GB-GOV-12,GB-COH-RC000346,GB-COH-03877777,GB-GOV-24'
85+
set :goverment_department_ids, 'GB-GOV-1,GB-1'
8586
set :google_recaptcha_publicKey, ENV["GOOGLE_PUBLIC_KEY"]
8687
set :google_recaptcha_privateKey, ENV["GOOGLE_PRIVATE_KEY"]
8788

@@ -361,7 +362,7 @@ def getCacheData(fileName)
361362
:locals => {
362363
oipa_api_url: settings.oipa_api_url,
363364
region: region,
364-
regionYearWiseBudgets: get_country_region_yearwise_budget_graph_datav2(RestClient.get api_simple_log(settings.oipa_api_url + 'activity/?q=recipient_region_code:'+n+' AND reporting_org_ref:('+settings.goverment_department_ids.gsub(","," OR ")+') &fl=budget_value,default-currency,budget_period_start_iso_date,budget_period_end_iso_date,budget.period-start.quarter,budget.period-end.quarter')),
365+
regionYearWiseBudgets: get_country_region_yearwise_budget_graph_datav2(RestClient.get api_simple_log(settings.oipa_api_url + 'activity/?q=recipient_region_code:'+n+' AND reporting_org_ref:('+settings.goverment_department_ids.gsub(","," OR ")+') &fl=budget_value_gbp,budget_value,default-currency,budget_period_start_iso_date,budget_period_end_iso_date,budget.period-start.quarter,budget.period-end.quarter&rows=10000')),
365366
mapMarkers: getRegionMapMarkersv2(region[:code]),
366367
}
367368
end
@@ -803,7 +804,7 @@ def getCacheData(fileName)
803804
country_sector_data = ''
804805
getMaxBudget = ''
805806
if (!canLoadFromCache('country_sector_data'))
806-
storeCacheData(generateCountryDatav5(), 'country_sector_data')
807+
storeCacheData(generateCountryDatav6(), 'country_sector_data')
807808
getMainData = getCacheData('country_sector_data')
808809
map_data = getMainData['map_data']
809810
storeCacheData(map_data[1].sort_by{|k,val| val['budget']}.reverse!.first[1]['budget'], 'getMaxBudgetCountryLocation')

helpers/common_helpers.rb

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ def get_actual_budget_per_fyv2(yearWiseBudgets)
141141
t = Time.parse(data)
142142
fy = if project['budget.period-start.quarter'][index].to_i == 1 then t.year - 1 else t.year end
143143
if hash.has_key?(fy)
144-
hash[fy] = hash[fy] + project['budget_value'][index]
144+
hash[fy] = hash[fy] + project['budget_value_gbp'][index]
145145
else
146-
hash[fy] = project['budget_value'][index]
146+
hash[fy] = project['budget_value_gbp'][index]
147147
end
148148
end
149149
end
@@ -311,9 +311,9 @@ def last_day_of_financial_year(date_value)
311311

312312
def convert_numbers_to_human_readable_format(num)
313313
begin
314-
number_to_human(num.gsub(",",""), :format => '%n%u', :precision => 3, :units => { :thousand => 'K', :million => 'M', :billion => 'B' })
314+
number_to_human(num.gsub(",",""), :format => '%n%u', :precision => 4, :units => { :thousand => 'K', :million => 'M', :billion => 'B' })
315315
rescue
316-
number_to_human(num, :format => '%n%u', :precision => 3, :units => { :thousand => 'K', :million => 'M', :billion => 'B' })
316+
number_to_human(num, :format => '%n%u', :precision => 4, :units => { :thousand => 'K', :million => 'M', :billion => 'B' })
317317
end
318318
end
319319

@@ -1265,6 +1265,109 @@ def generateCountryDatav4()
12651265
output
12661266
end
12671267

1268+
def find_string_index(string_array, target_string)
1269+
index = string_array.index(target_string)
1270+
if index
1271+
return index
1272+
else
1273+
return -1
1274+
end
1275+
end
1276+
###############################
1277+
def generateCountryDatav6()
1278+
count = 20
1279+
activityTracker = Set.new
1280+
newApiCall = settings.oipa_api_url + "activity?q=budget_period_start_iso_date:[#{settings.current_first_day_of_financial_year}T00:00:00Z TO *] AND budget_period_end_iso_date:[* TO #{settings.current_last_day_of_financial_year}T00:00:00Z] AND budget_value_gbp:* AND participating_org_ref:(GB-GOV-* OR GB-COH-*) AND reporting_org_ref:(#{settings.goverment_department_ids.gsub(","," OR ")}) AND recipient_country_code:*&fl=reporting_org_ref,recipient_country_percentage,activity_status_code,iati_identifier,budget.period-start.quarter,budget.period-end.quarter,recipient_country_code,budget_period_start_iso_date,budget_period_end_iso_date,budget_value_gbp,recipient_country_name,hierarchy,related_activity_type,related_activity_ref&start=0&rows=#{count}"
1281+
##pagination stuff
1282+
page = 1
1283+
page = page.to_i - 1
1284+
finalPage = page * count
1285+
######
1286+
pd = RestClient.get newApiCall
1287+
pd = JSON.parse(pd)
1288+
numOActivities = pd['response']['numFound'].to_i
1289+
pulledData = pd['response']['docs']
1290+
if (numOActivities > count)
1291+
pages = (numOActivities.to_f/count).ceil
1292+
for p in 2..pages do
1293+
p = p - 1
1294+
finalPage = p * count
1295+
tempData = JSON.parse(RestClient.get settings.oipa_api_url + "activity?q=budget_period_start_iso_date:[#{settings.current_first_day_of_financial_year}T00:00:00Z TO *] AND budget_period_end_iso_date:[* TO #{settings.current_last_day_of_financial_year}T00:00:00Z] AND budget_value_gbp:* AND participating_org_ref:(GB-GOV-* OR GB-COH-*) AND reporting_org_ref:(#{settings.goverment_department_ids.gsub(","," OR ")}) AND recipient_country_code:*&fl=reporting_org_ref,recipient_country_percentage,activity_status_code,iati_identifier,budget.period-start.quarter,budget.period-end.quarter,recipient_country_code,budget_period_start_iso_date,budget_period_end_iso_date,budget_value_gbp,recipient_country_name,hierarchy,related_activity_type,related_activity_ref&start=#{finalPage}&rows=#{count}")
1296+
tempData = tempData['response']['docs']
1297+
tempData.each do |item|
1298+
pulledData.push(item)
1299+
end
1300+
end
1301+
end
1302+
puts ('Activity count: ' + pulledData.count.to_s)
1303+
projectDataHash = {}
1304+
##
1305+
fcdoCountryProjectTracker = {}
1306+
countryDataHash = {}
1307+
##
1308+
pulledData.each do |element|
1309+
######New 2.0 version starts here#####
1310+
tempTotalBudget = 0
1311+
element['budget_value_gbp'].each_with_index do |data, index|
1312+
if(element['budget_period_start_iso_date'][index].to_datetime >= settings.current_first_day_of_financial_year && element['budget_period_end_iso_date'][index].to_datetime <= settings.current_last_day_of_financial_year)
1313+
tempTotalBudget = tempTotalBudget + data.to_f
1314+
end
1315+
end
1316+
## Process project budget and count now
1317+
# Get the parent identifier for this activity
1318+
if element["hierarchy"].to_i != 1
1319+
parentProgrammeID = element['related_activity_ref'][find_string_index(element['related_activity_type'],"1")]
1320+
else
1321+
parentProgrammeID = element['iati_identifier']
1322+
end
1323+
if activityTracker.include?(parentProgrammeID)
1324+
isNewProgramme = false
1325+
else
1326+
isNewProgramme = true
1327+
activityTracker.add(parentProgrammeID)
1328+
end
1329+
element['recipient_country_code'].each_with_index do |c, i|
1330+
if element.has_key?('recipient_country_percentage')
1331+
countryPercentage = element['recipient_country_percentage'][i].to_f
1332+
else
1333+
countryPercentage = 100
1334+
end
1335+
countryBudget = tempTotalBudget*countryPercentage/100
1336+
budgetTracker = budgetTracker - countryBudget
1337+
if(projectDataHash.has_key?(c))
1338+
if(isNewProgramme)
1339+
projectDataHash[c]["projects"] = projectDataHash[c]["projects"] + 1
1340+
end
1341+
projectDataHash[c]["budget"] = (projectDataHash[c]["budget"] + countryBudget).round(2)
1342+
else
1343+
projectDataHash[c] = {}
1344+
if c =='FK'
1345+
projectDataHash[c]["country"] = 'Falkland Islands'
1346+
elsif c =='PS'
1347+
projectDataHash[c]["country"] = 'Occupied Palestinian Territories (OPT)'
1348+
else
1349+
projectDataHash[c]["country"] = begin get_country_code_name(c)['name'] rescue 'N/A' end# element.has_key?('recipient_country_name') ? element["recipient_country_name"][i] : 'N/A'
1350+
end
1351+
projectDataHash[c]["id"] = c
1352+
if(isNewProgramme)
1353+
projectDataHash[c]["projects"] = 1
1354+
else
1355+
projectDataHash[c]["projects"] = 0
1356+
end
1357+
projectDataHash[c]["budget"] = countryBudget.round(2)
1358+
projectDataHash[c]["flag"] = '/images/flags/' + c.downcase + '.png'
1359+
end
1360+
end
1361+
end
1362+
finalOutput = Array.new
1363+
finalOutput.push(projectDataHash.to_s.gsub("[", "").gsub("]", "").gsub("=>",":").gsub("}}, {","},"))
1364+
finalOutput.push(projectDataHash)
1365+
output = {}
1366+
output['map_data'] = finalOutput
1367+
output
1368+
end
1369+
###############################
1370+
12681371
###############################
12691372
def generateCountryDatav5()
12701373
count = 20

0 commit comments

Comments
 (0)