Skip to content

Commit

Permalink
Merge pull request #94 from tim-moody/9-25-enh
Browse files Browse the repository at this point in the history
add in job create datetime and refactor
  • Loading branch information
tim-moody authored Sep 27, 2018
2 parents 1f5a4b2 + d6c9776 commit 68d143c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 61 deletions.
50 changes: 24 additions & 26 deletions roles/cmdsrv/files/iiab-cmdsrv
Original file line number Diff line number Diff line change
Expand Up @@ -1881,8 +1881,8 @@ def get_last_jobs_stat(cmd_info):
db_lock.acquire() # will block if lock is already held
try:
conn = sqlite3.connect(cmdsrv_dbpath)
cur = conn.execute ("SELECT jobs.rowid, job_command, job_output, job_status, strftime('%s',jobs.create_datetime), strftime('%s',last_update_datetime), strftime('%s','now', 'localtime'), cmd_msg FROM jobs, commands where cmd_rowid = commands.rowid ORDER BY jobs.rowid DESC LIMIT 30")
last_jobs = cur.fetchall()
cur = conn.execute ("SELECT jobs.rowid, job_command, job_output, job_status, strftime('%m-%d %H:%M', jobs.create_datetime), strftime('%s', jobs.create_datetime), strftime('%s',last_update_datetime), strftime('%s','now', 'localtime'), cmd_msg FROM jobs, commands where cmd_rowid = commands.rowid ORDER BY jobs.rowid DESC LIMIT 30")
status_jobs = cur.fetchall()
conn.close()
except sqlite3.Error, e:
tprint ("Error %s:" % e.args[0])
Expand All @@ -1894,42 +1894,40 @@ def get_last_jobs_stat(cmd_info):

#print "job running"
#print jobs_running
# get status output for incomplete ansible jobs
last_jobs_cc = []
for job in last_jobs:
# get status output for recent jobs
status_jobs_return = []
for job in status_jobs:
status_job = {}
job_id = job[0]
status_job['job_id'] = job_id
status_job['job_command'] = job[1]
status_job['job_output'] = job[2]
status_job['job_status'] = job[3]
status_job['create_datetime'] = job[4]
create_datetime_sec = job[5]
last_update_datetime_sec = job[6]
cur_time_sec = job[7]
# elapsed time is from creation, not start
status_job['elapsed_sec'] = int(last_update_datetime_sec) - int(create_datetime_sec) # not valid for running jobs
status_job['cmd_msg'] = job[8]

if job_id in jobs_running:
job_cc = []
job_cc.append(job[0])
job_cc.append(job[1])
job_output = job[2]
#if jobs_running[job_id]['cmd'] == "RUN-ANSIBLE" and jobs_running[job_id]['status'] == "STARTED":
if jobs_running[job_id]['status'] == "STARTED" or jobs_running[job_id]['status'] == "RESTARTED":
status_job['elapsed_sec'] = int(cur_time_sec) - int(create_datetime_sec) # last_update_datetime not update while running
# load output from tmp file
output_file = jobs_running[job_id]['output_file']
#print output_file
#file = open(output_file, 'r')
#job_output = file.read()

command = "tail " + output_file
args = shlex.split(command)
job_output = subprocess.check_output(args)
#print job_output
#job_output = escape_html(job_output)
#file.close()
#print "job output" + job_output
status_job['job_output'] = job_output

job_cc.append(job_output)
job_cc.append(job[3])
job_cc.append(job[4])
job_cc.append(job[5])
job_cc.append(job[6])
job_cc.append(job[7])
#print "job output" + job_output

last_jobs_cc.append(job_cc)
else:
last_jobs_cc.append(job)
status_jobs_return.append(status_job)

resp = json.dumps(last_jobs_cc)
resp = json.dumps(status_jobs_return)
return resp

