Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: manager库支持了sequelize库的使用 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions app/service/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,43 @@ const SYSTEM_KEY = [

class MysqlService extends Service {
consoleQuery(sql, params = []) {
const { ctx: { app: { mysql } } } = this;
const xprofiler_console = mysql.get('xprofiler_console');
return xprofiler_console.query(sql, params);
return this.app.model.query(sql, { replacements: params, type: this.app.model.QueryTypes.SELECT });
// const { ctx: { app: { mysql } } } = this;
// const xprofiler_console = mysql.get('xprofiler_console');
// return xprofiler_console.query(sql, params);
}

logsQuery(sql, params = []) {
const { ctx: { app: { mysql } } } = this;
const xprofiler_logs = mysql.get('xprofiler_logs');
return xprofiler_logs.query(sql, params);
return this.app.model.query(sql, { replacements: params, type: this.app.model.QueryTypes.SELECT });
// const { ctx: { app: { mysql } } } = this;
// const xprofiler_logs = mysql.get('xprofiler_logs');
// return xprofiler_logs.query(sql, params);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consoleQuery 和 logsQuery 在 seq 下如果都一致了,没必要区分两个方法了

/* table <apps> */
getAppByAppId(appId) {
const sql = 'SELECT * FROM apps WHERE id = ?';
const sql = 'SELECT * FROM xprofiler_console.apps WHERE id = ?';
const params = [appId];
return this.consoleQuery(sql, params).then(data => data[0] || {});
}

/* table <files> */
updateFileStatusByAppAgentFile(appId, agentId, filePath) {
const sql = 'UPDATE files SET status = ? WHERE app = ? AND agent = ? AND file = ? AND status = ?';
const sql = 'UPDATE xprofiler_console.files SET status = ? WHERE app = ? AND agent = ? AND file = ? AND status = ?';
const params = [1, appId, agentId, filePath, 0];
return this.consoleQuery(sql, params);
}

/* table strategies */
getStrategiesByAppIdAndContextType(appId, contextType) {
const sql = 'SELECT * FROM strategies WHERE app = ? AND context = ? AND status = 1';
const sql = 'SELECT * FROM xprofiler_console.strategies WHERE app = ? AND context = ? AND status = 1';
const params = [appId, contextType];
return this.consoleQuery(sql, params);
}

/* table <contacts> */
getContactsByStrategyId(strategyId) {
const sql = 'SELECT * FROM contacts WHERE strategy = ?';
const sql = 'SELECT * FROM xprofiler_console.contacts WHERE strategy = ?';
const params = [strategyId];
return this.consoleQuery(sql, params);
}
Expand All @@ -122,7 +124,7 @@ class MysqlService extends Service {
if (!userIds.length) {
return [];
}
const sql = `SELECT * FROM user WHERE id in (${userIds.map(() => '?').join(',')})`;
const sql = `SELECT * FROM xprofiler_console.user WHERE id in (${userIds.map(() => '?').join(',')})`;
const params = [...userIds];
return this.consoleQuery(sql, params);
}
Expand Down Expand Up @@ -151,7 +153,7 @@ class MysqlService extends Service {
const { time, version, statusMap } = log;
const table = this.getTable('process_', time);
const sql =
`INSERT INTO ${table} (`
`INSERT INTO xprofiler_logs.${table} (`
+ 'app, agent, pid, log_time, version'
+ `, ${XPROFILER_KEY.join(', ')}, `
+ 'response_codes) '
Expand All @@ -174,7 +176,7 @@ class MysqlService extends Service {
const { log_time, version, total_memory, free_memory, disks, statusMap } = log;
const table = this.getTable('osinfo_', log_time);
const sql =
`INSERT INTO ${table} (`
`INSERT INTO xprofiler_logs.${table} (`
+ 'app, agent, log_time, position, version, total_memory, free_memory, disks, response_codes'
+ `, ${SYSTEM_KEY.join(', ')}) `
+ 'values ('
Expand All @@ -193,7 +195,7 @@ class MysqlService extends Service {
saveAlarmLog(strategyId, agentId, message, pid = null) {
message = message.length > 250 ? message.slice(0, 245) + '...' : message;
const table = this.getTable('alarm_', Date.now());
const sql = `INSERT INTO ${table} (strategy, agent, message, pid) `
const sql = `INSERT INTO xprofiler_logs.${table} (strategy, agent, message, pid) `
+ 'VALUES (?, ?, ?, ?)';
const params = [strategyId, agentId, message, pid];
return this.logsQuery(sql, params);
Expand All @@ -214,8 +216,8 @@ class MysqlService extends Service {
if (remains.includes(table)) {
return;
}
await this.logsQuery(`DELETE FROM ${table}`);
await this.logsQuery(`OPTIMIZE TABLE ${table}`);
await this.logsQuery(`DELETE FROM xprofiler_logs.${table}`);
// await this.logsQuery(`OPTIMIZE TABLE xprofiler_logs.${table}`);
}, { concurrency: 2 });
}

Expand Down
88 changes: 53 additions & 35 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,16 @@ module.exports = appInfo => {
};

const userConfig = {};

// mysql
userConfig.mysql = {
app: true,
agent: false,
clients: {
xprofiler_console: {
host: '',
port: 3306,
user: '',
password: '',
database: 'xprofiler_console',
},
xprofiler_logs: {
host: '',
port: 3306,
user: '',
password: '',
database: 'xprofiler_logs',
},
},
// sequelize
config.sequelize = {
dialect: 'postgres',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认 mysql

database: '',
host: '',
port: 7092,
username: '',
password: '',
};

// redis
userConfig.redis = {
config.redis = {
client: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 userConfig,config 是原则上不需要配置的参数,用默认的 dfault 即可

sentinels: null,
port: 6379,
Expand All @@ -118,19 +103,52 @@ module.exports = appInfo => {
db: 0,
},
};
config.xprofilerConsole = 'http://127.0.0.1:8443';
// // mysql
// userConfig.mysql = {
// app: true,
// agent: false,
// clients: {
// xprofiler_console: {
// host: '',
// port: 3306,
// user: '',
// password: '',
// database: 'xprofiler_console',
// },
// xprofiler_logs: {
// host: '',
// port: 3306,
// user: '',
// password: '',
// database: 'xprofiler_logs',
// },
// },
// };

// // redis
// userConfig.redis = {
// client: {
// sentinels: null,
// port: 6379,
// host: '',
// password: '',
// db: 0,
// },
// };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分可以删除了

// mailer
userConfig.mailer = {
host: '',
port: 25,
secure: false,
auth: {
user: '',
pass: '',
},
};

userConfig.xprofilerConsole = '';
// userConfig.mailer = {
// host: '',
// port: 25,
// secure: false,
// auth: {
// user: '',
// pass: '',
// },
// };

// userConfig.xprofilerConsole = '';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里保持原样,不要注释掉

return {
...config,
Expand Down