ssh 执行记录添加工作空间ID

This commit is contained in:
bwcx_jzy 2021-12-04 23:52:21 +08:00
parent 898971c46b
commit a529135452
9 changed files with 104 additions and 25 deletions

View File

@ -22,9 +22,6 @@
*/
package io.jpom.common;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
@ -117,18 +114,7 @@ public abstract class BaseServerController extends BaseJpomController {
page.addOrder(new Order(colName, Direction.DESC));
// 时间
if (StrUtil.isNotEmpty(time)) {
String[] val = StrUtil.splitToArray(time, "~");
if (val.length == 2) {
DateTime startDateTime = DateUtil.parse(val[0], DatePattern.NORM_DATETIME_FORMAT);
entity.set(colName, ">= " + startDateTime.getTime());
DateTime endDateTime = DateUtil.parse(val[1], DatePattern.NORM_DATETIME_FORMAT);
if (startDateTime.equals(endDateTime)) {
endDateTime = DateUtil.endOfDay(endDateTime);
}
// 防止字段重复
entity.set(colName + " ", "<= " + endDateTime.getTime());
}
}
}
}

View File

@ -24,7 +24,7 @@ package io.jpom.model.log;
import cn.hutool.core.util.StrUtil;
import io.jpom.JpomApplication;
import io.jpom.model.BaseDbModel;
import io.jpom.model.BaseWorkspaceModel;
import io.jpom.model.data.UserModel;
import io.jpom.service.h2db.TableName;
@ -35,7 +35,7 @@ import io.jpom.service.h2db.TableName;
* @date 2021/08/04
*/
@TableName("SSHTERMINALEXECUTELOG")
public class SshTerminalExecuteLog extends BaseDbModel {
public class SshTerminalExecuteLog extends BaseWorkspaceModel {
/**
* 操作ip
*/

View File

@ -27,7 +27,7 @@ import cn.hutool.core.util.StrUtil;
import io.jpom.model.data.SshModel;
import io.jpom.model.data.UserModel;
import io.jpom.model.log.SshTerminalExecuteLog;
import io.jpom.service.h2db.BaseDbService;
import io.jpom.service.h2db.BaseWorkspaceService;
import org.springframework.stereotype.Service;
import java.util.List;
@ -41,7 +41,7 @@ import java.util.stream.Collectors;
* @date 2021/08/04
*/
@Service
public class SshTerminalExecuteLogService extends BaseDbService<SshTerminalExecuteLog> {
public class SshTerminalExecuteLogService extends BaseWorkspaceService<SshTerminalExecuteLog> {
/**
* 批量记录日志
@ -61,6 +61,7 @@ public class SshTerminalExecuteLogService extends BaseDbService<SshTerminalExecu
if (sshItem != null) {
sshTerminalExecuteLog.setSshId(sshItem.getId());
sshTerminalExecuteLog.setSshName(sshItem.getName());
sshTerminalExecuteLog.setWorkspaceId(sshItem.getWorkspaceId());
}
sshTerminalExecuteLog.setCommands(s);
sshTerminalExecuteLog.setRefuse(refuse);

View File

@ -478,6 +478,11 @@ public abstract class BaseDbCommonService<T> {
* @return list
*/
public List<Entity> query(String sql, Object... params) {
if (!DbConfig.getInstance().isInit()) {
// ignore
DefaultSystemLog.getLog().error("The database is not initialized, this execution will be ignored");
return null;
}
try {
return Db.use().query(sql, params);
} catch (SQLException e) {
@ -485,6 +490,26 @@ public abstract class BaseDbCommonService<T> {
}
}
/**
* sql 执行
*
* @param sql sql 语句
* @param params 参数
* @return list
*/
public int execute(String sql, Object... params) {
if (!DbConfig.getInstance().isInit()) {
// ignore
DefaultSystemLog.getLog().error("The database is not initialized, this execution will be ignored");
return 0;
}
try {
return Db.use().execute(sql, params);
} catch (SQLException e) {
throw new JpomRuntimeException("数据库异常", e);
}
}
/**
* sql 查询 list

View File

@ -23,6 +23,9 @@
package io.jpom.service.h2db;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.SystemClock;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.*;
@ -205,6 +208,19 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
where.setIgnoreNull(StrUtil.format("`{}`", key), StrUtil.format(" like '{}%'", value));
} else if (StrUtil.startWith(stringStringEntry.getKey(), "%")) {
where.setIgnoreNull(StrUtil.format("`{}`", key), StrUtil.format(" like '%{}'", value));
} else if (StrUtil.containsIgnoreCase(key, "time") && StrUtil.contains(value, "~")) {
String[] val = StrUtil.splitToArray(value, "~");
if (val.length == 2) {
DateTime startDateTime = DateUtil.parse(val[0], DatePattern.NORM_DATETIME_FORMAT);
where.set(key, ">= " + startDateTime.getTime());
DateTime endDateTime = DateUtil.parse(val[1], DatePattern.NORM_DATETIME_FORMAT);
if (startDateTime.equals(endDateTime)) {
endDateTime = DateUtil.endOfDay(endDateTime);
}
// 防止字段重复
where.set(key + " ", "<= " + endDateTime.getTime());
}
} else {
where.setIgnoreNull(StrUtil.format("`{}`", key), value);
}

View File

@ -1,10 +1,17 @@
package io.jpom.service.system;
import cn.hutool.core.util.ClassUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.spring.SpringUtil;
import io.jpom.model.BaseWorkspaceModel;
import io.jpom.model.data.WorkspaceModel;
import io.jpom.service.h2db.BaseDbService;
import io.jpom.service.h2db.TableName;
import io.jpom.service.node.NodeService;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* @author bwcx_jzy
* @since 2021/12/3
@ -27,4 +34,23 @@ public class WorkspaceService extends BaseDbService<WorkspaceModel> {
super.insert(defaultWorkspace);
DefaultSystemLog.getLog().info("init created default workspace");
}
/**
* 将没有工作空间ID 的数据添加默认值
*/
public void convertNullWorkspaceId() {
Set<Class<?>> classes = ClassUtil.scanPackage("io.jpom.model", BaseWorkspaceModel.class::isAssignableFrom);
for (Class<?> aClass : classes) {
TableName tableName = aClass.getAnnotation(TableName.class);
if (tableName == null) {
continue;
}
String sql = "update " + tableName.value() + " set workspaceId=? where workspaceId is null";
NodeService nodeService = SpringUtil.getBean(NodeService.class);
int execute = nodeService.execute(sql, WorkspaceModel.DEFAULT_ID);
DefaultSystemLog.getLog().info("convertNullWorkspaceId {} {}", tableName.value(), execute);
}
}
}

View File

@ -158,6 +158,8 @@ public class InitDb implements DisposableBean, InitializingBean {
//
instance.loadNodeInfo();
instance.loadSshInfo();
//
workspaceService.convertNullWorkspaceId();
}
@Override

View File

@ -132,3 +132,8 @@ ALTER TABLE SSHTERMINALEXECUTELOG
-- @author jzy
ALTER TABLE BUILDHISTORYLOG
ADD IF NOT EXISTS BUILDNAME VARCHAR(100) COMMENT '构建名称';
-- @author bwcx_jzy
ALTER TABLE SSHTERMINALEXECUTELOG
ADD IF NOT EXISTS workspaceId VARCHAR(50) comment '所属工作空间';

View File

@ -7,7 +7,7 @@
<a-button type="primary" @click="handleAdd">新增</a-button>
</div>
<!-- 数据表格 -->
<a-table :data-source="list" :loading="loading" :columns="columns" :pagination="this.pagination" bordered :rowKey="(record, index) => index">
<a-table :data-source="list" :loading="loading" :columns="columns" :pagination="this.pagination" @change="changePage" bordered :rowKey="(record, index) => index">
<template slot="nodeId" slot-scope="text, record">
<a-button v-if="!record.nodeModel" type="primary" @click="install(record)" :disabled="record.installed">安装节点</a-button>
<a-tooltip v-else placement="topLeft" :title="`${record.nodeModel.id} ( ${record.nodeModel.name} )`">
@ -175,9 +175,12 @@
<!-- 操作日志 -->
<a-modal v-model="viewOperationLog" title="操作日志" width="80vw" :footer="null" :maskClosable="false">
<div ref="filter" class="filter">
<a-range-picker class="filter-item" :show-time="{ format: 'HH:mm:ss' }" format="YYYY-MM-DD HH:mm:ss" @change="onchangeListLogTime" />
<a-input class="search-input-item" v-model="viewOperationLogListQuery['userId']" placeholder="操作人" />
<a-input class="search-input-item" v-model="viewOperationLogListQuery['name']" placeholder="ssh name" />
<a-input class="search-input-item" v-model="viewOperationLogListQuery['ip']" placeholder="ip" />
<a-input class="search-input-item" v-model="viewOperationLogListQuery['%commands%']" placeholder="执行命令" />
<a-range-picker class="filter-item search-input-item" :show-time="{ format: 'HH:mm:ss' }" format="YYYY-MM-DD HH:mm:ss" @change="onchangeListLogTime" />
<a-button type="primary" @click="handleListLog">搜索</a-button>
<a-button type="primary" @click="handleListLog">刷新</a-button>
</div>
<!-- 数据表格 -->
<a-table
@ -263,7 +266,7 @@ export default {
{
title: "操作时间",
dataIndex: "optTime",
dataIndex: "createTimeMillis",
sorter: true,
customRender: (text) => {
return parseTime(text);
@ -274,6 +277,7 @@ export default {
viewOperationLogListQuery: {
page: 1,
limit: 10,
total: 0,
},
columns: [
{ title: "名称", dataIndex: "name", sorter: true, ellipsis: true },
@ -459,17 +463,21 @@ export default {
this.viewOperationLoading = false;
});
},
changeListLog(page) {
changeListLog(page, f, sorter) {
this.viewOperationLogListQuery.page = page.current;
this.viewOperationLogListQuery.limit = page.pageSize;
if (sorter) {
this.viewOperationLogListQuery.order = sorter.order;
this.viewOperationLogListQuery.order_field = sorter.field;
}
this.handleListLog();
},
//
onchangeListLogTime(value, dateString) {
if (dateString[0]) {
this.viewOperationLogListQuery.time = `${dateString[0]} ~ ${dateString[1]}`;
this.viewOperationLogListQuery.createTimeMillis = `${dateString[0]} ~ ${dateString[1]}`;
} else {
this.viewOperationLogListQuery.time = "";
this.viewOperationLogListQuery.createTimeMillis = "";
}
},
//
@ -570,6 +578,16 @@ export default {
});
});
},
//
changePage(pagination, filters, sorter) {
this.listQuery.page = pagination.current;
this.listQuery.limit = pagination.pageSize;
if (sorter) {
this.listQuery.order = sorter.order;
this.listQuery.order_field = sorter.field;
}
this.loadData();
},
//
onClose() {
this.drawerVisible = false;