|
@ -1,12 +1,16 @@ |
|
|
|
|
|
|
|
|
const Automate = require('sequelize-automate-freesun') |
|
|
const Automate = require('sequelize-automate-freesun') |
|
|
const moment = require('moment'); |
|
|
const moment = require('moment'); |
|
|
//@toto删除数据库源时 同步删除该数据源资源目录树下元数据
|
|
|
const schedule = require('node-schedule') |
|
|
|
|
|
const initJob = require('./initJob'); |
|
|
|
|
|
//删除数据库源时 同步删除该数据源资源目录树下元数据
|
|
|
//数据源 数据库配置信息不能更改 若需更改则需要删除数据源重新配置
|
|
|
//数据源 数据库配置信息不能更改 若需更改则需要删除数据源重新配置
|
|
|
|
|
|
let i = 0; |
|
|
async function handleTask(app, task) { |
|
|
async function handleTask(app, task) { |
|
|
const transaction = await app.fs.dc.orm.transaction(); |
|
|
// const transaction = await app.fs.dc.orm.transaction();
|
|
|
try { |
|
|
const startTime = moment() |
|
|
const { models } = app.fs.dc |
|
|
const { models } = app.fs.dc |
|
|
|
|
|
try { |
|
|
const dataSource = await models.DataSource.findOne({ |
|
|
const dataSource = await models.DataSource.findOne({ |
|
|
where: { |
|
|
where: { |
|
|
id: task.dataSourceId |
|
|
id: task.dataSourceId |
|
@ -16,7 +20,7 @@ async function handleTask(app, task) { |
|
|
if (dataSource) { |
|
|
if (dataSource) { |
|
|
const dbOptions = createDbOptions(dataSource.config); |
|
|
const dbOptions = createDbOptions(dataSource.config); |
|
|
const automate = new Automate(dbOptions, {}); |
|
|
const automate = new Automate(dbOptions, {}); |
|
|
const tables = await automate.getTables(); |
|
|
const tables = await automate.getTables(); //获取当前采集任务数据源pg库表字段索引外键数据
|
|
|
const dataToSave = { |
|
|
const dataToSave = { |
|
|
code: dataSource.config.database, |
|
|
code: dataSource.config.database, |
|
|
name: dataSource.config.database, |
|
|
name: dataSource.config.database, |
|
@ -29,7 +33,7 @@ async function handleTask(app, task) { |
|
|
updateAt: null, |
|
|
updateAt: null, |
|
|
user: { id: 1, name: "超级管理员", username: "SuperAdmin" }, |
|
|
user: { id: 1, name: "超级管理员", username: "SuperAdmin" }, |
|
|
attributesParam: null, |
|
|
attributesParam: null, |
|
|
catalogKey: 'rc' |
|
|
catalogKey: dataSource.catalogKey |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//初始化数据 新增 数据库 表 字段索引外键数据
|
|
|
//初始化数据 新增 数据库 表 字段索引外键数据
|
|
@ -40,7 +44,7 @@ async function handleTask(app, task) { |
|
|
catalog: dataSource.mountPath, |
|
|
catalog: dataSource.mountPath, |
|
|
}//目录树下库只会存在一个 判断是否有库类型元数据 没有则新增库和表元数据 有库数据则比较更新表类型元数据 全量更新字段索引数据
|
|
|
}//目录树下库只会存在一个 判断是否有库类型元数据 没有则新增库和表元数据 有库数据则比较更新表类型元数据 全量更新字段索引数据
|
|
|
}) |
|
|
}) |
|
|
|
|
|
let databaseId = databaseFind ? databaseFind.id : null; |
|
|
if (databaseFind) { //更新表类型元数据
|
|
|
if (databaseFind) { //更新表类型元数据
|
|
|
const newTableNames = [] |
|
|
const newTableNames = [] |
|
|
Object.keys(tables).forEach(key => { newTableNames.push(key) }); |
|
|
Object.keys(tables).forEach(key => { newTableNames.push(key) }); |
|
@ -78,6 +82,7 @@ async function handleTask(app, task) { |
|
|
} else { //新增库、表类型数据
|
|
|
} else { //新增库、表类型数据
|
|
|
//库类型元数据存储
|
|
|
//库类型元数据存储
|
|
|
const databaseRslt = await models.MetadataDatabase.create(dataToSave) |
|
|
const databaseRslt = await models.MetadataDatabase.create(dataToSave) |
|
|
|
|
|
databaseId = databaseRslt.id |
|
|
//表元数据存储
|
|
|
//表元数据存储
|
|
|
if (databaseRslt && databaseRslt.id) { |
|
|
if (databaseRslt && databaseRslt.id) { |
|
|
const tableBodys = [] |
|
|
const tableBodys = [] |
|
@ -93,10 +98,12 @@ async function handleTask(app, task) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (databaseId) { |
|
|
const metaDatabaseTables = await models.MetadataDatabase.findAll({ |
|
|
const metaDatabaseTables = await models.MetadataDatabase.findAll({ |
|
|
where: { |
|
|
where: { |
|
|
type: '表', |
|
|
type: '表', |
|
|
catalog: dataSource.mountPath, |
|
|
catalog: dataSource.mountPath, |
|
|
|
|
|
parent: databaseId |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
@ -110,10 +117,6 @@ async function handleTask(app, task) { |
|
|
|
|
|
|
|
|
const fieldBodys = [] |
|
|
const fieldBodys = [] |
|
|
for (let table of metaDatabaseTables) { |
|
|
for (let table of metaDatabaseTables) { |
|
|
if(!tables[table.name].structures){ |
|
|
|
|
|
console.log(table) |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
Object.keys(tables[table.name].structures).forEach(key => { |
|
|
Object.keys(tables[table.name].structures).forEach(key => { |
|
|
dataToSave.parent = table.id; |
|
|
dataToSave.parent = table.id; |
|
|
dataToSave.name = tables[table.name].structures[key].comment || key; |
|
|
dataToSave.name = tables[table.name].structures[key].comment || key; |
|
@ -122,14 +125,57 @@ async function handleTask(app, task) { |
|
|
const tableObj = { ...dataToSave } |
|
|
const tableObj = { ...dataToSave } |
|
|
fieldBodys.push(tableObj) |
|
|
fieldBodys.push(tableObj) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
tables[table.name].foreignKeys.forEach(v => { |
|
|
|
|
|
dataToSave.parent = table.id; |
|
|
|
|
|
dataToSave.name = v.columnName; |
|
|
|
|
|
dataToSave.code = v.columnName; |
|
|
|
|
|
dataToSave.type = '外键'; |
|
|
|
|
|
const tableObj = { ...dataToSave } |
|
|
|
|
|
fieldBodys.push(tableObj) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
tables[table.name].indexes.forEach(v => { |
|
|
|
|
|
dataToSave.parent = table.id; |
|
|
|
|
|
dataToSave.name = v.name; |
|
|
|
|
|
dataToSave.code = v.name; |
|
|
|
|
|
dataToSave.type = '索引'; |
|
|
|
|
|
const tableObj = { ...dataToSave } |
|
|
|
|
|
fieldBodys.push(tableObj) |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const fieldRslt = await models.MetadataDatabase.bulkCreate(fieldBodys); |
|
|
const fieldRslt = await models.MetadataDatabase.bulkCreate(fieldBodys); |
|
|
if (fieldRslt) { |
|
|
if (fieldRslt) { |
|
|
|
|
|
const endTime = moment() |
|
|
|
|
|
const logBody = { |
|
|
|
|
|
task: task.id, |
|
|
|
|
|
success: true, |
|
|
|
|
|
details: '采集成功', |
|
|
|
|
|
startTime: startTime, |
|
|
|
|
|
endTime: endTime |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
//日志记录
|
|
|
//日志记录
|
|
|
|
|
|
await models.AcquisitionLog.create(logBody) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
i++ |
|
|
|
|
|
console.log('===========------------------------------------------------------------' + task.taskName + moment().format('HH:mm:ss')) |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
// await transaction.rollback();
|
|
|
// await transaction.rollback();
|
|
|
|
|
|
const endTime = moment() |
|
|
|
|
|
const logBody = { |
|
|
|
|
|
task: task.id, |
|
|
|
|
|
success: true, |
|
|
|
|
|
details: '采集失败' + JSON.stringify(error).substring(0, 248), |
|
|
|
|
|
startTime: startTime, |
|
|
|
|
|
endTime: endTime |
|
|
|
|
|
} |
|
|
|
|
|
await models.AcquisitionLog.create(logBody) |
|
|
|
|
|
// if (app.fs.schedule['taskJobs'][task.id]) schedule.cancelJob(app.fs.schedule['taskJobs'][task.id])
|
|
|
|
|
|
console.log('===========------------------------------------------------------------' + i) |
|
|
app.fs.logger.error(`sechedule: handleTask, error: ${error}`); |
|
|
app.fs.logger.error(`sechedule: handleTask, error: ${error}`); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|