Преглед изворни кода

add cache + report unit test case(Promise)

TonyKang пре 8 година
родитељ
комит
62022cf397

+ 12 - 0
config/cacheCfg.js

@@ -0,0 +1,12 @@
+/**
+ * Created by Tony on 2017/3/24.
+ */
+var rptUtil = require("../modules/reports/util/rpt_util");
+//rptUtil.setReportDefaultCache();
+
+module.exports = {
+    setupDftCache: function() {
+        rptUtil.setReportDefaultCache();
+        //and others...(if any)
+    }
+}

+ 3 - 5
modules/reports/controllers/rpt_controller.js

@@ -6,7 +6,8 @@ var JV = require('../rpt_component/Jpc_ValueDefine');
 var Template = require('../models/rpt_template');
 var Template = require('../models/rpt_template');
 var TemplateData = require('../models/rpt_temp_data');
 var TemplateData = require('../models/rpt_temp_data');
 var JpcEx = require('../rpt_component/JpcEx');
 var JpcEx = require('../rpt_component/JpcEx');
-var cache = require('../../../public/cache/cacheUtil');
+//var cache = require('../../../public/cache/cacheUtil');
+var rptUtil = require("../util/rpt_util");
 
 
 //统一回调函数
 //统一回调函数
 var callback = function(req, res, err, data){
 var callback = function(req, res, err, data){
@@ -31,10 +32,7 @@ module.exports = {
                     if (tplData) {
                     if (tplData) {
                         var printCom = JpcEx.createNew();
                         var printCom = JpcEx.createNew();
                         rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
                         rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
-                        var dftControls = cache.getCache("common_ctrls");
-                        var dftFonts = cache.getCache("common_fonts");
-                        var dftStyles = cache.getCache("common_styles");
-                        var defProperties = {ctrls: dftControls, fonts: dftFonts, styles: dftStyles};
+                        var defProperties = rptUtil.getReportDefaultCache();
                         printCom.initialize(rptTpl);
                         printCom.initialize(rptTpl);
                         printCom.analyzeData(rptTpl, tplData, defProperties);
                         printCom.analyzeData(rptTpl, tplData, defProperties);
                         var maxPages = printCom.totalPages;
                         var maxPages = printCom.totalPages;

+ 0 - 48
modules/reports/controllers/rtp_cfg_controller.js

@@ -1,48 +0,0 @@
-/**
- * Created by Tony on 2017/3/17.
- */
-var cmn_ctrl = require('../models/rpt_control');
-var cmn_font = require('../models/rpt_font');
-var cmn_style = require('../models/rpt_style');
-
-var callback = function(req, res, err, data){
-    if(err){
-        //res.send({success: false, error: err});
-        res.json({success: false, error: err});
-    }
-    else{
-        //res.send({success: true, data: data});
-        res.json({success:true, data: data});
-    }
-}
-
-module.exports = {
-    setReportDefaultCache: function (req, res) {
-        var err = "", defProperties = {ctrls: null, fonts: null, styles: null};
-        cmn_ctrl.getAll(null, function(err, ctrls){
-            if (ctrls) {
-                cache.common_ctrls = ctrls;
-                defProperties.ctrls = ctrls;
-            } else {
-                err += 'No default controls! ';
-            }
-        });
-        cmn_font.getAll(null, function(err, fonts){
-            if (fonts) {
-                cache.common_fonts = fonts;
-                defProperties.fonts = fonts;
-            } else {
-                err += 'No default fonts! ';
-            }
-        });
-        cmn_style.getAll(null, function(err, styles){
-            if (styles) {
-                cache.common_styles = styles;
-                defProperties.styles = styles;
-            } else {
-                err += 'No default styles!';
-            }
-        });
-        callback(req, res, err, defProperties);
-    }
-}

+ 2 - 2
modules/reports/models/cfg_control.js

@@ -20,7 +20,7 @@ var CtrlDAO = function(){};
 
 
 //根据id
 //根据id
 CtrlDAO.prototype.get = function(id, callback){
 CtrlDAO.prototype.get = function(id, callback){
-    Control.find({ID: id}, function(err, templates){
+    Control.find({ID: id}, '-_id', function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates[0]);
             callback(false, templates[0]);
         }
         }
@@ -31,7 +31,7 @@ CtrlDAO.prototype.get = function(id, callback){
 }
 }
 
 
 CtrlDAO.prototype.getAll = function(id, callback){
 CtrlDAO.prototype.getAll = function(id, callback){
-    Control.find({}, function(err, templates){
+    Control.find({}, '-_id', function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates);
             callback(false, templates);
         }
         }

+ 2 - 2
modules/reports/models/cfg_font.js

@@ -23,7 +23,7 @@ var FontDAO = function(){};
 
 
 //根据id
 //根据id
 FontDAO.prototype.get = function(id, callback){
 FontDAO.prototype.get = function(id, callback){
-    Font.find({ID: id}, function(err, templates){
+    Font.find({ID: id}, '-_id', function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates[0]);
             callback(false, templates[0]);
         }
         }
@@ -34,7 +34,7 @@ FontDAO.prototype.get = function(id, callback){
 }
 }
 
 
 FontDAO.prototype.getAll = function(id, callback){
 FontDAO.prototype.getAll = function(id, callback){
-    Font.find({}, function(err, templates){
+    Font.find({}, '-_id', function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates);
             callback(false, templates);
         }
         }

+ 2 - 2
modules/reports/models/cfg_style.js

@@ -16,7 +16,7 @@ var StyleDAO = function(){};
 
 
 //根据id
 //根据id
 StyleDAO.prototype.get = function(id, callback){
 StyleDAO.prototype.get = function(id, callback){
-    Style.find({ID: id}, function(err, templates){
+    Style.find({ID: id}, '-_id', function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates[0]);
             callback(false, templates[0]);
         }
         }
@@ -27,7 +27,7 @@ StyleDAO.prototype.get = function(id, callback){
 }
 }
 
 
 StyleDAO.prototype.getAll = function(id, callback){
 StyleDAO.prototype.getAll = function(id, callback){
-    Style.find({}, function(err, templates){
+    Style.find({}, '-_id', function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates);
             callback(false, templates);
         }
         }

+ 5 - 2
modules/reports/models/rpt_temp_data.js

@@ -18,8 +18,6 @@ var RplTplDataDAO = function(){};
 
 
 //根据id获取临时数据
 //根据id获取临时数据
 RplTplDataDAO.prototype.get = function(tpl_id, callback){
 RplTplDataDAO.prototype.get = function(tpl_id, callback){
-    //TemplateData.find({"Data_Key": tpl_id}, function(err, templates){
-    //TemplateData.find({}, function(err, templates){
     TemplateData.find({"Data_Key": tpl_id}, function(err, templates){
     TemplateData.find({"Data_Key": tpl_id}, function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates[0]);
             callback(false, templates[0]);
@@ -28,6 +26,11 @@ RplTplDataDAO.prototype.get = function(tpl_id, callback){
             callback('查找不到模板临时数据!');
             callback('查找不到模板临时数据!');
         }
         }
     })
     })
+};
+
+RplTplDataDAO.prototype.getPromise = function(tpl_id, callback){
+    return TemplateData.findOne({"Data_Key": tpl_id}).exec();
 }
 }
 
 
+
 module.exports = new RplTplDataDAO();
 module.exports = new RplTplDataDAO();

+ 6 - 2
modules/reports/models/rpt_template.js

@@ -22,9 +22,8 @@ var Template = smartcostdb.model("rpt_templates", RptTemplateSchema);
 
 
 var RplTplDAO = function(){};
 var RplTplDAO = function(){};
 
 
-//根据id获取试卷
 RplTplDAO.prototype.get = function(grpId, id, callback){
 RplTplDAO.prototype.get = function(grpId, id, callback){
-    Template.find({GROUP_KEY: grpId, ID_KEY: id}, function(err, templates){
+    Template.find({GROUP_KEY: grpId, ID_KEY: id}, '-_id', function(err, templates){
         if(templates.length){
         if(templates.length){
             callback(false, templates[0]);
             callback(false, templates[0]);
         }
         }
@@ -32,6 +31,11 @@ RplTplDAO.prototype.get = function(grpId, id, callback){
             callback('查找不到报表模板!');
             callback('查找不到报表模板!');
         }
         }
     })
     })
+};
+
+RplTplDAO.prototype.getPromise = function(grpId, id){
+    var rst = Template.findOne({GROUP_KEY: grpId, ID_KEY: id}, '-_id').exec() ;
+    return rst;
 }
 }
 
 
 module.exports = new RplTplDAO();
 module.exports = new RplTplDAO();

+ 36 - 0
modules/reports/util/rpt_util.js

@@ -0,0 +1,36 @@
+/**
+ * Created by Tony on 2017/3/24.
+ */
+var cache = require('../../../public/cache/cacheUtil');
+var cmn_ctrl = require('../models/cfg_control');
+var cmn_font = require('../models/cfg_font');
+var cmn_style = require('../models/cfg_style');
+
+const RPT_CFG_GRP = 'rpt_cfg';
+
+module.exports = {
+    setReportDefaultCache: function () {
+        cmn_ctrl.getAll(null, function(err, ctrls){
+            if (ctrls) {
+                cache.setCache(RPT_CFG_GRP,'common_ctrls',ctrls);
+            }
+        });
+        cmn_font.getAll(null, function(err, fonts){
+            if (fonts) {
+                cache.setCache(RPT_CFG_GRP,'common_fonts',fonts);
+            }
+        });
+        cmn_style.getAll(null, function(err, styles){
+            if (styles) {
+                cache.setCache(RPT_CFG_GRP,'common_styles',styles);
+            }
+        });
+    },
+    getReportDefaultCache: function () {
+        var rst = {ctrls: null, fonts: null, styles: null};
+        rst.ctrls = cache.getCache(RPT_CFG_GRP,'common_ctrls');
+        rst.fonts = cache.getCache(RPT_CFG_GRP,'common_fonts');
+        rst.styles = cache.getCache(RPT_CFG_GRP,'common_styles');
+        return rst;
+    }
+}

+ 7 - 2
server.js

@@ -3,6 +3,9 @@ var express = require('express');
 var config = require("./config/config.js");
 var config = require("./config/config.js");
 //config.setToLocalDb();
 //config.setToLocalDb();
 config.setToQaDb();
 config.setToQaDb();
+//config.setupCache();
+var cfgCacheUtil = require("./config/cacheCfg");
+cfgCacheUtil.setupDftCache();
 
 
 var path = require('path');
 var path = require('path');
 var session = require('express-session');
 var session = require('express-session');
@@ -82,8 +85,10 @@ app.use(function(req, res, next) {
 	res.status(404).sendFile(_rootDir+'/src/404.html');
 	res.status(404).sendFile(_rootDir+'/src/404.html');
 });
 });
 app.use(function(err, req, res, next) {
 app.use(function(err, req, res, next) {
-	console.error(err.stack);
-	res.status(500).send('500 Error');
+    console.error(err.stack);
+    res.status(500).send('500 Error');
 });
 });
 
 
 app.listen(6060);
 app.listen(6060);
+
+

+ 25 - 1
test/unit/cache/testCacheUsage.js

@@ -1,8 +1,11 @@
 /**
 /**
  * Created by Tony on 2017/3/20.
  * Created by Tony on 2017/3/20.
  */
  */
+var mongoose = require('mongoose');
 var test = require('tape');
 var test = require('tape');
 var cache = require('../../../public/cache/cacheUtil');
 var cache = require('../../../public/cache/cacheUtil');
+var rptUtil = require("../../../modules/reports/util/rpt_util");
+//var cfgCacheUtil = require("../../../config/cacheCfg");
 
 
 test('test cache usage:', function(t) {
 test('test cache usage:', function(t) {
     cache.setCache("unit_Test_Grp","unit_TestKey","hello my cache!");
     cache.setCache("unit_Test_Grp","unit_TestKey","hello my cache!");
@@ -10,4 +13,25 @@ test('test cache usage:', function(t) {
     console.log(msg);
     console.log(msg);
     t.equal(msg, "hello my cache!")
     t.equal(msg, "hello my cache!")
     t.end();
     t.end();
-})
+});
+
+test('test report default config cache', function(t){
+    //setReportDefaultCache
+    rptUtil.setReportDefaultCache();
+    //cfgCacheUtil.setupDftRptCache();
+    setTimeout(function(){
+        var cfg = rptUtil.getReportDefaultCache();
+        console.log(cfg)
+        t.equal(cfg.ctrls != null, true);
+        t.equal(cfg.fonts != null, true);
+        t.equal(cfg.styles != null, true);
+        //t.pass('just pass');
+        t.end();
+    }, 1000 );
+});
+
+test('finish', function (t) {
+    mongoose.disconnect();
+    t.pass('closing db connection');
+    t.end();
+});

+ 36 - 34
test/unit/reports/testRpt.js

@@ -2,48 +2,50 @@
  * Created by Tony on 2017/3/17.
  * Created by Tony on 2017/3/17.
  */
  */
 var test = require('tape');
 var test = require('tape');
-var cmn_ctrl = require('../../../modules/reports/models/cfg_control');
-var cmn_font = require('../../../modules/reports/models/cfg_font');
-var cmn_style = require('../../../modules/reports/models/cfg_style');
 var mongoose = require('mongoose');
 var mongoose = require('mongoose');
+var JV = require('../../../modules/reports/rpt_component/Jpc_ValueDefine');
+var Template = require('../../../modules/reports/models/rpt_template');
+var TemplateData = require('../../../modules/reports/models/rpt_temp_data');
+var JpcEx = require('../../../modules/reports/rpt_component/JpcEx');
+var rptUtil = require("../../../modules/reports/util/rpt_util");
 
 
 test('test get report pages function: ', function (t) {
 test('test get report pages function: ', function (t) {
-    /*/
-    t.plan(1);
-    t.equal(2 + 3, 5);
-    /*/
-    cmn_ctrl.getAll(null, function(err, ctrls){
-        t.notEqual(ctrls, null);
-        t.end();
-        return true;
-    });
-    //*/
+    rptUtil.setReportDefaultCache();
+    t.pass('just pass for cache setup!');
+    t.end();
 });
 });
 
 
 test('test get report pages function: ', function (t) {
 test('test get report pages function: ', function (t) {
-    /*/
-    t.plan(1);
-    t.equal(7 * 8 + 9, 65);
-    /*/
-    cmn_font.getAll(null, function(err, fonts){
-        t.notEqual(fonts, null);
+    var grp_id = "SC";
+    var tpl_id = "07_1";
+    var pageSize = "A4";
+    var rptTpl = null, tplData = null;
+    Template.getPromise(grp_id, tpl_id).then(function(rst) {
+        //console.log(rst);
+        rptTpl = rst;
+        console.log(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE]);
+        t.equal(rptTpl != null, true);
+        return TemplateData.getPromise(tpl_id);
+    }).then(function(rst){
+        //console.log(rst);
+        tplData = rst;
+        t.equal(tplData != null, true);
+        if (tplData) {
+            var printCom = JpcEx.createNew();
+            rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
+            var defProperties = rptUtil.getReportDefaultCache();
+            printCom.initialize(rptTpl);
+            printCom.analyzeData(rptTpl, tplData, defProperties);
+            var maxPages = printCom.totalPages;
+            var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
+            t.equal(pageRst != null, true);
+        }
         t.end();
         t.end();
-        return true;
-    });
-    //*/
-});
-
-test('test get report pages function: ', function (t) {
-    /*/
-    t.plan(1);
-    t.equal(1+2, 65);
-    /*/
-    cmn_style.getAll(null, function(err, styles){
-        t.notEqual(styles, null);
+    }).catch(function (err) {
+        console.log("err: " + err);
         t.end();
         t.end();
-        return true;
-    });
-    //*/
+    })
+
 });
 });
 
 
 test('finish', function (t) {
 test('finish', function (t) {