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

feat: init sequelize and pgsql #5

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
112 changes: 60 additions & 52 deletions app/service/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@
const Service = require('egg').Service;

class MysqlService extends Service {
async newS() {
const sql = 'SELECT * FROM user';
const a = await this.consoleQuery(sql);
const b = await this.app.model.query('SELECT * FROM user', { raw: true, type: this.app.model.QueryTypes.SELECT });
return { a, b };
}
Copy link
Member

Choose a reason for hiding this comment

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

这个方法没用到?

Copy link
Author

Choose a reason for hiding this comment

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

finish fix

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);
}

/* table <user> */
getUserByName(name) {
const sql = 'SELECT * FROM user WHERE name = ?';
const sql = 'SELECT * FROM xprofiler_console.user WHERE name = ?';
const params = [name];
return this.consoleQuery(sql, params).then(data => data[0]);
}

saveUser(name, nick, pass, identity, mail) {
const sql = 'INSERT INTO user (name, nick, pass, identity, mail) VALUES (?, ?, ?, ?, ?)';
const sql = 'INSERT INTO xprofiler_console.user (name, nick, pass, identity, mail) VALUES (?, ?, ?, ?, ?)';
const params = [name, nick, pass, identity, mail];
return this.consoleQuery(sql, params);
}

getUserByIdentity(identity) {
const sql = 'SELECT * FROM user WHERE identity = ?';
const sql = 'SELECT * FROM xprofiler_console.user WHERE identity = ?';
const params = [identity];
return this.consoleQuery(sql, params).then(data => data[0]);
}
Expand All @@ -38,62 +46,62 @@ 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);
}

/* table <apps> */
getMyApps(userId) {
const sql = 'SELECT * FROM apps WHERE owner = ? ORDER BY gm_modified ASC';
const sql = 'SELECT * FROM xprofiler_console.apps WHERE owner = ? ORDER BY gm_modified ASC';
const params = [userId];
return this.consoleQuery(sql, params);
}

getJoinedApps(userId, status) {
const sql = 'SELECT * FROM apps WHERE id in (SELECT app FROM members WHERE user = ? AND status = ?) ORDER BY gm_modified ASC';
const sql = 'SELECT * FROM xprofiler_console.apps WHERE id in (SELECT app FROM xprofiler_console.members WHERE user = ? AND status = ?) ORDER BY gm_modified ASC';
const params = [userId, status];
return this.consoleQuery(sql, params);
}

saveApp(userId, appName, secret) {
const sql = 'INSERT INTO apps (name, owner, secret) VALUES (?, ?, ?)';
const sql = 'INSERT INTO xprofiler_console.apps (name, owner, secret) VALUES (?, ?, ?)';
const params = [appName, userId, secret];
return this.consoleQuery(sql, params);
}

renameApp(appId, newAppName) {
const sql = 'UPDATE apps SET name = ? WHERE id = ?';
const sql = 'UPDATE xprofiler_console.apps SET name = ? WHERE id = ?';
const params = [newAppName, appId];
return this.consoleQuery(sql, params);
}

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] || {});
}

deleteAppByAppId(appId) {
const sql = 'DELETE FROM apps WHERE id = ?';
const sql = 'DELETE FROM xprofiler_console.apps WHERE id = ?';
const params = [appId];
return this.consoleQuery(sql, params);
}

checkAppOwnerByUserId(appId, userId) {
const sql = 'SELECT * FROM apps WHERE id = ? AND owner = ?';
const sql = 'SELECT * FROM xprofiler_console.apps WHERE id = ? AND owner = ?';
const params = [appId, userId];
return this.consoleQuery(sql, params).then(data => data[0]);
}

checkAppMemberByUserId(appId, userId, status) {
const sql = 'SELECT * FROM apps WHERE id in (SELECT app FROM members WHERE app = ? AND user = ? AND status = ?)';
const sql = 'SELECT * FROM xprofiler_console.apps WHERE id in (SELECT app FROM xprofiler_console.members WHERE app = ? AND user = ? AND status = ?)';
const params = [appId, userId, status];
return this.consoleQuery(sql, params).then(data => data[0]);
}

updateAppOwner(appId, userId) {
const sql = 'UPDATE apps SET owner = ? WHERE id = ?';
const sql = 'UPDATE xprofiler_console.apps SET owner = ? WHERE id = ?';
const params = [userId, appId];
return this.consoleQuery(sql, params);
}
Expand All @@ -103,28 +111,28 @@ class MysqlService extends Service {
let sql = '';
let params = [];
if (type === 'all') {
sql = 'SELECT * FROM files WHERE app = ?';
sql = 'SELECT * FROM xprofiler_console.files WHERE app = ?';
params = [appId];
} else if (type === 'favor') {
sql = 'SELECT * FROM files WHERE app = ? AND favor = ?';
sql = 'SELECT * FROM xprofiler_console.files WHERE app = ? AND favor = ?';
params = [appId, 1];
} else {
sql = 'SELECT * FROM files WHERE app = ? AND type = ?';
sql = 'SELECT * FROM xprofiler_console.files WHERE app = ? AND type = ?';
params = [appId, type];
}
sql += ' ORDER BY gm_create DESC';
return this.consoleQuery(sql, params);
}

