Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit d0b0ca0

Browse files
authored
Merge pull request #381 from RunestoneInteractive/dbperformance
Dbperformance
2 parents 55ed651 + 9e04a95 commit d0b0ca0

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

runestone/activecode/activecode.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,22 @@ def process_activcode_nodes(app, env, docname):
122122
def purge_activecodes(app, env, docname):
123123
pass
124124

125+
database_connection = True
126+
try:
127+
engine = create_engine(get_dburl(locals()))
128+
meta = MetaData()
129+
Source_code = Table('source_code', meta, autoload=True, autoload_with=engine)
130+
Div = Table('div_ids', meta, autoload=True, autoload_with=engine)
131+
except:
132+
print("Cannot connect")
133+
database_connection = False
134+
125135

126136
class ActiveCode(RunestoneDirective):
127137
"""
128138
.. activecode:: uniqueid
129139
:nocanvas: do not create a canvas
140+
:autograde: normally set this to unittest
130141
:nopre: do not create an output component
131142
:above: put the canvas above the code
132143
:autorun: run this activecode as soon as the page is loaded
@@ -183,12 +194,15 @@ class ActiveCode(RunestoneDirective):
183194
'available_files' : directives.unchanged,
184195
})
185196

197+
198+
186199
def run(self):
187200

188201
addQuestionToDB(self)
189202

190203
env = self.state.document.settings.env
191-
# keep track of how many activecodes we have.... could be used to automatically make a unique id for them.
204+
# keep track of how many activecodes we have....
205+
# could be used to automatically make a unique id for them.
192206
if not hasattr(env, 'activecodecounter'):
193207
env.activecodecounter = 0
194208
env.activecodecounter += 1
@@ -305,13 +319,12 @@ def run(self):
305319
else:
306320
source = '\n'
307321
suffix = '\n'
308-
try:
309-
engine = create_engine(get_dburl(locals()))
310-
meta = MetaData()
311-
course_name = env.config.html_context['course_id']
312-
Source_code = Table('source_code', meta, autoload=True, autoload_with=engine)
313-
divid = self.options['divid']
314322

323+
324+
course_name = env.config.html_context['course_id']
325+
divid = self.options['divid']
326+
327+
try:
315328
engine.execute(Source_code.delete().where(Source_code.c.acid == divid).where(Source_code.c.course_id == course_name))
316329
engine.execute(Source_code.insert().values(
317330
acid = divid,
@@ -325,7 +338,7 @@ def run(self):
325338
ch, sub_ch = env.docname.split('/')
326339
except:
327340
ch, sub_ch = (env.docname, 'null subchapter')
328-
Div = Table('div_ids', meta, autoload=True, autoload_with=engine)
341+
329342
engine.execute(Div.delete()\
330343
.where(Div.c.course_name == course_name)\
331344
.where(Div.c.chapter == ch)\

runestone/server/componentdb.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
if all(name in os.environ for name in ['DBHOST', 'DBPASS', 'DBUSER', 'DBNAME']):
2727
dburl = 'postgresql://{DBUSER}:{DBPASS}@{DBHOST}/{DBNAME}'.format(**os.environ)
2828
engine = create_engine(dburl)
29+
meta = MetaData()
30+
questions = Table('questions', meta, autoload=True, autoload_with=engine)
31+
assignment_types = Table('assignment_types', meta, autoload=True, autoload_with=engine)
32+
assignment_questions = Table('assignment_questions', meta, autoload=True, autoload_with=engine)
33+
courses = Table('courses', meta, autoload=True, autoload_with=engine)
34+
2935
else:
3036
dburl = None
3137
engine = None
@@ -58,9 +64,6 @@ def addQuestionToDB(self):
5864

5965
last_changed = datetime.now()
6066

61-
engine = create_engine(dburl)
62-
meta = MetaData()
63-
questions = Table('questions', meta, autoload=True, autoload_with=engine)
6467
if 'difficulty' in self.options:
6568
difficulty = self.options['difficulty']
6669
else:
@@ -94,8 +97,6 @@ def addQuestionToDB(self):
9497

9598
def getQuestionID(base_course, name):
9699
meta = MetaData()
97-
questions = Table('questions', meta, autoload=True, autoload_with=engine)
98-
99100

100101
sel = select([questions]).where(and_(questions.c.name == name,
101102
questions.c.base_course == base_course))
@@ -108,10 +109,6 @@ def getQuestionID(base_course, name):
108109
def getOrInsertQuestionForPage(base_course=None, name=None, is_private='F', question_type="page", autograde = "visited", author=None, difficulty=1,chapter=None):
109110
last_changed = datetime.now()
110111

111-
meta = MetaData()
112-
questions = Table('questions', meta, autoload=True, autoload_with=engine)
113-
114-
115112
sel = select([questions]).where(and_(questions.c.name == name,
116113
questions.c.base_course == base_course))
117114
res = engine.execute(sel).first()
@@ -144,8 +141,7 @@ def getOrInsertQuestionForPage(base_course=None, name=None, is_private='F', ques
144141

145142
def getOrCreateAssignmentType(assignment_type_name, grade_type = None, points_possible = None, assignments_count = None, assignments_dropped = None):
146143

147-
meta = MetaData()
148-
assignment_types = Table('assignment_types', meta, autoload=True, autoload_with=engine)
144+
149145

150146
# search for it in the DB
151147
sel = select([assignment_types]).where(assignment_types.c.name == assignment_type_name)
@@ -164,10 +160,6 @@ def getOrCreateAssignmentType(assignment_type_name, grade_type = None, points_po
164160
return res.inserted_primary_key[0]
165161

166162
def addAssignmentQuestionToDB(question_id, assignment_id, points, assessment_type = None, timed=None, autograde=None):
167-
meta = MetaData()
168-
questions = Table('questions', meta, autoload=True, autoload_with=engine)
169-
assignment_questions = Table('assignment_questions', meta, autoload=True, autoload_with=engine)
170-
171163
# now insert or update the assignment_questions row
172164
sel = select([assignment_questions]).where(and_(assignment_questions.c.assignment_id == assignment_id,
173165
assignment_questions.c.question_id == question_id))
@@ -196,21 +188,13 @@ def addAssignmentQuestionToDB(question_id, assignment_id, points, assessment_typ
196188
engine.execute(ins)
197189

198190
def getCourseID(coursename):
199-
meta = MetaData()
200-
courses = Table('courses', meta, autoload=True, autoload_with=engine)
201-
202191
sel = select([courses]).where(courses.c.course_name == coursename)
203192
res = engine.execute(sel).first()
204193
return res['id']
205194

206195
def addAssignmentToDB(name = None, course_id = None, assignment_type_id = None, deadline = None, points = None, threshold = None):
207196

208197
last_changed = datetime.now()
209-
210-
meta = MetaData()
211-
assignments = Table('assignments', meta, autoload=True, autoload_with=engine)
212-
assignment_questions = Table('assignment_questions', meta, autoload=True, autoload_with=engine)
213-
214198
sel = select([assignments]).where(and_(assignments.c.name == name,
215199
assignments.c.course == course_id))
216200
res = engine.execute(sel).first()
@@ -249,9 +233,6 @@ def addHTMLToDB(divid, basecourse, htmlsrc):
249233

250234
if dburl:
251235
last_changed = datetime.now()
252-
engine = create_engine(dburl)
253-
meta = MetaData()
254-
questions = Table('questions', meta, autoload=True, autoload_with=engine)
255236
sel = select([questions]).where(and_(questions.c.name == divid,
256237
questions.c.base_course == basecourse))
257238
res = engine.execute(sel).first()
@@ -266,8 +247,6 @@ def addHTMLToDB(divid, basecourse, htmlsrc):
266247
print("Error while trying to add directive {} to the DB".format(divid))
267248

268249
def get_HTML_from_DB(divid, basecourse):
269-
meta = MetaData()
270-
questions = Table('questions', meta, autoload=True, autoload_with=engine)
271250
sel = select([questions]).where(and_(questions.c.name == divid,
272251
questions.c.base_course == basecourse))
273252
res = engine.execute(sel).first()

0 commit comments

Comments
 (0)