convert all raw sqls to ORM
This commit is contained in:
29
job.py
29
job.py
@@ -90,8 +90,8 @@ def GetJM_Message():
|
||||
# active jobs being processed in the background
|
||||
################################################################################
|
||||
def GetNumActiveJobs():
|
||||
ret = db.engine.execute("select count(1) from job where pa_job_state is distinct from 'Completed'").first()
|
||||
return ret.count
|
||||
ret=Job.query.filter(Job.pa_job_state != 'Completed').with_entities(func.count(Job.id).label('count') ).first()
|
||||
return ret[0]
|
||||
|
||||
################################################################################
|
||||
# this function uses sockets to force wake the job mgr / option from Admin menu
|
||||
@@ -197,7 +197,7 @@ def joblog(id):
|
||||
refresh=False
|
||||
else:
|
||||
refresh=True
|
||||
log_cnt = db.session.execute( f"select count(id) from joblog where job_id = {id}" ).first()[0]
|
||||
log_cnt = Joblog.query.filter(Joblog.job_id==id).with_entities( func.count(1) ).first()[0]
|
||||
newest_logs = Joblog.query.filter(Joblog.job_id==id).order_by(Joblog.log_date.desc()).limit(NEWEST_LOG_LIMIT).all()
|
||||
oldest_logs = Joblog.query.filter(Joblog.job_id==id).order_by(Joblog.log_date).limit(OLDEST_LOG_LIMIT).all()
|
||||
logs=sorted( set( oldest_logs + newest_logs ), key=lambda el: el.log_date )
|
||||
@@ -256,9 +256,8 @@ def stale_job(id):
|
||||
WithdrawDependantJobs( job, job.id, "(Stale) Job withdrawn manually by user" )
|
||||
FinishJob(job, f"Job (#{job.id}) (Stale) Job withdrawn manually by user", "Withdrawn" )
|
||||
|
||||
# clear out message for this job being stale (and do this via raw sql to
|
||||
# avoid circulr import)
|
||||
db.engine.execute( f"delete from pa_job_manager_fe_message where job_id = {id}" )
|
||||
# clear out persistent message for this job being stale
|
||||
PA_JobManager_Message.query.filter(PA_JobManager_Message.job_id==id).delete()
|
||||
|
||||
db.session.commit()
|
||||
WakePAJobManager(job.id)
|
||||
@@ -282,23 +281,25 @@ def stale_jobs():
|
||||
@app.route("/joblog_search", methods=["POST"])
|
||||
@login_required
|
||||
def joblog_search():
|
||||
from files import Entry
|
||||
from sqlalchemy import text
|
||||
|
||||
eid=request.form['eid']
|
||||
ent_cursor=db.engine.execute( f"select name from entry where id = {eid}" )
|
||||
for ent in ent_cursor:
|
||||
jobs_cursor=db.engine.execute( f"select l.log, j.id, j.name, j.state, l.log_date from joblog l, job j where l.job_id = j.id and l.log ilike '%%{ent[0]}%%' order by l.log_date")
|
||||
ent=Entry.query.get(eid)
|
||||
logs=Joblog.query.join(Job).filter(Joblog.log.ilike(text(f"'%%{ent.name}%%'"))).with_entities(Joblog.log, Job.id, Job.name, Job.state, Joblog.log_date).all()
|
||||
|
||||
# turn DB output into json and return it to the f/e
|
||||
ret='[ '
|
||||
first_job=1
|
||||
last_job_id = -1
|
||||
for j in jobs_cursor:
|
||||
for l in logs:
|
||||
if not first_job:
|
||||
ret +=", "
|
||||
ret+= '{'
|
||||
ret+= f'"id":"{j.id}", '
|
||||
ret+= f'"name":"{j.name}", '
|
||||
ret+= f'"log_date":"{j.log_date}", '
|
||||
ret+= f'"log": "{j.log}"'
|
||||
ret+= f'"id":"{l.id}", '
|
||||
ret+= f'"name":"{l.name}", '
|
||||
ret+= f'"log_date":"{l.log_date}", '
|
||||
ret+= f'"log": "{l.log}"'
|
||||
ret+= '}'
|
||||
first_job=0
|
||||
ret+= ' ]'
|
||||
|
||||
Reference in New Issue
Block a user