Skip to content

Commit

Permalink
feat: add script to generate next weekly quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
atnanahidiw committed Sep 27, 2023
1 parent af47dc4 commit b92013f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
62 changes: 62 additions & 0 deletions utils/scripts/next_weekly_quiz_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import os
import random
from datetime import datetime, timedelta
from firebase_admin import credentials, initialize_app, firestore


creds = credentials.Certificate(
json.loads(os.environ["FIREBASE_SECRET_KEY_JSON"])
)
initialize_app(creds)

firestore_client = firestore.client()
configuration_coll_ref = firestore_client.collection("configuration")
problemset_coll_ref = firestore_client.collection("problem_set")
weeklyquizlist_coll_ref = firestore_client.collection("weekly_quiz_list")

config_global_variables = configuration_coll_ref.document("global_variables").get().to_dict()
config_global_variables["weeklyquiz_number"] += 1 # increase weekly quiz number ID
today = datetime.now()
monday = (
today - timedelta(days=today.weekday())
).replace(
hour=0,
minute=0,
second=0,
microsecond=0
)
result = {
"title": "LATIHAN BEBRAS MINGGUAN #" + config_global_variables["weeklyquiz_number"],
"created_at": str(today),
"start_at": str(monday + timedelta(days=7)),
"end_at": str(monday + timedelta(days=14)),
"problems": {},
"max_attempts": {},
"sponsors": {}
}

challengegroup_map = configuration_coll_ref.document("challenge_group").get().to_dict()
for cg_group, cg_val in challengegroup_map.items():
problem_id_list = []

query_ref = problemset_coll_ref.where("challenge_group", "==", cg_group)
for doc in query_ref.stream():
problem_id_list.append(doc.to_dict()["id"])
random.shuffle(problem_id_list)

result["problems"][cg_group] = problem_id_list[:cg_val["weeklyquiz_problem_number"]]
result["max_attempts"][cg_group] = cg_val["weeklyquiz_max_attempts"]
result["sponsors"][cg_group] = None

# for history data, generate random id & put it in the `result` for latest data
_, doc_ref = weeklyquizlist_coll_ref.add(result)
result["id"] = doc_ref.id

# for latest data
to_running = configuration_coll_ref.document("next_weekly_quiz").get().to_dict()
configuration_coll_ref.document("next_weekly_quiz").set(result)
configuration_coll_ref.document("running_weekly_quiz").set(to_running)

# update global variables
configuration_coll_ref.document("global_variables").set(config_global_variables)
1 change: 1 addition & 0 deletions utils/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
firebase-admin==6.2.0
23 changes: 23 additions & 0 deletions utils/scripts/update_problem_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json
import os
from firebase_admin import credentials, initialize_app, firestore
from glob import glob

creds = credentials.Certificate(
json.loads(os.environ["FIREBASE_SECRET_KEY_JSON"])
)
initialize_app(creds)

firestore_client = firestore.client()
col_ref = firestore_client.collection("problem_set")

file_list = glob("data/question_bank/*/*/*.json")
for file in file_list:
with open(file, "r") as f:
file_ref = file.replace(".json", "").split("/")
name = "_".join(file_ref[-2:])

content = json.load(f)
content["challenge_group"] = file_ref[-2]

col_ref.document(name).set(content)

0 comments on commit b92013f

Please sign in to comment.