Skip to content

Commit 5d9a157

Browse files
committed
Removing the hardcoded proejct ids and making it configurable.
1 parent 495f00a commit 5d9a157

File tree

4 files changed

+63
-50
lines changed

4 files changed

+63
-50
lines changed

config/cron_udp.hjson

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// and it wouldn’t reflect when the data was actually dumped from canvas.
66
// More info on UDP's batch-ingest DAG process can be found here: https://resources.unizin.org/display/UDP/Batch-ingest+application
77
'''
8-
SELECT * FROM EXTERNAL_QUERY("unizin-shared.context_store", "select 'canvasdatadate' as pkey, min(dag_run) as pvalue from report.publish_info pi2");
8+
SELECT * FROM EXTERNAL_QUERY("context_store", "select 'canvasdatadate' as pkey, min(dag_run) as pvalue from report.publish_info pi2");
99
''',
1010
"user" :
1111
'''
@@ -27,18 +27,18 @@
2727
when cse.role = 'Teacher' then 'TeacherEnrollment'
2828
else '' end
2929
as enrollment_type
30-
from unizin-shared.context_store_entity.course_section_enrollment cse
31-
left join unizin-shared.context_store_entity.course_section cs
30+
from context_store_entity.course_section_enrollment cse
31+
left join context_store_entity.course_section cs
3232
on cse.course_section_id = cs.course_section_id
33-
left join unizin-shared.context_store_keymap.course_offering co
33+
left join context_store_keymap.course_offering co
3434
on cs.le_current_course_offering_id = co.id
35-
left join unizin-shared.context_store_entity.person p
35+
left join context_store_entity.person p
3636
on cse.person_id = p.person_id
37-
left join unizin-shared.context_store_keymap.person p2
37+
left join context_store_keymap.person p2
3838
on p.person_id = p2.id
39-
left join unizin-shared.context_store_entity.person_email pe
39+
left join context_store_entity.person_email pe
4040
on p.person_id = pe.person_id
41-
left join unizin-shared.context_store_entity.course_grade cg
41+
left join context_store_entity.course_grade cg
4242
on cse.course_section_id = cg.course_section_id and cse.person_id = cg.person_id
4343
where
4444
co.lms_int_id IN UNNEST(@course_ids)
@@ -51,15 +51,15 @@
5151
'''
5252
with assignment_details as (
5353
select la.due_date, title, la.course_offering_id, la.learner_activity_id, la.points_possible, la.learner_activity_group_id
54-
from unizin-shared.context_store_entity.learner_activity la, unizin-shared.context_store_keymap.course_offering co
54+
from context_store_entity.learner_activity la, context_store_keymap.course_offering co
5555
where
5656
la.visibility = 'everyone'
5757
and la.status = 'published'
5858
and la.course_offering_id = co.id
5959
and co.lms_int_id IN UNNEST(@course_ids)
6060
), assignment_grp as (
6161
select lg.*
62-
from unizin-shared.context_store_entity.learner_activity_group lg, unizin-shared.context_store_keymap.course_offering co
62+
from context_store_entity.learner_activity_group lg, context_store_keymap.course_offering co
6363
where
6464
lg.status = 'available'
6565
and lg.course_offering_id = co.id
@@ -84,7 +84,7 @@
8484
), assign_rules as (
8585
select distinct ad.learner_activity_group_id, agr.drop_lowest_amount as drop_lowest, agr.drop_highest_amount as drop_highest
8686
from grp_full ad
87-
join unizin-shared.context_store_entity.learner_activity_group agr
87+
join context_store_entity.learner_activity_group agr
8888
on ad.learner_activity_group_id = agr.learner_activity_group_id
8989
), assignment_grp_points as (
9090
select ag.*, am.group_points AS group_points, ar.drop_lowest as drop_lowest, ar.drop_highest as drop_highest
@@ -101,8 +101,8 @@
101101
agp.drop_lowest as drop_lowest,
102102
agp.drop_highest as drop_highest
103103
from assignment_grp_points agp,
104-
unizin-shared.context_store_keymap.course_offering co_km,
105-
unizin-shared.context_store_keymap.learner_activity_group lag_km
104+
context_store_keymap.course_offering co_km,
105+
context_store_keymap.learner_activity_group lag_km
106106
where agp.course_offering_id = co_km.id
107107
and agp.learner_activity_group_id = lag_km.id
108108
order by id
@@ -119,10 +119,10 @@
119119
la.points_possible as points_possible,
120120
cast(lag_km.lms_int_id as INT64) as assignment_group_id
121121
from
122-
unizin-shared.context_store_entity.learner_activity la,
123-
unizin-shared.context_store_keymap.course_offering co,
124-
unizin-shared.context_store_keymap.learner_activity la_km,
125-
unizin-shared.context_store_keymap.learner_activity_group lag_km
122+
context_store_entity.learner_activity la,
123+
context_store_keymap.course_offering co,
124+
context_store_keymap.learner_activity la_km,
125+
context_store_keymap.learner_activity_group lag_km
126126
where
127127
la.visibility = 'everyone'
128128
and la.status = 'published'
@@ -145,8 +145,8 @@
145145
cast(0 as boolean)
146146
end as consider_weight
147147
from
148-
unizin-shared.context_store_entity.learner_activity_group lag,
149-
unizin-shared.context_store_keymap.course_offering co_km
148+
context_store_entity.learner_activity_group lag,
149+
context_store_keymap.course_offering co_km
150150
where
151151
lag.course_offering_id = co_km.id
152152
and co_km.lms_int_id IN UNNEST(@course_ids)
@@ -161,8 +161,8 @@
161161
a.le_term_begin_date as date_start,
162162
a.le_term_end_date as date_end
163163
from
164-
unizin-shared.context_store_entity.academic_term as a
165-
left join unizin-shared.context_store_keymap.academic_term as ka on ka.id = a.academic_term_id
164+
context_store_entity.academic_term as a
165+
left join context_store_keymap.academic_term as ka on ka.id = a.academic_term_id
166166
where
167167
ka.lms_ext_id is not null
168168
order by id
@@ -180,10 +180,10 @@
180180
TIMESTAMP(co.le_start_date) as start_at,
181181
TIMESTAMP(co.le_end_date) as conclude_at
182182
FROM
183-
unizin-shared.context_store_entity.course_offering co
184-
LEFT OUTER JOIN unizin-shared.context_store_entity.academic_term at1 on (co.academic_term_id = at1.academic_term_id),
185-
unizin-shared.context_store_keymap.course_offering co2,
186-
unizin-shared.context_store_keymap.academic_term at2
183+
context_store_entity.course_offering co
184+
LEFT OUTER JOIN context_store_entity.academic_term at1 on (co.academic_term_id = at1.academic_term_id),
185+
context_store_keymap.course_offering co2,
186+
context_store_keymap.academic_term at2
187187
WHERE co2.lms_int_id IN UNNEST(@course_ids)
188188
and co.course_offering_id = co2.id
189189
and at1.academic_term_id = at2.id
@@ -194,7 +194,7 @@
194194
cast(f_km.lms_int_id as BIGINT) as id,
195195
f.status as file_state,
196196
f.display_name as display_name
197-
from unizin-shared.context_store_entity.file f, unizin-shared.context_store_keymap.file f_km, unizin-shared.context_store_keymap.course_offering co_km
197+
from context_store_entity.file f, context_store_keymap.file f_km, context_store_keymap.course_offering co_km
198198
where
199199
f.course_offering_id = co_km.id
200200
and f.file_id = f_km.id
@@ -207,10 +207,10 @@
207207
(
208208
select
209209
distinct cse.person_id as user_id
210-
from unizin-shared.context_store_entity.course_section_enrollment cse
211-
left join unizin-shared.context_store_entity.course_section cs
210+
from context_store_entity.course_section_enrollment cse
211+
left join context_store_entity.course_section cs
212212
on cse.course_section_id = cs.course_section_id
213-
left join unizin-shared.context_store_keymap.course_offering co
213+
left join context_store_keymap.course_offering co
214214
on cs.le_current_course_offering_id = co.id
215215
where
216216
co.lms_int_id in UNNEST(@course_ids)
@@ -236,14 +236,14 @@
236236
lar.person_id as short_user_id,
237237
lar2.lms_int_id as submission_id,
238238
CAST(@canvas_data_id_increment AS INT64) + CAST(p.lms_ext_id AS INT64) as canvas_user_id
239-
from unizin-shared.context_store_entity.learner_activity_result lar
239+
from context_store_entity.learner_activity_result lar
240240
join enrollment on lar.person_id= enrollment.user_id
241241
join enrollment e on lar.person_id = e.user_id
242-
join unizin-shared.context_store_keymap.learner_activity_result lar2 on lar.learner_activity_result_id = lar2.id
243-
left join unizin-shared.context_store_entity.learner_activity la on lar.learner_activity_id = la.learner_activity_id
244-
left join unizin-shared.context_store_keymap.learner_activity la2 on la.learner_activity_id = la2.id
245-
left join unizin-shared.context_store_keymap.course_offering co on co.id = la.course_offering_id
246-
join unizin-shared.context_store_keymap.person p on p.id = lar.person_id
242+
join context_store_keymap.learner_activity_result lar2 on lar.learner_activity_result_id = lar2.id
243+
left join context_store_entity.learner_activity la on lar.learner_activity_id = la.learner_activity_id
244+
left join context_store_keymap.learner_activity la2 on la.learner_activity_id = la2.id
245+
left join context_store_keymap.course_offering co on co.id = la.course_offering_id
246+
join context_store_keymap.person p on p.id = lar.person_id
247247
where
248248
co.lms_int_id in UNNEST(@course_ids)
249249
and la.status = 'published'

config/env_sample.hjson

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,5 +304,11 @@
304304
"COURSES_ENABLED": false,
305305

306306
# Path to the hjson file contains cron queries
307-
"CRON_QUERY_FILE": "config/cron_udp.hjson"
307+
"CRON_QUERY_FILE": "config/cron_udp.hjson",
308+
309+
# Change the default Bigquery Project ID
310+
"DEFAULT_PROJECT_ID": "udp-umich-prod",
311+
# Change the dataset project ID where queries are run against
312+
"DATASET_PROJECT_ID": "unizin-shared"
313+
308314
}

dashboard/cron.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,24 @@ def execute_bq_query(self, query: str, bq_job_config: Optional[bigquery.QueryJob
100100
# Remove the newlines from the query
101101
query = query.replace("\n", " ")
102102

103-
if bq_job_config:
104-
try:
105-
# Convert to bq schema object
106-
query_job = self.bigquery_client.query(query, job_config=bq_job_config)
107-
query_job_result = query_job.result()
103+
# Create a new QueryJobConfig if none is provided
104+
if bq_job_config is None:
105+
bq_job_config = bigquery.QueryJobConfig()
108106

109-
self.total_bytes_billed += query_job.total_bytes_billed
110-
logger.debug(f"This job had {query_job.total_bytes_billed} bytes. Total: {self.total_bytes_billed}")
111-
return query_job_result
112-
except Exception as e:
113-
logger.error(f"Error ({str(e)}) in setting up schema for query {query}.")
114-
raise Exception(e)
115-
else:
116-
query_job = self.bigquery_client.query(query)
107+
# Add the dataset_project_id connection property to the job config
108+
bq_job_config.connection_properties = [bigquery.ConnectionProperty("dataset_project_id", settings.DATASET_PROJECT_ID)]
109+
110+
try:
111+
# Convert to bq schema object
112+
query_job = self.bigquery_client.query(query, job_config=bq_job_config)
117113
query_job_result = query_job.result()
118114
self.total_bytes_billed += query_job.total_bytes_billed
119115
logger.debug(f"This job had {query_job.total_bytes_billed} bytes. Total: {self.total_bytes_billed}")
120-
return query_job_result
116+
except Exception as e:
117+
logger.error(f"Error ({str(e)}) in setting up schema for query {query}.")
118+
raise Exception(e)
119+
120+
return query_job_result
121121

122122
# Execute a query against the MyLA database
123123
def execute_myla_query(self, query: str, params: Optional[Dict] = None) -> ResultProxy:
@@ -314,6 +314,7 @@ def update_resource_access(self):
314314
'canvas_event_urls', 'STRING', settings.CANVAS_EVENT_URLS))
315315
job_config = bigquery.QueryJobConfig()
316316
job_config.query_parameters = query_params
317+
job_config.connection_properties = [bigquery.ConnectionProperty("dataset_project_id", settings.DATASET_PROJECT_ID)]
317318

318319
# Location must match that of the dataset(s) referenced in the query.
319320
bq_job = self.bigquery_client.query(final_query, location='US', job_config=job_config)

dashboard/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ def apply_env_overrides(env: Dict[str, Any], environ: os._Environ) -> Dict[str,
449449
# Only need view permission for exports
450450
IMPORT_EXPORT_EXPORT_PERMISSION_CODE = 'view'
451451

452+
# Change the default project ID for BigQuery if needed (This is typically the one that quotas are run against and logged into)
453+
DEFAULT_PROJECT_ID = ENV.get("DEFAULT_PROJECT_ID", None)
454+
455+
# Override the default project ID for BigQuery if needed, like to unizin-shared
456+
DATASET_PROJECT_ID = ENV.get("DATASET_PROJECT_ID", None)
457+
452458
# IMPORT LOCAL ENV
453459
# =====================
454460
try:

0 commit comments

Comments
 (0)