JsonImporter.template.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // ___classname___ JSON数据导入程序
  2. // ___datetime_now___ 由 JsonImportGen.js 自动生成,请勿手动修改
  3. // 输入输出参数格式化
  4. input = INPUT;
  5. try {
  6. input = decodeURIComponent(input);
  7. } catch(e) {}
  8. try {
  9. input = base64.decode(input);
  10. } catch(e) {}
  11. try {
  12. input = JSON.parse(input);
  13. input = JSON.parse(input);
  14. } catch(e) {}
  15. output = {};
  16. testcfg = {}
  17. try {
  18. testcfg = JSON.parse(etcd.get("/api/test.json"));
  19. } catch(e) {}
  20. function teststoredfs() {
  21. // 输入JSON临时存入DFS
  22. dir = "/opt___classname___";
  23. dtm = new Date().toJSON().replace(/[\-\:\.TZ ]/mg, "");
  24. fn = dir + "/" + dtm;
  25. if (input.id) {
  26. fn += "." + input.id;
  27. } else {
  28. fn += "_" + _.random(0, 1000000);
  29. }
  30. fn += ".json";
  31. dfs.write(fn, JSON.stringify(input, " ", 4));
  32. output.filename = fn;
  33. // 刪除旧交件
  34. files = dfs.readdir(dir);
  35. dt = Date.now() - 1000 * 3600 * 24 * 10;
  36. for (i = 0; i < files.length; i++) {
  37. if (files[i].mtime < dt) {
  38. dfs.remove(files[i].fullname);
  39. }
  40. }
  41. // 留最后10个
  42. files.sort(function(a,b){ return a.mtime-b.mtime; });
  43. for (i = 0; i < files.length-10; i++) {
  44. dfs.remove(files[i].fullname);
  45. }
  46. }
  47. // 主执行阶段
  48. try {
  49. if (testcfg.storedfs) {
  50. teststoredfs();
  51. }
  52. // 数据合法性检查
  53. if (___datacheck___) {}
  54. // mql定义
  55. mql = `___mql___`;
  56. // 执行mql
  57. ret = odb.mql(mql, ___values___);
  58. // 打印完成信息
  59. output.info=___datainfo___;
  60. log.info(output.info);
  61. } catch(e) {
  62. if (typeof(e) == "object") {
  63. output.error = e;
  64. } else if (typeof(e) == "string") {
  65. output.error = "插库错误:" + e;
  66. } else {
  67. output.error = JSON.stringify(e);
  68. }
  69. log.error(output.error);
  70. }
  71. // 返回输出信息
  72. OUTPUT = output;