DataCollectController.java 14 KB

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