libf 2 年 前
コミット
d66721ed91

+ 69 - 65
json_importer_gen_v2/InitJsonImporter.js

@@ -5,60 +5,60 @@ defaultinput = {
     clsoption: "with ttl=10 day, version=true, nickname='test'",
     data: null,
     datafilename: "",
-    datafiledir: "/opt/cncc/ITIL/change_main",
-    jsfilename: "/script/cncc/ITIL/测试.js",
-    fieldmap: {id: "jid"},
-    fields: {idUnique: "string,key"},
+    datafiledir: "/opt/cncc/data/test",
+    jsfilename: "/script/cncc/api/测试.js",
+    fieldmap: { id: "jid" },
+    fields: { idUnique: "string,key" },
     mustfield: "id,idUnique",
 };
 inputmapping = {
     "change": {
-        classname: "/cncc/action/change_main",
-        datafiledir: "/opt/cncc/ITIL/change_main",
-        jsfilename: "/script/cncc/ITIL/变更单.js",
-        clsoption: "with ttl=10 day, version=true, nickname='change_main'",
+        classname: "/cncc/action/v2_change_main",
+        datafiledir: "/opt/cncc/action/change_main",
+        jsfilename: "/script/cncc/ITIL/v2_变更单.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_change_main'",
     },
     "event": {
-        classname: "/cncc/action/event",
+        classname: "/cncc/action/v2_event",
         datafiledir: "/opt/cncc/ITIL/event",
-        jsfilename: "/script/cncc/ITIL/事件单.js",
-        clsoption: "with ttl=10 day, version=true, nickname='event'",
+        jsfilename: "/script/cncc/ITIL/v2_事件单.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_event'",
     },
     "problem": {
-        classname: "/cncc/action/problem",
+        classname: "/cncc/action/v2_problem",
         datafiledir: "/opt/cncc/ITIL/problem",
-        jsfilename: "/script/cncc/ITIL/问题单.js",
-        clsoption: "with ttl=10 day, version=true, nickname='problem'",
+        jsfilename: "/script/cncc/ITIL/v2_问题单.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_problem'",
     },
     "report": {
-        classname: "/cncc/action/report",
+        classname: "/cncc/action/v2_report",
         datafiledir: "/opt/cncc/ITIL/report",
-        jsfilename: "/script/cncc/ITIL/报备.js",
-        clsoption: "with ttl=10 day, version=true, nickname='report'",
+        jsfilename: "/script/cncc/ITIL/v2_报备.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_report'",
     },
     "device_power_on_off": {
-        classname: "/cncc/action/device_power_on_off",
+        classname: "/cncc/action/v2_device_power_on_off",
         datafiledir: "/opt/cncc/ITIL/device_power_on_off",
-        jsfilename: "/script/cncc/ITIL/设备上下电.js",
-        clsoption: "with ttl=10 day, version=true, nickname='device_power_on_off'",
+        jsfilename: "/script/cncc/ITIL/v2_设备上下电.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_device_power_on_off'",
     },
     "data_collect": {
-        classname: "/cncc/action/data_collect",
+        classname: "/cncc/action/v2_data_collect",
         datafiledir: "/opt/cncc/ITIL/data_collect",
-        jsfilename: "/script/cncc/ITIL/数据获取.js",
-        clsoption: "with ttl=10 day, version=true, nickname='data_collect'",
+        jsfilename: "/script/cncc/ITIL/v2_数据获取.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_data_collect'",
     },
     "drill": {
-        classname: "/cncc/action/drill",
+        classname: "/cncc/action/v2_drill",
         datafiledir: "/opt/cncc/ITIL/drill",
-        jsfilename: "/script/cncc/ITIL/应急演练.js",
-        clsoption: "with ttl=10 day, version=true, nickname='drill'",
+        jsfilename: "/script/cncc/ITIL/v2_应急演练.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_drill'",
     },
     "implement": {
-        classname: "/cncc/action/implement",
+        classname: "/cncc/action/v2_implement",
         datafiledir: "/opt/cncc/ITIL/implement",
-        jsfilename: "/script/cncc/ITIL/项目实施或巡检.js",
-        clsoption: "with ttl=10 day, version=true, nickname='implement'",
+        jsfilename: "/script/cncc/ITIL/v2_项目实施或巡检.js",
+        clsoption: "with ttl=10 day, version=true, nickname='v2_implement'",
     }
 };
 
