add:日志清除按钮

This commit is contained in:
zhangzhiqiang
2023-03-01 14:36:30 +08:00
parent 0e177b00a7
commit 83424bb995
5 changed files with 94 additions and 1 deletions

View File

@@ -34,5 +34,11 @@ public class EsLogController {
public ResponseEntity<Object> queryAll(@RequestBody LogQuery query) { public ResponseEntity<Object> queryAll(@RequestBody LogQuery query) {
return new ResponseEntity<>(esLogService.query(query), HttpStatus.OK); return new ResponseEntity<>(esLogService.query(query), HttpStatus.OK);
} }
@DeleteMapping("/clearLogs")
@ApiOperation("清空日志")
public ResponseEntity<Object> clearLogs(@RequestBody LogQuery query) {
esLogService.clearLogs(query);
return new ResponseEntity<>(HttpStatus.OK);
}
} }

View File

@@ -12,6 +12,11 @@ import org.nl.modules.logging.service.dto.LogQuery;
* @desc desc * @desc desc
*/ */
public interface EsLogService { public interface EsLogService {
/**
* 清空日志
*/
void clearLogs(LogQuery query);
/** /**
* 获取labels和values树 * 获取labels和values树
* @return * @return

View File

@@ -1,5 +1,7 @@
package org.nl.modules.logging.service.impl; package org.nl.modules.logging.service.impl;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
@@ -27,6 +29,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter; import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -99,6 +102,20 @@ public class EsLogServiceImpl implements EsLogService {
} }
} }
@Override
public void clearLogs(LogQuery logQuery) {
String system = logQuery.getSystem();
BoolQueryBuilder query = QueryBuilders.boolQuery();
if (!StringUtils.isEmpty(system)){
query.must().add(QueryBuilders.matchQuery("system", system));
}
long time = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -10).getTime();
String script = "doc['@timestamp'].value.millis < " + time + "L";
query.must().add(QueryBuilders.scriptQuery(new Script(script)));
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(query);
elasticsearchRestTemplate.delete(deleteQuery,new LogRepositoryDTO().getClass());
}
@Override @Override

View File

@@ -15,4 +15,12 @@ export function labelsValues(type) {
}) })
} }
export default { getLogData, labelsValues } export function clearLogs(param) {
return request({
url: '/api/esLog/clearLogs',
method: 'delete',
data: param
})
}
export default { getLogData, labelsValues, clearLogs }

View File

@@ -118,6 +118,14 @@
:step="1" :step="1"
/> />
</el-form-item> </el-form-item>
<el-form-item label="总条数">
<el-input
v-model="total"
size="small"
disabled
style="width: 110px"
/>
</el-form-item>
<el-form-item> <el-form-item>
<el-dropdown split-button type="primary" size="mini" @click="queryData"> <el-dropdown split-button type="primary" size="mini" @click="queryData">
查询 查询
@@ -126,6 +134,9 @@
</el-dropdown-menu> </el-dropdown-menu>
</el-dropdown> </el-dropdown>
</el-form-item> </el-form-item>
<el-form-item>
<el-button @click="clearLogs">清空日志</el-button>
</el-form-item>
</el-form> </el-form>
</div> </div>
<!-- <div style="margin: 3px; min-height: 80vh;"> <!-- <div style="margin: 3px; min-height: 80vh;">
@@ -180,6 +191,7 @@ let queryParam = {
filterSql: true, filterSql: true,
isRequest: true, isRequest: true,
page: null, page: null,
total: 0,
size: 20 size: 20
} }
export default { export default {
@@ -308,6 +320,7 @@ export default {
logOperation.getLogData(queryParam).then(res => { logOperation.getLogData(queryParam).then(res => {
this.showEmpty = false this.showEmpty = false
this.total = res.total
// 清空 // 清空
this.logs = [] this.logs = []
for (const j in res.records) { // 用push的方式将所有日志数组添加进去 for (const j in res.records) { // 用push的方式将所有日志数组添加进去
@@ -315,6 +328,50 @@ export default {
} }
}) })
}, },
clearLogs() {
var message = '您确定要清空'
if (this.system !== '' && this.system !== null) {
message += '标签为:' + this.system + ' '
}
message += '10天前的日志吗 ?'
this.$confirm(message, '确认信息', {
distinguishCancelAndClose: true,
confirmButtonText: '确定',
cancelButtonText: '取消'
})
.then(() => {
// 清空查询数据
this.clearParam()
queryParam.logLevel = this.logLevelValue
const time = new Date()
if (this.timeZoneValue !== '') {
queryParam.startTime = new Date(((time.getTime() - this.timeZoneValue)))
}
if (this.timeRange !== '' && this.timeRange.length > 0) {
queryParam.startTime = this.timeRange[0]
queryParam.endTime = this.timeRange[1]
}
queryParam.message = this.message.replace(/^\s*|\s*$/g, '')
queryParam.traceId = this.traceId
queryParam.system = this.system
logOperation.clearLogs(queryParam).then(res => {
this.queryData()
this.$message({
type: 'info',
message: '清除成功'
})
})
})
.catch(action => {
this.$message({
type: 'info',
message: action === 'cancel'
? '已取消'
: '已取消'
})
})
},
changetype() { changetype() {
}, },