From 1db9445f84656b7a3c5a9861afca1ecea9328528 Mon Sep 17 00:00:00 2001 From: Newnius Date: Wed, 1 Jul 2020 10:52:31 +0800 Subject: [PATCH] update --- ajax.php | 12 ++++++++++ job.logic.php | 48 +++++++++++++++++++++++++++++++++++++++ modals.php | 3 ++- static/job.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) diff --git a/ajax.php b/ajax.php index 5618e00..381b527 100644 --- a/ajax.php +++ b/ajax.php @@ -112,6 +112,18 @@ switch ($action) { $res = job_predict_req($job, $role); break; + case 'job_predict_time': + $job = new CRObject(); + $job->set('name', cr_get_POST('name', 'jobName')); + $job->set('virtual_cluster', cr_get_POST('cluster')); + $job->set('workspace', cr_get_POST('workspace')); + $job->set('priority', cr_get_POST('priority')); + $job->set('run_before', cr_get_POST('run_before')); + $job->set('locality', cr_get_POST('locality')); + $job->set('tasks', cr_get_POST('tasks')); + $res = job_predict_time($job, $role); + break; + case 'summary_get': $res = summary_get(); break; diff --git a/job.logic.php b/job.logic.php index bc47098..41966a8 100644 --- a/job.logic.php +++ b/job.logic.php @@ -197,6 +197,54 @@ function job_predict_req(CRObject $job, $role) return $res; } +function job_predict_time(CRObject $job, $role) +{ + if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'job.list')) { + $res['errno'] = Code::NO_PRIVILEGE; + return $res; + } + + $spider = new Spider(); + $tasks = json_decode($job->get('tasks'), true); + foreach ($tasks as $i => $task) { + $task['cpu_number'] = intval($task['cpu_number']); + $task['memory'] = intval($task['memory']); + $task['gpu_number'] = intval($task['gpu_number']); + $task['gpu_memory'] = intval($task['gpu_memory']); + $task['is_ps'] = $task['is_ps'] == 1; + $tasks[$i] = $task; + } + $job->set('tasks', $tasks); + $job->set('workspace', $job->get('workspace')); + $job->set('group', $job->get('virtual_cluster')); + $job->set('priority', $job->getInt('priority')); + $job->set('locality', $job->getInt('locality')); + $job->set('run_before', $job->getInt('run_before')); + $job->set('created_by', $job->getInt('created_by')); + $data['job'] = json_encode($job); + + $spider->doPost(YAO_SCHEDULER_ADDR . '?action=job_predict_time', $data); + $msg = json_decode($spider->getBody(), true); + + if ($msg === NULL) { + $res['errno'] = Code::UNKNOWN_ERROR; + $res['msg'] = 'response is null'; + return $res; + } + + if ($msg['code'] !== 0) { + $res['errno'] = $msg['code']; + $res['msg'] = $msg['error']; + return $res; + } + + $res['total'] = $msg['total']; + $res['pre'] = $msg['pre']; + $res['post'] = $msg['post']; + $res['errno'] = Code::SUCCESS; + return $res; +} + function summary_get() { if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'system.summary')) { diff --git a/modals.php b/modals.php index c638dd2..7ffd4aa 100755 --- a/modals.php +++ b/modals.php @@ -211,7 +211,8 @@
- + +
diff --git a/static/job.js b/static/job.js index be4a24c..20bd2a3 100755 --- a/static/job.js +++ b/static/job.js @@ -107,6 +107,68 @@ function register_events_job() { }); }); + $("#form-job-predict-time").click(function (e) { + var name = $('#form-job-name').val(); + var workspace = $('#form-job-workspace').val(); + var cluster = $('#form-job-cluster').val(); + var priority = $('#form-job-priority').val(); + var run_before = $('#form-job-run-before').val(); + var locality = $('#form-job-locality').val(); + if (run_before.length !== 0) { + run_before = moment(run_before).unix(); + } + var tasks = []; + $('#form-job-tasks').find('.row').each(function () { + var task = {}; + task['name'] = $(this).find('.task-name').eq(0).val(); + task['image'] = $(this).find('.task-image').eq(0).val(); + task['cmd'] = $(this).find('.task-cmd').eq(0).val(); + task['cpu_number'] = $(this).find('.task-cpu').eq(0).val(); + task['memory'] = $(this).find('.task-mem').eq(0).val(); + task['gpu_number'] = $(this).find('.task-gpu-num').eq(0).val(); + task['gpu_memory'] = $(this).find('.task-gpu-mem').eq(0).val(); + task['is_ps'] = $(this).find('.task-is-ps').eq(0).val(); + task['gpu_model'] = $(this).find('.task-gpu-model').eq(0).val(); + tasks.push(task); + }); + + /* TODO validate form */ + if (name.length === 0) { + return true; + } + $.each(tasks, function (i, task) { + if (task['name'].length === 0) { + return true; + } + }); + + var ajax = $.ajax({ + url: "service?action=job_predict_time", + type: 'POST', + data: { + name: name, + workspace: workspace, + cluster: cluster, + priority: priority, + run_before: run_before, + locality: locality, + tasks: JSON.stringify(tasks) + } + }); + ajax.done(function (res) { + if (res["errno"] !== 0) { + $("#modal-msg-content").html(res["msg"]); + $("#modal-msg").modal('show'); + } else { + console.log(res); + } + }); + ajax.fail(function (jqXHR, textStatus) { + $("#modal-msg-content").html("Request failed : " + jqXHR.statusText); + $("#modal-msg").modal('show'); + }); + }); + $('#form-job-task-add').click(function (e) { var tasks = $('#form-job-tasks'); var newTask = $('#form-job-tasks').find('.row').eq(0).clone();