@@ -67,29 +67,32 @@ input = INPUT;
 output = {};
 try {
     input = decodeURIComponent(input);
-} catch(e) {}
+} catch (e) { }
 try {
     input = base64.decode(input);
-} catch(e) {}
+} catch (e) { }
 try {
     input = JSON.parse(input);
     input = JSON.parse(input);
-} catch(e) {}
+} catch (e) { }
 
 // 通用函数
 function isempty(o) {
     if (o) {
-        for (var k in o){
+        for (var k in o) {
             return false;
         }
-    } 
+    }
     return true;
 }
 
-// 主执行阶段
+// 引用函数库
+eval(dfs.read("/script/matrix/utils/ajs/JsonImporterFuncs.js"));
+
+// 定义主程序
 function main() {
     output.info = {};
-    output.info.p0 = {name: "确认参数"};
+    output.info.p0 = { name: "确认参数" };
     p = inputmapping[input.name];
     if (p) {
         for (var k in p) {
@@ -111,7 +114,7 @@ function main() {
     if (classname[0] != "/") {
         throw ("classname必须以 / 开头");
     }
-    if (classname.substring(0,8) == "/matrix/") {
+    if (classname.substring(0, 8) == "/matrix/") {
         throw ("classname不能以 /matrix/ 开头");
     }
     data = input.data;
@@ -126,7 +129,7 @@ function main() {
                 try {
                     data = dfs.read(datafilename);
                     data = JSON.parse(data);
-                } catch(e) {}
+                } catch (e) { }
             }
         }
         if (isempty(data)) {
@@ -141,41 +144,41 @@ function main() {
             // 读取所有文件,综合数据
             data = {};
             files = dfs.readdir(datafiledir);
-            for (i = 0; i < files.length; i++) { 
+            for (i = 0; i < files.length; i++) {
                 try {
                     adat = dfs.read(files[i].fullname);
                     adat = JSON.parse(adat);
-                    if (typeof(adat) != "object") {
-                        throw("只接收JSON对象数据");
+                    if (typeof (adat) != "object") {
+                        throw ("只接收JSON对象数据");
                     }
                     for (var k in adat) {
                         if (!data[k]) {
                             data[k] = adat[k];
                         } else if (adat[k]) {
-                            if (typeof(adat[k]) == "object") {
+                            if (typeof (adat[k]) == "object") {
                                 if (JSON.stringify(adat[k]).length > JSON.stringify(data[k]).length) {
                                     data[k] = adat[k];
                                 }
-                            } else if (typeof(adat[k]) == "string") {
-                                if (typeof(data[k]) != "string") {
+                            } else if (typeof (adat[k]) == "string") {
+                                if (typeof (data[k]) != "string") {
                                     data[k] = adat[k];
                                 } else if (adat[k].length > data[k].length) {
                                     data[k] = adat[k];
                                 }
-                            } else if (typeof(adat[k]) == "number") {
-                                if (typeof(data[k]) == "number") {
-                                    if ((""+adat[k]).indexOf(".")>=0) {
+                            } else if (typeof (adat[k]) == "number") {
+                                if (typeof (data[k]) == "number") {
+                                    if (("" + adat[k]).indexOf(".") >= 0) {
                                         data[k] = adat[k];
                                     }
                                 }
-                            } else if (typeof(adat[k]) == "boolean") {
-                                if (typeof(data[k]) != "boolean") {
-                                    data[k] = ""+adat[k];
+                            } else if (typeof (adat[k]) == "boolean") {
+                                if (typeof (data[k]) != "boolean") {
+                                    data[k] = "" + adat[k];
                                 }
                             }
                         }
                     }
-                } catch(e) {}
+                } catch (e) { }
             }
         }
     }
@@ -200,42 +203,43 @@ function main() {
         fields = defaultinput.fields;
     }
     reqinput = {
-        classname: classname, 
+        classname: classname,
         jsfilename: jsfilename,
-        clsoption: clsoption, 
+        clsoption: clsoption,
         fieldmap: fieldmap,
         fields: fields,
         mustfield: mustfield,
         data: data,
     };
-    output.info.p1 = {name:"生成类"};
-    output.info.p1.result = alterClass(reqinput)
+    output.info.p1 = { name: "生成类" };
+    output.info.p1.result = alterClass(reqinput);
     if (output.info.p1.result.error) {
         return;
     }
-    output.info.p2 = {name:"生成脚本"};
-    output.info.p2.result = generateJsonImporterJS(reqinput)
+    output.info.p2 = { name: "生成脚本" };
+    output.info.p2.result = generateJsonImporterJS(reqinput);
     if (output.info.p2.result.error) {
         return;
     }
-    output.info.p3 = {name:"激活脚本"};
-    output.info.p3.result = activeServerJS(jsfilename)
+    output.info.p3 = { name: "激活脚本" };
+    output.info.p3.result = activeServerJS(jsfilename);
     if (output.info.p3.result.error) {
         return;
     }
-    output.info.p4 = {name:"验证测试"};
-    output.info.p4.result = runServerJS(jsfilename, data)
+    output.info.p4 = { name: "验证测试" };
+    output.info.p4.result = runServerJS(jsfilename, data);
     if (output.info.p4.result.error) {
         return;
     }
-    output.info.p5 = {name:"完成"};
+    output.info.p5 = { name: "完成" };
 }
+// 执行主程序
 try {
     main();
-} catch(e) {
-    if (typeof(e) == "object") {
+} catch (e) {
+    if (typeof (e) == "object") {
         output.error = e;
-    } else if (typeof(e) == "string") {
+    } else if (typeof (e) == "string") {
         output.error = "错误:" + e;
     } else {
         output.error = JSON.stringify(e);

+ 6 - 2
json_importer_gen_v2/JsonImporter.template.js

@@ -15,7 +15,7 @@ try {
 } catch(e) {}
 output = {};
 
-testcfg = {}
+testcfg = {};
 try {
     testcfg = JSON.parse(etcd.get("/api/test.json"));
 } catch(e) {}
@@ -48,9 +48,13 @@ function teststoredfs() {
 }
 // 主执行阶段
 try {
-    if (testcfg.storedfs) {
+    if (testcfg.stoedfs) {
         teststoredfs();
     }
+    if (testcfg.checkfields) {
+        eval(dfg.read("/script/matrix/utils/ajs/checkfields.js"));
+        checkfields();
+    }
     // 数据合法性检查
     if (___datacheck___) {}
     // mql定义

+ 13 - 13
json_importer_gen_v2/jsonImporterFuncs.js

@@ -1,7 +1,7 @@
 // JSON 对象数据导入数据库的函数库
 
 // 应用配置信息
-eval(dfs.read("/script/matrix/utils/config.js"))
+eval(dfs.read("/script/matrix/utils/ajs/config.js"))
 
 // 根据 JSON 对象数据建类或新增字段
 // classname 类名
@@ -11,7 +11,7 @@ eval(dfs.read("/script/matrix/utils/config.js"))
 // fields    字段类型
 function alterClass(input) {
     // 返回信息
-    output = {}
+    var output = {};
     // 参数检查
     if (!input.classname) { // like /cncc/itil/project
         throw ("需要指定classname");
@@ -70,7 +70,7 @@ function alterClass(input) {
             }
         } catch(e) {}
     }
-    output.info.clsfields = clsfields;
+    output.clsfields = clsfields;
 
     // 通过参数确定的字段类型信息
     fields = input.fields;
@@ -177,7 +177,7 @@ function alterClass(input) {
         }
         xfields[xk] = dt;
     }
-    output.info.xfields = xfields;
+    output.xfields = xfields;
     
     if (!clsexist) {
         // 类不存在,自动创建类
@@ -217,7 +217,7 @@ function alterClass(input) {
             mql += input.clsoption;
         }
         odb.mql(mql);
-        output.info = {mql:mql};
+        output = {mql:mql};
     } else {
         // 类已存在,检查主键信息是否存在
         for (var pki = 0; pki < xpks.length; pki++) {
@@ -241,7 +241,7 @@ function alterClass(input) {
         }
         if (addn > 0) {
             odb.mql(mql);
-            output.info = {mql:mql};
+            output = {mql:mql};
         }
     }
     return output
@@ -249,7 +249,7 @@ function alterClass(input) {
 
 // 根据 JSON 数据生成导入脚本
 function generateJsonImporterJS(input) {
-    output = {}
+    var output = {};
     // 参数检查
     if (!input.classname) { // like /cncc/itil/project
         throw ("需要指定classname");
@@ -371,7 +371,7 @@ function generateJsonImporterJS(input) {
     datainfo += "\n    ";
     datainfo += "}";
 
-    importjs = dfs.read("/script/matrix/utils/JsonImporter.template.js");
+    importjs = dfs.read("/script/matrix/utils/ajs/JsonImporter.template.js");
     importjs = importjs.replace(/___istesting___/mg, input.istesting);
     importjs = importjs.replace(/___classname___/mg, input.classname);
     importjs = importjs.replace(/___datetime_now___/mg, dtm);
@@ -388,7 +388,7 @@ function generateJsonImporterJS(input) {
 
 // 激活脚本,生成的脚本文件需要激活后才能生效
 function activeServerJS(jsfilename) {
-    result = {}
+    result = {};
     calljsfp = encodeURIComponent(jsfilename.replace(/\/script/, ""));
     http.do("POST", 
         cfg.server+"/fs/tolocal/script"+calljsfp+"?issys=true",
@@ -412,12 +412,12 @@ function activeServerJS(jsfilename) {
 
 // 验证测试执行脚本
 function runServerJS(jsfilename, data) {
-    result = {}
+    result = {};
     reqinput = encodeURIComponent(base64.encode(JSON.stringify(data)));
     calljsfp = encodeURIComponent(jsfilename.replace(/\/script/, ""));
     http.do("POST", 
-        server+"/script/exec/js?filepath="+calljsfp+"&input="+reqinput,
-        {"Authorization": "Basic "+author},
+        cfg.server+"/script/exec/js?filepath="+calljsfp+"&input="+reqinput,
+        {"Authorization": "Basic "+cfg.author},
         '',
         function(response){
             // success func
@@ -433,4 +433,4 @@ function runServerJS(jsfilename, data) {
             result.error = response.data;
         });
     return result;
-}
+}

+ 4 - 0
ajs/checkfields.js

@@ -0,0 +1,4 @@
+
+function checkfields() {
+
+}

+ 3 - 3
json_importer_gen_v2/config.js

@@ -3,8 +3,8 @@ cfg = {
     author: base64.encode("meta.admin:admin"),
 };
 try{
-    etc = JSON.parse(etcd.get("api/cfg.json"))
-    for(k in etc) {
-        cfg[k] = etc[k]
+    etc = JSON.parse(etcd.get("api/cfg.json"));
+    for(var k in etc) {
+        cfg[k] = etc[k];
     }
 }catch(e){}

+ 19 - 0
ajs/test.js

@@ -0,0 +1,19 @@
+
+output = {};
+
+try {
+    eval(dfs.read("/script/matrix/utils/ajs/JsonImporterFuncs.js"));
+
+    output.result = runServerJS("/matrix/utils/ajs/InitJsonImporter.js", {name:"change"});
+} catch (e) {
+    if (typeof (e) == "object") {
+        output.error = e;
+    } else if (typeof (e) == "string") {
+        output.error = "错误:" + e;
+    } else {
+        output.error = JSON.stringify(e);
+    }
+}
+
+// 返回输出信息
+OUTPUT = output;