optimize recorder

This commit is contained in:
OttoMao 2015-08-06 23:13:17 +08:00
parent edaf638f3c
commit 39ae0f4648
7 changed files with 101 additions and 43 deletions

View File

@ -148,6 +148,12 @@ function Recorder(option){
db.find({},cb); db.find({},cb);
}; };
self.getRecords = function(idStart, limit, cb){
limit = limit || 10;
idStart = typeof idStart == "number" ? idStart : (id - limit);
db.find({ _id: { $gte: parseInt(idStart) } }).limit(limit).exec(cb);
};
self.db = db; self.db = db;
} }

View File

@ -1,58 +1,75 @@
var utils = require("./util"), var utils = require("./util"),
bodyParser = require("body-parser"), bodyParser = require("body-parser"),
path = require("path"), path = require("path"),
fs = require("fs"); fs = require("fs"),
Promise = require("promise");
var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(), var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(),
interceptFlag = false; interceptFlag = false;
//e.g. [ { keyword: 'aaa', local: '/Users/Stella/061739.pdf' } ] //e.g. [ { keyword: 'aaa', local: '/Users/Stella/061739.pdf' } ]
var mapConfig = []; var mapConfig = [],
configFile = "mapConfig.json";
var configFile = "mapConfig.json";
function saveMapConfig(content,cb){ function saveMapConfig(content,cb){
try{ new Promise(function(resolve,reject){
var anyproxyHome = utils.getAnyProxyHome(), var anyproxyHome = utils.getAnyProxyHome(),
mapCfgPath = path.join(anyproxyHome,configFile); mapCfgPath = path.join(anyproxyHome,configFile);
if(typeof content == "object"){ if(typeof content == "object"){
content = JSON.stringify(content); content = JSON.stringify(content);
} }
fs.writeFile(mapCfgPath,content,cb); resolve({
}catch(e){ path :mapCfgPath,
cb && cb(e) content :content
} });
})
.then(function(config){
return new Promise(function(resolve,reject){
fs.writeFile(config.path, config.content, function(e){
if(e){
reject(e);
}else{
resolve();
}
});
});
})
.catch(function(e){
cb && cb(e);
})
.done(function(){
cb && cb();
});
} }
function getMapConfig(cb){ function getMapConfig(cb){
try{ var read = Promise.denodeify(fs.readFile);
new Promise(function(resolve,reject){
var anyproxyHome = utils.getAnyProxyHome(), var anyproxyHome = utils.getAnyProxyHome(),
mapCfgPath = path.join(anyproxyHome,configFile); mapCfgPath = path.join(anyproxyHome,configFile);
fs.readFile(mapCfgPath,{encoding:"utf8"},function(err,content){ resolve(mapCfgPath);
if(err){ })
cb && cb(err) .then(read)
}else{ .then(function(content){
try{ return JSON.parse(content);
var obj = JSON.parse(content); })
cb && cb(null,obj); .catch(function(e){
}catch(e){ cb && cb(e);
cb && cb(e); })
} .done(function(obj){
} cb && cb(null,obj);
}); });
}catch(e){
cb && cb(e)
}
} }
//load saved config file setTimeout(function(){
getMapConfig(function(err,result){ //load saved config file
if(result){ getMapConfig(function(err,result){
mapConfig = result; if(result){
} mapConfig = result;
}); }
});
},1000);
module.exports = { module.exports = {

View File

@ -37,15 +37,15 @@ function webInterface(config){
return next(); return next();
}); });
// app.get("/summary",function(req,res){ app.get("/lastestLog",function(req,res){
// recorder.getSummaryList(function(err,docs){ recorder.getRecords(null,60,function(err,docs){
// if(err){ if(err){
// res.end(err.toString()); res.end(err.toString());
// }else{ }else{
// res.json(docs.slice(docs.length -500)); res.json(docs);
// } }
// }); });
// }); });
app.get("/fetchCrtFile",function(req,res){ app.get("/fetchCrtFile",function(req,res){
if(crtFilePath){ if(crtFilePath){
@ -108,6 +108,7 @@ function webInterface(config){
app.use(express.static(staticDir)); app.use(express.static(staticDir));
//plugin from rule file
if(typeof userRule._plugIntoWebinterface == "function"){ if(typeof userRule._plugIntoWebinterface == "function"){
userRule._plugIntoWebinterface(app,function(){ userRule._plugIntoWebinterface(app,function(){
app.listen(port); app.listen(port);

View File

@ -19,6 +19,7 @@
"juicer": "^0.6.6-stable", "juicer": "^0.6.6-stable",
"nedb": "^0.11.0", "nedb": "^0.11.0",
"npm": "^2.7.0", "npm": "^2.7.0",
"promise": "^7.0.4",
"qrcode-npm": "0.0.3", "qrcode-npm": "0.0.3",
"stream-throttle": "^0.1.3", "stream-throttle": "^0.1.3",
"ws": "^0.4.32" "ws": "^0.4.32"

View File

@ -114,6 +114,15 @@ var recorder;
} }
} }
function initRecordSet(){
$.getJSON("/lastestLog",function(res){
if(typeof res == "object"){
recordSet = res;
eventCenter.dispatchEvent("recordSetUpdated");
}
});
}
eventCenter.addListener("wsGetUpdate",updateRecordSet); eventCenter.addListener("wsGetUpdate",updateRecordSet);
eventCenter.addListener('recordSetUpdated',function(){ eventCenter.addListener('recordSetUpdated',function(){
@ -137,6 +146,8 @@ var recorder;
React.createElement(RecordPanel, {onSelect: showDetail}), React.createElement(RecordPanel, {onSelect: showDetail}),
document.getElementById("J_content") document.getElementById("J_content")
); );
initRecordSet();
})(); })();

View File

@ -160,6 +160,15 @@
} }
} }
function initRecordSet(){
$.getJSON("/lastestLog",function(res){
if(typeof res == "object"){
recordSet = res;
eventCenter.dispatchEvent("recordSetUpdated");
}
});
}
eventCenter.addListener("wsGetUpdate",updateRecordSet); eventCenter.addListener("wsGetUpdate",updateRecordSet);
eventCenter.addListener('recordSetUpdated',function(){ eventCenter.addListener('recordSetUpdated',function(){
@ -183,6 +192,8 @@
React.createElement(RecordPanel, {onSelect: showDetail}), React.createElement(RecordPanel, {onSelect: showDetail}),
document.getElementById("J_content") document.getElementById("J_content")
); );
initRecordSet();
})(); })();

View File

@ -114,6 +114,15 @@ var recorder;
} }
} }
function initRecordSet(){
$.getJSON("/lastestLog",function(res){
if(typeof res == "object"){
recordSet = res;
eventCenter.dispatchEvent("recordSetUpdated");
}
});
}
eventCenter.addListener("wsGetUpdate",updateRecordSet); eventCenter.addListener("wsGetUpdate",updateRecordSet);
eventCenter.addListener('recordSetUpdated',function(){ eventCenter.addListener('recordSetUpdated',function(){
@ -137,6 +146,8 @@ var recorder;
<RecordPanel onSelect={showDetail}/>, <RecordPanel onSelect={showDetail}/>,
document.getElementById("J_content") document.getElementById("J_content")
); );
initRecordSet();
})(); })();