Commit fb93c651 authored by Spencer Chang's avatar Spencer Chang

[fix]

parent d078dbc3
......@@ -44,7 +44,7 @@ public class SysDpExecuteHistoryController extends BaseController {
final String param = (String) requestData.get("parameter");
final String dpContext = URLDecoder.decode(new String(Base64.decodeBase64(param), StandardCharsets.UTF_8),"UTF-8");
if (ck == 0){
service.query4ck(requestContext, ip, dpContext);
service.query4Verify(requestContext, ip, dpContext);
}else if(ck == 1){
request.setAttribute("_NO_PAGE_","Y");
final String context = new String(dpContext.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
......
......@@ -3,12 +3,14 @@ package com.hand.hls.dp.core;
import com.hand.hap.core.IRequest;
import com.hand.hls.dp.service.SysDpExecuteHistoryService;
import java.util.Objects;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/8/13 09:50
* @since 1.0
* sql执行上下文
*/
public class SubExecSqlContext {
public class ExecSqlContext {
private SysDpExecuteHistoryService service;
private IRequest requestContext;
private String sql;
......@@ -63,4 +65,29 @@ public class SubExecSqlContext {
public void setSeq(Integer seq) {
this.seq = seq;
}
}
\ No newline at end of file
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExecSqlContext that = (ExecSqlContext) o;
return Objects.equals(service, that.service) && Objects.equals(requestContext, that.requestContext) && Objects.equals(sql, that.sql) && Objects.equals(ip, that.ip) && Objects.equals(execType, that.execType) && Objects.equals(seq, that.seq);
}
@Override
public int hashCode() {
return Objects.hash(service, requestContext, sql, ip, execType, seq);
}
@Override
public String toString() {
return "ExecSqlContext{" +
"service=" + service +
", requestContext=" + requestContext +
", sql='" + sql + '\'' +
", ip='" + ip + '\'' +
", execType='" + execType + '\'' +
", seq=" + seq +
'}';
}
}
......@@ -14,7 +14,7 @@ import javax.servlet.http.HttpServletResponse;
* @since
*/
public interface SysDpExecuteHistoryService extends IBaseService<SysDpExecuteHistory>, ProxySelf<SysDpExecuteHistoryService> {
void query4ck(IRequest requestContext, final String ip, final String dpContext) throws Exception;
void query4Verify(IRequest requestContext, final String ip, final String dpContext) throws Exception;
void query(IRequest requestContext, HttpServletResponse response, final String ip, final String dpContext) throws Exception;
ResponseData insert(IRequest requestContext, final String ip, final String dpContext);
ResponseData update(IRequest requestContext, final String ip, final String dpContext);
......
......@@ -2,6 +2,7 @@ package com.hand.hls.dp.service.impl;
import com.hand.hap.message.IMessageConsumer;
import com.hand.hap.message.TopicMonitor;
import com.hand.hls.dp.util.IPUtils;
import com.hand.hls.dp.util.LogUtils;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
......@@ -48,14 +49,14 @@ public class SysClusterLogFileSubscriber implements IMessageConsumer<String>, In
ZipOutputStream zos = new ZipOutputStream(cos);
// 当天日志名称
String currLogFileName = logPath + "leaf.log";
String localServerIp = IPUtils.getIpAddressLocal();
// 查找日志目录下所有文件
List<String> files = new ArrayList<>(31);
LogUtils.findFiles(new File(logPath), files);
files.forEach(file -> {
if (file.equals(currLogFileName)) {
// 创建ZipEntry
ZipEntry entry = new ZipEntry(file.substring(logPath.length()));
ZipEntry entry = new ZipEntry(localServerIp+"-"+file.substring(logPath.length()));
// 放入文件,防止文件名相同的附件报错
try {
zos.putNextEntry(entry);
......@@ -70,20 +71,21 @@ public class SysClusterLogFileSubscriber implements IMessageConsumer<String>, In
zos.closeEntry();
zos.close();
if (logger.isDebugEnabled()) {
logger.debug("redis key: [{}]", LOGFILE_KEY);
logger.debug("[{}] redis key: [{}]", localServerIp, LOGFILE_KEY);
}
redisTemplate.opsForList().rightPush(LOGFILE_KEY, Base64.encodeBase64String(byteOutPut.toByteArray()));
cos.close();
} catch (Exception e) {
logger.warn(e.getMessage());
logger.warn("[{}] 日志暂存redis 出错: [{}]", localServerIp, e.getMessage());
}
}
@Override
public void onMessage(String message, String pattern) {
if (LOGFILE_DOWNLOAD.equals(pattern)) {
String localServerIp = IPUtils.getIpAddressLocal();
if (logger.isDebugEnabled()) {
logger.debug("start do [{}]", message);
logger.debug("[{}] start do [{}]", localServerIp, message);
}
// 收集当天日志暂存redis
collectLog();
......
......@@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
......@@ -66,9 +67,9 @@ public class SysDpDownloadLogServiceImpl implements SysDpDownloadLogService {
*/
@Override
public void downloadClusterLogZipFile(IRequest requestContext, HttpServletRequest request, HttpServletResponse response) throws Exception {
LocalDate tmp = LocalDate.now();
Date date = Date.from(tmp.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
String zipFileName = "leaf-" + DateUtil.format(date, "yyyy-MM-dd") + ".zip";
LocalDateTime tmp = LocalDateTime.now();
Date date = Date.from(tmp.atZone(ZoneId.systemDefault()).toInstant());
String zipFileName = "leaf-cluster-" + DateUtil.format(date, "yyyyMMddHHmmss") + ".zip";
response.reset();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache,must-revalidate");
......@@ -103,11 +104,12 @@ public class SysDpDownloadLogServiceImpl implements SysDpDownloadLogService {
byte[] buffer = new byte[LogUtils.DEFAULT_LARGE_BUFFER_SIZE];
while (LogUtils.EOF != (n = byteArrayInputStream.read(buffer))) {
zos.write(buffer, 0, n);
zos.flush();
}
}
zos.closeEntry();
zos.close();
response.setHeader("Content-Length", String.valueOf(byteOutPut.toByteArray().length));
response.setHeader("Content-Length", String.valueOf(byteOutPut.size()));
IOUtils.copy(new ByteArrayInputStream(byteOutPut.toByteArray()), response.getOutputStream());
cos.close();
}
......
......@@ -3,9 +3,12 @@ package com.hand.hls.dp.util;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.Buffer;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
......@@ -15,8 +18,8 @@ import java.util.Objects;
public class IPUtils {
private static final String IPV4_LOCAL_HOST = "127.0.0.1";
private static final String IPV6_LOCAL_HOST = "0:0:0:0:0:0:0:1";
public static String getIpAddress(HttpServletRequest request) {
boolean ipv6 = false;
if (Objects.isNull(request)) {
return "";
}
......@@ -43,41 +46,40 @@ public class IPUtils {
if (Objects.isNull(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
// 本地局域网放行
if (Objects.equals(IPV4_LOCAL_HOST,ip) || Objects.equals(IPV6_LOCAL_HOST,ip)) {
if (Objects.equals(IPV4_LOCAL_HOST, ip) || Objects.equals(IPV6_LOCAL_HOST, ip)) {
// 根据网卡获取本机配置的ip
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
ip = inetAddress.getHostAddress();
} catch (Exception e) {
// 默认
ip = IPV4_LOCAL_HOST;
}
byte[] address = inetAddress.getAddress();
if (address.length == 16) {
// IPV6
ip = inetAddress.getHostAddress();
ipv6 = true;
} else if (address.length == 4) {
// IPV4
ip = inetAddress.getHostAddress();
} else {
//throw new Exception("Invalid IPv6 address: '" + ip + '\'');
// 默认
ip = IPV6_LOCAL_HOST;
ipv6 = true;
}
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
// "***.***.***.***".length() = 15
if (Objects.nonNull(ip) && ip.length() > 15 && !ipv6) {
if (Objects.nonNull(ip) && ip.length() > 15) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return extractString(ip);
}
// 获取本机ip
public static String getIpAddressLocal() {
String ip = IPV4_LOCAL_HOST;
// 根据网卡获取本机配置的ip
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
ip = inetAddress.getHostAddress();
} catch (Exception e) {
// ignore
}
return ip;
}
/**
* 获取访问主机的网段(4位)
......
......@@ -74,6 +74,7 @@ public class LogUtils {
buffer.get(bytes);
// 内容写入ZipOutputStream
zos.write(bytes,0,n);
zos.flush();
}
// 切换buffer写模式
buffer.clear();
......@@ -81,12 +82,12 @@ public class LogUtils {
} catch (FileNotFoundException fe){
// 文件没找到
if (logger.isErrorEnabled()) {
logger.error("{} file not found!", filePath);
logger.error("[{}] file not found!", filePath);
}
} catch (IOException e){
// io例外
if (logger.isWarnEnabled()) {
logger.warn("{} file IOException!Message:[{}]", filePath, e.getMessage());
logger.warn("[{}] file IOException!Message:[{}]", filePath, e.getMessage());
}
}
}
......
package com.hand.hls.dp.util;
import java.util.HashMap;
/**
* @author <a href="mailto:zhangnet14@gmail.com">Spencer Chang</a>
* @date 2020/9/18 16:11
* @see com.google.common.collect.Maps#newHashMapWithExpectedSize
*/
public final class Maps {
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* default expected size
*/
static final int DEFAULT_EXPECTED_SIZE = 3;
/**
* Creates a {@code HashMap} instance, with a high enough "initial capacity" that it <i>should</i>
* hold {@code expectedSize} elements without growth. This behavior cannot be broadly guaranteed,
* but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed that the method
* isn't inadvertently <i>oversizing</i> the returned map.
*
* @param expectedSize the number of entries you expect to add to the returned map
* @return a new, empty {@code HashMap} with enough capacity to hold {@code expectedSize} entries
* without resizing
* @throws IllegalArgumentException if {@code expectedSize} is negative
*/
public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) {
return new HashMap<>(capacity(expectedSize));
}
static int capacity(int expectedSize) {
if (expectedSize < DEFAULT_EXPECTED_SIZE) {
if (expectedSize < 0) {
throw new IllegalArgumentException("expectedSize cannot be negative but was: " + expectedSize);
}
return expectedSize + 1;
} else {
return expectedSize < MAXIMUM_CAPACITY ? (int) ((float) expectedSize / DEFAULT_LOAD_FACTOR + 1.0f) : Integer.MAX_VALUE;
}
}
}
\ No newline at end of file
......@@ -18,7 +18,7 @@
form.method = "post";
let url = '${/request/@context_path}/sys/dp/query?ck=1';
let token = $jq('meta[name=_csrf]').attr('content');
form.action = url + (url.indexOf('?') == -1 ? '?' : '&') + 'r=' + Math.random() + '&_csrf=' + token;
form.action = url + (url.indexOf('?') === -1 ? '?' : '&') + 'r=' + Math.random() + '&_csrf=' + token;
let iframe = Ext.get('_export_dp_query_window') || new Ext.Template('<iframe id ="_export_dp_query_window" name="_export_dp_query_window" style="position:absolute;left:-1000px;top:-1000px;width:1px;height:1px;display:none"></iframe>').insertFirst(document.body, {}, true)
var s = document.createElement("input");
s.id = "_request_data";
......@@ -112,38 +112,12 @@
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
function download_log() {
$('dp001_download_log_btn').disable();
Leaf.showConfirm('${l:HLS.PROMPT}', '确认下载日志吗?', function okFun() {
Leaf.Masker.mask(Ext.getBody(), '正在下载中...');
let form = document.createElement("form");
form.target = "_export_dp_log_window";
form.method = "post";
let url = '${/request/@context_path}/sys/dp/download/log';
let token = $jq('meta[name=_csrf]').attr('content');
form.action = url + (url.indexOf('?') == -1 ? '?' : '&') + 'r=' + Math.random() + '&_csrf=' + token;
let iframe = Ext.get('_export_dp_log_window') || new Ext.Template('<iframe id ="_export_dp_log_window" name="_export_dp_log_window" style="position:absolute;left:-1000px;top:-1000px;width:1px;height:1px;display:none"></iframe>').insertFirst(document.body, {}, true)
document.body.appendChild(form);
form.submit();
Ext.fly(form).remove();
setTimeout(function () {
$('dp001_download_log_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 10000);
}, function cancel() {
$('dp001_download_log_btn').enable();
Leaf.Masker.unmask(Ext.getBody());
}, 200, 100);
}
]]></script>
<a:screenBody>
<a:screenTopToolbar>
<a:gridButton id="dp001_query_btn" click="dp001_query" text="查询"/>
<a:gridButton id="dp001_exec_btn" click="dp001_exec" text="执行"/>
<a:gridButton id="dp001_download_cluster_log_btn" click="download_cluster_log" text="日志下载(当天集群环境日志)"/>
<a:gridButton id="dp001_download_log_btn" click="download_log" text="日志下载(7天当前请求环境日志)"/>
</a:screenTopToolbar>
<a:fieldSet title="待执行">
<a:textArea id="context_tta" name="context" marginWidth="50" marginHeight="450"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment