-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
159 lines (137 loc) · 5.22 KB
/
queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import pandas as pd
from databricks import sql
SERVER_HOSTNAME = os.getenv("SERVER_HOSTNAME")
HTTP_PATH = os.getenv("HTTP_PATH")
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
PUBLIC_ONLY = os.getenv("PUBLIC_ONLY", "False").lower() in ("true", "1", "yes")
class QueryService:
_instance = None
@staticmethod
def get_instance():
if QueryService._instance is None:
QueryService._instance = QueryService()
return QueryService._instance
def __init__(self):
self.country_whitelist = None
if PUBLIC_ONLY:
query = """
SELECT country_name
FROM boost.data_availability
WHERE boost_public = 'Yes'
"""
self.country_whitelist = self.execute_query(query)["country_name"].tolist()
def execute_query(self, query):
"""
Executes a query and returns the result as a pandas DataFrame.
"""
conn = sql.connect(
server_hostname=SERVER_HOSTNAME,
http_path=HTTP_PATH,
access_token=ACCESS_TOKEN,
)
cursor = conn.cursor()
cursor.execute(query)
df = cursor.fetchall_arrow().to_pandas()
cursor.close()
conn.close()
return df
def fetch_data(self, query):
df = self.execute_query(query)
return self._apply_country_whitelist_filter(df)
def _apply_country_whitelist_filter(self, df):
if self.country_whitelist is not None and 'country_name' in df.columns:
return df[df['country_name'].isin(self.country_whitelist)]
return df
def get_expenditure_w_poverty_by_country_year(self):
query = """
SELECT *
FROM boost.pov_expenditure_by_country_year
"""
df = self.fetch_data(query)
df.loc[:, "decentralized_expenditure"] = df["decentralized_expenditure"].fillna(0)
return df
def get_edu_private_expenditure(self):
query = """
SELECT country_name, year, real_expenditure
FROM boost.edu_private_expenditure_by_country_year
"""
return self.fetch_data(query)
def get_hd_index(self, countries):
country_list = "', '".join(countries)
query = f"""
SELECT * FROM indicator.global_data_lab_hd_index
WHERE country_name IN ('{country_list}')
ORDER BY country_name, year
"""
return self.fetch_data(query)
def get_learning_poverty_rate(self):
query = """
SELECT * FROM indicator.learning_poverty_rate
"""
return self.fetch_data(query)
def get_expenditure_by_country_func_econ_year(self):
query = """
SELECT * FROM boost.expenditure_by_country_func_econ_year
"""
return self.fetch_data(query)
def get_expenditure_by_country_sub_func_year(self):
query = """
SELECT country_name, admin0, year, func, latest_year, func_sub, expenditure, real_expenditure
FROM boost.expenditure_by_country_admin0_func_sub_year
"""
return self.fetch_data(query)
def get_basic_country_data(self, countries):
country_list = "', '".join(countries)
query = f"""
SELECT country_name, display_lon, display_lat, zoom, income_level
FROM indicator.country
WHERE country_name IN ('{country_list}')
"""
return self.fetch_data(query)
def get_expenditure_by_country_geo1_year(self):
query = """
SELECT country_name, year, adm1_name, expenditure, per_capita_expenditure
FROM boost.expenditure_by_country_geo1_year
"""
return self.fetch_data(query)
def get_adm_boundaries(self, countries):
country_list = "', '".join(countries)
query = f"""
SELECT country_name, admin1_region, boundary
FROM indicator.admin1_boundaries_gold
WHERE country_name IN ('{country_list}')
ORDER BY country_name
"""
return self.fetch_data(query)
def get_subnational_poverty_index(self, countries):
country_list = "', '".join(countries)
query = f"""
SELECT * FROM indicator.subnational_poverty_index
WHERE country_name IN ('{country_list}')
"""
return self.fetch_data(query)
def get_universal_health_coverage_index(self):
query = """
SELECT * FROM indicator.universal_health_coverage_index_gho
"""
return self.fetch_data(query)
def get_health_private_expenditure(self):
query = """
SELECT country_name, year, real_expenditure
FROM boost.health_private_expenditure_by_country_year
"""
return self.fetch_data(query)
def expenditure_and_outcome_by_country_geo1_func_year(self):
query = """
SELECT * FROM boost.expenditure_and_outcome_by_country_geo1_func_year
"""
return self.fetch_data(query)
def get_pefa(self, countries):
country_list = "', '".join(countries)
query = f"""
SELECT * FROM indicator.pefa_by_pillar
WHERE country_name IN ('{country_list}')
ORDER BY country_name, year
"""
return self.fetch_data(query)