...
|
...
|
@@ -3,8 +3,12 @@ package com.monitor.user.ctrl; |
|
|
import com.model.HostGroup;
|
|
|
import com.model.User;
|
|
|
import com.monitor.common.service.MailService;
|
|
|
import com.monitor.common.util.MD5Util;
|
|
|
import com.monitor.common.util.MailUtil;
|
|
|
import com.monitor.influxdb.contants.InfluxDBContants;
|
|
|
import com.monitor.influxdb.mapper.UserOperateMapper;
|
|
|
import com.monitor.influxdb.mapper.impl.CommonQuery;
|
|
|
import com.monitor.influxdb.util.QueryResultUtil;
|
|
|
import com.monitor.model.domain.Mail;
|
|
|
import com.monitor.model.domain.PageBean;
|
|
|
import com.monitor.model.domain.UserOperate;
|
...
|
...
|
@@ -13,15 +17,21 @@ import com.monitor.model.page.PageResponse; |
|
|
import com.monitor.model.request.UserOperateReq;
|
|
|
import com.monitor.model.response.BaseResponse;
|
|
|
import com.monitor.mysql.mapper.UserMapper;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.influxdb.dto.QueryResult;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Controller
|
...
|
...
|
@@ -39,19 +49,22 @@ public class UserCtrl { |
|
|
@Autowired
|
|
|
private MailService mailService;
|
|
|
|
|
|
@Autowired
|
|
|
private CommonQuery commonQuery;
|
|
|
|
|
|
@RequestMapping("/getAllUser")
|
|
|
@ResponseBody
|
|
|
public BaseResponse getAllUser() {
|
|
|
try{
|
|
|
List<User> list=userMapper.selectAll();
|
|
|
try {
|
|
|
List<User> list = userMapper.selectAll();
|
|
|
|
|
|
if (list == null || CollectionUtils.isEmpty(list)) {
|
|
|
return new BaseResponse<List<User>>();
|
|
|
}
|
|
|
|
|
|
return new BaseResponse<List<User>>(list);
|
|
|
}catch (Exception e){
|
|
|
log.error("getAllUser error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getAllUser error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -59,11 +72,11 @@ public class UserCtrl { |
|
|
@RequestMapping("/getUserByName")
|
|
|
@ResponseBody
|
|
|
public BaseResponse getUserByName(String name) {
|
|
|
try{
|
|
|
User user=userMapper.selectByName(name);
|
|
|
try {
|
|
|
User user = userMapper.selectByName(name);
|
|
|
return new BaseResponse<User>(user);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUserByName error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUserByName error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -71,11 +84,11 @@ public class UserCtrl { |
|
|
@RequestMapping("/getUserById")
|
|
|
@ResponseBody
|
|
|
public BaseResponse getUserById(int id) {
|
|
|
try{
|
|
|
User user=userMapper.selectById(id);
|
|
|
try {
|
|
|
User user = userMapper.selectById(id);
|
|
|
return new BaseResponse<User>(user);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUserById error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUserById error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -83,11 +96,17 @@ public class UserCtrl { |
|
|
@RequestMapping("/insert")
|
|
|
@ResponseBody
|
|
|
public BaseResponse insert(@RequestBody User user) {
|
|
|
try{
|
|
|
int result = userMapper.insert(user);
|
|
|
try {
|
|
|
Mail.Builder builder = new Mail.Builder("YOHO!运维平台密码");
|
|
|
builder.receiver(user.getEmail());
|
|
|
builder.message("您的密码:" + user.getPwd());
|
|
|
mailService.sendHTMLMail(builder.build());
|
|
|
String pwd = MD5Util.encryption(user.getPwd());
|
|
|
user.setPwd(pwd);
|
|
|
int result = userMapper.insert(user);
|
|
|
return new BaseResponse<Integer>(result);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUserById error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUserById error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -95,11 +114,11 @@ public class UserCtrl { |
|
|
@RequestMapping("/update")
|
|
|
@ResponseBody
|
|
|
public BaseResponse update(@RequestBody User user) {
|
|
|
try{
|
|
|
int result = userMapper.update(user);
|
|
|
try {
|
|
|
int result = userMapper.update(user);
|
|
|
return new BaseResponse<Integer>(result);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUserById error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUserById error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -107,11 +126,18 @@ public class UserCtrl { |
|
|
@RequestMapping("/updatePwd")
|
|
|
@ResponseBody
|
|
|
public BaseResponse updatePwd(@RequestBody User user) {
|
|
|
try{
|
|
|
int result = userMapper.updatePwd(user);
|
|
|
try {
|
|
|
Mail.Builder builder = new Mail.Builder("YOHO!运维平台密码");
|
|
|
builder.receiver(user.getEmail());
|
|
|
builder.message("您的密码:" + user.getPwd());
|
|
|
mailService.sendHTMLMail(builder.build());
|
|
|
String pwd = MD5Util.encryption(user.getPwd());
|
|
|
user.setPwd(pwd);
|
|
|
|
|
|
int result = userMapper.updatePwd(user);
|
|
|
return new BaseResponse<Integer>(result);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUserById error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUserById error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -119,11 +145,11 @@ public class UserCtrl { |
|
|
@RequestMapping("/deleteByName")
|
|
|
@ResponseBody
|
|
|
public BaseResponse deleteByName(String name) {
|
|
|
try{
|
|
|
int result = userMapper.deleteByName(name);
|
|
|
try {
|
|
|
int result = userMapper.deleteByName(name);
|
|
|
return new BaseResponse<Integer>(result);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUserById error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUserById error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -131,11 +157,11 @@ public class UserCtrl { |
|
|
@RequestMapping("/deleteById")
|
|
|
@ResponseBody
|
|
|
public BaseResponse deleteById(int id) {
|
|
|
try{
|
|
|
int result = userMapper.deleteById(id);
|
|
|
try {
|
|
|
int result = userMapper.deleteById(id);
|
|
|
return new BaseResponse<Integer>(result);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUserById error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUserById error", e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -143,7 +169,7 @@ public class UserCtrl { |
|
|
@RequestMapping("/getUsers")
|
|
|
@ResponseBody
|
|
|
public BaseResponse<PageResponse<User>> getUsers(@RequestBody PageRequest req) {
|
|
|
try{
|
|
|
try {
|
|
|
log.info("getUsers with param is {}", req);
|
|
|
// 组装分页对象
|
|
|
PageBean page = PageBean.initPageInfo(req.getCurrentPage(),
|
...
|
...
|
@@ -167,8 +193,8 @@ public class UserCtrl { |
|
|
response.setTotal(total);
|
|
|
response.setRows(users);
|
|
|
return new BaseResponse<PageResponse<User>>(response);
|
|
|
}catch (Exception e){
|
|
|
log.error("getUsers error",e);
|
|
|
} catch (Exception e) {
|
|
|
log.error("getUsers error", e);
|
|
|
return new BaseResponse<>(e.getMessage());
|
|
|
}
|
|
|
|
...
|
...
|
@@ -178,7 +204,7 @@ public class UserCtrl { |
|
|
@ResponseBody
|
|
|
public String operate(@RequestBody UserOperateReq userOperateReq) {
|
|
|
List<UserOperate> list = userOperateReq.getList();
|
|
|
for(UserOperate userOperate : list){
|
|
|
for (UserOperate userOperate : list) {
|
|
|
userOperateMapper.insert(userOperate);
|
|
|
}
|
|
|
return "";
|
...
|
...
|
@@ -186,7 +212,7 @@ public class UserCtrl { |
|
|
|
|
|
@RequestMapping("/getUsersByRole")
|
|
|
@ResponseBody
|
|
|
public BaseResponse<List<User>> getUsersByRole(String role) {
|
|
|
public BaseResponse<List<User>> getUsersByRole(String role) {
|
|
|
List<User> list = userMapper.selectUsersByRole(role);
|
|
|
return new BaseResponse<>(list);
|
|
|
}
|
...
|
...
|
@@ -194,19 +220,90 @@ public class UserCtrl { |
|
|
/**
|
|
|
* 测试邮件
|
|
|
*/
|
|
|
@RequestMapping("/testMail")
|
|
|
public void testMail() {
|
|
|
//@RequestMapping("/testMail")
|
|
|
public void testMail() {
|
|
|
List<User> list = userMapper.selectAll();
|
|
|
for (User user : list){
|
|
|
for (User user : list) {
|
|
|
Mail.Builder builder = new Mail.Builder("TEST 邮件");
|
|
|
builder.receiver(user.getEmail());
|
|
|
builder.message("这是一封测试邮箱是否可用的邮件,无需关注!");
|
|
|
boolean result = mailService.sendHTMLMail(builder.build());
|
|
|
if (!result){
|
|
|
log.info("error mail:" + user.getName()+"---"+user.getEmail());
|
|
|
if (!result) {
|
|
|
log.info("error mail:" + user.getName() + "---" + user.getEmail());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//@Scheduled(fixedDelay = 10*1000*60)
|
|
|
public void gatewayacess() {
|
|
|
try {
|
|
|
QueryResult queryResult = commonQuery.query(InfluxDBContants.AWS, "SHOW TAG VALUES FROM \"gateway_access\" WITH KEY = \"event\"", "yoho_event");
|
|
|
List<List<Object>> tags = QueryResultUtil.getValues(queryResult);
|
|
|
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e:/tags", true)));
|
|
|
for (List<Object> tag : tags) {
|
|
|
String sql = "SELECT count(\"ip\") FROM \"gateway_access\" WHERE \"event\" = '" + tag.get(1) + "' AND time > now() - 1d";
|
|
|
QueryResult queryResult2 = commonQuery.query(InfluxDBContants.AWS, sql, "yoho_event");
|
|
|
List<List<Object>> list2 = QueryResultUtil.getValues(queryResult2);
|
|
|
Object total = list2.get(0).get(1);
|
|
|
|
|
|
String sql2 = "SELECT count(\"ip\") FROM \"gateway_access\" WHERE \"event\" = '" + tag.get(1) + "' AND time > now() - 1d GROUP BY time(10s) fill(null)";
|
|
|
QueryResult queryResult3 = commonQuery.query(InfluxDBContants.AWS, sql2, "yoho_event");
|
|
|
List<List<Object>> list3 = QueryResultUtil.getValues(queryResult3);
|
|
|
List<countClass> countClassesss = new ArrayList<>();
|
|
|
for (List<Object> list : list3) {
|
|
|
Object time = list.get(0);
|
|
|
int count = (int) (double) list.get(1);
|
|
|
countClass countClasss = new countClass();
|
|
|
countClasss.setTime(time);
|
|
|
countClasss.setCount(count);
|
|
|
countClassesss.add(countClasss);
|
|
|
}
|
|
|
Collections.sort(countClassesss);
|
|
|
String text = StringUtils.rightPad(String.valueOf(tag.get(1)), 40) + " " + total + " " + countClassesss.get(0).getCount() + "\r\n";
|
|
|
bufferedWriter.write(text);
|
|
|
}
|
|
|
bufferedWriter.flush();
|
|
|
bufferedWriter.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
System.out.println("end");
|
|
|
|
|
|
}
|
|
|
|
|
|
public class countClass implements Comparable<countClass> {
|
|
|
private Object time;
|
|
|
|
|
|
private int count;
|
|
|
|
|
|
public Object getTime() {
|
|
|
return time;
|
|
|
}
|
|
|
|
|
|
public void setTime(Object time) {
|
|
|
this.time = time;
|
|
|
}
|
|
|
|
|
|
public int getCount() {
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
public void setCount(int count) {
|
|
|
this.count = count;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int compareTo(countClass o) {
|
|
|
if (this.count > o.count) {
|
|
|
return -1;
|
|
|
} else if (this.count == o.count) {
|
|
|
return 0;
|
|
|
} else {
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
...
|
...
|
|