diff --git a/api/.vscode/launch.json b/api/.vscode/launch.json index 46c4981..65ffa54 100644 --- a/api/.vscode/launch.json +++ b/api/.vscode/launch.json @@ -18,7 +18,8 @@ "-g postgres://FashionAdmin:123456@10.8.30.39:5432/GovernmentDataResourceCenter", // "-g postgres://FashionAdmin:123456@10.8.30.156:5432/gdrcenter", "-b http://10.8.30.161:31420", - "-s http://10.8.30.161:32258" + "-s http://10.8.30.161:32258", + "-d postgres/example/10.8.30.160/30432", ] }, { diff --git a/api/app/lib/controllers/homepage/index.js b/api/app/lib/controllers/homepage/index.js index 1b3c8f7..91cb542 100644 --- a/api/app/lib/controllers/homepage/index.js +++ b/api/app/lib/controllers/homepage/index.js @@ -209,6 +209,24 @@ function getRestfulInfo(opts) { } } +function getAccessData(opts) { + return async function (ctx, next) { + const models = ctx.fs.dc.models; + try { + const projects = '1,2,3,4,5,6,7,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38' + const url = 'https://statistics.anxinyun.cn/statistic?projects=' + projects + let res = await request.get(url) + ctx.status = 200; + ctx.body = { res: res.body || {}, projects }; + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = { message: '获取预警列表失败' } + } + } +} + + function getQualityCheckAlarm(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; @@ -225,11 +243,11 @@ function getQualityCheckAlarm(opts) { } } - module.exports = { getNodeResources, getDataTotalTop5, getClusterInfo, getRestfulInfo, - getQualityCheckAlarm + getQualityCheckAlarm, + getAccessData } diff --git a/api/app/lib/controllers/metadataSearch/index.js b/api/app/lib/controllers/metadataSearch/index.js index 6b69440..9832f1d 100644 --- a/api/app/lib/controllers/metadataSearch/index.js +++ b/api/app/lib/controllers/metadataSearch/index.js @@ -70,27 +70,42 @@ function searchMeta(opts) { // 下载表数据 function getTableData(opts) { return async function (ctx, next) { - - const models = ctx.fs.dc.models; try { + const models = ctx.fs.dc.models; const { id } = ctx.query; const metaData = await models.MetadataDatabase.findOne({ where: { id: id } }) let rslt = [], metaDataChildren = [] - if (metaData && metaData.datasourceConfig) { - metaDataChildren = await models.MetadataDatabase.findAll({ where: { parent: id, type: '字段' } }) + const { dbConfig } = opts; + + metaDataChildren = await models.MetadataDatabase.findAll({ where: { parent: id, type: '字段' } }) + if (metaData.datasourceConfig) { let { user, host, database, password, port } = metaData.datasourceConfig; - const pool = new Pool({ - user: user, - host: host, - database: database, - password: password, - port: port, - }) + const pool = new Pool({ user: user, host: host, database: database, password: password, port: port }) const client = await pool.connect() const tableName = metaData.name; const ress = await client.query(`SELECT * from "${tableName}"`, []) rslt = ress.rows + } else { + let databaseFind = await models.MetadataDatabase.findOne({ where: { id: metaData.parent, type: '库' } }) + if (databaseFind) { + let dbconfigObj = { + user: dbConfig.split('/')[0], + host: dbConfig.split('/')[2], + database: databaseFind.code, + password: dbConfig.split('/')[1], + port: dbConfig.split('/')[3], + } + let { user, host, database, password, port } = dbconfigObj; + const pool = new Pool({ user: user, host: host, database: database, password: password, port: port }) + const client = await pool.connect() + + const tableName = metaData.name; + const ress = await client.query(`SELECT * from "${tableName}"`, []) + rslt = ress.rows + } else { + rslt = [] + } } ctx.status = 200; @@ -98,7 +113,7 @@ function getTableData(opts) { } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 200; - ctx.body = { message: '连接失败' } + ctx.body = { message: '连接失败', rslt: [], metaDataChildren: [] } } } } diff --git a/api/app/lib/routes/homepage/index.js b/api/app/lib/routes/homepage/index.js index 0d0061b..009026e 100644 --- a/api/app/lib/routes/homepage/index.js +++ b/api/app/lib/routes/homepage/index.js @@ -19,4 +19,6 @@ module.exports = function (app, router, opts, AuthCode) { app.fs.api.logAttr['GET/homepage/alarms'] = { content: '获取预警列表', visible: true }; router.get('/homepage/alarms', backups.getQualityCheckAlarm(opts)) + app.fs.api.logAttr['GET/homepage/accessdata'] = { content: '获取接入数据统计', visible: true }; + router.get('/homepage/accessdata', backups.getAccessData(opts)) }; diff --git a/api/config.js b/api/config.js index a4be807..40af44e 100644 --- a/api/config.js +++ b/api/config.js @@ -12,6 +12,7 @@ args.option(['p', 'port'], '启动端口'); args.option(['g', 'pg'], 'postgre 服务 URL'); args.option(['b', 'backups'], '后端数据库备份恢复接口地址'); args.option(['s', 'kubesphere'], 'kubesphere地址'); +args.option(['d', 'dbconfig'], '后台同步数据库host示例:postgres/example/10.8.30.160/30432 格式:用户名/密码/host/port'); const flags = args.parse(process.argv); @@ -25,8 +26,9 @@ const QINIU_SK = process.env.ANXINCLOUD_QINIU_SECRETKEY || flags.qnsk; const BACKUPS_URL = process.env.BACKUPS_URL || flags.backups; const KUBESPHERE_URL = process.env.KUBESPHERE_URL || flags.kubesphere; +const DATABASE_CONFIG = process.env.DATABASE_HOST || flags.dbconfig;//同步数据库配置 -if (!DB || !BACKUPS_URL || !KUBESPHERE_URL) { +if (!DB || !BACKUPS_URL || !KUBESPHERE_URL || !DATABASE_CONFIG) { console.log('缺少启动参数,异常退出'); args.showHelp(); process.exit(-1); @@ -72,7 +74,9 @@ const product = { }, pssaRequest: [], backupsUrl: BACKUPS_URL, - k8s: KUBESPHERE_URL + k8s: KUBESPHERE_URL, + dbConfig: DATABASE_CONFIG, + } } ], diff --git a/web/client/src/sections/homePage/components/accessData.js b/web/client/src/sections/homePage/components/accessData.js index f98ab0b..71a117e 100644 --- a/web/client/src/sections/homePage/components/accessData.js +++ b/web/client/src/sections/homePage/components/accessData.js @@ -1,19 +1,27 @@ import React, { useEffect, useState } from 'react' import Box from './public/table-card'; - +import { useFsRequest } from '$utils'; +import { mathRound } from './util' function AccessData(props) { + const { data: accessdata = [] } = useFsRequest({ + url: 'homepage/accessdata', + pollingInterval: 1000 * 60, + cacheKey: 'accessdata', + }); + + const renderBody = () => { return