diff --git a/AgentManager.class.php b/AgentManager.class.php index a987bb8..72cd1b4 100755 --- a/AgentManager.class.php +++ b/AgentManager.class.php @@ -10,7 +10,7 @@ class AgentManager private static $table = 'yao_agent'; /* - * do add link + * do add agent */ public static function add(CRObject $agent) { diff --git a/WorkspaceManager.class.php b/WorkspaceManager.class.php new file mode 100755 index 0000000..daa540f --- /dev/null +++ b/WorkspaceManager.class.php @@ -0,0 +1,119 @@ +get('name'); + $content = $workspace->get('content'); + $virtual_cluster = $workspace->getInt('virtual_cluster'); + $permission = $workspace->getInt('permission'); + $created_at = $workspace->getInt('created_at', time()); + $updated_at = $workspace->getInt('updated_at', time()); + $created_by = $workspace->get('created_by'); + + $key_values = array( + 'name' => '?', 'content' => '?', 'virtual_cluster' => '?', 'permission' => '?', + 'created_at' => '?', 'updated_at' => '?', 'created_by' => '?' + ); + $builder = new SQLBuilder(); + $builder->insert(self::$table, $key_values); + $sql = $builder->build(); + $params = array($name, $content, $virtual_cluster, $permission, $created_at, $updated_at, $created_by); + return (new MysqlPDO())->execute($sql, $params); + } + + /* */ + public static function gets(CRObject $rule) + { + $offset = $rule->getInt('offset', 0); + $limit = $rule->getInt('limit', -1); + $selected_rows = array(); + $where = array(); + $params = array(); + $order_by = array('id' => 'DESC'); + $builder = new SQLBuilder(); + $builder->select(self::$table, $selected_rows); + $builder->where($where); + $builder->order($order_by); + $builder->limit($offset, $limit); + $sql = $builder->build(); + $workspaces = (new MysqlPDO())->executeQuery($sql, $params); + return $workspaces; + } + + /* */ + public static function count(CRObject $rule) + { + $selected_rows = array('COUNT(1) as cnt'); + $where = array(); + $params = array(); + $builder = new SQLBuilder(); + $builder->select(self::$table, $selected_rows); + $builder->where($where); + $sql = $builder->build(); + $res = (new MysqlPDO())->executeQuery($sql, $params); + return $res === null ? 0 : intval($res[0]['cnt']); + } + + /* get workspace by ip */ + public static function getByID($id) + { + $selected_rows = array(); + $where = array('id' => '?'); + $params = array($id); + $builder = new SQLBuilder(); + $builder->select(self::$table, $selected_rows); + $builder->where($where); + $sql = $builder->build(); + $workspaces = (new MysqlPDO())->executeQuery($sql, $params); + return $workspaces !== null && count($workspaces) === 1 ? $workspaces[0] : null; + } + + /* + * do update workspace + */ + public static function update(CRObject $workspace) + { + $id = $workspace->getInt('id'); + $name = $workspace->get('name'); + $content = $workspace->get('content'); + $permission = $workspace->getInt('permission'); + $updated_at = $workspace->getInt('updated_at', time()); + + $key_values = array( + 'name' => '?', 'content' => '?', 'permission' => '?', 'updated_at' => '?' + ); + $where = array('id' => '?'); + $builder = new SQLBuilder(); + $builder->update(self::$table, $key_values); + $builder->where($where); + $sql = $builder->build(); + $params = array($name, $content, $permission, $updated_at, $id); + return (new MysqlPDO())->execute($sql, $params); + } + + /* */ + public static function remove(CRObject $workspace) + { + $id = $workspace->getInt('id'); + $where = array('id' => '?'); + $builder = new SQLBuilder(); + $builder->delete(self::$table); + $builder->where($where); + $sql = $builder->build(); + $params = array($id); + return (new MysqlPDO())->execute($sql, $params); + } + +} diff --git a/ajax.php b/ajax.php index 114d78b..53a3560 100644 --- a/ajax.php +++ b/ajax.php @@ -9,6 +9,7 @@ require_once('Securer.class.php'); require_once('user.logic.php'); require_once('job.logic.php'); require_once('agent.logic.php'); +require_once('workspace.logic.php'); require_once('config.inc.php'); require_once('init.inc.php'); @@ -25,8 +26,6 @@ function csrf_check($action) return false; } $post_methods = array( - 'job_submit', - 'job_stop', 'signout', 'oauth_get_url' ); @@ -82,7 +81,7 @@ switch ($action) { case 'job_stop': $job = new CRObject(); $job->set('id', cr_get_POST('id')); - $res = job_stop($link); + $res = job_stop($job); break; case 'job_describe': @@ -112,6 +111,38 @@ switch ($action) { $res = agent_remove($job); break; + case 'workspace_list': + $rule = new CRObject(); + $rule->set('offset', cr_get_GET('offset')); + $rule->set('limit', cr_get_GET('limit')); + $res = workspace_list($rule); + break; + + case 'workspace_add': + $workspace = new CRObject(); + $workspace->set('name', cr_get_POST('name')); + $workspace->set('content', cr_get_POST('content')); + $workspace->set('virtual_cluster', cr_get_POST('virtual_cluster')); + $workspace->set('permission', cr_get_POST('permission')); + $res = workspace_add($workspace); + break; + + case 'workspace_update': + $workspace = new CRObject(); + $workspace->set('id', cr_get_POST('id')); + $workspace->set('name', cr_get_POST('name')); + $workspace->set('content', cr_get_POST('content')); + $workspace->set('virtual_cluster', cr_get_POST('virtual_cluster')); + $workspace->set('permission', cr_get_POST('permission')); + $res = workspace_update($workspace); + break; + + case 'workspace_remove': + $workspace = new CRObject(); + $workspace->set('id', cr_get_POST('id')); + $res = workspace_remove($workspace); + break; + case 'user_signout': $res = user_signout(); break; diff --git a/init.inc.php b/init.inc.php index f654d47..f03c1db 100644 --- a/init.inc.php +++ b/init.inc.php @@ -75,6 +75,12 @@ function init_accessMap() 'agent.add' => array('root', 'admin'), 'agent.remove' => array('root', 'admin'), + /* workspace */ + 'workspace.list' => array('root', 'admin', 'normal'), + 'workspace.add' => array('root', 'admin', 'normal'), + 'workspace.update' => array('root', 'admin', 'normal'), + 'workspace.remove' => array('root', 'admin', 'normal'), + /* ucenter entry show control */ 'ucenter.home' => array('root', 'admin', 'developer', 'normal'), 'ucenter.jobs' => array('root', 'admin', 'developer', 'normal'), diff --git a/install.php b/install.php index 66af5f5..0c36cca 100755 --- a/install.php +++ b/install.php @@ -64,6 +64,7 @@ function create_table_workspace() `name` varchar(64) NOT NULL, `content` json NOT NULL, `created_at` BIGINT NOT NULL, + `updated_at` BIGINT NOT NULL, `virtual_cluster` varchar(64) NOT NULL, INDEX(`virtual_cluster`), `created_by` int NOT NULL, diff --git a/modals.php b/modals.php index 9b19b1d..45d5cce 100755 --- a/modals.php +++ b/modals.php @@ -188,4 +188,46 @@ + + + +
\ No newline at end of file diff --git a/static/ucenter.js b/static/ucenter.js index 7d7c0a7..0200b95 100755 --- a/static/ucenter.js +++ b/static/ucenter.js @@ -15,6 +15,10 @@ $(function () { register_events_agent(); load_agents(''); break; + case "workspaces": + register_events_workspace(); + load_workspaces(''); + break; default: break; } diff --git a/static/workspace.js b/static/workspace.js new file mode 100755 index 0000000..aebfe58 --- /dev/null +++ b/static/workspace.js @@ -0,0 +1,157 @@ +function register_events_workspace() { + $('#btn-workspace-add').click(function (e) { + $('#form-workspace-submit-type').val('add'); + $('#modal-workspace').modal('show'); + }); + + $("#form-workspace-submit").click(function (e) { + var id = $('#form-workspace-id').val(); + var name = $('#form-workspace-name').val(); + var content = $('#form-workspace-content').val(); + var virtual_cluster = $('#form-workspace-virtual-cluster').val(); + var permission = $('#form-workspace-permission').val(); + + /* TODO validate form */ + + $('#modal-workspace').modal('hide'); + var action = 'workspace_add'; + if ($('#form-workspace-submit-type').val() !== 'add') + action = 'workspace_update'; + + var ajax = $.ajax({ + url: window.config.BASE_URL + "/service?action=" + action, + type: 'POST', + data: { + id: id, + name: name, + content: "[]", + virtual_cluster: virtual_cluster, + permission: permission + } + }); + ajax.done(function (res) { + if (res["errno"] !== 0) { + $("#modal-msg-content").html(res["msg"]); + $("#modal-msg").modal('show'); + } + $('#table-workspace').bootstrapTable("refresh"); + + }); + ajax.fail(function (jqXHR, textStatus) { + $("#modal-msg-content").html("Request failed : " + jqXHR.statusText); + $("#modal-msg").modal('show'); + $('#table-workspace').bootstrapTable("refresh"); + }); + }); + +} + +function load_workspaces(cluster) { + $("#table-workspace").bootstrapTable({ + url: window.config.BASE_URL + '/service?action=workspace_list&who=' + cluster, + responseHandler: workspaceResponseHandler, + sidePagination: 'server', + cache: true, + striped: true, + pagination: true, + pageSize: 10, + pageList: [10, 25, 50, 100, 200], + search: false, + showColumns: true, + showRefresh: true, + showToggle: false, + showPaginationSwitch: true, + minimumCountColumns: 2, + clickToSelect: false, + sortName: 'nobody', + sortOrder: 'desc', + smartDisplay: true, + mobileResponsive: true, + showExport: true, + columns: [{ + field: 'id', + title: 'ID', + align: 'center', + valign: 'middle', + visible: false + }, { + field: 'name', + title: 'Name', + align: 'center', + valign: 'middle', + escape: true + }, { + field: 'virtual_cluster', + title: 'Virtual Cluster', + align: 'center', + valign: 'middle' + }, { + field: 'permission', + title: 'Permission', + align: 'center', + valign: 'middle' + }, { + field: 'operate', + title: 'Operate', + align: 'center', + events: workspaceOperateEvents, + formatter: workspaceOperateFormatter + }] + }); +} + +function workspaceResponseHandler(res) { + if (res['errno'] === 0) { + var tmp = {}; + tmp["total"] = res["count"]; + tmp["rows"] = res["workspaces"]; + return tmp; + } + $("#modal-msg-content").html(res["msg"]); + $("#modal-msg").modal('show'); + return []; +} + +function workspaceOperateFormatter(value, row, index) { + var div = '