AppServiceImpl.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.usthe.manager.service.impl;
  2. import com.usthe.common.entity.job.Job;
  3. import com.usthe.common.entity.job.Metrics;
  4. import com.usthe.manager.dao.ParamDefineDao;
  5. import com.usthe.manager.pojo.dto.Hierarchy;
  6. import com.usthe.manager.pojo.dto.ParamDefineDto;
  7. import com.usthe.common.entity.manager.ParamDefine;
  8. import com.usthe.manager.service.AppService;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.CommandLineRunner;
  12. import org.springframework.core.annotation.Order;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import org.yaml.snakeyaml.Yaml;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Objects;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. /**
  27. * 监控类型管理实现
  28. * TODO 暂时将监控配置和参数配置存放内存 之后存入数据库
  29. * @author tomsun28
  30. * @date 2021/11/14 17:17
  31. */
  32. @Service
  33. @Order(value = 1)
  34. @Transactional(rollbackFor = Exception.class)
  35. @Slf4j
  36. public class AppServiceImpl implements AppService, CommandLineRunner {
  37. private final Map<String, Job> appDefines = new ConcurrentHashMap<>();
  38. private final Map<String, List<ParamDefine>> paramDefines = new ConcurrentHashMap<>();
  39. @Autowired
  40. private ParamDefineDao paramDefineDao;
  41. @Override
  42. public List<ParamDefine> getAppParamDefines(String app) {
  43. List<ParamDefine> params = paramDefines.get(app);
  44. if (params == null) {
  45. params = Collections.emptyList();
  46. }
  47. return params;
  48. }
  49. @Override
  50. public Job getAppDefine(String app) throws IllegalArgumentException {
  51. Job appDefine = appDefines.get(app);
  52. if (appDefine == null) {
  53. throw new IllegalArgumentException("The app " + app + " not support.");
  54. }
  55. return appDefine.clone();
  56. }
  57. @Override
  58. public Map<String, String> getI18nResources(String lang) {
  59. Map<String, String> i18nMap = new HashMap<>(32);
  60. for (Job job : appDefines.values()) {
  61. // todo 暂时只国际化监控类型名称 后面需要支持指标名称
  62. Map<String, String> name = job.getName();
  63. if (name != null && !name.isEmpty()) {
  64. String i18nName = name.get(lang);
  65. if (i18nName == null) {
  66. i18nName = name.values().stream().findFirst().get();
  67. }
  68. i18nMap.put("monitor.app." + job.getApp(), i18nName);
  69. }
  70. }
  71. return i18nMap;
  72. }
  73. @Override
  74. public List<Hierarchy> getAllAppHierarchy(String lang) {
  75. List<Hierarchy> hierarchies = new LinkedList<>();
  76. for (Job job : appDefines.values()) {
  77. Hierarchy hierarchyApp = new Hierarchy();
  78. hierarchyApp.setCategory(job.getCategory());
  79. hierarchyApp.setValue(job.getApp());
  80. Map<String, String> nameMap = job.getName();
  81. if (nameMap != null) {
  82. String i18nName = nameMap.get(lang);
  83. if (i18nName == null) {
  84. i18nName = nameMap.values().stream().findFirst().get();
  85. }
  86. hierarchyApp.setLabel(i18nName);
  87. }
  88. List<Hierarchy> hierarchyMetricList = new LinkedList<>();
  89. if (job.getMetrics() != null) {
  90. for (Metrics metrics : job.getMetrics()) {
  91. Hierarchy hierarchyMetric = new Hierarchy();
  92. hierarchyMetric.setValue(metrics.getName());
  93. hierarchyMetric.setLabel(metrics.getName());
  94. List<Hierarchy> hierarchyFieldList = new LinkedList<>();
  95. if (metrics.getFields() != null) {
  96. for (Metrics.Field field : metrics.getFields()) {
  97. Hierarchy hierarchyField = new Hierarchy();
  98. hierarchyField.setValue(field.getField());
  99. hierarchyField.setLabel(field.getField());
  100. hierarchyField.setIsLeaf(true);
  101. hierarchyFieldList.add(hierarchyField);
  102. }
  103. hierarchyMetric.setChildren(hierarchyFieldList);
  104. }
  105. hierarchyMetricList.add(hierarchyMetric);
  106. }
  107. }
  108. hierarchyApp.setChildren(hierarchyMetricList);
  109. hierarchies.add(hierarchyApp);
  110. }
  111. return hierarchies;
  112. }
  113. @Override
  114. public void run(String... args) throws Exception {
  115. // 读取app定义配置加载到内存中 define/app/*.yml
  116. Yaml yaml = new Yaml();
  117. String classpath = this.getClass().getClassLoader().getResource("").getPath();
  118. String defineAppPath = classpath + File.separator + "define" + File.separator + "app";
  119. File directory = new File(defineAppPath);
  120. if (!directory.exists() || directory.listFiles() == null) {
  121. classpath = this.getClass().getResource(File.separator).getPath();
  122. defineAppPath = classpath + File.separator + "define" + File.separator + "app";
  123. directory = new File(defineAppPath);
  124. if (!directory.exists() || directory.listFiles() == null) {
  125. throw new IllegalArgumentException("define app directory not exist: " + defineAppPath);
  126. }
  127. }
  128. log.info("query define path {}", defineAppPath);
  129. for (File appFile : Objects.requireNonNull(directory.listFiles())) {
  130. if (appFile.exists()) {
  131. try (FileInputStream fileInputStream = new FileInputStream(appFile)) {
  132. Job app = yaml.loadAs(fileInputStream, Job.class);
  133. appDefines.put(app.getApp().toLowerCase(), app);
  134. } catch (IOException e) {
  135. log.error(e.getMessage(), e);
  136. throw new IOException(e);
  137. }
  138. }
  139. }
  140. // 读取监控参数定义配置加载到数据库中 define/param/*.yml
  141. String defineParamPath = classpath + File.separator + "define" + File.separator + "param";
  142. directory = new File(defineParamPath);
  143. if (!directory.exists() || directory.listFiles() == null) {
  144. throw new IllegalArgumentException("define param directory not exist: " + defineParamPath);
  145. }
  146. for (File appFile : Objects.requireNonNull(directory.listFiles())) {
  147. if (appFile.exists()) {
  148. try (FileInputStream fileInputStream = new FileInputStream(appFile)) {
  149. ParamDefineDto paramDefine = yaml.loadAs(fileInputStream, ParamDefineDto.class);
  150. paramDefines.put(paramDefine.getApp().toLowerCase(), paramDefine.getParam());
  151. } catch (IOException e) {
  152. log.error(e.getMessage(), e);
  153. throw new IOException(e);
  154. }
  155. }
  156. }
  157. }
  158. }