DataCollectController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.DatacollectManager;
  8. import com.yihu.hos.datacollect.service.DatacollectService;
  9. import com.yihu.hos.resource.service.StdService;
  10. import com.yihu.hos.standard.service.adapter.AdapterSchemeService;
  11. import com.yihu.hos.system.service.DatasourceManager;
  12. import com.yihu.hos.web.framework.constant.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. DatacollectManager datacollect;
  36. @Resource(name = Services.DatacollectService)
  37. DatacollectService datacollectService;
  38. @Resource(name = StdService.BEAN_ID)
  39. StdService stdService;
  40. @Resource(name = Services.Datasource)
  41. DatasourceManager 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. ex.printStackTrace();
  155. return Result.error("新增任务失败!");
  156. }
  157. }
  158. /*
  159. 修改任务
  160. */
  161. @RequestMapping("updateJob")
  162. @ResponseBody
  163. public Result updateJob(HttpServletRequest request) {
  164. try {
  165. RsJobConfig obj = new RsJobConfig();
  166. // ConvertUtils.register(new DateLocaleConverter(), Date.class);
  167. BeanUtils.populate(obj, request.getParameterMap());
  168. String time=request.getParameter("jobNextTime");
  169. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  170. if(df.parse(time).before(new Date())) {
  171. return Result.error("任务开始时间不能小于当前时间");
  172. }
  173. String cron = request.getParameter("cronExpression");
  174. String jobDataset = request.getParameter("jobDataset");
  175. return datacollect.updateJob(obj, cron, jobDataset);
  176. } catch (Exception ex) {
  177. ex.printStackTrace();
  178. return Result.error("修改任务失败!");
  179. }
  180. }
  181. /*
  182. 修改任务状态
  183. */
  184. @RequestMapping("validJob")
  185. @ResponseBody
  186. public Result validJob(String jobId, String valid) {
  187. try {
  188. return datacollect.validJob(jobId, valid);
  189. } catch (Exception ex) {
  190. return Result.error(ex.getMessage());
  191. }
  192. }
  193. /*
  194. 删除任务
  195. */
  196. @RequestMapping("deleteJob")
  197. @ResponseBody
  198. public Result deleteJob(String jobId) {
  199. try {
  200. return datacollect.deleteJob(jobId);
  201. } catch (Exception ex) {
  202. ex.printStackTrace();
  203. return Result.error(ex.getMessage());
  204. }
  205. }
  206. /**
  207. * 获取任务信息
  208. *
  209. * @return
  210. */
  211. @RequestMapping("getJobInfo")
  212. @ResponseBody
  213. public Result getJobInfo(String jobId) {
  214. try {
  215. RsJobConfig job = datacollect.getJobById(jobId);
  216. if (job != null) {
  217. ActionResult re = new ActionResult(true, "");
  218. re.setData(job.getValid());
  219. return re;
  220. } else {
  221. return new ActionResult(false, "非法操作!");
  222. }
  223. } catch (Exception ex) {
  224. return Result.error(ex.getMessage());
  225. }
  226. }
  227. /**
  228. * 根据任务Id获取相关数据集下拉数据
  229. *
  230. * @return
  231. */
  232. @RequestMapping("getJobDatasetByJobId")
  233. @ResponseBody
  234. public Result getJobDatasetByJobId(String jobId) {
  235. try {
  236. return datacollect.getJobDatasetByJobId(jobId);
  237. } catch (Exception ex) {
  238. return Result.error(ex.getMessage());
  239. }
  240. }
  241. /*************************
  242. * 数据集--数据源管理
  243. ******************************************/
  244. /*
  245. 数据集配置
  246. */
  247. @RequestMapping("configDataset")
  248. public String configDataset(Model model) {
  249. try {
  250. model.addAttribute("stdVersion", "569e19522e3d");
  251. //获取数据源数据
  252. model.addAttribute("datasourceList", datasource.getDatasourceByOrg("").toJson());
  253. model.addAttribute("contentPage", "/datacollect/configDataset");
  254. return "partView";
  255. } catch (Exception ex) {
  256. model.addAttribute("contentPage", "error");
  257. return "partView";
  258. }
  259. }
  260. /**
  261. * 数据集数据源管理列表(包含全部数据集)
  262. *
  263. * @return
  264. */
  265. @RequestMapping("getDatasetSource")
  266. @ResponseBody
  267. public Result getDatasetSource(String stdVersion) {
  268. try {
  269. return datacollect.getDatasetSource(stdVersion);
  270. } catch (Exception ex) {
  271. return Result.error(ex.getMessage());
  272. }
  273. }
  274. /**
  275. * 更新数据集数据源
  276. *
  277. * @return
  278. */
  279. @RequestMapping("saveDatasetSource")
  280. @ResponseBody
  281. public Result saveDatasetSource(String stdVersion, String json) {
  282. try {
  283. return datacollect.saveDatasetSource(json);
  284. } catch (Exception ex) {
  285. return Result.error(ex.getMessage());
  286. }
  287. }
  288. /*************************** 任务执行 ************************************************/
  289. /**
  290. * 执行任务
  291. *
  292. * @return
  293. */
  294. @RequestMapping("executeJob")
  295. @ResponseBody
  296. public Result executeJob(String jobId) {
  297. try {
  298. //获取任务详细信息
  299. RsJobConfig job = datacollect.getJobById(jobId);
  300. String content = job.getJobContent();
  301. Class<?> classType = Class.forName(content);
  302. Method method = classType.getMethod("execute", new Class[]{String.class});
  303. method.invoke(classType.newInstance(), jobId);
  304. return Result.success("处理完成!");
  305. } catch (Exception ex) {
  306. return Result.error(ex.getMessage());
  307. }
  308. }
  309. /*
  310. 任务补采界面
  311. */
  312. @RequestMapping("repeatDatacollect")
  313. public String repeatDatacollect(Model model, String jobId, String jobDatasetId, String jobStatus, String jobTimeFrom, String jobTimeTo) {
  314. try {
  315. model.addAttribute("jobId", jobId == null ? "" : jobId);
  316. model.addAttribute("jobDatasetId", jobDatasetId == null ? "" : jobDatasetId);
  317. model.addAttribute("jobStatus", jobStatus == null ? "" : jobStatus);
  318. if (jobTimeFrom != null && jobTimeFrom.length() > 0) {
  319. Date timeFrom = DateConvert.toDate(jobTimeFrom);
  320. model.addAttribute("jobTimeFrom", DateConvert.toString(timeFrom));
  321. } else {
  322. GregorianCalendar gc = new GregorianCalendar();
  323. gc.setTime(new Date());
  324. gc.add(5, -1);
  325. model.addAttribute("jobTimeFrom", DateConvert.toString(gc.getTime()));
  326. }
  327. if (jobTimeTo != null && jobTimeTo.length() > 0) {
  328. Date timeTo = DateConvert.toDate(jobTimeTo);
  329. model.addAttribute("jobTimeTo", DateConvert.toString(timeTo));
  330. } else {
  331. model.addAttribute("jobTimeTo", DateConvert.toString(new Date()));
  332. }
  333. model.addAttribute("contentPage", "/datacollect/repeatDatacollect");
  334. return "partView";
  335. } catch (Exception ex) {
  336. model.addAttribute("contentPage", "error");
  337. return "partView";
  338. }
  339. }
  340. /**
  341. * 任务详细日志列表
  342. *
  343. * @return
  344. */
  345. @RequestMapping("getJobLogDetail")
  346. @ResponseBody
  347. public Result getJobLogDetail(String jobId, String jobDatasetId, String jobStatus, String jobTimeFrom, String jobTimeTo, int page, int rows) {
  348. try {
  349. Map<String, Object> conditionMap = new HashMap<String, Object>();
  350. conditionMap.put("jobId", jobId);
  351. conditionMap.put("jobDatasetId", jobDatasetId);
  352. conditionMap.put("jobStatus", jobStatus);
  353. conditionMap.put("jobTimeFrom", jobTimeFrom);
  354. conditionMap.put("jobTimeTo", jobTimeTo);
  355. return datacollect.getJobLogDetail(conditionMap, page, rows);
  356. } catch (Exception ex) {
  357. return Result.error(ex.getMessage());
  358. }
  359. }
  360. /**
  361. * 补采数据
  362. *
  363. * @return
  364. */
  365. @RequestMapping("repeat")
  366. @ResponseBody
  367. public Result repeat(String ids) {
  368. try {
  369. if (ids.length() > 0) {
  370. String[] logId = ids.split(",");
  371. if (logId.length == 1) {
  372. return datacollectService.repeatJob(ids);
  373. } else {
  374. StringBuilder str = new StringBuilder();
  375. for (String id : logId) {
  376. ActionResult re = datacollectService.repeatJob(id);
  377. if (re.isSuccessFlg()) {
  378. str.append(id + "补采成功!\n");
  379. } else {
  380. str.append(id + "补采失败!\n");
  381. }
  382. }
  383. return Result.success(str.toString());
  384. }
  385. } else {
  386. return Result.error("非法操作!");
  387. }
  388. } catch (Exception ex) {
  389. return Result.error(ex.getMessage());
  390. }
  391. }
  392. /*************************** 任务跟踪 ***********************************/
  393. /**
  394. * 任务跟踪界面
  395. *
  396. * @return
  397. */
  398. @RequestMapping("trackJob")
  399. public String trackJob(Model model, String jobId) {
  400. try {
  401. model.addAttribute("contentPage", "/datacollect/trackJob");
  402. return "pageView";
  403. } catch (Exception ex) {
  404. model.addAttribute("contentPage", "error");
  405. return "partView";
  406. }
  407. }
  408. /**
  409. * 任务日志列表
  410. *
  411. * @return
  412. */
  413. @RequestMapping("getJobLog")
  414. @ResponseBody
  415. public Result getJobLog(String jobId, int page, int rows) {
  416. try {
  417. Map<String, Object> conditionMap = new HashMap<String, Object>();
  418. conditionMap.put("jobId", jobId);
  419. return datacollect.getJobLog(conditionMap, page, rows);
  420. } catch (Exception ex) {
  421. return Result.error(ex.getMessage());
  422. }
  423. }
  424. /**
  425. * 任务详细根据数据集分组
  426. *
  427. * @return
  428. */
  429. @RequestMapping("getJobLogDataset")
  430. @ResponseBody
  431. public Result getJobLogDataset(String logId) {
  432. try {
  433. return datacollect.getJobLogDataset(logId);
  434. } catch (Exception ex) {
  435. return Result.error(ex.getMessage());
  436. }
  437. }
  438. }