DataCollectController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package com.yihu.hos.datacollect.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.fasterxml.jackson.databind.node.ArrayNode;
  4. import com.yihu.hos.common.Services;
  5. import com.yihu.hos.core.datatype.CollectionUtil;
  6. import com.yihu.hos.datacollect.model.RsJobConfig;
  7. import com.yihu.hos.datacollect.service.intf.IDatacollectManager;
  8. import com.yihu.hos.datacollect.service.intf.IDatacollectService;
  9. import com.yihu.hos.resource.service.IStdService;
  10. import com.yihu.hos.standard.service.adapter.AdapterSchemeService;
  11. import com.yihu.hos.system.service.intf.IDatasourceManager;
  12. import com.yihu.hos.web.framework.constrant.DateConvert;
  13. import com.yihu.hos.web.framework.model.ActionResult;
  14. import com.yihu.hos.web.framework.model.Result;
  15. import com.yihu.hos.web.framework.util.controller.BaseController;
  16. import org.apache.commons.beanutils.BeanUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.ui.Model;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.ResponseBody;
  22. import javax.annotation.Resource;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.lang.reflect.Method;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. /**
  28. * 数据采集配置页面
  29. * Created by hzp on 2015/8/12.
  30. */
  31. @RequestMapping("/datacollect")
  32. @Controller("dataCollectController")
  33. public class DataCollectController extends BaseController {
  34. @Resource(name = Services.Datacollect)
  35. IDatacollectManager datacollect;
  36. @Resource(name = Services.DatacollectService)
  37. IDatacollectService datacollectService;
  38. @Resource(name = Services.StdService)
  39. IStdService stdService;
  40. @Resource(name = Services.Datasource)
  41. IDatasourceManager datasource;
  42. @Resource(name = AdapterSchemeService.BEAN_ID)
  43. private AdapterSchemeService adapterSchemeService;
  44. @Autowired
  45. private ObjectMapper objectMapper;
  46. /****************************
  47. * 任务管理
  48. ************************************************/
  49. /*
  50. 任务配置
  51. */
  52. @RequestMapping("configJob")
  53. public String configJob(Model model) {
  54. model.addAttribute("contentPage", "/datacollect/configJob");
  55. return "partView";
  56. }
  57. /*
  58. 任务新增/编辑
  59. */
  60. @RequestMapping("editorJob")
  61. public String editorJob(Model model, String jobId) {
  62. try {
  63. //是否编辑
  64. if (jobId != null && jobId.length() > 0) {
  65. //获取任务信息
  66. RsJobConfig job = datacollect.getJobById(jobId);
  67. model.addAttribute("model", job);
  68. String cron = datacollect.getCronByJobId(jobId);
  69. model.addAttribute("cronExpression", cron);
  70. }
  71. //获取方案列表
  72. List data = stdService.getSchemeVersion();
  73. String jsonlist = objectMapper.writeValueAsString(data);
  74. ArrayNode jsonArray = objectMapper.readValue(jsonlist,ArrayNode.class);
  75. model.addAttribute("schemeList", "{\"detailModelList\":" + (CollectionUtil.isEmpty(data) ? "[]" : jsonArray) + "}");
  76. model.addAttribute("contentPage", "/datacollect/editorJob");
  77. return "pageView";
  78. } catch (Exception ex) {
  79. model.addAttribute("contentPage", "/datacollect/editorJob");
  80. return "pageView";
  81. }
  82. }
  83. /*
  84. 获取任务列表
  85. */
  86. @RequestMapping("getJob")
  87. @ResponseBody
  88. public Result getJob(String name, int page, int rows) {
  89. try {
  90. Map<String, Object> map = new HashMap<>();
  91. map.put("name", name);
  92. return datacollect.getJobList(map, page, rows);
  93. } catch (Exception ex) {
  94. return Result.error(ex.getMessage());
  95. }
  96. }
  97. @RequestMapping("compareServeTime")
  98. @ResponseBody
  99. public Result getJob(String time){
  100. try {
  101. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  102. if(df.parse(time).before(new Date())) {
  103. return Result.success("beforeServeTime");
  104. }
  105. else
  106. return Result.error("afterServeTime");
  107. }catch (Exception ex){
  108. return Result.error(ex.getMessage());
  109. }
  110. }
  111. /**
  112. * 根据适配方案获取数据集列表
  113. */
  114. @RequestMapping("getSchemeDataset")
  115. @ResponseBody
  116. public Result getSchemeDataset(String schemeId, String schemeVersion, String jobId) {
  117. try {
  118. return datacollect.getSchemeDataset(schemeId, schemeVersion, jobId);
  119. } catch (Exception ex) {
  120. return Result.error(ex.getMessage());
  121. }
  122. }
  123. /**
  124. * 根据适配方案+数据集获取字段列表
  125. */
  126. @RequestMapping("getSchemeDatasetCol")
  127. @ResponseBody
  128. public Result getSchemeDatasetCol(String schemeId, String schemeVersion, String datasetId) {
  129. try {
  130. return datacollect.getSchemeDatasetCol(schemeId, schemeVersion, datasetId);
  131. } catch (Exception ex) {
  132. return Result.error(ex.getMessage());
  133. }
  134. }
  135. /*
  136. 新增任务
  137. */
  138. @RequestMapping("addJob")
  139. @ResponseBody
  140. public Result addJob(HttpServletRequest request) {
  141. try {
  142. RsJobConfig obj = new RsJobConfig();
  143. BeanUtils.populate(obj, request.getParameterMap());
  144. obj.setValid("1");
  145. String time=request.getParameter("jobNextTime");
  146. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  147. if(df.parse(time).before(new Date())) {
  148. return Result.error("任务开始时间不能小于当前时间");
  149. }
  150. String cron = request.getParameter("cronExpression");
  151. String jobDataset = request.getParameter("jobDataset");
  152. return datacollect.addJob(obj, cron, jobDataset);
  153. } catch (Exception ex) {
  154. return Result.error("新增任务失败!");
  155. }
  156. }
  157. /*
  158. 修改任务
  159. */
  160. @RequestMapping("updateJob")
  161. @ResponseBody
  162. public Result updateJob(HttpServletRequest request) {
  163. try {
  164. RsJobConfig obj = new RsJobConfig();
  165. BeanUtils.populate(obj, request.getParameterMap());
  166. String time=request.getParameter("jobNextTime");
  167. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  168. if(df.parse(time).before(new Date())) {
  169. return Result.error("任务开始时间不能小于当前时间");
  170. }
  171. String cron = request.getParameter("cronExpression");
  172. String jobDataset = request.getParameter("jobDataset");
  173. return datacollect.updateJob(obj, cron, jobDataset);
  174. } catch (Exception ex) {
  175. return Result.error("修改任务失败!");
  176. }
  177. }
  178. /*
  179. 修改任务状态
  180. */
  181. @RequestMapping("validJob")
  182. @ResponseBody
  183. public Result validJob(String jobId, String valid) {
  184. try {
  185. return datacollect.validJob(jobId, valid);
  186. } catch (Exception ex) {
  187. return Result.error(ex.getMessage());
  188. }
  189. }
  190. /*
  191. 删除任务
  192. */
  193. @RequestMapping("deleteJob")
  194. @ResponseBody
  195. public Result deleteJob(String jobId) {
  196. try {
  197. return datacollect.deleteJob(jobId);
  198. } catch (Exception ex) {
  199. return Result.error(ex.getMessage());
  200. }
  201. }
  202. /**
  203. * 获取任务信息
  204. *
  205. * @return
  206. */
  207. @RequestMapping("getJobInfo")
  208. @ResponseBody
  209. public Result getJobInfo(String jobId) {
  210. try {
  211. RsJobConfig job = datacollect.getJobById(jobId);
  212. if (job != null) {
  213. ActionResult re = new ActionResult(true, "");
  214. re.setData(job.getValid());
  215. return re;
  216. } else {
  217. return new ActionResult(false, "非法操作!");
  218. }
  219. } catch (Exception ex) {
  220. return Result.error(ex.getMessage());
  221. }
  222. }
  223. /**
  224. * 根据任务Id获取相关数据集下拉数据
  225. *
  226. * @return
  227. */
  228. @RequestMapping("getJobDatasetByJobId")
  229. @ResponseBody
  230. public Result getJobDatasetByJobId(String jobId) {
  231. try {
  232. return datacollect.getJobDatasetByJobId(jobId);
  233. } catch (Exception ex) {
  234. return Result.error(ex.getMessage());
  235. }
  236. }
  237. /*************************
  238. * 数据集--数据源管理
  239. ******************************************/
  240. /*
  241. 数据集配置
  242. */
  243. @RequestMapping("configDataset")
  244. public String configDataset(Model model) {
  245. try {
  246. model.addAttribute("stdVersion", "569e19522e3d");
  247. //获取数据源数据
  248. model.addAttribute("datasourceList", datasource.getDatasourceByOrg("").toJson());
  249. model.addAttribute("contentPage", "/datacollect/configDataset");
  250. return "partView";
  251. } catch (Exception ex) {
  252. model.addAttribute("contentPage", "error");
  253. return "partView";
  254. }
  255. }
  256. /**
  257. * 数据集数据源管理列表(包含全部数据集)
  258. *
  259. * @return
  260. */
  261. @RequestMapping("getDatasetSource")
  262. @ResponseBody
  263. public Result getDatasetSource(String stdVersion) {
  264. try {
  265. return datacollect.getDatasetSource(stdVersion);
  266. } catch (Exception ex) {
  267. return Result.error(ex.getMessage());
  268. }
  269. }
  270. /**
  271. * 更新数据集数据源
  272. *
  273. * @return
  274. */
  275. @RequestMapping("saveDatasetSource")
  276. @ResponseBody
  277. public Result saveDatasetSource(String stdVersion, String json) {
  278. try {
  279. return datacollect.saveDatasetSource(json);
  280. } catch (Exception ex) {
  281. return Result.error(ex.getMessage());
  282. }
  283. }
  284. /*************************** 任务执行 ************************************************/
  285. /**
  286. * 执行任务
  287. *
  288. * @return
  289. */
  290. @RequestMapping("executeJob")
  291. @ResponseBody
  292. public Result executeJob(String jobId) {
  293. try {
  294. //获取任务详细信息
  295. RsJobConfig job = datacollect.getJobById(jobId);
  296. String content = job.getJobContent();
  297. Class<?> classType = Class.forName(content);
  298. Method method = classType.getMethod("execute", new Class[]{String.class});
  299. method.invoke(classType.newInstance(), jobId);
  300. return Result.success("处理完成!");
  301. } catch (Exception ex) {
  302. return Result.error(ex.getMessage());
  303. }
  304. }
  305. /*
  306. 任务补采界面
  307. */
  308. @RequestMapping("repeatDatacollect")
  309. public String repeatDatacollect(Model model, String jobId, String jobDatasetId, String jobStatus, String jobTimeFrom, String jobTimeTo) {
  310. try {
  311. model.addAttribute("jobId", jobId == null ? "" : jobId);
  312. model.addAttribute("jobDatasetId", jobDatasetId == null ? "" : jobDatasetId);
  313. model.addAttribute("jobStatus", jobStatus == null ? "" : jobStatus);
  314. if (jobTimeFrom != null && jobTimeFrom.length() > 0) {
  315. Date timeFrom = DateConvert.toDate(jobTimeFrom);
  316. model.addAttribute("jobTimeFrom", DateConvert.toString(timeFrom));
  317. } else {
  318. GregorianCalendar gc = new GregorianCalendar();
  319. gc.setTime(new Date());
  320. gc.add(5, -1);
  321. model.addAttribute("jobTimeFrom", DateConvert.toString(gc.getTime()));
  322. }
  323. if (jobTimeTo != null && jobTimeTo.length() > 0) {
  324. Date timeTo = DateConvert.toDate(jobTimeTo);
  325. model.addAttribute("jobTimeTo", DateConvert.toString(timeTo));
  326. } else {
  327. model.addAttribute("jobTimeTo", DateConvert.toString(new Date()));
  328. }
  329. model.addAttribute("contentPage", "/datacollect/repeatDatacollect");
  330. return "partView";
  331. } catch (Exception ex) {
  332. model.addAttribute("contentPage", "error");
  333. return "partView";
  334. }
  335. }
  336. /**
  337. * 任务详细日志列表
  338. *
  339. * @return
  340. */
  341. @RequestMapping("getJobLogDetail")
  342. @ResponseBody
  343. public Result getJobLogDetail(String jobId, String jobDatasetId, String jobStatus, String jobTimeFrom, String jobTimeTo, int page, int rows) {
  344. try {
  345. Map<String, Object> conditionMap = new HashMap<String, Object>();
  346. conditionMap.put("jobId", jobId);
  347. conditionMap.put("jobDatasetId", jobDatasetId);
  348. conditionMap.put("jobStatus", jobStatus);
  349. conditionMap.put("jobTimeFrom", jobTimeFrom);
  350. conditionMap.put("jobTimeTo", jobTimeTo);
  351. return datacollect.getJobLogDetail(conditionMap, page, rows);
  352. } catch (Exception ex) {
  353. return Result.error(ex.getMessage());
  354. }
  355. }
  356. /**
  357. * 补采数据
  358. *
  359. * @return
  360. */
  361. @RequestMapping("repeat")
  362. @ResponseBody
  363. public Result repeat(String ids) {
  364. try {
  365. if (ids.length() > 0) {
  366. String[] logId = ids.split(",");
  367. if (logId.length == 1) {
  368. return datacollectService.repeatJob(ids);
  369. } else {
  370. StringBuilder str = new StringBuilder();
  371. for (String id : logId) {
  372. ActionResult re = datacollectService.repeatJob(id);
  373. if (re.isSuccessFlg()) {
  374. str.append(id + "补采成功!\n");
  375. } else {
  376. str.append(id + "补采失败!\n");
  377. }
  378. }
  379. return Result.success(str.toString());
  380. }
  381. } else {
  382. return Result.error("非法操作!");
  383. }
  384. } catch (Exception ex) {
  385. return Result.error(ex.getMessage());
  386. }
  387. }
  388. /*************************** 任务跟踪 ***********************************/
  389. /**
  390. * 任务跟踪界面
  391. *
  392. * @return
  393. */
  394. @RequestMapping("trackJob")
  395. public String trackJob(Model model, String jobId) {
  396. try {
  397. model.addAttribute("contentPage", "/datacollect/trackJob");
  398. return "pageView";
  399. } catch (Exception ex) {
  400. model.addAttribute("contentPage", "error");
  401. return "partView";
  402. }
  403. }
  404. /**
  405. * 任务日志列表
  406. *
  407. * @return
  408. */
  409. @RequestMapping("getJobLog")
  410. @ResponseBody
  411. public Result getJobLog(String jobId, int page, int rows) {
  412. try {
  413. Map<String, Object> conditionMap = new HashMap<String, Object>();
  414. conditionMap.put("jobId", jobId);
  415. return datacollect.getJobLog(conditionMap, page, rows);
  416. } catch (Exception ex) {
  417. return Result.error(ex.getMessage());
  418. }
  419. }
  420. /**
  421. * 任务详细根据数据集分组
  422. *
  423. * @return
  424. */
  425. @RequestMapping("getJobLogDataset")
  426. @ResponseBody
  427. public Result getJobLogDataset(String logId) {
  428. try {
  429. return datacollect.getJobLogDataset(logId);
  430. } catch (Exception ex) {
  431. return Result.error(ex.getMessage());
  432. }
  433. }
  434. }