addFile(appId, agentId, type, file, user, status = 0, storage = '') {
const sql = 'INSERT INTO files (app, agent, type, file, user, status, storage) '
const sql = 'INSERT INTO xprofiler_console.files (app, agent, type, file, user, status, storage) '
+ 'VALUES (?, ?, ?, ?, ?, ?, ?)';
const params = [appId, agentId, type, file, user, status, storage];
return this.consoleQuery(sql, params);
}

getFileByIdAndType(fileId, fileType) {
const sql = 'SELECT * FROM files WHERE id = ? AND type = ?';
const sql = 'SELECT * FROM xprofiler_console.files WHERE id = ? AND type = ?';
const params = [fileId, fileType];
return this.consoleQuery(sql, params).then(data => data[0]);
}
Expand All @@ -133,29 +141,29 @@ class MysqlService extends Service {
let sql = '';
let params = [];
if (storage) {
sql = 'UPDATE files SET status = ?, token = ?, storage = ? WHERE id = ?';
sql = 'UPDATE xprofiler_console.files SET status = ?, token = ?, storage = ? WHERE id = ?';
params = [status, token, storage, fileId];
} else {
sql = 'UPDATE files SET status = ?, token = ? WHERE id = ?';
sql = 'UPDATE xprofiler_console.files SET status = ?, token = ? WHERE id = ?';
params = [status, token, fileId];
}
return this.consoleQuery(sql, params);
}

deleteFileById(fileId) {
const sql = 'DELETE FROM files WHERE id = ?';
const sql = 'DELETE FROM xprofiler_console.files WHERE id = ?';
const params = [fileId];
return this.consoleQuery(sql, params);
}

deleteFiles(appId) {
const sql = 'DELETE FROM files WHERE app = ?';
const sql = 'DELETE FROM xprofiler_console.files WHERE app = ?';
const params = [appId];
return this.consoleQuery(sql, params);
}

updateFileFavor(fileId, favor) {
const sql = 'UPDATE files SET favor = ? WHERE id = ?';
const sql = 'UPDATE xprofiler_console.files SET favor = ? WHERE id = ?';
const params = [favor, fileId];
return this.consoleQuery(sql, params);
}
Expand All @@ -165,88 +173,88 @@ class MysqlService extends Service {
let sql;
let params = [];
if (type === 'favor') {
sql = 'SELECT * FROM coredumps WHERE app = ? AND favor = ?';
sql = 'SELECT * FROM xprofiler_console.coredumps WHERE app = ? AND favor = ?';
params = [appId, 1];
} else {
sql = 'SELECT * FROM coredumps WHERE app = ?';
sql = 'SELECT * FROM xprofiler_console.coredumps WHERE app = ?';
params = [appId];
}
return this.consoleQuery(sql, params);
}

addCoredump(appId, agentId, file, node, user, fileStatus = 0, fileStorage = '', nodeStatus, nodeStorage = '') {
const sql = 'INSERT INTO coredumps (app, agent, file, node, user, file_status, file_storage, node_status, node_storage) '
const sql = 'INSERT INTO xprofiler_console.coredumps (app, agent, file, node, user, file_status, file_storage, node_status, node_storage) '
+ 'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
const params = [appId, agentId, file, node, user, fileStatus, fileStorage, nodeStatus, nodeStorage];
return this.consoleQuery(sql, params);
}

getCoredumpById(fileId) {
const sql = 'SELECT * FROM coredumps WHERE id = ?';
const sql = 'SELECT * FROM xprofiler_console.coredumps WHERE id = ?';
const params = [fileId];
return this.consoleQuery(sql, params).then(data => data[0]);
}

deleteCoredumpById(fileId) {
const sql = 'DELETE FROM coredumps WHERE id = ?';
const sql = 'DELETE FROM xprofiler_console.coredumps WHERE id = ?';
const params = [fileId];
return this.consoleQuery(sql, params);
}

deleteCoredumps(appId) {
const sql = 'DELETE FROM coredumps WHERE app = ?';
const sql = 'DELETE FROM xprofiler_console.coredumps WHERE app = ?';
const params = [appId];
return this.consoleQuery(sql, params);
}

updateCoredumpFavor(fileId, favor) {
const sql = 'UPDATE coredumps SET favor = ? WHERE id = ?';
const sql = 'UPDATE xprofiler_console.coredumps SET favor = ? WHERE id = ?';
const params = [favor, fileId];
return this.consoleQuery(sql, params);
}

/* table <members> */
getTeamMembersByAppId(appId) {
const sql = 'SELECT * FROM members WHERE app = ?';
const sql = 'SELECT * FROM xprofiler_console.members WHERE app = ?';
const params = [appId];
return this.consoleQuery(sql, params);
}

inviteMember(appId, invitedUser, status) {
const sql = 'INSERT INTO members (app, user, status) VALUES (?, ?, ?)';
const sql = 'INSERT INTO xprofiler_console.members (app, user, status) VALUES (?, ?, ?)';
const params = [appId, invitedUser, status];
return this.consoleQuery(sql, params);
}