def get_jobs_running(cmd_info): # Not used
Expand Down
2 changes: 1 addition & 1 deletion roles/console/files/htmlf/70-utilities.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h2>Job Status</h2><br>
<th style="width:30%">Command</th>
<th style="width:40%">Result</th>
<th style="width:10%">Status</th>
<th style="width:10%">Duration</th>
<th style="width:10%">Sched/Elapsed</th>
</tr>
</thead>
<tbody>
Expand Down
57 changes: 23 additions & 34 deletions roles/console/files/js/admin_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -1395,60 +1395,49 @@ function procJobStat(data)
job_status = {};
var html = "";
var html_break = '<br>';
var duration = 0;
var durationStr = "";

data.forEach(function(entry) {
//console.log(entry);

data.forEach(function(statusJob) {
//console.log(statusJob);
html += "<tr>";
var job_info = {};
//var job_info = {};

job_info['job_no'] = entry[0];
html += "<td>" + entry[0] + "<BR>"; // job number
// html += '<input type="checkbox" name="' gw_squid_whitelist + '" id="' xo-gw_squid_whitelist +'">';
var jobId = "job_stat_id-" + entry[0];
html += '<input type="checkbox" id="' + jobId + '">';
//job_info['job_no'] = entry[0];
html += "<td>" + statusJob.job_id + "<BR>"; // job number
html += '<input type="checkbox" id="' + statusJob.job_id + '">';
html += "</td>";
job_info['command'] = entry[1];
html += '<td style="overflow: hidden; text-overflow: ellipsis">' + entry[1] + "</td>";
html += '<td style="overflow: hidden; text-overflow: ellipsis">' + statusJob.job_command + "</td>";

result = entry[2].replace(/(?:\r\n|\r|\n)/g, html_break); // change newline to BR
var result = statusJob.job_output.replace(/(?:\r\n|\r|\n)/g, html_break); // change newline to BR
// result = result.replace(html_break+html_break, html_break); // remove blank lines, but doesn't work
var idx = result.indexOf(html_break);
if (idx =0) result = result.substring(html_break.length); // strip off first newline
if (idx == 0) result = result.substring(html_break.length); // strip off first newline
idx = result.lastIndexOf(html_break);
if (idx >=0) result = result.substring(0,idx); // strip off last newline
job_info['result'] = result;
if (idx >= 0) result = result.substring(0,idx); // strip off last newline
//job_info['result'] = result;

idx = result.lastIndexOf(html_break); // find 2nd to last newline
var result_end = "";
if (idx >=0) result_end = result.substring(0,idx + html_break.length);
if (idx >= 0) result_end = result.substring(0,idx + html_break.length);
html += '<td> <div class = "statusJobResult">' + result + "</div></td>";

job_info['status'] = entry[3];
html += "<td>" + entry[3] + "</td>";
duration = entry[5] - entry[4]; // unless is running

if (["STARTED","RESTARTED"].includes(entry[3]))
duration = entry[6] - entry[4]; // then use current time on server
durationStr = secondsToDuration(duration)

job_info['duration'] = durationStr;
html += "<td>" + statusJob.job_status + "</td>";

html += "<td>" + durationStr + "</td>";
var elapsedStr = secondsToDuration(statusJob.elapsed_sec);
html += "<td>" + statusJob.create_datetime + '<BR>' + elapsedStr + "</td>";

html += "</tr>";

// there should be one or two parts
var cmd_parse = entry[5].split(" ");
job_info['cmd_verb'] = cmd_parse[0];
// there should be one or two parts - ? still need this; for cancel
var cmd_parse = statusJob.cmd_msg.split(" ");
job_status['cmd_verb'] = cmd_parse[0];
if(cmd_parse.length == 0 || typeof cmd_parse[1] === 'undefined')
job_info['cmd_args'] = ""
job_status['cmd_args'] = ""
else
job_info['cmd_args'] = JSON.parse(cmd_parse[1]);
job_status['cmd_args'] = JSON.parse(cmd_parse[1]);

consoleLog(job_info);
job_status[job_info['job_no']] = job_info;
consoleLog(statusJob);
job_status[statusJob.job_no] = statusJob;

});
$( "#jobStatTable tbody" ).html(html);
Expand Down

0 comments on commit 68d143c

Please sign in to comment.