DataCollectController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. RsJobConfig obj = new RsJobConfig();
  140. BeanUtils.populate(obj, request.getParameterMap());
  141. obj.setValid("1");
  142. String time=request.getParameter("jobNextTime");
  143. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  144. if(df.parse(time).before(new Date())) {
  145. return Result.error("任务开始时间不能小于当前时间");
  146. }
  147. String cron = request.getParameter("cronExpression");
  148. String jobDataset = request.getParameter("jobDataset");
  149. return datacollect.addJob(obj, cron, jobDataset);
  150. } catch (Exception ex) {
  151. ex.printStackTrace();
  152. return Result.error("新增任务失败!");
  153. }
  154. }
  155. /*
  156. 修改任务
  157. */
  158. @RequestMapping("updateJob")
  159. @ResponseBody
  160. public Result updateJob(HttpServletRequest request) {
  161. try {
  162. RsJobConfig obj = new RsJobConfig();
  163. // ConvertUtils.register(new DateLocaleConverter(), Date.class);
  164. BeanUtils.populate(obj, request.getParameterMap());
  165. String time=request.getParameter("jobNextTime");
  166. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  167. if(df.parse(time).before(new Date())) {
  168. return Result.error("任务开始时间不能小于当前时间");
  169. }
  170. String cron = request.getParameter("cronExpression");
  171. String jobDataset = request.getParameter("jobDataset");
  172. return datacollect.updateJob(obj, cron, jobDataset);
  173. } catch (Exception ex) {
  174. ex.printStackTrace();
  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. ex.printStackTrace();
  200. return Result.error(ex.getMessage());
  201. }
  202. }
  203. /**
  204. * 获取任务信息
  205. *
  206. * @return
  207. */
  208. @RequestMapping("getJobInfo")
  209. @ResponseBody
  210. public Result getJobInfo(String jobId) {
  211. try {
  212. RsJobConfig job = datacollect.getJobById(jobId);
  213. if (job != null) {
  214. ActionResult re = new ActionResult(true, "");
  215. re.setData(job.getValid());
  216. return re;
  217. } else {
  218. return new ActionResult(false, "非法操作!");
  219. }
  220. } catch (Exception ex) {
  221. return Result.error(ex.getMessage());
  222. }
  223. }
  224. /**
  225. * 根据任务Id获取相关数据集下拉数据
  226. *
  227. * @return
  228. */
  229. @RequestMapping("getJobDatasetByJobId")
  230. @ResponseBody
  231. public Result getJobDatasetByJobId(String jobId) {
  232. try {
  233. return datacollect.getJobDatasetByJobId(jobId);
  234. } catch (Exception ex) {
  235. return Result.error(ex.getMessage());
  236. }
  237. }
  238. /*************************
  239. * 数据集--数据源管理
  240. ******************************************/
  241. /*
  242. 数据集配置
  243. */
  244. @RequestMapping("configDataset")
  245. public String configDataset(Model model) {
  246. try {
  247. model.addAttribute("stdVersion", "569e19522e3d");
  248. //获取数据源数据
  249. model.addAttribute("datasourceList", datasource.getDatasourceByOrg("").toJson());
  250. model.addAttribute("contentPage", "/datacollect/configDataset");
  251. return "partView";
  252. } catch (Exception ex) {
  253. model.addAttribute("contentPage", "error");
  254. return "partView";
  255. }
  256. }
  257. /**
  258. * 数据集数据源管理列表(包含全部数据集)
  259. *
  260. * @return
  261. */
  262. @RequestMapping("getDatasetSource")
  263. @ResponseBody
  264. public Result getDatasetSource(String stdVersion) {
  265. try {
  266. return datacollect.getDatasetSource(stdVersion);
  267. } catch (Exception ex) {
  268. return Result.error(ex.getMessage());
  269. }
  270. }
  271. /**
  272. * 更新数据集数据源
  273. *
  274. * @return
  275. */
  276. @RequestMapping("saveDatasetSource")
  277. @ResponseBody
  278. public Result saveDatasetSource(String stdVersion, String json) {
  279. try {
  280. return datacollect.saveDatasetSource(json);
  281. } catch (Exception ex) {
  282. return Result.error(ex.getMessage());
  283. }
  284. }
  285. /*************************** 任务执行 ************************************************/
  286. /**
  287. * 执行任务
  288. *
  289. * @return
  290. */
  291. @RequestMapping("executeJob")
  292. @ResponseBody
  293. public Result executeJob(String jobId) {
  294. try {
  295. //获取任务详细信息
  296. RsJobConfig job = datacollect.getJobById(jobId);
  297. String content = job.getJobContent();
  298. Class<?> classType = Class.forName(content);
  299. Method method = classType.getMethod("execute", new Class[]{String.class});
  300. method.invoke(classType.newInstance(), jobId);
  301. return Result.success("处理完成!");
  302. } catch (Exception ex) {
  303. return Result.error(ex.getMessage());
  304. }
  305. }
  306. /*
  307. 任务补采界面
  308. */
  309. @RequestMapping("repeatDatacollect")
  310. public String repeatDatacollect(Model model, String jobId, String jobDatasetId, String jobStatus, String jobTimeFrom, String jobTimeTo) {
  311. try {
  312. model.addAttribute("jobId", jobId == null ? "" : jobId);
  313. model.addAttribute("jobDatasetId", jobDatasetId == null ? "" : jobDatasetId);
  314. model.addAttribute("jobStatus", jobStatus == null ? "" : jobStatus);
  315. if (jobTimeFrom != null && jobTimeFrom.length() > 0) {
  316. Date timeFrom = DateConvert.toDate(jobTimeFrom);
  317. model.addAttribute("jobTimeFrom", DateConvert.toString(timeFrom));
  318. } else {
  319. GregorianCalendar gc = new GregorianCalendar();
  320. gc.setTime(new Date());
  321. gc.add(5, -1);
  322. model.addAttribute("jobTimeFrom", DateConvert.toString(gc.getTime()));
  323. }
  324. if (jobTimeTo != null && jobTimeTo.length() > 0) {
  325. Date timeTo = DateConvert.toDate(jobTimeTo);
  326. model.addAttribute("jobTimeTo", DateConvert.toString(timeTo));
  327. } else {
  328. model.addAttribute("jobTimeTo", DateConvert.toString(new Date()));
  329. }
  330. model.addAttribute("contentPage", "/datacollect/repeatDatacollect");
  331. return "partView";
  332. } catch (Exception ex) {
  333. model.addAttribute("contentPage", "error");
  334. return "partView";
  335. }
  336. }
  337. /**
  338. * 任务详细日志列表
  339. *
  340. * @return
  341. */
  342. @RequestMapping("getJobLogDetail")
  343. @ResponseBody
  344. public Result getJobLogDetail(String jobId, String jobDatasetId, String jobStatus, String jobTimeFrom, String jobTimeTo, int page, int rows) {
  345. try {
  346. Map<String, Object> conditionMap = new HashMap<String, Object>();
  347. conditionMap.put("jobId", jobId);
  348. conditionMap.put("jobDatasetId", jobDatasetId);
  349. conditionMap.put("jobStatus", jobStatus);
  350. conditionMap.put("jobTimeFrom", jobTimeFrom);
  351. conditionMap.put("jobTimeTo", jobTimeTo);
  352. return datacollect.getJobLogDetail(conditionMap, page, rows);
  353. } catch (Exception ex) {
  354. return Result.error(ex.getMessage());
  355. }
  356. }
  357. /**
  358. * 补采数据
  359. *
  360. * @return
  361. */
  362. @RequestMapping("repeat")
  363. @ResponseBody
  364. public Result repeat(String ids) {
  365. return Result.error("非法操作!");
  366. }
  367. /*************************** 任务跟踪 ***********************************/
  368. /**
  369. * 任务跟踪界面
  370. *
  371. * @return
  372. */
  373. @RequestMapping("trackJob")
  374. public String trackJob(Model model, String jobId) {
  375. try {
  376. model.addAttribute("contentPage", "/datacollect/trackJob");
  377. return "pageView";
  378. } catch (Exception ex) {
  379. model.addAttribute("contentPage", "error");
  380. return "partView";
  381. }
  382. }
  383. /**
  384. * 任务日志列表
  385. *
  386. * @return
  387. */
  388. @RequestMapping("getJobLog")
  389. @ResponseBody
  390. public Result getJobLog(String jobId, int page, int rows) {
  391. try {
  392. Map<String, Object> conditionMap = new HashMap<String, Object>();
  393. conditionMap.put("jobId", jobId);
  394. return datacollect.getJobLog(conditionMap, page, rows);
  395. } catch (Exception ex) {
  396. return Result.error(ex.getMessage());
  397. }
  398. }
  399. /**
  400. * 任务详细根据数据集分组
  401. *
  402. * @return
  403. */
  404. @RequestMapping("getJobLogDataset")
  405. @ResponseBody
  406. public Result getJobLogDataset(String logId) {
  407. try {
  408. return datacollect.getJobLogDataset(logId);
  409. } catch (Exception ex) {
  410. return Result.error(ex.getMessage());
  411. }
  412. }
  413. }