confirmInvitation(invitedApp, userId) {
const sql = 'UPDATE members SET status = ? WHERE app = ? AND user = ?';
const sql = 'UPDATE xprofiler_console.members SET status = ? WHERE app = ? AND user = ?';
const params = [2, invitedApp, userId];
return this.consoleQuery(sql, params);
}

deleteMember(appId, userId) {
const sql = 'DELETE FROM members WHERE app = ? AND user = ?';
const sql = 'DELETE FROM xprofiler_console.members WHERE app = ? AND user = ?';
const params = [appId, userId];
return this.consoleQuery(sql, params);
}

deleteMembersByAppId(appId) {
const sql = 'DELETE FROM members WHERE app = ?';
const sql = 'DELETE FROM xprofiler_console.members WHERE app = ?';
const params = [appId];
return this.consoleQuery(sql, params);
}

/* table <strategies> */
getStrategiesByAppId(appId) {
const sql = 'SELECT * FROM strategies WHERE app = ? ORDER BY gm_create DESC';
const sql = 'SELECT * FROM xprofiler_console.strategies WHERE app = ? ORDER BY gm_create DESC';
const params = [appId];
return this.consoleQuery(sql, params);
}

addStrategy(data) {
const { appId, contextType, pushType, customRuleExpr, customRuleDesc, webhookPush,
webhookType = '', webhookAddress = '', webhookSign = '' } = data;
const sql = 'INSERT INTO strategies (app, context, push, webhook, wtype, waddress, wsign, '
const sql = 'INSERT INTO xprofiler_console.strategies (app, context, push, webhook, wtype, waddress, wsign, '
+ 'expression, content) '
+ 'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
const params = [appId, contextType, pushType,
Expand All @@ -259,15 +267,15 @@ class MysqlService extends Service {
}

getStrategyById(strategyId) {
const sql = 'SELECT * FROM strategies WHERE id = ?';
const sql = 'SELECT * FROM xprofiler_console.strategies WHERE id = ?';
const params = [strategyId];
return this.consoleQuery(sql, params).then(data => data[0]);
}

updateStrategy(data) {
const { strategyId, contextType, pushType, customRuleExpr, customRuleDesc,
webhookPush, webhookType = '', webhookAddress = '', webhookSign = '' } = data;
const sql = 'UPDATE strategies SET context = ?, push = ?, expression = ?, content = ?, '
const sql = 'UPDATE xprofiler_console.strategies SET context = ?, push = ?, expression = ?, content = ?, '
+ 'webhook = ?, wtype = ?, waddress = ?, wsign = ? WHERE id = ?';
const params = [contextType, pushType, customRuleExpr, customRuleDesc,
webhookPush ? 1 : 0,
Expand All @@ -279,39 +287,39 @@ class MysqlService extends Service {
}

updateStrategyStatus(strategyId, status) {
const sql = 'UPDATE strategies SET status = ? WHERE id = ?';
const sql = 'UPDATE xprofiler_console.strategies SET status = ? WHERE id = ?';
const params = [status, strategyId];
return this.consoleQuery(sql, params);
}

deleteStrategyById(strategyId) {
const sql = 'DELETE FROM strategies WHERE id = ?';
const sql = 'DELETE FROM xprofiler_console.strategies WHERE id = ?';
const params = [strategyId];
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);
}

addContactToStrategy(strategyId, userId) {
const sql = 'INSERT INTO contacts (strategy, user) VALUES (?, ?)';
const sql = 'INSERT INTO xprofiler_console.contacts (strategy, user) VALUES (?, ?)';
const params = [strategyId, userId];
return this.consoleQuery(sql, params);
}

deleteContactFromStrategy(strategyId, userId) {
const sql = 'DELETE FROM contacts WHERE strategy = ? AND user = ?';
const sql = 'DELETE FROM xprofiler_console.contacts WHERE strategy = ? AND user = ?';
const params = [strategyId, userId];
return this.consoleQuery(sql, params);
}

/* process_${DD} or osinfo_${DD} */
getXnppLogs(table, appId, agentId, start, end, pid) {
const sql = `SELECT * FROM ${table} WHERE app = ? AND agent = ? `
const sql = `SELECT * FROM xprofiler_logs.${table} WHERE app = ? AND agent = ? `
+ 'AND log_time >= ? AND log_time < ? '
+ (pid ? 'AND pid = ?' : '')
+ 'ORDER BY log_time DESC';
Expand All @@ -324,7 +332,7 @@ class MysqlService extends Service {

/* alarm_${DD} */
getAlarmHistory(table, strategyId, start, end) {
const sql = `SELECT * FROM ${table} WHERE strategy = ? `
const sql = `SELECT * FROM xprofiler_logs.${table} WHERE strategy = ? `
+ 'AND gm_create >= ? AND gm_create < ? '
+ 'ORDER BY gm_create DESC';
const params = [strategyId, start, end];
Expand Down
Loading