Skip to content

Commit

Permalink
feat: report commands before execute (#134)
Browse files Browse the repository at this point in the history
* refactor: remove useless branch and code

* feat: feflow report
  • Loading branch information
fXy-during authored and cpselvis committed Nov 20, 2019
1 parent 7d5d511 commit ca20a16
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/feflow-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"typescript": "^3.5.2"
},
"dependencies": {
"@feflow/report": "^0.0.5",
"abbrev": "^1.1.1",
"bunyan": "^1.8.12",
"chalk": "^2.4.2",
Expand Down
5 changes: 5 additions & 0 deletions packages/feflow-cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Feflow from '../core';
import figlet from 'figlet';
import minimist from 'minimist';
import semver from 'semver';
import Report from '@feflow/report';
const pkg = require('../../package.json');

const checkNodeVersion = (wanted: any, id: string) => {
Expand Down Expand Up @@ -47,8 +48,10 @@ export default function entry() {

const feflow = new Feflow(args);
const { commander, logger } = feflow;
const report = new Report(feflow);

if (args.v || args.version) {
report.report('version', args);
console.log(chalk.green(pkg.version));
return;
}
Expand All @@ -72,6 +75,8 @@ export default function entry() {
cmd = 'help';
}

report.report(cmd, args);

return feflow.call(cmd, feflow).then(() => {
logger.debug(`call ${cmd} success`);
}).catch((err) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/feflow-report/src/api/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
REPORT_URL: 'http://193.112.196.252/api/v1/report/command'
REPORT_URL: 'http://193.112.196.252/api/v1/report/command',
REPORT_PROXY: 'http://127.0.0.1:12639'
};
58 changes: 54 additions & 4 deletions packages/feflow-report/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,70 @@
import rp from 'request-promise';
import URI from './config';
import shell from 'shelljs';

export default {
report(param) {
console.log('param', param);
log: {},
report(param, log) {
this.log = log || {};
const proxyIsOn = shell
.exec(`curl ${URI.REPORT_PROXY}`, { silent: true })
.stdout.trim();

if (proxyIsOn) {
log.debug('Report, chose proxy curl');
this.reportWithProxy(param);
} else {
log.debug('Report, chose normal http');
this.reportWithoutProxy(param);
}
},
reportWithoutProxy(param) {
rp({
method: 'POST',
uri: URI.REPORT_URL,
body: param,
json: true
})
.then(res => {
console.log('got report response', res);
this.log.debug('got report response', res);
})
.catch(e => {
console.log('rp report fail', e);
this.log.debug('feflow report fail', e);
});
},
reportWithProxy(param) {
let dataString = '';
// get formated report params
Object.keys(param).forEach(key => {
let value = param[key];
value = typeof value == 'object' ? JSON.stringify(value) : value;
if (value != undefined) {
dataString += `&${key}=${value}`;
}
});

const cmdWithProxy = [];

// curl
cmdWithProxy.push('curl');

// proxy
cmdWithProxy.push('-x');
cmdWithProxy.push(URI.REPORT_PROXY);

// url
cmdWithProxy.push(URI.REPORT_URL);

// params
cmdWithProxy.push('-d');
cmdWithProxy.push("'" + dataString.slice(1) + "'");

this.log.debug("cmdWithProxy.join(' ')", cmdWithProxy.join(' '));

const res = shell
.exec(cmdWithProxy.join(' '), { silent: true })
.stdout.trim();

this.log.debug('res', res);
}
};
59 changes: 22 additions & 37 deletions packages/feflow-report/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ import {
getSystemInfoByOS,
getProjectByPackage
} from './common/utils';
import objectFactory from "./common/objectFactory";
import objectFactory from './common/objectFactory';

interface ReportContext {
base: String; // pathFn.join(osenv.home(), './.feflow');
rcPath: String; // pathFn.join(base, '.feflowrc.yml');
version: String; // pkg.version;
baseDir: String; // base + sep;
pkgPath: String; // pathFn.join(base, 'package.json');
pluginDir: String; // pathFn.join(base, 'node_modules') + sep;
logDir: String; // pathFn.join(base, 'logs');
args: String; // camelizeKeys(args);
config: String; // utils.parseYaml(rcPath); // Read feflow local config.
pwd: String;
log: any;
logger: any;
pkgConfig: {
name: string;
};
Expand All @@ -37,12 +29,16 @@ class Report {
constructor(feflowContext: any) {
this.ctx = feflowContext;

this.loadContextLogger();
this.userName = this.getUserName();
this.systemInfo = this.getSystemInfo();
this.project = this.getProject();
}
get timestamp() {
return Date.now();
loadContextLogger() {
this.ctx.log = this.ctx.log || this.ctx.logger;
this.ctx.log = this.ctx.log
? this.ctx.log
: { info: console.log, debug: console.log };
}
getProject() {
const { pkgConfig } = this.ctx;
Expand All @@ -67,44 +63,33 @@ class Report {
return JSON.stringify(systemDetailInfo);
}

cmdStart() {
this.timestampCmdStart = this.timestamp;
}

cmdEnd() {
this.timestampSpenTime = this.timestamp - this.timestampCmdStart;
}

getReportBody(cmd, args): ReportBody {
const reportBody: ReportBody = objectFactory
.create()
.load("command", cmd)
.load("user_name", this.userName)
.load("params", args)
.load("system_info", this.systemInfo)
.load("project", this.project)
// .load("spent_time", this.systemInfo)
// .load("is_fail", this.systemInfo)
// .load("error_message", this.systemInfo)
.load('command', cmd)
.load('user_name', this.userName)
.load('params', args)
.load('system_info', this.systemInfo)
.load('project', this.project)
.done();

return reportBody;
}

checkBeforeReport(cmd, args) {
if (!cmd) {
return false;
}
return true;
return !!cmd;
}

report(cmd, args?) {
// args check
if (!this.checkBeforeReport(cmd, args)) return;

const reportBody: ReportBody = this.getReportBody(cmd, args);

Api.report(reportBody);
try {
const reportBody: ReportBody = this.getReportBody(cmd, args);
this.ctx.log.debug('reportBody', reportBody);
Api.report(reportBody, this.ctx.log);
} catch (error) {
this.ctx.log.debug('feflow 上报报错,请联系相关负责人排查 ', error);
}
}
}

Expand Down

0 comments on commit ca20a16

Please sign in to comment.