Authored by 周奇琪

Merge branch 'feature/influxdb-log' into develop

Conflicts:
	library/api.js
... ... @@ -3,14 +3,18 @@
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/4/25
*/
'use strict';
var express = require('express'),
let express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
favicon = require('serve-favicon'),
cookieParser = require('cookie-parser');
cookieParser = require('cookie-parser'),
favicon = require('serve-favicon');
var app = express();
require('express-handlebars');
let app = express();
app.set('view engine', '.hbs');
... ...
/**
* sub app guang
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
var express = require('express'),
path = require('path'),
hbs = require('express-handlebars');
var app = express();
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
app.set('views', path.join(__dirname, 'views/action'));
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: ['./views/partial', `${doraemon}/partial`],
helpers: 'helpers'
}));
// router
app.use(require('./router'));
module.exports = app;
... ...
/**
* router of sub app guang
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
'use strict';
const router = require('express').Router();
const cRoot = './controllers';
let API = require('../../library/api');
// Your controller here
router.get('/', function(req, res) {
let api = new API();
let name = '/productColor/queryProductColors';
api.get(name, {}).then(function(body) {
res.send(body);
}).catch(function(error) {
res.send('some thing wrong');
});
});
module.exports = router;
... ...
/**
* 系统配置
*
* @author hbomb qiqi.zhou@yoho.cn
* @date 2016/05/06
*/
module.exports = {
domains: {
api:'http://192.168.102.202:8088/platform'
},
loggers: {
file:{
level:'info',
maxsize:100 * 1024 * 1024,
handleExceptions:true,
zippedArchive:true,
timestamp:true,
filename:'info.log'
},
udp: { // send by udp
level:'debug', // logger level
host:'192.168.102.162', // influxdb host
port:'4444'// influxdb port
},
'console':{
level:'debug',
colorize: 'all',
prettyPrint:true
}
}
};
... ...
... ... @@ -8,8 +8,12 @@
const rp = require('request-promise');
const _ = require('lodash');
const log = require('./logger');
const api = require('../config/common').domains.api;
const Timer = require('./timer');
const ApiUrl = 'http://testapi.yoho.cn:28078/';
const ApiUrl = api;
class API {
... ... @@ -19,10 +23,26 @@ class API {
* @param data Obejct
*/
get(url, data) {
return rp({
let options = {
url: `${ApiUrl}${url}`,
qs: data
};
let timer = new Timer();
timer.put('getApi');// 统计时间开始
let ret = rp(options);
ret.then((body)=>{
let duration = timer.put('getApi');// 接口返回
log.info('API GET: %s, parms: %j , durationMs: %d ms , body: %s', options.url, options.qs, duration, body);
}).catch((error)=>{
let duration = timer.put('getApi');// 接口返回
log.error('API GET: %s, parms: %j , durationMs: %d ms error: %s , statusCode: %d', options.url, options.qs, duration, error.message, error.statusCode);
});
return ret;
}
/**
... ...
/**
* 日志工具类
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
'use strict';
let winston = require('winston'),
config = require('../config/common');
require('influxdb-winston');
let logger = new (winston.Logger)({
transports: [
new (winston.transports.File)(config.loggers.file),
new (winston.transports.UdpTransport)(config.loggers.udp),
new (winston.transports.Console)(config.loggers.console)
]
});
module.exports = logger;
... ...
'use strict';
/**
* 计时类
* @example
* let timer = new Timer();
* timer.put('profile');
* timer.put('proflie'); // console output: 12.14
*
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/07
*/
class Timer {
constructor() {
this.timers = {};
}
/**
* 打点计时
*/
put(label) {
let labelTime = this.timers[label];
if (labelTime) {
let duration = process.hrtime(labelTime);
return this._round(duration[1]);
} else {
this.timers[label] = process.hrtime();
}
}
/**
* 格式化成毫秒
* @param {Number} value 纳秒
*/
_round(value) {
return Math.round(value / 10000) / 100;
}
}
module.exports = Timer;
... ...
... ... @@ -12,8 +12,8 @@
"dev": "node_modules/.bin/nodemon -e js,hbs -i public/ app.js",
"online": "NODE_ENV=\"production\" node app.js",
"debug": "DEBUG=\"express:*\" node app.js",
"lint-js": "node_modules/.bin/eslint -c .eslintrc --cache --fix `git diff --cached --name-only --diff-filter=ACM | grep .js$` app.js",
"lint-css": "node_modules/.bin/stylelint --config .stylelintrc `git diff --cached --name-only --diff-filter=ACM | grep .css$`",
"lint-js": "file=`git diff --cached --name-only --diff-filter=ACM | grep .js$`;node_modules/.bin/eslint -c .eslintrc --cache --fix $file app.js",
"lint-css": "file=`git diff --cached --name-only --diff-filter=ACM | grep .css$`;node_modules/.bin/stylelint --config .stylelintrc $file",
"precommit": "npm run lint-js && npm run lint-css"
},
"license": "MIT",
... ... @@ -22,11 +22,13 @@
"cookie-parser": "^1.4.1",
"express": "^4.13.1",
"express-handlebars": "^3.0.0",
"influxdb-winston": "^1.0.1",
"lodash": "^4.8.2",
"md5": "^2.1.0",
"morgan": "^1.7.0",
"request-promise": "^2.0.1",
"serve-favicon": "^2.3.0"
"serve-favicon": "^2.3.0",
"winston": "^2.2.0"
},
"devDependencies": {
"autoprefixer": "^6.3.6",
... ... @@ -39,6 +41,8 @@
"gulp-sourcemaps": "^2.0.0-alpha",
"gulp-util": "^3.0.7",
"husky": "^0.11.4",
"mocha": "^2.4.5",
"nodemon": "1.9.2",
"postcss-assets": "^4.0.1",
"postcss-cachebuster": "^0.1.2",
"postcss-calc": "^5.2.1",
... ... @@ -51,11 +55,12 @@
"postcss-sprites": "^3.1.2",
"postcss-use": "^2.0.2",
"precss": "^1.4.0",
"rewire": "^2.5.1",
"shelljs": "^0.7.0",
"stylelint": "^6.2.2",
"stylelint-config-yoho": "^1.2.0",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.14.1",
"webpack-stream": "^3.1.0",
"shelljs": "^0.7.0"
"webpack-stream": "^3.1.0"
}
}
... ...
let expect = require('expect.js');
let Timer = require('../../library/timer');
describe('/library/timer', function() {
it('延迟100ms,期望大于或等于100ms', function(done) {
let t = new Timer();
t.put('aa');
setTimeout(function() {
let time = t.put('aa');
expect(Math.round(time) >= 100).to.be.ok();
done();
}, 100);
});
});
... ...
require('./library/timer.test');
... ...