diff options
| author | tanghao <admin@LAPTOP-QCSKVLI9> | 2020-11-20 11:58:07 +0600 |
|---|---|---|
| committer | tanghao <admin@LAPTOP-QCSKVLI9> | 2020-11-20 11:58:07 +0600 |
| commit | 11f5117e7ec6cb802aece363db97c06629282bdd (patch) | |
| tree | 931d01e42f47415895d4d3cf51f7af41c14a82cd | |
| parent | 1281d53e49a3026ac6feb233d581003b60f0ff88 (diff) | |
feat: endpoint任务只下发给分中心启用且在库的设备 trap时间改为utc时间
| -rw-r--r-- | src/main/java/com/nis/job/ConfagentJob.java | 2 | ||||
| -rw-r--r-- | src/main/java/com/nis/server/SNMPTrapServer.java | 3 | ||||
| -rw-r--r-- | src/main/java/com/nis/util/Constant.java | 7 | ||||
| -rw-r--r-- | src/main/java/com/nis/util/DateUtil.java | 455 | ||||
| -rw-r--r-- | src/main/java/com/nis/util/ToolUtil.java | 718 | ||||
| -rw-r--r-- | src/main/resources/mapper/EndpointDao.xml | 2 | ||||
| -rw-r--r-- | src/main/resources/mapper/PromserverDao.xml | 2 |
7 files changed, 1185 insertions, 4 deletions
diff --git a/src/main/java/com/nis/job/ConfagentJob.java b/src/main/java/com/nis/job/ConfagentJob.java index 6075122..51dcb0c 100644 --- a/src/main/java/com/nis/job/ConfagentJob.java +++ b/src/main/java/com/nis/job/ConfagentJob.java @@ -179,8 +179,10 @@ public class ConfagentJob extends QuartzJobBean { // prometheus全局配置修改 热加载 try { String scrapeInterval = sysConfigService.queryValueByParamkey(Constant.SCRAPE_INTERVAL); + String scrapeTimeout = sysConfigService.queryValueByParamkey(Constant.SCRAPE_TIMEOUT); Map param = new HashMap(); param.put(Constant.SCRAPE_INTERVAL, scrapeInterval); + param.put(Constant.SCRAPE_TIMEOUT, scrapeTimeout); boolean prometheusSettingHandle = YamlUtil.prometheusSettingHandle(param, ymlPath); logger.info("prometheusSettingHandle result {}", JSON.toJSON(prometheusSettingHandle)); if (prometheusSettingHandle) { diff --git a/src/main/java/com/nis/server/SNMPTrapServer.java b/src/main/java/com/nis/server/SNMPTrapServer.java index 4bfcad8..73199e5 100644 --- a/src/main/java/com/nis/server/SNMPTrapServer.java +++ b/src/main/java/com/nis/server/SNMPTrapServer.java @@ -72,6 +72,7 @@ import com.nis.entity.SysConfig; import com.nis.entity.TrapMessage;
import com.nis.service.AlertRuleService;
import com.nis.service.SysConfigService;
+import com.nis.util.DateUtil;
@@ -324,7 +325,7 @@ public class SNMPTrapServer implements CommandResponder,ApplicationRunner{// imp summary.append(trapmessageinfo.getTrapOID()+":"+value+")");
boolean snmptrap=true;
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
- String startsAt = sdf.format(new Date());
+ String startsAt = sdf.format(DateUtil.getUTCTimeByConfigTimeZone());
JSONObject labels =new JSONObject();
labels.put("alertname", alertName.toString());
diff --git a/src/main/java/com/nis/util/Constant.java b/src/main/java/com/nis/util/Constant.java index f86f93c..ffa4ec7 100644 --- a/src/main/java/com/nis/util/Constant.java +++ b/src/main/java/com/nis/util/Constant.java @@ -56,7 +56,12 @@ public class Constant { * prometheus全局抓取数据时间 */ public static final String SCRAPE_INTERVAL="scrape_interval"; - + + /** + * prometheus全局抓取数据超时时间 + */ + public static final String SCRAPE_TIMEOUT="scrape_timeout"; + /** * 告警评估时长 */ diff --git a/src/main/java/com/nis/util/DateUtil.java b/src/main/java/com/nis/util/DateUtil.java new file mode 100644 index 0000000..45b1cb7 --- /dev/null +++ b/src/main/java/com/nis/util/DateUtil.java @@ -0,0 +1,455 @@ +/** + * Copyright (c) 2015-2016, Chill Zhuang 庄骞 ([email protected]). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.nis.util; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.time.DateFormatUtils; + +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +public class DateUtil { + + /** + * 配置时区 默认为0 + */ + public static int configTimeZone = 0; + + private static final Object lock = new Object(); + + private static final Map<String, ThreadLocal<SimpleDateFormat>> pool = new HashMap<String, ThreadLocal<SimpleDateFormat>>(); + /** + * 获取YYYY格式 + * + * @return + */ + public static String getYear() { + return formatDate(new Date(), "yyyy"); + } + + /** + * 获取YYYY格式 + * + * @return + */ + public static String getYear(Date date) { + return formatDate(date, "yyyy"); + } + + /** + * 获取YYYY-MM-DD格式 + * + * @return + */ + public static String getDay() { + return formatDate(new Date(), "yyyy-MM-dd"); + } + + /** + * 获取YYYY-MM-DD格式 + * + * @return + */ + public static String getDay(Date date) { + return formatDate(date, "yyyy-MM-dd"); + } + + /** + * 获取YYYYMMDD格式 + * + * @return + */ + public static String getDays() { + return formatDate(new Date(), "yyyyMMdd"); + } + + /** + * 获取YYYYMMDD格式 + * + * @return + */ + public static String getDays(Date date) { + return formatDate(date, "yyyyMMdd"); + } + + /** + * 获取YYYY-MM-DD HH:mm:ss格式 + * + * @return + */ + public static String getTime() { + return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); + } + + /** + * 获取YYYY-MM-DD HH:mm:ss.SSS格式 + * + * @return + */ + public static String getMsTime() { + return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"); + } + + /** + * 获取YYYYMMDDHHmmss格式 + * + * @return + */ + public static String getAllTime() { + return formatDate(new Date(), "yyyyMMddHHmmss"); + } + + /** + * 获取YYYYMMDDHHmmss格式 + * + * @return + */ + public static String getNowTime() { + return formatDate(new Date(), "yyyyMMddHHmmsss"); + } + + /** + * 获取YYYY-MM-DD HH:mm:ss格式 + * + * @return + */ + public static String getTime(Date date) { + return formatDate(date, "yyyy-MM-dd HH:mm:ss"); + } + + public static String formatDate(Date date, String pattern) { + String formatDate = null; + if (StringUtils.isNotEmpty(pattern)) { + formatDate = DateFormatUtils.format(date, pattern); + } else { + formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); + } + return formatDate; + } + + /** + * @Title: compareDate + * @Description:(日期比较,如果s>=e 返回true 否则返回false) + * @param s + * @param e + * @return boolean + * @throws + * @author luguosui + */ + public static boolean compareDate(String s, String e) { + if (parseDate(s) == null || parseDate(e) == null) { + return false; + } + return parseDate(s).getTime() >= parseDate(e).getTime(); + } + + /** + * 格式化日期 + * + * @return + */ + public static Date parseDate(String date) { + return parse(date,"yyyy-MM-dd"); + } + + /** + * 格式化日期 + * + * @return + */ + public static Date parseTime(String date) { + return parse(date,"yyyy-MM-dd HH:mm:ss"); + } + + /** + * @Title: compareDate + * @Description:(日期比较,如果s>=e 返回true 否则返回false) + * @param s + * @param e + * @param pattern 格式 + * @return boolean + * @throws + * @author luguosui + */ + public static boolean compareDatePattern(String s, String e,String pattern) { + if (parse(s,pattern) == null || parse(e,pattern) == null) { + return false; + } + return parse(s,pattern).getTime() >= parse(e,pattern).getTime(); + } + + + /** + * @Title: compareDate + * @Description:(日期比较,如果s>e 返回true 否则返回false) + * @param s + * @param e + * @param pattern 格式 + * @return boolean + * @throws + * @author luguosui + */ + public static boolean compareDate(String s, String e,String pattern) { + if (parse(s,pattern) == null || parse(e,pattern) == null) { + return false; + } + return parse(s,pattern).getTime() > parse(e,pattern).getTime(); + } + + + public static boolean compareDated(String s, String e,String pattern) { + if (parse(s,pattern) == null || parse(e,pattern) == null) { + return false; + } + return parse(s,pattern).getTime() >= parse(e,pattern).getTime(); + } + + + /** + * 格式化日期 + * + * @return + */ + public static Date parse(String date, String pattern) { + if (ToolUtil.isNotEmpty(date)) { + if (ToolUtil.isEmpty(pattern)) { + return null; + } + DateFormat format = getDFormat(pattern); + format.setLenient(false); + try { + return format.parse(date); + } catch (ParseException e) { + throw new RuntimeException(e.getMessage()); + } + } + return null; + } + + public static SimpleDateFormat getDFormat(String pattern) { + ThreadLocal<SimpleDateFormat> tl = pool.get(pattern); + if (tl == null) { + synchronized (lock) { + tl = pool.get(pattern); + if (tl == null) { + final String p = pattern; + tl = new ThreadLocal<SimpleDateFormat>() { + @Override + protected synchronized SimpleDateFormat initialValue() { + return new SimpleDateFormat(p); + } + }; + pool.put(p, tl); + } + } + } + return tl.get(); + } + + /** + * 格式化日期 + * + * @return + */ + public static String format(Date date, String pattern) { + return DateFormatUtils.format(date, pattern); + } + + /** + * 把日期转换为Timestamp + * + * @param date + * @return + */ + public static Timestamp format(Date date) { + return new Timestamp(date.getTime()); + } + + /** + * 校验日期是否合法 + * + * @return + */ + public static boolean isValidDate(String s) { + return parse(s, "yyyy-MM-dd HH:mm:ss") != null; + } + + + + + public static int getDiffYear(String startTime, String endTime) { + DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); + try { + int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse( + startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365); + return years; + } catch (Exception e) { + // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 + return 0; + } + } + + /** + * <li>功能描述:时间相减得到天数 + * + * @param beginDateStr + * @param endDateStr + * @return long + * @author Administrator + */ + public static long getDaySub(String beginDateStr, String endDateStr) { + long day = 0; + SimpleDateFormat format = new SimpleDateFormat( + "yyyy-MM-dd"); + Date beginDate = null; + Date endDate = null; + + try { + beginDate = format.parse(beginDateStr); + endDate = format.parse(endDateStr); + } catch (ParseException e) { + e.printStackTrace(); + } + day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000); + // System.out.println("相隔的天数="+day); + + return day; + } + + /** + * 得到n天之后的日期 + * + * @param days + * @return + */ + public static String getAfterDayDate(String days) { + int daysInt = Integer.parseInt(days); + + Calendar canlendar = Calendar.getInstance(); // java.util包 + canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动 + Date date = canlendar.getTime(); + + SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String dateStr = sdfd.format(date); + + return dateStr; + } + + /** + * 得到n天之后是周几 + * + * @param days + * @return + */ + public static String getAfterDayWeek(String days) { + int daysInt = Integer.parseInt(days); + + Calendar canlendar = Calendar.getInstance(); // java.util包 + canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动 + Date date = canlendar.getTime(); + + SimpleDateFormat sdf = new SimpleDateFormat("E"); + String dateStr = sdf.format(date); + + return dateStr; + } + public static String addtime(String timeStr,String addnumber){ + String str=null; + try{ + DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date date = df.parse(timeStr); + Calendar gc =new GregorianCalendar(); + gc.setTime(date); + gc.add(GregorianCalendar.MINUTE,Integer.parseInt(addnumber)); + str=df.format(gc.getTime()); + }catch (Exception e) { + } + return str; + } + + + /** + * 验证一个日期字符串,是否符合格式 + * + * @param simpleDateFormat + * @param dates + */ + public static void validDate(SimpleDateFormat simpleDateFormat, String... dates) { + try { + for (String date : dates) { + simpleDateFormat.setLenient(false); + simpleDateFormat.parse(date); + } + } catch (Exception ex) { + throw new RuntimeException(ex.getMessage()); + } + } + /** + * 验证一个日期字符串,是否符合格式 + * + * @param simpleDateFormat + * @param dates + */ + public static void validDate(SimpleDateFormat simpleDateFormat, List<String> dates) { + try { + for (String date : dates) { + simpleDateFormat.setLenient(false); + simpleDateFormat.parse(date); + } + } catch (Exception ex) { + throw new RuntimeException(ex.getMessage()); + } + } + + /** + * 获取配置时区 + * + * @return + */ + public static TimeZone getConfigTimeZone(int configTimeZone) { + int newTime = configTimeZone * 60 * 60 * 1000; + TimeZone timeZone; + String[] ids = TimeZone.getAvailableIDs(newTime); + if (ids.length == 0) { + timeZone = TimeZone.getDefault(); + } else { + timeZone = new SimpleTimeZone(newTime, ids[0]); + } + return timeZone; + } + + /** + * 根据sysConfig表中配置的时区获取当前UTC时间 + * + * @return + */ + public static Date getUTCTimeByConfigTimeZone() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sdf.setTimeZone(DateUtil.getConfigTimeZone(0)); + String format = sdf.format(new Date()); + Date parse = null; + try { + sdf.setTimeZone(TimeZone.getDefault()); + parse = sdf.parse(format); + } catch (ParseException e) { + e.printStackTrace(); + } + return parse; + } +} diff --git a/src/main/java/com/nis/util/ToolUtil.java b/src/main/java/com/nis/util/ToolUtil.java new file mode 100644 index 0000000..d5f36f8 --- /dev/null +++ b/src/main/java/com/nis/util/ToolUtil.java @@ -0,0 +1,718 @@ +/** + * Copyright (c) 2015-2016, Chill Zhuang 庄骞 ([email protected]). + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.nis.util; + + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.annotation.Annotation; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.math.BigDecimal; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.util.*; +import java.util.Map.Entry; +import java.util.regex.Pattern; + +/** + * 高频方法集合类 + */ +public class ToolUtil { + + + /** + * 字符连接符 + */ + private static final char SEPARATOR = '_'; + /** + * 默认编码 + */ + private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + + private static Pattern humpPattern = Pattern.compile("[A-Z]"); + + + + + + /** + * 下划线命名 转 驼峰命名 首字母小写 + * + * @param s the s + * @return String toCamelCase("hello_world") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld" toUnderScoreCase("helloWorld") = "hello_world" + */ + public static String toCamelCase(String s) { + if (s == null) { + return null; + } + + String ls = s.toLowerCase(); + + StringBuilder sb = new StringBuilder(ls.length()); + boolean upperCase = false; + for (int i = 0; i < ls.length(); i++) { + char c = ls.charAt(i); + + if (c == SEPARATOR) { + upperCase = true; + } else if (upperCase) { + sb.append(Character.toUpperCase(c)); + upperCase = false; + } else { + sb.append(c); + } + } + + return sb.toString(); + } + + /** + * 下划线命名 转 驼峰命名 首字母大写 + * + * @param s the s + * @return String toCamelCase("hello_world") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld" toUnderScoreCase("helloWorld") = "hello_world" + */ + public static String toCapitalizeCamelCase(String s) { + if (s == null) { + return null; + } + String cs = toCamelCase(s); + return cs.substring(0, 1).toUpperCase() + cs.substring(1); + } + + /** + * 驼峰命名 转 下划线命名 + * + * @param s the s + * @return String toCamelCase("hello_world") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld" toUnderScoreCase("helloWorld") = "hello_world" + */ + public static String toUnderScoreCase(String s) { + if (s == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + boolean upperCase = false; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + boolean nextUpperCase = true; + + if (i < (s.length() - 1)) { + nextUpperCase = Character.isUpperCase(s.charAt(i + 1)); + } + + if ((i > 0) && Character.isUpperCase(c)) { + if (!upperCase || !nextUpperCase) { + sb.append(SEPARATOR); + } + upperCase = true; + } else { + upperCase = false; + } + + sb.append(Character.toLowerCase(c)); + } + + return sb.toString(); + } + + /** + * 获取随机位数的字符串 + * + * @author fengshuonan + * @Date 2017/8/24 14:09 + */ + public static String getRandomString(int length) { + String base = "abcdefghijklmnopqrstuvwxyz0123456789"; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < length; i++) { + int number = random.nextInt(base.length()); + sb.append(base.charAt(number)); + } + return sb.toString(); + } + + /** + * 判断一个对象是否是时间类型 + * + * @author stylefeng + * @Date 2017/4/18 12:55 + */ + public static String dateType(Object o) { + if (o instanceof Date) { + return DateUtil.getDay((Date) o); + } else { + return o.toString(); + } + } + + /** + * 获取异常的具体信息 + * + * @author fengshuonan + * @Date 2017/3/30 9:21 + * @version 2.0 + */ + public static String getExceptionMsg(Exception e) { + StringWriter sw = new StringWriter(); + try { + e.printStackTrace(new PrintWriter(sw)); + } finally { + try { + sw.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + return sw.getBuffer().toString().replaceAll("\\$", "T"); + } + + /** + * 比较两个对象是否相等。<br> + * 相同的条件有两个,满足其一即可:<br> + * 1. obj1 == null && obj2 == null; 2. obj1.equals(obj2) + * + * @param obj1 对象1 + * @param obj2 对象2 + * @return 是否相等 + */ + public static boolean equals(Object obj1, Object obj2) { + return (obj1 != null) ? (obj1.equals(obj2)) : (obj2 == null); + } + + /** + * 计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,其他可遍历对象遍历计算长度 + * + * @param obj 被计算长度的对象 + * @return 长度 + */ + public static int length(Object obj) { + if (obj == null) { + return 0; + } + if (obj instanceof CharSequence) { + return ((CharSequence) obj).length(); + } + if (obj instanceof Collection) { + return ((Collection<?>) obj).size(); + } + if (obj instanceof Map) { + return ((Map<?, ?>) obj).size(); + } + + int count; + if (obj instanceof Iterator) { + Iterator<?> iter = (Iterator<?>) obj; + count = 0; + while (iter.hasNext()) { + count++; + iter.next(); + } + return count; + } + if (obj instanceof Enumeration) { + Enumeration<?> enumeration = (Enumeration<?>) obj; + count = 0; + while (enumeration.hasMoreElements()) { + count++; + enumeration.nextElement(); + } + return count; + } + if (obj.getClass().isArray() == true) { + return Array.getLength(obj); + } + return -1; + } + + /** + * 对象中是否包含元素 + * + * @param obj 对象 + * @param element 元素 + * @return 是否包含 + */ + public static boolean contains(Object obj, Object element) { + if (obj == null) { + return false; + } + if (obj instanceof String) { + if (element == null) { + return false; + } + return ((String) obj).contains(element.toString()); + } + if (obj instanceof Collection) { + return ((Collection<?>) obj).contains(element); + } + if (obj instanceof Map) { + return ((Map<?, ?>) obj).values().contains(element); + } + + if (obj instanceof Iterator) { + Iterator<?> iter = (Iterator<?>) obj; + while (iter.hasNext()) { + Object o = iter.next(); + if (equals(o, element)) { + return true; + } + } + return false; + } + if (obj instanceof Enumeration) { + Enumeration<?> enumeration = (Enumeration<?>) obj; + while (enumeration.hasMoreElements()) { + Object o = enumeration.nextElement(); + if (equals(o, element)) { + return true; + } + } + return false; + } + if (obj.getClass().isArray() == true) { + int len = Array.getLength(obj); + for (int i = 0; i < len; i++) { + Object o = Array.get(obj, i); + if (equals(o, element)) { + return true; + } + } + } + return false; + } + + /** + * 对象是否不为空(新增) + * + * @param o String,List,Map,Object[],int[],long[] + * @return + */ + public static boolean isNotEmpty(Object o) { + return !isEmpty(o); + } + + /** + * 对象是否为空 + * + * @param o String,List,Map,Object[],int[],long[] + * @return + */ + @SuppressWarnings("rawtypes") + public static boolean isEmpty(Object o) { + if (o == null) { + return true; + } + if (o instanceof String) { + if (o.toString().trim().equals("")) { + return true; + } + } else if (o instanceof List) { + if (((List) o).size() == 0) { + return true; + } + } else if (o instanceof Map) { + if (((Map) o).size() == 0) { + return true; + } + } else if (o instanceof Set) { + if (((Set) o).size() == 0) { + return true; + } + } else if (o instanceof Object[]) { + if (((Object[]) o).length == 0) { + return true; + } + } else if (o instanceof int[]) { + if (((int[]) o).length == 0) { + return true; + } + } else if (o instanceof long[]) { + if (((long[]) o).length == 0) { + return true; + } + } + return false; + } + + /** + * 对象组中是否存在 Empty Object + * + * @param os 对象组 + * @return + */ + public static boolean isOneEmpty(Object... os) { + for (Object o : os) { + if (isEmpty(o)) { + return true; + } + } + return false; + } + + /** + * 对象组中是否全是 Empty Object + * + * @param os + * @return + */ + public static boolean isAllEmpty(Object... os) { + for (Object o : os) { + if (!isEmpty(o)) { + return false; + } + } + return true; + } + /** + * 对象组都不是empty + */ + public static boolean isNotAllEmpty(Object... obj){ + for (Object o:obj){ + if (isEmpty(o)){ + return false; + } + } + return true; + } + + /** + * 是否为数字 + * + * @param obj + * @return + */ + public static boolean isNum(Object obj) { + try { + Integer.parseInt(obj.toString()); + } catch (Exception e) { + return false; + } + return true; + } + + /** + * 如果为空, 则调用默认值 + * + * @param str + * @return + */ + public static Object getValue(Object str, Object defaultValue) { + if (isEmpty(str)) { + return defaultValue; + } + return str; + } + + + + /** + * 强转->string,并去掉多余空格 + * + * @param str + * @return + */ + public static String toStr(Object str) { + return toStr(str, ""); + } + + /** + * 强转->string,并去掉多余空格 + * + * @param str + * @param defaultValue + * @return + */ + public static String toStr(Object str, String defaultValue) { + if (null == str) { + return defaultValue; + } + return str.toString().trim(); + } + + /** + * map的key转为小写 + * + * @param map + * @return Map<String,Object> + */ + public static Map<String, Object> caseInsensitiveMap(Map<String, Object> map) { + Map<String, Object> tempMap = new HashMap<>(); + for (String key : map.keySet()) { + tempMap.put(key.toLowerCase(), map.get(key)); + } + return tempMap; + } + + /** + * 获取map中第一个数据值 + * + * @param <K> Key的类型 + * @param <V> Value的类型 + * @param map 数据源 + * @return 返回的值 + */ + public static <K, V> V getFirstOrNull(Map<K, V> map) { + V obj = null; + for (Entry<K, V> entry : map.entrySet()) { + obj = entry.getValue(); + if (obj != null) { + break; + } + } + return obj; + } + + /** + * 创建StringBuilder对象 + * + * @return StringBuilder对象 + */ + public static StringBuilder builder(String... strs) { + final StringBuilder sb = new StringBuilder(); + for (String str : strs) { + sb.append(str); + } + return sb; + } + + /** + * 创建StringBuilder对象 + * + * @return StringBuilder对象 + */ + public static void builder(StringBuilder sb, String... strs) { + for (String str : strs) { + sb.append(str); + } + } + + /** + * 去掉指定后缀 + * + * @param str 字符串 + * @param suffix 后缀 + * @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串 + */ + public static String removeSuffix(String str, String suffix) { + if (isEmpty(str) || isEmpty(suffix)) { + return str; + } + + if (str.endsWith(suffix)) { + return str.substring(0, str.length() - suffix.length()); + } + return str; + } + + /** + * 判断是否是windows操作系统 + * + * @author stylefeng + * @Date 2017/5/24 22:34 + */ + public static Boolean isWinOs() { + String os = System.getProperty("os.name"); + if (os.toLowerCase().startsWith("win")) { + return true; + } else { + return false; + } + } + + /** + * 获取临时目录 + * + * @author stylefeng + * @Date 2017/5/24 22:35 + */ + public static String getTempPath() { + return System.getProperty("java.io.tmpdir"); + } + + /** + * 把一个数转化为int + * + * @author fengshuonan + * @Date 2017/11/15 下午11:10 + */ + public static Integer toInt(Object val) { + if (val instanceof Double) { + BigDecimal bigDecimal = new BigDecimal((Double) val); + return bigDecimal.intValue(); + } else { + return Integer.valueOf(val.toString()); + } + + } + + /** + * 获取项目路径 + */ + public static String getWebRootPath(String filePath) { + try { + String path = ToolUtil.class.getClassLoader().getResource("").toURI().getPath(); + path = path.replace("/WEB-INF/classes/", ""); + path = path.replace("/target/classes/", ""); + path = path.replace("file:/", ""); + if (ToolUtil.isEmpty(filePath)) { + return path; + } else { + return path + "/" + filePath; + } + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + /** + * 字符串是否包含不可见字符 + * 包含:true + * 不包含:false + * @param content + * @return Boolean + */ + public static Boolean containsInvisibleChar(String content) { + if (content != null && content.length() > 0) { + char[] contentCharArr = content.toCharArray(); + for (int i = 0; i < contentCharArr.length; i++) { + if ((contentCharArr[i] <= 0x1F) || contentCharArr[i] == 0x7F) { + return true; + } + } + return false; + } + return false; + } + + /** + * 获取客户端ip + * @param req + * @return + */ + public static String getRealIp(HttpServletRequest req) { + String ip = req.getHeader("x-forwarded-for"); + + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = req.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = req.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = req.getRemoteAddr(); + } + if (ip == null || ip.length() == 0 || "0:0:0:0:0:0:0:1".equals(ip)) { + ip = "127.0.0.1"; + } + return ip; + } + + /** + * 判断开始时间是否在结束时间之前 + * @param opStartTime + * @param opEndTime + */ + public static void validateOpTime(String opStartTime,String opEndTime){ + Date startTime = DateUtil.parseTime(opStartTime); + Date endTime = DateUtil.parseTime(opEndTime); + if (ToolUtil.isNotAllEmpty(opStartTime,opEndTime)){ + if (startTime.after(endTime)){ + throw new RuntimeException("Incorrect Date Format"); + } + } + + } + + /** + * 验证开始时间和结束时间:格式 开始时间不能大于结束时间 + * @param startTime + * @param endTime + * @return + */ + public static Boolean validateStartAndEndDateFormat(String startTime,String endTime){ + Boolean result = true; + try { + Date start = ToolUtil.isNotEmpty(startTime)?DateUtil.parseTime(startTime):null; + Date end = ToolUtil.isNotEmpty(endTime)?DateUtil.parseTime(endTime):null; + if (ToolUtil.isNotEmpty(start)&&ToolUtil.isNotEmpty(end)&&start.after(end)){ + result = false; + } + }catch (Exception e){ + result = false; + } + return result; + } + + /** + * 获取Annotation 中 memberValues的值 + * + * @param object + * @param property + * @param <T> + * @return + */ + public static <T> Object getFieldValue(T object, String property) { + if (object != null && property != null) { + Class<T> currClass = (Class<T>) object.getClass(); + try { + Field field = currClass.getDeclaredField(property); + field.setAccessible(true); + return field.get(object); + } catch (Exception e) { + e.printStackTrace(); + } + } + return null; + } + /** + * //递归获取cls实体对象及父级对象的属性 wx:修改,子类覆盖父类的同名方法 + * + * @param list + * @param cls + */ + public static void getFields(List<Field> list, Class<?> cls) { + Field[] fields = cls.getDeclaredFields(); + if (fields != null && fields.length > 0) { + List<Field> tempList = new ArrayList<>(); + for (Field field : fields) { + if (list.size() == 0) { + tempList.add(field); + } else { + boolean has = false; + for (Field checkF : list) { + if (checkF.getName().equals(field.getName())) { + has = true; + break; + } + } + if (!has) { + tempList.add(field); + } + } + } + list.addAll(tempList); + } + if (cls.getSuperclass() != null) { + getFields(list, cls.getSuperclass()); + } + } +} diff --git a/src/main/resources/mapper/EndpointDao.xml b/src/main/resources/mapper/EndpointDao.xml index 414475a..4794ea6 100644 --- a/src/main/resources/mapper/EndpointDao.xml +++ b/src/main/resources/mapper/EndpointDao.xml @@ -101,7 +101,7 @@ left join module m on e.module_id = m.id left join idc i on a.idc_id=i.id left join project p on m.project_id =p.id - where a.idc_id=#{idcId} + where a.idc_id=#{idcId} and a.state=1 and idc.state='on' order by e.id asc </select> diff --git a/src/main/resources/mapper/PromserverDao.xml b/src/main/resources/mapper/PromserverDao.xml index a917c14..f38714a 100644 --- a/src/main/resources/mapper/PromserverDao.xml +++ b/src/main/resources/mapper/PromserverDao.xml @@ -24,7 +24,7 @@ </select> <select id="selectSubInfosByType" resultMap="promserver"> - select * from prom_server where type=#{type} and status=1 + select p.* from prom_server p left join idc i on p.idc_id=i.id where p.type=#{type} and p.status=1 and i.state='on' </select> <select id="queryPromServerInfosAndIdcName" resultType="com.nis.entity.Promserver"> |
