Compare commits

..

6 Commits

Author SHA1 Message Date
meilin.huang
9bb9861d88 refactor: 参数绑定等优化 2026-02-07 13:12:07 +08:00
meilin.huang
403d1c45e5 refactor: sshtunnel等 2026-02-01 13:35:23 +08:00
meilin.huang
400db0402a refactor: 包优化&其他问题修复 2026-01-25 14:16:16 +08:00
fanzhouqi
f0ae178183 !143 fix:mysql查询时,如果出现列名一样,会覆盖数据,github issues #124
* fix:mysql查询时,如果出现列名一样,会覆盖数据,github issues #124
2026-01-23 09:13:14 +00:00
meilin.huang
4641e448d2 fix: 数据库迁移、同步保存时定时任务未清除问题 2026-01-21 12:22:54 +08:00
meilin.huang
f0de65b7ce refactor: 协程启动优化、tagviews调整 2026-01-20 19:45:46 +08:00
188 changed files with 2508 additions and 1854 deletions

View File

@@ -57,7 +57,7 @@ function build() {
execFileName="${execFileName}.exe"
fi
go mod tidy
CGO_ENABLE=0 GOOS=${os} GOARCH=${arch} go build -ldflags=-w -o ${execFileName} main.go
CGO_ENABLE=0 GOOS=${os} GOARCH=${arch} go build -trimpath -ldflags=-w -o ${execFileName} main.go
if [ -d ${toFolder} ] ; then
echo_green "The desired folder already exists. Clear the folder"

View File

@@ -13,7 +13,7 @@
"@element-plus/icons-vue": "^2.3.2",
"@logicflow/core": "^2.1.7",
"@logicflow/extension": "^2.1.9",
"@vueuse/core": "^14.1.0",
"@vueuse/core": "^14.2.0",
"@xterm/addon-fit": "^0.11.0",
"@xterm/addon-search": "^0.16.0",
"@xterm/addon-web-links": "^0.12.0",
@@ -24,22 +24,22 @@
"crypto-js": "^4.2.0",
"dayjs": "^1.11.19",
"echarts": "^6.0.0",
"element-plus": "^2.13.1",
"element-plus": "^2.13.2",
"js-base64": "^3.7.8",
"jsencrypt": "^3.5.4",
"monaco-editor": "^0.55.1",
"monaco-sql-languages": "^0.15.1",
"nprogress": "^0.2.0",
"pinia": "^3.0.4",
"qrcode.vue": "^3.6.0",
"qrcode.vue": "^3.8.0",
"screenfull": "^6.0.2",
"sortablejs": "^1.15.6",
"sql-formatter": "^15.6.12",
"sql-formatter": "^15.7.0",
"trzsz": "^1.1.5",
"uuid": "^13.0.0",
"vue": "^v3.6.0-beta.2",
"vue-i18n": "^11.2.8",
"vue-router": "^4.6.4",
"vue-router": "^5.0.2",
"vuedraggable": "^4.1.0",
"xlsx": "^0.18.5"
},

View File

@@ -13,9 +13,6 @@ export function getBaseApiUrl() {
const config = {
baseApiUrl: `${(window as any).globalConfig.BaseApiUrl || location.protocol + '//' + getBaseApiUrl()}/api`,
baseWsUrl: `${(window as any).globalConfig.BaseWsUrl || `${location.protocol == 'https:' ? 'wss:' : 'ws:'}//${getBaseApiUrl()}`}/api`,
// 系统版本
version: 'v1.10.7',
};
export default config;

View File

@@ -225,10 +225,12 @@ const initMonacoEditorIns = () => {
let options = Object.assign(defaultOptions, props.options as any);
monacoEditorIns = monaco.editor.create(monacoTextareaRef.value, options);
// 监听内容改变,双向绑定
monacoEditorIns.onDidChangeModelContent(() => {
modelValue.value = monacoEditorIns.getModel()?.getValue();
});
if (!options.readOnly) {
// 监听内容改变,双向绑定
monacoEditorIns.onDidChangeModelContent(() => {
modelValue.value = monacoEditorIns.getModel()?.getValue();
});
}
};
const changeLanguage = (value: any) => {

View File

@@ -34,7 +34,7 @@ const websocketUrl = ref(props.wsUrl);
const { data } = useWebSocket(websocketUrl);
const editorRef: any = useTemplateRef('editorRef');
const editorRef = useTemplateRef<InstanceType<typeof MonacoEditor>>('editorRef');
const modelValue = defineModel<string>('modelValue', {
type: String,
@@ -56,9 +56,9 @@ const reload = (wsUrl: string) => {
};
const revealLastLine = () => {
const editor = editorRef.value.getEditor();
const lineCount = editor?.getModel().getLineCount();
editor.revealLine(lineCount);
const editor = editorRef.value?.getEditor();
const lineCount = editor?.getModel()?.getLineCount();
editor?.revealLine(lineCount || 0);
};
defineExpose({

View File

@@ -4,7 +4,7 @@
<span class="logo-title">
{{ `${themeConfig.globalTitle}` }}
<sub
><span style="font-size: 10px; color: goldenrod">{{ ` ${config.version}` }}</span></sub
><span style="font-size: 10px; color: goldenrod">{{ ` ${themeConfig.version}` }}</span></sub
>
</span>
</div>
@@ -17,7 +17,6 @@
import { computed } from 'vue';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '@/store/themeConfig';
import config from '@/common/config';
const { themeConfig } = storeToRefs(useThemeConfig());

View File

@@ -308,17 +308,7 @@
<!-- 其它设置 -->
<el-divider content-position="left">{{ $t('layout.config.otherSetting') }}</el-divider>
<div class="layout-breadcrumb-seting-bar-flex !mt-3.5">
<div class="layout-breadcrumb-seting-bar-flex-label">{{ $t('layout.config.tagsStyle') }}</div>
<div class="layout-breadcrumb-seting-bar-flex-value">
<el-select v-model="themeConfig.tagsStyle" placeholder="请选择" size="small" style="width: 90px">
<el-option label="风格1" value="tags-style-one"></el-option>
<el-option label="风格2" value="tags-style-two"></el-option>
<el-option label="风格3" value="tags-style-three"></el-option>
</el-select>
</div>
</div>
<div class="layout-breadcrumb-seting-bar-flex !mt-3.5">
<div class="layout-breadcrumb-seting-bar-flex mt-3.5!">
<div class="layout-breadcrumb-seting-bar-flex-label">{{ $t('layout.config.animation') }}</div>
<div class="layout-breadcrumb-seting-bar-flex-value">
<el-select v-model="themeConfig.animation" size="small" style="width: 90px">
@@ -328,7 +318,7 @@
</el-select>
</div>
</div>
<div class="layout-breadcrumb-seting-bar-flex !mt-3.5 !mb-5.5">
<div class="layout-breadcrumb-seting-bar-flex mt-3.5! mb-5.5!">
<div class="layout-breadcrumb-seting-bar-flex-label">
{{ $t('layout.config.columnsAsideStyle') }}
</div>

View File

@@ -1,7 +1,7 @@
<template>
<div class="layout-navbars-tagsview" :class="{ 'layout-navbars-tagsview-shadow': themeConfig.layout === 'classic' }">
<el-scrollbar ref="scrollbarRef" @wheel.prevent="onHandleScroll">
<ul class="layout-navbars-tagsview-ul" :class="setTagsStyle" ref="tagsUlRef">
<ul class="layout-navbars-tagsview-ul" ref="tagsUlRef">
<li
v-for="(v, k) in tagsViews"
:key="k"
@@ -18,26 +18,20 @@
>
<SvgIcon :name="v.icon" class="layout-navbars-tagsview-ul-li-iconfont" v-if="themeConfig.isTagsviewIcon" />
<span>{{ $t(v.title) }}</span>
<template v-if="isActive(v)">
<SvgIcon
name="RefreshRight"
class="!text-[14px] ml-1 layout-navbars-tagsview-ul-li-refresh"
class="text-[14px]! ml-1 layout-navbars-tagsview-ul-li-icon layout-navbars-tagsview-ul-li-refresh"
@click.stop="refreshCurrentTagsView($route.fullPath)"
/>
<SvgIcon
name="Close"
class="!text-[14px] layout-navbars-tagsview-ul-li-icon layout-icon-active"
class="text-[14px]! layout-navbars-tagsview-ul-li-icon layout-navbars-tagsview-ul-li-close layout-icon-active"
v-if="!v.isAffix"
@click.stop="closeCurrentTagsView(themeConfig.isShareTagsView ? v.path : v.path)"
/>
</template>
<SvgIcon
name="Close"
class="!text-[14px] layout-navbars-tagsview-ul-li-icon layout-icon-three"
v-if="!v.isAffix"
@click.stop="closeCurrentTagsView(themeConfig.isShareTagsView ? v.path : v.path)"
/>
</li>
</ul>
</el-scrollbar>
@@ -46,7 +40,7 @@
</template>
<script lang="ts" setup name="layoutTagsView">
import { reactive, onMounted, computed, ref, nextTick, onBeforeUpdate, getCurrentInstance, watch } from 'vue';
import { reactive, onMounted, ref, nextTick, onBeforeUpdate, getCurrentInstance, watch } from 'vue';
import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router';
import screenfull from 'screenfull';
import { storeToRefs } from 'pinia';
@@ -105,11 +99,6 @@ const state = reactive({
},
});
// 动态设置 tagsView 风格样式
const setTagsStyle = computed(() => {
return themeConfig.value.tagsStyle;
});
// 存储 tagsViewList 到浏览器临时缓存中,页面刷新时,保留记录
const addBrowserSetSession = (tagsViewList: Array<object>) => {
setTagViews(tagsViewList);
@@ -403,163 +392,120 @@ onBeforeRouteUpdate((to) => {
});
</script>
<style scoped lang="scss">
<style scoped lang="css">
.layout-navbars-tagsview {
background-color: var(--bg-main-color);
border-bottom: 1px solid var(--el-border-color-light, #ebeef5);
position: relative;
z-index: 4;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
:deep(.el-scrollbar__wrap) {
overflow-x: auto !important;
}
.layout-navbars-tagsview :deep(.el-scrollbar__wrap) {
overflow-x: auto !important;
}
&-ul {
list-style: none;
margin: 0;
padding: 0;
height: 34px;
display: flex;
align-items: center;
color: var(--el-text-color-regular);
font-size: 12px;
white-space: nowrap;
padding: 0 15px;
.layout-navbars-tagsview-ul {
list-style: none;
margin: 0;
padding: 0;
height: 38px;
display: flex;
align-items: center;
color: var(--el-text-color-regular);
font-size: 13px;
white-space: nowrap;
padding: 0 15px;
}
&-li {
height: 26px;
line-height: 26px;
display: flex;
align-items: center;
border: 1px solid var(--el-border-color-lighter);
padding: 0 15px;
margin-right: 5px;
border-radius: 2px;
position: relative;
z-index: 0;
cursor: pointer;
justify-content: space-between;
.layout-navbars-tagsview-ul-li {
height: 30px;
line-height: 30px;
display: flex;
align-items: center;
border-radius: 6px;
padding: 0 12px;
margin-right: 6px;
position: relative;
z-index: 0;
cursor: pointer;
justify-content: space-between;
transition: all 0.3s ease;
border: 1px solid var(--el-border-color, #dcdfe6);
box-sizing: border-box;
background-color: var(--el-bg-color, #fafafa);
color: var(--el-text-color-regular, #606266);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
&:hover {
background-color: var(--el-color-primary-light-9);
color: var(--el-color-primary);
border-color: var(--el-color-primary-light-5);
}
.layout-navbars-tagsview-ul-li:not(.is-active):hover {
background-color: var(--el-fill-color-blank, #f5f7fa);
color: var(--el-text-color-primary, #303133);
border-color: var(--el-color-primary-light-7, #c6e2ff);
transform: translateY(-1px);
}
&-iconfont {
position: relative;
left: -5px;
font-size: 12px;
}
.layout-navbars-tagsview-ul-li-iconfont {
position: relative;
left: -3px;
font-size: 12px;
margin-right: 4px;
}
&-icon {
border-radius: 100%;
position: relative;
height: 14px;
width: 14px;
text-align: center;
line-height: 14px;
right: -5px;
.layout-navbars-tagsview-ul-li-icon {
border-radius: 4px;
position: relative;
height: 18px;
width: 18px;
text-align: center;
line-height: 18px;
right: -3px;
margin-left: 4px;
transition: all 0.25s ease;
color: var(--el-text-color-secondary, #909399);
display: flex;
align-items: center;
justify-content: center;
}
&:hover {
color: var(--el-color-white);
background-color: var(--el-color-primary-light-3);
}
}
.layout-navbars-tagsview-ul-li-icon:hover {
background-color: var(--el-color-info-light-7);
border-radius: 4px;
}
.layout-icon-active {
display: block;
}
.layout-icon-active {
display: flex;
align-items: center;
justify-content: center;
}
.layout-icon-three {
display: none;
}
}
.layout-navbars-tagsview-ul .is-active {
color: var(--el-color-primary, #409eff);
background: var(--el-color-primary-light-9, #ecf5ff);
border-color: var(--el-color-primary-light-5, #409eff);
box-shadow: 0 2px 4px rgba(64, 158, 255, 0.2);
}
.is-active {
color: var(--el-color-white);
background: var(--el-color-primary);
border-color: var(--el-color-primary);
transition: border-color 3s ease;
}
}
.layout-navbars-tagsview-ul .is-active .layout-navbars-tagsview-ul-li-icon {
color: var(--el-color-primary, #409eff);
}
// 风格2
.tags-style-two {
.layout-navbars-tagsview-ul-li {
margin-right: 0 !important;
border: none !important;
position: relative;
border-radius: 3px !important;
.layout-navbars-tagsview-ul .is-active .layout-navbars-tagsview-ul-li-icon:hover {
background-color: var(--el-color-primary);
color: var(--el-color-white);
transform: scale(1.1);
}
.layout-icon-active {
display: none;
}
.layout-navbars-tagsview-ul .is-active .layout-navbars-tagsview-ul-li-close:hover {
background-color: var(--el-color-danger);
color: var(--el-color-white);
border-radius: 4px;
}
.layout-icon-three {
display: block;
}
&:hover {
background: none !important;
}
}
.is-active {
background: none !important;
color: var(--el-color-primary) !important;
}
}
// 风格3
.tags-style-three {
align-items: flex-end;
.tgs-style-three-svg {
-webkit-mask-image:
url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzAiIGhlaWdodD0iNzAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbD0ibm9uZSI+CgogPGc+CiAgPHRpdGxlPkxheWVyIDE8L3RpdGxlPgogIDxwYXRoIHRyYW5zZm9ybT0icm90YXRlKC0wLjEzMzUwNiA1MC4xMTkyIDUwKSIgaWQ9InN2Z18xIiBkPSJtMTAwLjExOTE5LDEwMGMtNTUuMjI4LDAgLTEwMCwtNDQuNzcyIC0xMDAsLTEwMGwwLDEwMGwxMDAsMHoiIG9wYWNpdHk9InVuZGVmaW5lZCIgc3Ryb2tlPSJudWxsIiBmaWxsPSIjRjhFQUU3Ii8+CiAgPHBhdGggZD0ibS0wLjYzNzY2LDcuMzEyMjhjMC4xMTkxOSwwIDAuMjE3MzcsMC4wNTc5NiAwLjQ3Njc2LDAuMTE5MTljMC4yMzIsMC4wNTQ3NyAwLjI3MzI5LDAuMDM0OTEgMC4zNTc1NywwLjExOTE5YzAuMDg0MjgsMC4wODQyOCAwLjM1NzU3LDAgMC40NzY3NiwwbDAuMTE5MTksMGwwLjIzODM4LDAiIGlkPSJzdmdfMiIgc3Ryb2tlPSJudWxsIiBmaWxsPSJub25lIi8+CiAgPHBhdGggZD0ibTI4LjkyMTM0LDY5LjA1MjQ0YzAsMC4xMTkxOSAwLDAuMjM4MzggMCwwLjM1NzU3bDAsMC4xMTkxOWwwLDAuMTE5MTkiIGlkPSJzdmdfMyIgc3Ryb2tlPSJudWxsIiBmaWxsPSJub25lIi8+CiAgPHJlY3QgaWQ9InN2Z180IiBoZWlnaHQ9IjAiIHdpZHRoPSIxLjMxMTA4IiB5PSI2LjgzNTUyIiB4PSItMC4wNDE3MSIgc3Ryb2tlPSJudWxsIiBmaWxsPSJub25lIi8+CiAgPHJlY3QgaWQ9InN2Z181IiBoZWlnaHQ9IjEuNzg3ODQiIHdpZHRoPSIwLjExOTE5IiB5PSI2OC40NTY1IiB4PSIyOC45MjEzNCIgc3Ryb2tlPSJudWxsIiBmaWxsPSJub25lIi8+CiAgPHJlY3QgaWQ9InN2Z182IiBoZWlnaHQ9IjQuODg2NzciIHdpZHRoPSIxOS4wNzAzMiIgeT0iNTEuMjkzMjEiIHg9IjM2LjY2ODY2IiBzdHJva2U9Im51bGwiIGZpbGw9Im5vbmUiLz4KIDwvZz4KPC9zdmc+'),
url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzAiIGhlaWdodD0iNzAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbD0ibm9uZSI+CiA8Zz4KICA8dGl0bGU+TGF5ZXIgMTwvdGl0bGU+CiAgPHBhdGggdHJhbnNmb3JtPSJyb3RhdGUoLTg5Ljc2MjQgNy4zMzAxNCA1NS4xMjUyKSIgc3Ryb2tlPSJudWxsIiBpZD0ic3ZnXzEiIGZpbGw9IiNGOEVBRTciIGQ9Im02Mi41NzQ0OSwxMTcuNTIwODZjLTU1LjIyOCwwIC0xMDAsLTQ0Ljc3MiAtMTAwLC0xMDBsMCwxMDBsMTAwLDB6IiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGZpbGwtcnVsZT0iZXZlbm9kZCIvPgogIDxwYXRoIGQ9Im0tMC42Mzc2Niw3LjMxMjI4YzAuMTE5MTksMCAwLjIxNzM3LDAuMDU3OTYgMC40NzY3NiwwLjExOTE5YzAuMjMyLDAuMDU0NzcgMC4yNzMyOSwwLjAzNDkxIDAuMzU3NTcsMC4xMTkxOWMwLjA4NDI4LDAuMDg0MjggMC4zNTc1NywwIDAuNDc2NzYsMGwwLjExOTE5LDBsMC4yMzgzOCwwIiBpZD0ic3ZnXzIiIHN0cm9rZT0ibnVsbCIgZmlsbD0ibm9uZSIvPgogIDxwYXRoIGQ9Im0yOC45MjEzNCw2OS4wNTI0NGMwLDAuMTE5MTkgMCwwLjIzODM4IDAsMC4zNTc1N2wwLDAuMTE5MTlsMCwwLjExOTE5IiBpZD0ic3ZnXzMiIHN0cm9rZT0ibnVsbCIgZmlsbD0ibm9uZSIvPgogIDxyZWN0IGlkPSJzdmdfNCIgaGVpZ2h0PSIwIiB3aWR0aD0iMS4zMTEwOCIgeT0iNi44MzU1MiIgeD0iLTAuMDQxNzEiIHN0cm9rZT0ibnVsbCIgZmlsbD0ibm9uZSIvPgogIDxyZWN0IGlkPSJzdmdfNSIgaGVpZ2h0PSIxLjc4Nzg0IiB3aWR0aD0iMC4xMTkxOSIgeT0iNjguNDU2NSIgeD0iMjguOTIxMzQiIHN0cm9rZT0ibnVsbCIgZmlsbD0ibm9uZSIvPgogIDxyZWN0IGlkPSJzdmdfNiIgaGVpZ2h0PSI0Ljg4Njc3IiB3aWR0aD0iMTkuMDcwMzIiIHk9IjUxLjI5MzIxIiB4PSIzNi42Njg2NiIgc3Ryb2tlPSJudWxsIiBmaWxsPSJub25lIi8+CiA8L2c+Cjwvc3ZnPg=='),
url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'><rect rx='8' width='100%' height='100%' fill='%23F8EAE7'/></svg>");
-webkit-mask-size:
18px 30px,
20px 30px,
calc(100% - 30px) calc(100% + 17px);
-webkit-mask-position:
right bottom,
left bottom,
center top;
-webkit-mask-repeat: no-repeat;
}
.layout-navbars-tagsview-ul-li {
padding: 0 5px;
border-width: 15px 27px 15px;
border-style: solid;
border-color: transparent;
margin: 0 -15px;
.layout-icon-active {
display: none;
}
.layout-icon-three {
display: block;
}
&:hover {
@extend .tgs-style-three-svg;
background: var(--tagsview3-active-background-color);
color: unset;
}
}
.is-active {
@extend .tgs-style-three-svg;
background: var(--tagsview3-active-background-color) !important;
color: var(--el-color-primary) !important;
z-index: 1;
}
}
.layout-navbars-tagsview-ul .is-active .layout-navbars-tagsview-ul-li-refresh:hover {
background-color: var(--el-color-primary);
color: var(--el-color-white);
border-radius: 4px;
}
.layout-navbars-tagsview-shadow {

View File

@@ -98,8 +98,6 @@ export const useThemeConfig = defineStore('themeConfig', {
/* 其它设置
------------------------------- */
// 默认 Tagsview 风格,可选 1、 tags-style-one 2、 tags-style-two 3、 tags-style-three
tagsStyle: 'tags-style-three',
// 默认主页面切换动画,可选 1、 slide-right 2、 slide-left 3、 opacitys
animation: 'slide-right',
// 默认分栏高亮风格,可选 1、 圆角 columns-round 2、 卡片 columns-card
@@ -137,6 +135,7 @@ export const useThemeConfig = defineStore('themeConfig', {
appSlogan: 'common.appSlogan',
// 网站logo icon, base64编码内容
logoIcon: logoIcon,
version: 'latest',
// 默认初始语言,可选值"<zh-cn|en|zh-tw>",默认 zh-cn
globalI18n: 'zh-cn',
// 默认全局组件大小,可选值"<|large|default|small>",默认 ''
@@ -155,12 +154,15 @@ export const useThemeConfig = defineStore('themeConfig', {
if (tc) {
this.themeConfig = tc;
document.documentElement.style.cssText = getLocal('themeConfigStyle');
} else {
getServerConf().then((res) => {
this.themeConfig.globalI18n = res.i18n;
});
}
getServerConf().then((res) => {
this.themeConfig.globalI18n = res.i18n;
this.themeConfig.version = res.version;
});
this.themeConfig.defaultListPageSize = calculatePageSizeByScreenHeight();
// 根据后台系统配置初始化
getSysStyleConfig().then((res) => {
if (res?.title) {
@@ -215,3 +217,34 @@ export const useThemeConfig = defineStore('themeConfig', {
},
},
});
// 计算每页显示数量的方法
const calculatePageSizeByScreenHeight = (): number => {
const windowHeight = window.innerHeight || document.documentElement.clientHeight;
// 计算页面其他部分的高度(这是一个大概的估算)
// 包括顶部导航、面包屑、搜索区域、分页控件等
const headerHeight = 60; // 页面顶部导航高度
const subHeaderHeight = 50; // 子页面头部或其他内容高度
const searchFormHeight = 100; // 搜索表单高度,如果显示的话
const tableHeaderHeight = 44; // 表格头部高度
const paginationHeight = 40; // 分页控件高度
const paddingMarginHeight = 30; // 额外的内外边距
// 计算可用于表格内容的高度
const availableContentHeight =
windowHeight - headerHeight - subHeaderHeight - searchFormHeight - tableHeaderHeight - paginationHeight - paddingMarginHeight;
// 根据表格尺寸确定行高
const rowHeight = 40;
// 计算理论上的行数
const calculatedRows = Math.floor(availableContentHeight / rowHeight);
// 设置限制范围
const minPageSize = 10;
const maxPageSize = 30;
// 确保返回值在合理范围内,且至少有基本的行数
return Math.max(minPageSize, Math.min(maxPageSize, calculatedRows));
};

View File

@@ -40,7 +40,6 @@ declare interface ThemeConfigState {
isInvert: boolean;
isWatermark: boolean;
watermarkText: Array<string>;
tagsStyle: string;
animation: string;
columnsAsideStyle: string;
layout: string;
@@ -49,6 +48,7 @@ declare interface ThemeConfigState {
globalViceTitle: string;
appSlogan: string;
logoIcon: string;
version: string;
globalI18n: string;
globalComponentSize: string;
terminalTheme: string;

View File

@@ -27,16 +27,14 @@
</el-table-column>
<el-table-column :label="$t('flow.approvalRemark')" min-width="150">
<template #default="scope">
{{ scope.row.remark }}
</template>
<template #default="scope"> {{ scope.row.remark }} </template>
</el-table-column>
</el-table>
</el-tab-pane>
<el-tab-pane :label="$t('common.basic')" :name="basicTabName">
<el-form-item prop="auditRule" :label="$t('flow.aiAuditRule')">
<el-input v-model="form.auditRule" type="textarea" :rows="10" :placeholder="$t('flow.aiAuditRuleTip')" clearable />
<MonacoEditor class="w-full!" height="calc(100vh - 330px)" v-model="form.auditRule" language="markdown" />
</el-form-item>
</el-tab-pane>
</el-tabs>
@@ -45,6 +43,7 @@
import { notEmpty } from '@/common/assert';
import { formatDate } from '@/common/utils/format';
import EnumTag from '@/components/enumtag/EnumTag.vue';
import MonacoEditor from '@/components/monaco/MonacoEditor.vue';
import { useI18nPleaseInput } from '@/hooks/useI18n';
import { ProcinstTaskStatus } from '@/views/flow/enums';
import { computed } from 'vue';

View File

@@ -39,7 +39,7 @@
</div>
</div>
<el-splitter style="height: calc(100vh - 215px)" layout="vertical" @resize-end="onResizeTableHeight">
<el-splitter style="height: calc(100vh - 220px)" layout="vertical" @resize-end="onResizeTableHeight">
<el-splitter-panel :size="state.editorSize" max="80%">
<MonacoEditor ref="monacoEditorRef" class="mt-1" v-model="state.sql" language="sql" height="100%" :id="'MonacoTextarea-' + getKey()" />
</el-splitter-panel>
@@ -289,7 +289,7 @@ const onResizeTableHeight = (index: number, sizes: number[]) => {
editorHeight = plitpaneHeight / 2;
}
let tableDataHeight = plitpaneHeight - editorHeight - 43;
let tableDataHeight = plitpaneHeight - editorHeight - 47;
state.editorSize = editorHeight;
state.tableDataHeight = tableDataHeight + 'px';
@@ -332,6 +332,7 @@ const onRunSql = async (newTab = false) => {
* 执行多条SQL并合并结果
*/
const runMultipleSqls = async (sqls: string[], newTab: boolean) => {
state.execResTabs = [];
// 分类SQL语句
const nonQuerySqls: string[] = []; // 影响行数类SQL (UPDATE, INSERT, DELETE等)
const querySqls: string[] = []; // 查询类SQL (SELECT等)
@@ -399,7 +400,7 @@ const runNonQuerySqls = async (sqls: string[], newTab: boolean) => {
const result: any = (data.value as any)[0];
results.push({
sql: result.sql,
rowsAffected: result.res?.[0]?.rowsAffected,
rowsAffected: result.res?.[0].rowsAffected,
error: result.errorMsg || '-',
});
} catch (error: any) {
@@ -412,9 +413,9 @@ const runNonQuerySqls = async (sqls: string[], newTab: boolean) => {
// 设置表格列
state.execResTabs[i].tableColumn = [
{ columnName: 'sql', columnType: 'string', show: true },
{ columnName: 'rowsAffected', columnType: 'number', show: true },
{ columnName: 'error', columnType: 'string', show: true },
{ columnName: 'SQL', key: 'sql', columnType: 'string', show: true },
{ columnName: 'RowsAffected', key: 'rowsAffected', columnType: 'number', show: true },
{ columnName: 'Error', key: 'error', columnType: 'string', show: true },
];
state.execResTabs[i].data = results;
@@ -486,6 +487,7 @@ const runSql = async (sql: string, remark = '', newTab = false) => {
state.execResTabs[i].tableColumn = colAndData.columns.map((x: any) => {
return {
columnName: x.name,
key: x.key,
columnType: x.type,
show: true,
};

View File

@@ -77,9 +77,7 @@
<!-- 排序箭头图标 -->
<SvgIcon
v-if="
column.title == nowSortColumn?.columnName &&
!showColumnActions[column.key] &&
!columnActionVisible[column.key]
column.key == nowSortColumn?.key && !showColumnActions[column.key] && !columnActionVisible[column.key]
"
:color="'var(--el-color-primary)'"
:name="nowSortColumn?.order == 'asc' ? 'top' : 'bottom'"
@@ -135,7 +133,7 @@
<div v-else @dblclick="onEnterEditMode(rowData, column, rowIndex, columnIndex)">
<div v-if="canEdit(rowIndex, columnIndex)">
<ColumnFormItem
v-model="rowData[column.dataKey!]"
v-model="rowData[column.key!]"
:data-type="column.dataType"
@blur="onExitEditMode(rowData, column, rowIndex)"
:column-name="column.columnName"
@@ -143,11 +141,11 @@
/>
</div>
<div v-else :class="isUpdated(rowIndex, column.dataKey) ? 'update_field_active ml-0.5 mr-0.5' : 'ml-0.5 mr-0.5'">
<span v-if="rowData[column.dataKey!] === null" style="color: var(--el-color-info-light-5)"> NULL </span>
<div v-else :class="isUpdated(rowIndex, column.key) ? 'update_field_active ml-0.5 mr-0.5' : 'ml-0.5 mr-0.5'">
<span v-if="rowData[column.key!] === null" style="color: var(--el-color-info-light-5)"> NULL </span>
<span v-else :title="rowData[column.dataKey!]" class="el-text el-text--small is-truncated">
{{ rowData[column.dataKey!] }}
<span v-else :title="rowData[column.key!]" class="el-text el-text--small is-truncated">
{{ rowData[column.key!] }}
</span>
</div>
</div>
@@ -275,7 +273,7 @@ const columnActionVisible = ref({} as any);
const cmDataCopyCell = new ContextmenuItem('copyValue', 'common.copy')
.withIcon('CopyDocument')
.withOnClick(async (data: any) => {
await copyToClipboard(data.rowData[data.column.dataKey]);
await copyToClipboard(data.rowData[data.column.key]);
})
.withHideFunc(() => {
// 选中多条则隐藏该复制按钮
@@ -409,7 +407,6 @@ const dbConfig = useStorage('dbConfig', DbThemeConfig);
const rowNoColumn = {
title: 'No.',
key: 'tableDataRowNo',
dataKey: 'tableDataRowNo',
width: 45,
fixed: true,
align: 'center',
@@ -515,8 +512,6 @@ const setTableColumns = (columns: any) => {
x.remark = `${x.columnType} ${x.columnComment ? ' | ' + x.columnComment : ''}`;
return {
...x,
key: columnName,
dataKey: columnName,
width: DbInst.flexColumnWidth(columnName, state.datas),
title: columnName,
align: x.dataType == DataType.Number ? 'right' : 'left',
@@ -565,21 +560,21 @@ const hideColumnAction = () => {
const handleColumnCommand = (column: any, command: string) => {
switch (command) {
case 'sort-asc':
onTableSortChange({ columnName: column.dataKey, order: 'asc' });
onTableSortChange({ key: column.key, order: 'asc' });
break;
case 'sort-desc':
onTableSortChange({ columnName: column.dataKey, order: 'desc' });
onTableSortChange({ key: column.key, order: 'desc' });
break;
case 'fix':
state.columns.forEach((col: any) => {
if (col.dataKey == column.dataKey) {
if (col.key == column.key) {
col.fixed = true;
}
});
break;
case 'unfix':
state.columns.forEach((col: any) => {
if (col.dataKey == column.dataKey) {
if (col.key == column.key) {
col.fixed = false;
}
});
@@ -718,7 +713,7 @@ const onGenerateJson = async () => {
let obj: any = {};
for (let column of state.columns) {
if (column.show) {
obj[column.title] = selectionData[column.dataKey];
obj[column.title] = selectionData[column.key];
}
}
jsonObj.push(obj);
@@ -775,7 +770,7 @@ const onEnterEditMode = (rowData: any, column: any, rowIndex = 0, columnIndex =
nowUpdateCell.value = {
rowIndex: rowIndex,
colIndex: columnIndex,
oldValue: rowData[column.dataKey],
oldValue: rowData[column.key],
dataType: column.dataType,
};
};
@@ -785,7 +780,7 @@ const onExitEditMode = (rowData: any, column: any, rowIndex = 0) => {
return;
}
const oldValue = nowUpdateCell.value.oldValue;
const newValue = rowData[column.dataKey];
const newValue = rowData[column.key];
// 未改变单元格值
if (oldValue == newValue) {
@@ -800,7 +795,7 @@ const onExitEditMode = (rowData: any, column: any, rowIndex = 0) => {
cellUpdateMap.value.set(rowIndex, updatedRow);
}
const columnName = column.dataKey;
const columnName = column.key;
let cellData = updatedRow.columnsMap.get(columnName);
if (cellData) {
// 多次修改情况,可能又修改回原值,则移除该修改单元格

View File

@@ -152,7 +152,7 @@
<el-text
id="copyValue"
style="color: var(--el-color-info-light-3)"
class="is-truncated !text-[12px] mt-1"
class="is-truncated text-[12px]! mt-1"
@click="copyToClipboard(sql)"
:title="sql"
>{{ sql }}</el-text
@@ -392,6 +392,7 @@ const selectData = async () => {
const columns = await getNowDbInst().loadColumns(props.dbName, props.tableName);
columns.forEach((x: any) => {
x.show = true;
x.key = x.columnName;
});
state.columns = columns;
}
@@ -592,7 +593,7 @@ const onSelectByCondition = async () => {
*/
const onTableSortChange = async (sort: any) => {
const sortType = sort.order == 'desc' ? 'DESC' : 'ASC';
state.orderBy = `ORDER BY ${state.dbDialect.quoteIdentifier(sort.columnName)} ${sortType}`;
state.orderBy = `ORDER BY ${state.dbDialect.quoteIdentifier(sort.key)} ${sortType}`;
await onRefresh();
};

View File

@@ -316,7 +316,7 @@ watch(dialogVisible, async (newValue: boolean) => {
srcTreeRef.value.setCheckedKeys(form.checkedKeys.split(','));
// 初始化默认值
form.cronAble = state.form.cronAble || 0;
form.cronAble = form.cronAble || -1;
form.mode = form.mode || 1;
form.extra = form.extra || { fileType: fileTypeOptions[0].value };

View File

@@ -70,5 +70,5 @@ export function getMachineTerminalSocketUrl(authCertName: any) {
}
export function getMachineRdpSocketUrl(authCertName: any) {
return `/machines/rdp/${authCertName}`;
return `/api/machines/rdp/${authCertName}`;
}

View File

@@ -85,12 +85,18 @@ const NodeTypeAuthCert = new NodeType(12)
(await node.ctx?.addResourceComponent(MachineOpComp)).openTerminal(node.params);
})
.withContextMenuItems([
new ContextmenuItem('term', 'machine.openTerminal').withIcon('Monitor').withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params);
}),
new ContextmenuItem('term-ex', 'machine.newTabOpenTerminal').withIcon('Monitor').withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params, true);
}),
new ContextmenuItem('term', 'machine.openTerminal')
.withIcon('Monitor')
.withPermission('machine:terminal')
.withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params);
}),
new ContextmenuItem('term-ex', 'machine.newTabOpenTerminal')
.withIcon('Monitor')
.withPermission('machine:terminal')
.withOnClick(async (node: TagTreeNode) => {
(await node.ctx?.addResourceComponent(MachineOpComp))?.openTerminal(node.params, true);
}),
new ContextmenuItem('files', 'machine.fileManage').withIcon('FolderOpened').withOnClick(async (node: any) => {
(await node.ctx?.addResourceComponent(MachineOpComp)).showFileManage(node.params);
}),

View File

@@ -32,7 +32,7 @@
<template #dropdown>
<el-dropdown-menu>
<template v-for="item in contextMenuItems" :key="item.clickId">
<el-dropdown-item v-if="!item.isHide(props.data)" :command="item">
<el-dropdown-item v-if="!item.isHide(props.data) && hasPerm(item.permission)" :command="item">
<SvgIcon v-if="item.icon" :name="item.icon" class="mr-1" />{{ $t(item.txt) }}
</el-dropdown-item>
</template>
@@ -54,6 +54,7 @@ import SvgIcon from '@/components/svgIcon/index.vue';
import { ContextmenuItem } from '@/components/contextmenu';
import { ResourceOpCtx, TagTreeNode } from '@/views/ops/component/tag';
import { ResourceOpCtxKey } from '@/views/ops/resource/resource';
import { hasPerm } from '@/components/auth/auth';
const resourceOpCtx: ResourceOpCtx | undefined = inject(ResourceOpCtxKey, undefined);

View File

@@ -18,20 +18,19 @@ jwt:
expire-time: 720
# refreshToken过期时间单位分钟
refresh-token-expire-time: 4320
# 资源密码aes加密key
aes:
key: 1111111111111111
# 若存在mysql配置优先使用mysql
mysql:
host: mysql:3306
# 数据库配置dialect支持mysql、sqlite
db:
dialect: mysql
address: mysql:3306
name: mayfly-go
username: root
password: 111049
db-name: mayfly-go
config: charset=utf8&loc=Local&parseTime=true
max-idle-conns: 5
sqlite:
path: ./mayfly-go.sqlite
max-idle-conns: 5
# db:
# dialect: sqlite
# address: ./mayfly-go.db
# max-idle-conns: 5
# 若同时部署多台机器则需要配置redis信息用于缓存权限码、验证码、公私钥等
# redis:
# host: localhost
@@ -55,3 +54,6 @@ log:
# max-age: 60
# # 是否使用 gzip 压缩方式压缩轮转后的日志文件
# compress: true
# 资源密码aes加密key
aes:
key: 1111111111111111

View File

@@ -6,8 +6,8 @@ require (
gitee.com/chunanyong/dm v1.8.22
gitee.com/liuzongyang/libpq v1.10.11
github.com/antlr4-go/antlr/v4 v4.13.1
github.com/cloudwego/eino v0.7.13
github.com/cloudwego/eino-ext/components/model/openai v0.1.6
github.com/cloudwego/eino v0.7.32
github.com/cloudwego/eino-ext/components/model/openai v0.1.8
github.com/docker/docker v28.5.0+incompatible
github.com/docker/go-connections v0.6.0
github.com/gin-gonic/gin v1.11.0
@@ -18,17 +18,17 @@ require (
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.30.1
github.com/go-sql-driver/mysql v1.9.3
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20250930013652-2d71241a3bb9
github.com/microsoft/go-mssqldb v1.9.3
github.com/microsoft/go-mssqldb v1.9.6
github.com/mojocn/base64Captcha v1.3.8 //
github.com/opencontainers/image-spec v1.1.1
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.10
github.com/pquerna/otp v1.5.0
github.com/redis/go-redis/v9 v9.17.2
github.com/redis/go-redis/v9 v9.17.3
github.com/robfig/cron/v3 v3.0.1 //
github.com/sijms/go-ora/v2 v2.9.0
github.com/spf13/cast v1.10.0
@@ -59,7 +59,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/clipperhouse/uax29/v2 v2.2.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/cloudwego/eino-ext/libs/acl/openai v0.1.10 // indirect
github.com/cloudwego/eino-ext/libs/acl/openai v0.1.13 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
@@ -109,6 +109,7 @@ require (
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.55.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/slongfield/pyfmt v0.0.0-20220222012616-ea85ff4c361f // indirect
github.com/tidwall/match v1.2.0 // indirect

View File

@@ -2,52 +2,42 @@ package agent
import (
"context"
"errors"
"io"
"mayfly-go/internal/ai/config"
aimodel "mayfly-go/internal/ai/model"
"mayfly-go/pkg/gox"
"mayfly-go/pkg/logx"
"github.com/cloudwego/eino/adk"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/flow/agent"
"github.com/cloudwego/eino/flow/agent/react"
"github.com/cloudwego/eino/schema"
)
// GetAiAgent 获取AI Agent
func GetAiAgent(ctx context.Context, aiConfig *config.AIModelConfig, tools ...tool.BaseTool) (*react.Agent, error) {
aiModel := aimodel.GetAIModelByConfig(aiConfig)
if aiModel == nil {
return nil, errors.New("no supported AI model found")
}
toolableChatModel, err := aiModel.GetChatModel(ctx, aiConfig)
// GetAgent 获取AI Agent
func GetAgent(ctx context.Context, aiConfig *config.AIModelConfig, tools ...tool.BaseTool) (adk.Agent, error) {
toolableChatModel, err := aimodel.GetChatModel(ctx, aiConfig)
if err != nil {
return nil, err
}
// 初始化所需的 tools
toolsConf := compose.ToolsNodeConfig{
Tools: tools,
}
// 创建 agent
return react.NewAgent(ctx, &react.AgentConfig{
ToolCallingModel: toolableChatModel,
ToolsConfig: toolsConf,
MaxStep: len(toolsConf.Tools)*1 + 3,
MessageModifier: func(ctx context.Context, input []*schema.Message) []*schema.Message {
return input
},
toolsConfig := adk.ToolsConfig{}
toolsConfig.Tools = tools
chatAgent, err := adk.NewChatModelAgent(ctx, &adk.ChatModelAgentConfig{
Name: "ops_expert",
Description: "一位拥有20多年系统管理、数据库管理和基础设施优化经验的专业DevOps专家。",
Instruction: `你现在是一位专业的数据库管理员、Redis管理员和安全审核专家请根据用户的问题给出最合适的答案。`,
Model: toolableChatModel,
ToolsConfig: toolsConfig,
})
if err != nil {
return nil, err
}
return chatAgent, nil
}
type AiAgent struct {
*react.Agent
}
// NewAiAgent 创建AI Agent并注册指定类型的工具
func NewAiAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
// GetOpsExpertAgent 获取运维专家agent
func GetOpsExpertAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
tools := make([]tool.BaseTool, 0)
for _, toolType := range toolTypes {
if t, exists := GetTools(toolType); exists {
@@ -55,7 +45,7 @@ func NewAiAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
}
}
agent, err := GetAiAgent(ctx, config.GetAiModel(), tools...)
agent, err := GetAgent(ctx, config.GetAiModel(), tools...)
if err != nil {
return nil, err
}
@@ -64,85 +54,52 @@ func NewAiAgent(ctx context.Context, toolTypes ...ToolType) (*AiAgent, error) {
}, nil
}
// Chat 聊天,返回消息流通道
func (aiAgent *AiAgent) Chat(ctx context.Context, sysPrompt string, question string) (chan *schema.Message, chan error) {
ch := make(chan *schema.Message, 512)
errCh := make(chan error, 1)
type AiAgent struct {
adk.Agent
}
// Run 运行,并返回最终结果
func (aiAgent *AiAgent) Run(ctx context.Context, sysPrompt string, question string) (string, error) {
if sysPrompt == "" {
sysPrompt = "你现在是一位拥有20年实战经验的顶级系统运维专家精通Linux操作系统、数据库管理如MySQL、PostgreSQL、NoSQL数据库如Redis、MongoDB以及搜索引擎如Elasticsearch。"
}
agentOption := []agent.AgentOption{}
runner := adk.NewRunner(ctx, adk.RunnerConfig{
EnableStreaming: false,
Agent: aiAgent.Agent,
CheckPointStore: NewInMemoryStore(),
})
go func() {
defer gox.RecoverPanic()
defer close(ch)
defer close(errCh)
sr, err := aiAgent.Stream(ctx, []*schema.Message{
{
Role: schema.System,
Content: sysPrompt,
},
{
Role: schema.User,
Content: question,
},
}, agentOption...)
if err != nil {
errCh <- err // 将错误发送到错误通道
return
}
defer sr.Close()
iter := runner.Run(ctx, []adk.Message{
{
Role: schema.System,
Content: sysPrompt,
},
{
Role: schema.User,
Content: question,
},
})
for {
msg, err := sr.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
logx.Errorf("failed to recv response: %v", err)
break
}
// logx.Debugf("stream: %s", msg.String())
ch <- msg
}
}()
return ch, errCh
}
// GetChatMsg 获取完整的聊天回复内容
func (aiAgent *AiAgent) GetChatMsg(ctx context.Context, sysPrompt string, question string) (string, error) {
msgChan, errChan := aiAgent.Chat(ctx, sysPrompt, question)
res := ""
// 使用 select 同时监听消息通道和错误通道
for {
select {
case msg, ok := <-msgChan:
if !ok {
// 消息通道已关闭,说明正常结束
// 检查错误通道是否有错误
select {
case err := <-errChan:
if err != nil {
return "", err
}
default:
return res, nil
}
return res, nil
}
res += msg.Content
case err := <-errChan:
// 优先检查错误通道
if err != nil {
return "", err
}
case <-ctx.Done():
// 上下文被取消
return "", ctx.Err()
event, ok := iter.Next()
if !ok {
break
}
err := event.Err
if err != nil {
logx.Error(err.Error())
return res, err
}
LogEvent(event)
msg := event.Output.MessageOutput.Message
if msg != nil {
res = msg.Content
}
}
return res, nil
}

View File

@@ -0,0 +1,121 @@
package agent
import (
"fmt"
"io"
"log"
"mayfly-go/pkg/logx"
"strings"
"github.com/cloudwego/eino/adk"
"github.com/cloudwego/eino/schema"
)
func LogEvent(event *adk.AgentEvent) {
logx.Debugf("agent name: %s, path: %s", event.AgentName, event.RunPath)
if event.Output != nil && event.Output.MessageOutput != nil {
if m := event.Output.MessageOutput.Message; m != nil {
if len(m.Content) > 0 {
if m.Role == schema.Tool {
logx.Debugf("agent tool response: %s", m.Content)
} else {
logx.Debugf("agent answer: %s", m.Content)
}
}
if len(m.ToolCalls) > 0 {
for _, tc := range m.ToolCalls {
logx.Debugf("agent tool name: %s", tc.Function.Name)
logx.Debugf("agent tool arguments: %s", tc.Function.Arguments)
}
}
} else if s := event.Output.MessageOutput.MessageStream; s != nil {
toolMap := map[int][]*schema.Message{}
var contentStart bool
charNumOfOneRow := 0
maxCharNumOfOneRow := 120
for {
chunk, err := s.Recv()
if err != nil {
if err == io.EOF {
break
}
logx.Debugf("agent error: %v", err)
return
}
if chunk.Content != "" {
if !contentStart {
contentStart = true
if chunk.Role == schema.Tool {
logx.Debugf("agent tool response: ")
} else {
logx.Debugf("agent answer: ")
}
}
charNumOfOneRow += len(chunk.Content)
if strings.Contains(chunk.Content, "\n") {
charNumOfOneRow = 0
} else if charNumOfOneRow >= maxCharNumOfOneRow {
logx.Debugf("\n")
charNumOfOneRow = 0
}
logx.Debugf("%v", chunk.Content)
}
if len(chunk.ToolCalls) > 0 {
for _, tc := range chunk.ToolCalls {
index := tc.Index
if index == nil {
logx.Error("index is nil")
}
toolMap[*index] = append(toolMap[*index], &schema.Message{
Role: chunk.Role,
ToolCalls: []schema.ToolCall{
{
ID: tc.ID,
Type: tc.Type,
Index: tc.Index,
Function: schema.FunctionCall{
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
},
},
})
}
}
}
for _, msgs := range toolMap {
m, err := schema.ConcatMessages(msgs)
if err != nil {
log.Fatalf("ConcatMessage failed: %v", err)
return
}
logx.Debugf("agent tool name: %s", m.ToolCalls[0].Function.Name)
logx.Debugf("agent tool arguments: %s", m.ToolCalls[0].Function.Arguments)
}
}
}
if event.Action != nil {
if event.Action.TransferToAgent != nil {
logx.Debugf("agent action: transfer to %v", event.Action.TransferToAgent.DestAgentName)
}
if event.Action.Interrupted != nil {
for _, ic := range event.Action.Interrupted.InterruptContexts {
str, ok := ic.Info.(fmt.Stringer)
if ok {
logx.Debugf("\n%s", str.String())
} else {
logx.Debugf("\n%v", ic.Info)
}
}
}
if event.Action.Exit {
logx.Debugf("agent action: exit")
}
}
if event.Err != nil {
logx.Debugf("agent error: %v", event.Err)
}
}

View File

@@ -0,0 +1,27 @@
package agent
import (
"context"
"github.com/cloudwego/eino/compose"
)
func NewInMemoryStore() compose.CheckPointStore {
return &inMemoryStore{
mem: map[string][]byte{},
}
}
type inMemoryStore struct {
mem map[string][]byte
}
func (i *inMemoryStore) Set(ctx context.Context, key string, value []byte) error {
i.mem[key] = value
return nil
}
func (i *inMemoryStore) Get(ctx context.Context, key string) ([]byte, bool, error) {
v, ok := i.mem[key]
return v, ok, nil
}

View File

@@ -0,0 +1,101 @@
package agent
import (
"bytes"
"errors"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"regexp"
"strings"
)
// ParseLLMJSON 尝试从大模型输出中解析 JSON
func ParseLLMJSON[T any](raw string) (*T, error) {
candidates := extractJSONCandidates(raw)
var lastErr error
for _, c := range candidates {
if v, err := jsonx.To[T](c); err == nil {
return v, nil
} else {
lastErr = err
}
}
if lastErr == nil {
lastErr = errors.New("no json candidate found")
}
return nil, lastErr
}
// ParseLLMJSON2Map 解析 LLM 返回的JSON为map
func ParseLLMJSON2Map(raw string) (collx.M, error) {
if res, err := ParseLLMJSON[collx.M](raw); err != nil {
return nil, err
} else {
return *res, nil
}
}
func extractJSONCandidates(raw string) []string {
var results []string
text := strings.TrimSpace(raw)
// 1. 优先提取 code block 中的 JSON对象 or 数组)
codeBlockRe := regexp.MustCompile(
"(?s)```(?:json)?\\s*([\\[{].*?[\\]}])\\s*```",
)
matches := codeBlockRe.FindAllStringSubmatch(text, -1)
for _, m := range matches {
results = append(results, strings.TrimSpace(m[1]))
}
// 2. 如果没找到 code block尝试从全文裁剪 JSON
if len(results) == 0 {
if clipped := clipJSONValue(text); clipped != "" {
results = append(results, clipped)
}
}
return results
}
func clipJSONValue(s string) string {
objIdx := strings.Index(s, "{")
arrIdx := strings.Index(s, "[")
start := -1
var open, close byte
switch {
case objIdx != -1 && (arrIdx == -1 || objIdx < arrIdx):
start = objIdx
open, close = '{', '}'
case arrIdx != -1:
start = arrIdx
open, close = '[', ']'
default:
return ""
}
var buf bytes.Buffer
depth := 0
for i := start; i < len(s); i++ {
ch := s[i]
buf.WriteByte(ch)
switch ch {
case open:
depth++
case close:
depth--
if depth == 0 {
return buf.String()
}
}
}
return ""
}

View File

@@ -0,0 +1,108 @@
package agent
import (
"testing"
)
// TestParseLLMJSON 测试 ParseLLMJSON 函数
func TestParseLLMJSON(t *testing.T) {
// 定义测试用例结构体
tests := []struct {
name string
input string
expected any
hasError bool
}{
{
name: "Valid JSON Object",
input: "```json\n{\n \"name\": \"Alice\",\n \"age\": \"30\"\n}\n```",
expected: map[string]any{
"name": "Alice",
"age": "30",
},
hasError: false,
},
{
name: "Valid JSON Object",
input: "```\n{\n \"name\": \"Alice\",\n \"age\": \"40\"\n}\n```",
expected: map[string]any{
"name": "Alice",
"age": "40",
},
hasError: false,
},
{
name: "Valid JSON Object",
input: "aaabbbccc```\n{\n \"name\": \"Alice\",\n \"age\": \"50\"\n}\n```dddd",
expected: map[string]any{
"name": "Alice",
"age": "50",
},
hasError: false,
},
{
name: "Valid JSON Array",
input: "```json\n[\n {\"id\": \"1\", \"value\": \"foo\"},\n {\"id\": \"2\", \"value\": \"bar\"}\n]\n```",
expected: []map[string]any{
{"id": "1", "value": "foo"},
{"id": "2", "value": "bar"},
},
hasError: false,
},
{
name: "Valid JSON Array",
input: "aaaa```json\n[\n {\"id\": \"11\", \"value\": \"foo\"},\n {\"id\": \"22\", \"value\": \"bar\"}\n]\n```",
expected: []map[string]any{
{"id": "11", "value": "foo"},
{"id": "22", "value": "bar"},
},
hasError: false,
},
{
name: "Invalid JSON Format",
input: "This is not a valid JSON",
expected: nil,
hasError: true,
},
{
name: "Empty Input",
input: "",
expected: nil,
hasError: true,
},
}
// 执行测试用例
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result any
var err error
// 根据 expected 类型调用不同的 ParseLLMJSON 方法
switch tt.expected.(type) {
case map[string]any:
result, err = ParseLLMJSON[map[string]any](tt.input)
case []map[string]any:
result, err = ParseLLMJSON[[]map[string]any](tt.input)
default:
result, err = ParseLLMJSON[any](tt.input)
}
// 验证错误情况
if tt.hasError {
if err == nil {
t.Errorf("expected an error but got none")
}
return
}
// 验证无错误情况下的结果
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
t.Logf("%v", result)
})
}
}

View File

@@ -2,7 +2,6 @@ package api
import (
"fmt"
"mayfly-go/internal/ai/prompt"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/req"
@@ -116,16 +115,5 @@ func (a *AiDB) GenerateSql(rc *req.Ctx) {
func generateSqlPrompt(dbType, text string, tables []string) string {
// 使用prompt包中的GetPrompt函数获取提示词模板
// 如果没有找到模板,则使用默认模板
tableStr := strings.Join(tables, ", ")
promptTemplate := prompt.GetPrompt("SQL_GENERATE", dbType, tableStr)
if promptTemplate == "" {
promptTemplate = "你是一位专业的SQL开发工程师请根据用户的自然语言描述生成符合%s语法的SQL语句。\n"
if len(tables) > 0 {
promptTemplate += "相关表名:" + tableStr + "\n"
}
promptTemplate += "请确保生成的SQL语句语法正确仅返回SQL语句不要包含其他解释内容。"
promptTemplate = fmt.Sprintf(promptTemplate, dbType)
}
return promptTemplate
return ""
}

View File

@@ -1,13 +1,13 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/ai/api"
"mayfly-go/pkg/starter"
)
func init() {
// 注册AI模块的IoC组件
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
api.InitIoc()
})
}

View File

@@ -2,17 +2,28 @@ package model
import (
"context"
"errors"
"fmt"
"mayfly-go/internal/ai/config"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/collx"
"github.com/cloudwego/eino/components/model"
)
// 定义响应格式常量
const (
ResponseFormatJSON = "json_object"
ResponseFormatText = "text"
)
func init() {
RegisterAIModel(new(Openai))
}
var (
aiModelMap = map[string]AIModel{}
chatModels = collx.SM[string,model.ToolCallingChatModel]{}
)
type AIModel interface {
@@ -20,8 +31,8 @@ type AIModel interface {
// SupportModel 支持的模型
SupportModel() string
// GetChatModel 获取聊天模型
GetChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error)
// NewChatModel 创建chat模型
NewChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error)
}
// RegisterAIModel 注册AI模型
@@ -38,3 +49,41 @@ func GetAIModel(name string) AIModel {
func GetAIModelByConfig(aiConfig *config.AIModelConfig) AIModel {
return GetAIModel(aiConfig.ModelType)
}
// GetChatModel 获取Chat模型
func GetChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error) {
aiModel := GetAIModelByConfig(aiConfig)
if aiModel == nil {
return nil, errors.New("no supported AI model found")
}
cacheKey := generateCacheKey(aiConfig)
if chatModel, ok := chatModels.Load(cacheKey); ok {
logx.Debugf("ai model [%s/%s] - get chat model from cache", aiConfig.ModelType, aiConfig.Model)
return chatModel, nil
}
// 删除已存在的缓存
chatModels.Clear()
chatModel, err := aiModel.NewChatModel(ctx, aiConfig)
if err != nil {
return nil, err
}
logx.Debugf("ai model [%s/%s] - new chat model", aiConfig.ModelType,aiConfig.Model)
chatModels.Store(cacheKey, chatModel)
return chatModel, nil
}
// generateCacheKey 生成基于 aiConfig 关键字段的缓存键
func generateCacheKey(config *config.AIModelConfig) string {
return fmt.Sprintf("%s_%s_%s_%s_%d_%f",
config.ModelType,
config.Model,
config.BaseUrl,
config.ApiKey,
config.MaxTokens,
config.Temperature,
)
}

View File

@@ -16,7 +16,7 @@ func (o *Openai) SupportModel() string {
return "openai"
}
func (o *Openai) GetChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error) {
func (o *Openai) NewChatModel(ctx context.Context, aiConfig *config.AIModelConfig) (model.ToolCallingChatModel, error) {
return openai.NewChatModel(ctx, &openai.ChatModelConfig{
BaseURL: aiConfig.BaseUrl,
Model: aiConfig.Model,

View File

@@ -2,51 +2,20 @@ package prompt
import (
"embed"
"fmt"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/stringx"
"strings"
)
const (
FLOW_BIZ_AUDIT = "FLOW_BIZ_AUDIT"
SQL_GENERATE = "SQL_GENERATE"
)
//go:embed prompts.txt
//go:embed prompts/*.md
var prompts embed.FS
// prompt缓存 key: XXX_YYY value: 内容
var promptCache = make(map[string]string, 20)
// GetPrompt 获取本地prompts文件内容并进行模板解析
func GetPrompt(filename string, values any) (string, error) {
// 自动添加 prompts/ 前缀
fullPath := "prompts/" + filename
// 获取本地文件的prompt内容并进行解析获取对应key的prompt内容
func GetPrompt(key string, formatValues ...any) string {
prompt := promptCache[key]
if prompt != "" {
return fmt.Sprintf(prompt, formatValues...)
}
bytes, err := prompts.ReadFile("prompts.txt")
bytes, err := prompts.ReadFile(fullPath)
if err != nil {
logx.Error("failed to read prompt file: prompts.txt, err: %v", err)
return ""
return "", err
}
allPrompts := string(bytes)
propmts := strings.Split(allPrompts, "---------------------------------------")
var res string
for _, keyAndPrompt := range propmts {
keyAndPrompt = stringx.TrimSpaceAndBr(keyAndPrompt)
// 获取第一行的Key信息如--XXX_YYY
info := strings.SplitN(keyAndPrompt, "\n", 2)
// prompt即去除第一行的key与备注信息
prompt := info[1]
// 获取keyXXX_YYY
promptKey := strings.Split(strings.Split(info[0], " ")[0], "--")[1]
if key == promptKey {
res = prompt
}
promptCache[promptKey] = prompt
}
return fmt.Sprintf(res, formatValues...)
return stringx.TemplateParse(string(bytes), values)
}

View File

@@ -1,38 +0,0 @@
--FLOW_BIZ_AUDIT 流程业务审核
你现在是一位专业的数据库管理员、Redis管理员和安全审核专家。请根据以下审核规则分析用户提供的内容并以严格的JSON格式返回分析结果。
- 当用户询问表结构时,禁止凭经验回答,可以使用 QueryTableInfo 工具获取真实表DDL数据进行核验字段等。
审核规则:
%s
待审核内容为结构化的业务操作请求,包含以下要素:
1. 操作指令可能包含单条或多条SQL语句和/或Redis命令
2. 数据库上下文:每条指令关联的目标数据库信息,包括:
- 数据库唯一标识(ID)
- 数据库实例名称
- 数据库类型(如MySQL、PostgreSQL、Redis等)
请根据指令类型和目标数据库类型,应用相应的安全审核规则进行逐条验证。若存在任何不符合安全规范的指令,整体审核结果应判定为不通过。
请严格遵循以下要求:
1. 仅输出有效的JSON对象不要包含任何解释性文字
2. 禁止包含任何Markdown格式包括但不限于```json、```等代码引用符号)
3. JSON格式必须严格包含以下字段且无额外内容
{
"allowExecute": boolean, // 是否允许执行操作true或false
"suggestion": string // 具体的建议内容,如"通过"或"拒绝原因"等。如果是多条命令审核,请详细说明哪条命令存在问题
}
---------------------------------------
--SQL_GENERATE 生成SQL
你是一位专业的SQL开发工程师请根据用户的自然语言描述生成符合%s语法的SQL语句。
相关表名:%s
请确保生成的SQL语句
1. 语法正确,符合指定数据库类型的标准
2. 逻辑清晰,准确表达用户的需求
3. 仅返回SQL语句不要包含任何解释或说明
4. 避免使用可能导致性能问题的写法
5. 确保SQL语句的安全性防止SQL注入等安全问题
如果用户的需求不明确或无法完全实现,请说明原因。

View File

@@ -0,0 +1,141 @@
# 系统角色
你是一位 **专业的数据库管理员DBA**、**Redis 管理员** 和 **安全审核专家**
你的唯一职责是:
**对用户提交的结构化业务操作请求进行安全审核,并输出是否允许执行的最终裁决。**
⚠️ **重要限制**
- 不执行任何实际操作
- 不提供 SQL 改写或优化建议
- 不基于经验或推测回答任何数据库事实
---
# 审核规则
{{.rule}}
---
# 核心强制原则(必须严格遵守)
## 1. 禁止猜测与推测
- ❌ 严禁基于经验、习惯或主观推测回答以下内容:
- 表结构
- 字段信息(名称、类型等)
- 索引信息(主键、唯一索引、普通索引)
- ✅ 仅允许基于 **真实、已验证的数据库元数据** 进行审核判断
---
## 2. 工具调用强制性
### 触发条件
当审核规则中出现以下任一要求时,**必须调用对应工具获取真实数据**
- 校验表是否存在
- 校验字段是否存在或字段类型
- 校验主键、唯一索引、普通索引
- 判断 SQL 是否依赖索引或命中索引安全规则
### 严格限制
- ❌ 禁止在未调用工具的情况下直接给出审核结论
- ❌ 禁止基于经验或假设进行判断
---
## 3. 信息不足即不通过
- 若审核所需的事实信息缺失、无法确认,或未通过工具校验
-**必须直接判定为不通过allowExecute = false**
---
# 工具说明
## 工具QueryTableInfo
### 功能说明
用于查询指定数据库表的真实结构信息,包括:
- 字段名称
- 字段类型
- 主键字段
- 索引信息(唯一索引 / 普通索引)
### 调用规则
- 当审核规则要求校验 **表、字段或索引** 时:
-**必须调用该工具**
-**不得跳过、替代或基于经验判断**
### 参数约束
- 工具参数必须 **完全来源于 SQL 解析结果**
- ❌ 禁止编造表名、字段名或数据库信息
---
# 输入说明
每条业务操作请求包含以下内容:
1. SQL 或 Redis 指令(单条或多条)
2. 数据库上下文信息:
- 数据库唯一标识ID
- 数据库实例名称
- 数据库类型MySQL / PostgreSQL / Redis 等)
---
# 输出说明(极其重要)
## 输出格式强制要求
-**最终只能输出一个 JSON 对象**
-**不得输出任何额外文字、解释、Markdown、日志或调试信息**
-**输出结果必须可被 JSON.parse 成功解析**
-**即使审核失败,也必须输出 JSON**
---
## 输出结构(字段不可增删)
```json
{
"allowExecute": boolean,
"suggestion": string
}
```
---
## 字段说明
### allowExecute
- true所有指令均符合安全规范且所需信息已完整校验
- false存在任意不符合安全规范的指令或审核信息不足无法判断
### suggestion
- **通过**:填写“通过”或简要确认说明
- **不通过**
- 必须明确指出 **具体不通过原因**
- 若包含多条指令,需明确指出 **哪一条指令** 存在问题
- 表述需简洁、明确,不得模糊或推测
---
# 最终裁决规则(不可违背)
- 只要存在 **任意一条** 不符合安全规范的指令
**整体审核结果必须为不通过**
- ❌ 禁止输出以下非确定性结论:
- “部分通过”
- “建议执行”
- “可能有风险”
- “视情况而定”

View File

@@ -47,7 +47,7 @@ func (a *AccountLogin) ReqConfs() *req.Confs {
// @router /auth/accounts/login [post]
func (a *AccountLogin) Login(rc *req.Ctx) {
loginForm := req.BindJson[*form.LoginForm](rc)
loginForm := req.BindJson[form.LoginForm](rc)
ctx := rc.MetaCtx
accountLoginSecurity := config.GetAccountLoginSecurity()
@@ -96,7 +96,7 @@ type OtpVerifyInfo struct {
// OTP双因素校验
func (a *AccountLogin) OtpVerify(rc *req.Ctx) {
otpVerify := req.BindJson[*form.OtpVerfiy](rc)
otpVerify := req.BindJson[form.OtpVerfiy](rc)
ctx := rc.MetaCtx
tokenKey := fmt.Sprintf("otp:token:%s", otpVerify.OtpToken)

View File

@@ -13,6 +13,7 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/global"
"mayfly-go/pkg/gox"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/netx"
@@ -57,7 +58,9 @@ func LastLoginCheck(ctx context.Context, account *sysentity.Account, accountLogi
res["refresh_token"] = refreshToken
// 不进行otp二次校验则直接返回accessToken
// 保存登录消息
go saveLogin(ctx, account, loginIp)
gox.Go(func() {
saveLogin(ctx, account, loginIp)
})
}
// 赋值otp状态

View File

@@ -47,7 +47,7 @@ func (a *LdapLogin) GetLdapEnabled(rc *req.Ctx) {
// @router /auth/ldap/login [post]
func (a *LdapLogin) Login(rc *req.Ctx) {
loginForm := req.BindJson[*form.LoginForm](rc)
loginForm := req.BindJson[form.LoginForm](rc)
ctx := rc.MetaCtx
accountLoginSecurity := config.GetAccountLoginSecurity()
// 判断是否有开启登录验证码校验

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(new(oauth2AppImpl), ioc.WithComponentName("Oauth2App"))
ioc.Register(new(oauth2AppImpl))
}

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(newAuthAccountRepo(), ioc.WithComponentName("Oauth2AccountRepo"))
ioc.Register(newAuthAccountRepo())
}

View File

@@ -1,14 +1,14 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/auth/api"
"mayfly-go/internal/auth/application"
"mayfly-go/internal/auth/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()

View File

@@ -1,12 +1,12 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/common/api"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
api.InitIoc()
})
}

View File

@@ -4,59 +4,36 @@ import (
"context"
"mayfly-go/internal/db/application"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/jsonx"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/components/tool/utils"
)
func GetQueryTableInfo() tool.InvokableTool {
return &QueryTableInfo{}
type QueryTableInfoParam struct {
DbId uint64 `json:"dbId" jsonschema_description:"数据库ID"`
DbName string `json:"dbName" jsonschema_description:"数据库名称"`
TableName string `json:"tableName" jsonschema_description:"表名"`
}
type QueryTableInfo struct {
type QueryTableInfoOutput struct {
DDL string `json:"ddl" jsonschema_description:"表DDL"`
}
var _ tool.InvokableTool = (*QueryTableInfo)(nil)
func (q QueryTableInfo) Info(ctx context.Context) (*schema.ToolInfo, error) {
return &schema.ToolInfo{
Name: "QueryTableInfo",
Desc: "查询数据库表的详细信息,包括表结构、字段定义、索引等。当用户需要了解某个表的结构时使用此工具。",
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
"dbId": {
Type: schema.Number,
Desc: "数据库ID",
Required: true,
},
"dbName": {
Type: schema.String,
Desc: "数据库名称",
Required: true,
},
"tableName": {
Type: schema.String,
Desc: "表名",
Required: true,
},
}),
}, nil
}
func (q QueryTableInfo) InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error) {
logx.Debugf("开始查询数据库表信息: %s", argumentsInJSON)
m, err := jsonx.ToMap(argumentsInJSON)
if err != nil {
return "arguments json invalid", err
}
tableName := m.GetStr("tableName")
conn, err := application.GetDbApp().GetDbConn(ctx, uint64(m.GetInt64("dbId")), m.GetStr("dbName"))
if err != nil {
return "获取数据库连接失败", err
}
return conn.GetMetadata().GetTableDDL(tableName, false)
func GetQueryTableInfo() (tool.InvokableTool, error) {
return utils.InferTool("QueryTableInfo",
"当需要了解某个表结构时请调用此工具。使用它来查询数据库表的DDL信息包括表结构、字段定义、索引等。",
func(ctx context.Context, param *QueryTableInfoParam) (*QueryTableInfoOutput, error) {
conn, err := application.GetDbApp().GetDbConn(ctx, param.DbId, param.DbName)
if err != nil {
return nil, err
}
ddl, err := conn.GetMetadata().GetTableDDL(param.TableName, false)
if err != nil {
return nil, err
}
output := &QueryTableInfoOutput{DDL: ddl}
return output, nil
},
)
}

View File

@@ -1,7 +1,14 @@
package tools
import "mayfly-go/internal/ai/agent"
import (
"mayfly-go/internal/ai/agent"
"mayfly-go/pkg/logx"
)
func Init() {
agent.RegisterTool(agent.ToolTypeDb, GetQueryTableInfo())
if queryTableTool, err := GetQueryTableInfo(); err != nil {
logx.Errorf("agent tool - 获取QueryTableInfo工具失败: %v", err)
} else {
agent.RegisterTool(agent.ToolTypeDb, queryTableTool)
}
}

View File

@@ -74,7 +74,7 @@ func (d *Db) ReqConfs() *req.Confs {
// @router /api/dbs [get]
func (d *Db) Dbs(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbQuery](rc)
queryCond := req.BindQuery[entity.DbQuery](rc)
// 不存在可访问标签id即没有可操作数据
tags := d.tagApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
@@ -112,7 +112,7 @@ func (d *Db) Dbs(rc *req.Ctx) {
}
func (d *Db) Save(rc *req.Ctx) {
form, db := req.BindJsonAndCopyTo[*form.DbForm, *entity.Db](rc)
form, db := req.BindJsonAndCopyTo[form.DbForm, entity.Db](rc)
rc.ReqParam = form
biz.ErrIsNil(d.dbApp.SaveDb(rc.MetaCtx, db))
@@ -132,7 +132,7 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
/** 数据库操作相关、执行sql等 ***/
func (d *Db) ExecSql(rc *req.Ctx) {
form := req.BindJson[*form.DbSqlExecForm](rc)
form := req.BindJson[form.DbSqlExecForm](rc)
ctx, cancel := context.WithTimeout(rc.MetaCtx, time.Duration(config.GetDbms().SqlExecTl)*time.Second)
defer cancel()
@@ -339,7 +339,7 @@ func (d *Db) GetSchemas(rc *req.Ctx) {
}
func (d *Db) CopyTable(rc *req.Ctx) {
form, copy := req.BindJsonAndCopyTo[*form.DbCopyTableForm, *dbi.DbCopyTable](rc)
form, copy := req.BindJsonAndCopyTo[form.DbCopyTableForm, dbi.DbCopyTable](rc)
conn, err := d.dbApp.GetDbConn(rc.MetaCtx, form.Id, form.Db)
biz.ErrIsNilAppendErr(err, "copy table error: %s")

View File

@@ -50,21 +50,21 @@ func (d *DataSyncTask) ReqConfs() *req.Confs {
}
func (d *DataSyncTask) Tasks(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DataSyncTaskQuery](rc)
queryCond := req.BindQuery[entity.DataSyncTaskQuery](rc)
res, err := d.dataSyncTaskApp.GetPageList(queryCond)
biz.ErrIsNil(err)
rc.ResData = model.PageResultConv[*entity.DataSyncTask, *vo.DataSyncTaskListVO](res)
}
func (d *DataSyncTask) Logs(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DataSyncLogQuery](rc)
queryCond := req.BindQuery[entity.DataSyncLogQuery](rc)
res, err := d.dataSyncTaskApp.GetTaskLogList(queryCond)
biz.ErrIsNil(err)
rc.ResData = model.PageResultConv[*entity.DataSyncLog, *vo.DataSyncLogListVO](res)
}
func (d *DataSyncTask) SaveTask(rc *req.Ctx) {
form, task := req.BindJsonAndCopyTo[*form.DataSyncTaskForm, *entity.DataSyncTask](rc)
form, task := req.BindJsonAndCopyTo[form.DataSyncTaskForm, entity.DataSyncTask](rc)
// 解码base64 sql
sqlStr, err := utils.AesDecryptByLa(task.DataSql, rc.GetLoginAccount())
@@ -87,7 +87,7 @@ func (d *DataSyncTask) DeleteTask(rc *req.Ctx) {
}
func (d *DataSyncTask) ChangeStatus(rc *req.Ctx) {
form := req.BindJson[*form.DataSyncTaskStatusForm](rc)
form := req.BindJson[form.DataSyncTaskStatusForm](rc)
rc.ReqParam = form
task, err := d.dataSyncTaskApp.GetById(form.Id)

View File

@@ -55,7 +55,7 @@ func (d *Instance) ReqConfs() *req.Confs {
// Instances 获取数据库实例信息
// @router /api/instances [get]
func (d *Instance) Instances(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.InstanceQuery](rc)
queryCond := req.BindQuery[entity.InstanceQuery](rc)
tags := d.tagApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
TypePaths: collx.AsArray(tagentity.NewTypePaths(tagentity.TagTypeDbInstance, tagentity.TagTypeAuthCert)),
@@ -90,14 +90,14 @@ func (d *Instance) Instances(rc *req.Ctx) {
}
func (d *Instance) TestConn(rc *req.Ctx) {
form, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.DbInstance](rc)
form, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.DbInstance](rc)
biz.ErrIsNil(d.instanceApp.TestConn(rc.MetaCtx, instance, form.AuthCerts[0]))
}
// SaveInstance 保存数据库实例信息
// @router /api/instances [post]
func (d *Instance) SaveInstance(rc *req.Ctx) {
form, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.DbInstance](rc)
form, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.DbInstance](rc)
rc.ReqParam = form
id, err := d.instanceApp.SaveDbInstance(rc.MetaCtx, &dto.SaveDbInstance{
@@ -132,7 +132,7 @@ func (d *Instance) DeleteInstance(rc *req.Ctx) {
// 获取数据库实例的所有数据库名
func (d *Instance) GetDatabaseNames(rc *req.Ctx) {
form, instance := req.BindJsonAndCopyTo[*form.InstanceDbNamesForm, *entity.DbInstance](rc)
form, instance := req.BindJsonAndCopyTo[form.InstanceDbNamesForm, entity.DbInstance](rc)
res, err := d.instanceApp.GetDatabases(rc.MetaCtx, instance, form.AuthCert)
biz.ErrIsNil(err)
rc.ResData = res

View File

@@ -30,7 +30,7 @@ func (d *DbSql) ReqConfs() *req.Confs {
// @router /api/db/:dbId/sql [post]
func (d *DbSql) SaveSql(rc *req.Ctx) {
dbSqlForm := req.BindJson[*form.DbSqlSaveForm](rc)
dbSqlForm := req.BindJson[form.DbSqlSaveForm](rc)
rc.ReqParam = dbSqlForm
dbId := getDbId(rc)

View File

@@ -26,7 +26,7 @@ func (d *DbSqlExec) ReqConfs() *req.Confs {
}
func (d *DbSqlExec) DbSqlExecs(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbSqlExecQuery](rc)
queryCond := req.BindQuery[entity.DbSqlExecQuery](rc)
if statusStr := rc.Query("status"); statusStr != "" {
queryCond.Status = collx.ArrayMap[string, int8](strings.Split(statusStr, ","), func(val string) int8 {
return cast.ToInt8(val)

View File

@@ -61,7 +61,7 @@ func (d *DbTransferTask) ReqConfs() *req.Confs {
}
func (d *DbTransferTask) Tasks(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbTransferTaskQuery](rc)
queryCond := req.BindQuery[entity.DbTransferTaskQuery](rc)
res, err := d.dbTransferTaskApp.GetPageList(queryCond)
biz.ErrIsNil(err)
@@ -78,7 +78,7 @@ func (d *DbTransferTask) Tasks(rc *req.Ctx) {
}
func (d *DbTransferTask) SaveTask(rc *req.Ctx) {
reqForm, task := req.BindJsonAndCopyTo[*form.DbTransferTaskForm, *entity.DbTransferTask](rc)
reqForm, task := req.BindJsonAndCopyTo[form.DbTransferTaskForm, entity.DbTransferTask](rc)
rc.ReqParam = reqForm
biz.ErrIsNil(d.dbTransferTaskApp.Save(rc.MetaCtx, task))
@@ -94,7 +94,7 @@ func (d *DbTransferTask) DeleteTask(rc *req.Ctx) {
}
func (d *DbTransferTask) ChangeStatus(rc *req.Ctx) {
form := req.BindJson[*form.DbTransferTaskStatusForm](rc)
form := req.BindJson[form.DbTransferTaskStatusForm](rc)
rc.ReqParam = form
task, err := d.dbTransferTaskApp.GetById(form.Id)
@@ -117,7 +117,7 @@ func (d *DbTransferTask) Stop(rc *req.Ctx) {
}
func (d *DbTransferTask) Files(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.DbTransferFileQuery](rc)
queryCond := req.BindQuery[entity.DbTransferFileQuery](rc)
res, err := d.dbTransferFileApp.GetPageList(queryCond)
biz.ErrIsNil(err)
@@ -137,7 +137,7 @@ func (d *DbTransferTask) FileDel(rc *req.Ctx) {
}
func (d *DbTransferTask) FileRun(rc *req.Ctx) {
fm := req.BindJson[*form.DbTransferFileRunForm](rc)
fm := req.BindJson[form.DbTransferFileRunForm](rc)
rc.ReqParam = fm
@@ -151,13 +151,13 @@ func (d *DbTransferTask) FileRun(rc *req.Ctx) {
filename, reader, err := d.fileApp.GetReader(context.TODO(), tFile.FileKey)
biz.ErrIsNil(err)
go func() {
defer gox.RecoverPanic()
biz.ErrIsNil(d.dbSqlExecApp.ExecReader(rc.MetaCtx, &dto.SqlReaderExec{
gox.GoCtx(rc.MetaCtx, func(ctx context.Context) {
biz.ErrIsNil(d.dbSqlExecApp.ExecReader(ctx, &dto.SqlReaderExec{
Reader: reader,
Filename: filename,
DbConn: targetDbConn,
ClientId: fm.ClientId,
}))
}()
})
}

View File

@@ -4,7 +4,6 @@ type DataSyncTaskForm struct {
Id uint64 `json:"id"`
TaskName string `binding:"required" json:"taskName"`
TaskCron string `binding:"required" json:"taskCron"`
TaskKey string `json:"taskKey"`
Status int `binding:"required" json:"status"`
SrcDbId int64 `binding:"required" json:"srcDbId"`

View File

@@ -6,13 +6,13 @@ import (
)
func InitIoc() {
ioc.Register(new(instanceAppImpl), ioc.WithComponentName("DbInstanceApp"))
ioc.Register(new(dbAppImpl), ioc.WithComponentName("DbApp"))
ioc.Register(new(dbSqlExecAppImpl), ioc.WithComponentName("DbSqlExecApp"))
ioc.Register(new(dbSqlAppImpl), ioc.WithComponentName("DbSqlApp"))
ioc.Register(new(dataSyncAppImpl), ioc.WithComponentName("DbDataSyncTaskApp"))
ioc.Register(new(dbTransferAppImpl), ioc.WithComponentName("DbTransferTaskApp"))
ioc.Register(new(dbTransferFileAppImpl), ioc.WithComponentName("DbTransferFileApp"))
ioc.Register(new(instanceAppImpl))
ioc.Register(new(dbAppImpl))
ioc.Register(new(dbSqlExecAppImpl))
ioc.Register(new(dbSqlAppImpl))
ioc.Register(new(dataSyncAppImpl))
ioc.Register(new(dbTransferAppImpl))
ioc.Register(new(dbTransferFileAppImpl))
}
func Init() {

View File

@@ -71,7 +71,13 @@ func (app *dataSyncAppImpl) Save(ctx context.Context, taskEntity *entity.DataSyn
taskEntity.TaskKey = uuid.New().String()
err = app.Insert(ctx, taskEntity)
} else {
taskEntity.TaskKey = ""
if taskEntity.TaskKey == "" {
task, err := app.GetById(taskEntity.Id)
if err != nil {
return errorx.NewBiz("db sync task not found")
}
taskEntity.TaskKey = task.TaskKey
}
err = app.UpdateById(ctx, taskEntity)
}
if err != nil {
@@ -129,7 +135,7 @@ func (app *dataSyncAppImpl) Run(ctx context.Context, id uint64) error {
}
defer app.endRunning(task, syncLog)
defer gox.RecoverPanic(func(err error) {
defer gox.Recover(func(err error) {
syncLog.ErrText = i18n.T(imsg.DataSyncFailMsg, "msg", err.Error())
logx.ErrorContext(ctx, syncLog.ErrText)
syncLog.Status = entity.DataSyncTaskStateFail

View File

@@ -298,7 +298,7 @@ func (d *dbSqlExecAppImpl) FlowBizHandle(ctx context.Context, bizHandleParam *fl
return nil, nil
}
execSqlBizForm, err := jsonx.To[*FlowDbExecSqlBizForm](procinst.BizForm)
execSqlBizForm, err := jsonx.To[FlowDbExecSqlBizForm](procinst.BizForm)
if err != nil {
return nil, errorx.NewBizf("failed to parse the business form information: %s", err.Error())
}
@@ -603,7 +603,7 @@ func (d *dbSqlExecAppImpl) doExec(ctx context.Context, dbConn *dbi.DbConn, sql s
return &dto.DbSqlExecRes{
Columns: []*dbi.QueryColumn{
{Name: "rowsAffected", Type: "number"},
{Name: "rowsAffected", Key:"rowsAffected", Type: "number"},
},
Res: res,
Sql: sql,

View File

@@ -77,11 +77,19 @@ func (app *dbTransferAppImpl) Save(ctx context.Context, taskEntity *entity.DbTra
taskEntity.TaskKey = uuid.New().String()
err = app.Insert(ctx, taskEntity)
} else {
if taskEntity.TaskKey == "" {
task, err := app.GetById(taskEntity.Id)
if err != nil {
return errorx.NewBiz("db transfer task not found")
}
taskEntity.TaskKey = task.TaskKey
}
err = app.UpdateById(ctx, taskEntity)
}
if err != nil {
return err
}
app.addCronJob(ctx, taskEntity)
return nil
}
@@ -143,8 +151,7 @@ func (app *dbTransferAppImpl) Run(ctx context.Context, taskId uint64) (uint64, e
// 标记该任务开始执行
app.MarkRunning(taskId)
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
// 获取源库连接、目标库连接判断连接可用性否则记录日志xx连接不可用
// 获取源库表信息
srcConn, err := app.dbApp.GetDbConn(ctx, uint64(task.SrcDbId), task.SrcDbName)
@@ -180,7 +187,7 @@ func (app *dbTransferAppImpl) Run(ctx context.Context, taskId uint64) (uint64, e
app.EndTransfer(ctx, logId, taskId, "error in transfer mode, only migrating to files or databases is currently supported", err, nil)
return
}
}()
})
return logId, nil
}
@@ -207,7 +214,7 @@ func (app *dbTransferAppImpl) transfer2Db(ctx context.Context, logId uint64, tas
for _, tables := range tableGroups {
errGroup.Go(func() error {
defer gox.RecoverPanic()
defer gox.Recover()
if !app.IsRunning(taskId) {
return errorx.NewBiz("transfer stopped")
@@ -217,8 +224,7 @@ func (app *dbTransferAppImpl) transfer2Db(ctx context.Context, logId uint64, tas
pr, pw := io.Pipe()
defer pr.Close()
go func() {
defer gox.RecoverPanic()
gox.Go(func () {
defer pw.Close()
err := app.dbApp.DumpDb(ctx, &dto.DumpDb{
LogId: logId,
@@ -254,7 +260,7 @@ func (app *dbTransferAppImpl) transfer2Db(ctx context.Context, logId uint64, tas
pr.CloseWithError(err)
return
}
}()
})
tx, _ := targetConn.Begin()
err = sqlparser.SQLSplit(pr, ';', func(stmt string) error {
@@ -317,7 +323,7 @@ func (app *dbTransferAppImpl) transfer2File(ctx context.Context, logId uint64, t
defer closeFunc(&err)
defer app.MarkStop(taskId)
defer app.logApp.Flush(logId, true)
defer gox.RecoverPanic(func(e error) {
defer gox.Recover(func(e error) {
err = e
app.EndTransfer(ctx, logId, taskId, "transfer to file panic", e, nil)
tFile.Status = entity.DbTransferFileStatusFail
@@ -385,7 +391,7 @@ func (app *dbTransferAppImpl) Stop(ctx context.Context, taskId uint64) error {
func (d *dbTransferAppImpl) TimerDeleteTransferFile() {
logx.Debug("start deleting transfer files periodically...")
scheduler.AddFun("@every 100m", func() {
defer gox.RecoverPanic()
defer gox.Recover()
dts, err := d.ListByCond(model.NewCond().Eq("mode", entity.DbTransferTaskModeFile).Ge("file_save_days", 1))
if err != nil {
logx.Errorf("the task to periodically get database transfer to file failed: %s", err.Error())
@@ -413,12 +419,6 @@ func (app *dbTransferAppImpl) addCronJob(ctx context.Context, taskEntity *entity
// 根据状态添加新的任务
if taskEntity.Status == entity.DbTransferTaskStatusEnable && taskEntity.CronAble == entity.DbTransferTaskCronAbleEnable {
if key == "" {
taskEntity.TaskKey = uuid.New().String()
key = taskEntity.TaskKey
_ = app.UpdateById(ctx, taskEntity)
}
taskId := taskEntity.Id
if err := scheduler.AddFunByKey(key, taskEntity.Cron, func() {
logx.Infof("start the transfer task: %d", taskId)

View File

@@ -5,7 +5,9 @@ import (
"database/sql"
"errors"
"fmt"
"strconv"
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
)
@@ -26,15 +28,12 @@ type DbConn struct {
// 关闭连接
func (d *DbConn) Close() error {
if d.db != nil {
defer mcm.CloseSshTunnel(d.Info)
if err := d.db.Close(); err != nil {
logx.Errorf("关闭数据库实例[%s]连接失败: %v", d.Id, err)
return err
}
logx.Debugf("dbm - conn close success, connId: %s", d.Id)
// TODO 关闭实例隧道会影响其他正在使用的连接,所以暂时不关闭
// if d.Info.useSshTunnel {
// mcm.CloseSshTunnelMachine(uint64(d.Info.SshTunnelMachineId), fmt.Sprintf("db:%d", d.Info.Id))
// }
d.db = nil
}
@@ -42,14 +41,10 @@ func (d *DbConn) Close() error {
}
func (d *DbConn) Ping() error {
if d.db == nil {
return fmt.Errorf("db is nil")
}
stats := d.db.Stats()
logx.Debugf("[%s] db stats -> open: %d, idle: %d, inUse: %d, maxOpen: %d", d.Info.Name, stats.OpenConnections, stats.Idle, stats.InUse, stats.MaxOpenConnections)
if stats.OpenConnections == 0 {
logx.Infof("[%s] db stats: no open connections", d.Info.Name)
logx.Infof("[%s]-[%s] db stats: no open connections", d.Info.Name, d.Info.Database)
}
return d.db.Ping()
@@ -58,6 +53,7 @@ func (d *DbConn) Ping() error {
// 执行数据库查询返回的列信息
type QueryColumn struct {
Name string `json:"name"` // 列名
Key string `json:"key"` // 列唯一标识
Type string `json:"type"` // 数据类型
DbDataType *DbDataType `json:"-"`
@@ -67,6 +63,7 @@ type QueryColumn struct {
func NewQueryColumn(colName string, columnType *DbDataType) *QueryColumn {
return &QueryColumn{
Name: colName,
Key: colName,
Type: columnType.DataType.Name,
DbDataType: columnType,
valuer: columnType.DataType.Valuer(),
@@ -245,7 +242,12 @@ func (d *DbConn) walkQueryRows(ctx context.Context, selectSql string, walkFn Wal
rowData := make(map[string]any, lenCols)
// 把values中的数据复制到row中
for i := range scans {
rowData[cols[i].Name] = cols[i].value()
colname := cols[i].Name
if _, e := rowData[colname]; e {
colname = colname + strconv.Itoa(i)
cols[i].Key = colname
}
rowData[colname] = cols[i].value()
}
if err = walkFn(rowData, cols); err != nil {
logx.ErrorfContext(ctx, "[%s] cursor traversal query result set error, exit traversal: %s", selectSql, err.Error())

View File

@@ -43,11 +43,24 @@ type DbInfo struct {
CodePath []string
SshTunnelMachineId int
useSshTunnel bool // 是否使用系统自己实现的ssh隧道连接,而非库自带的
RemoteAddr string `json:"-"` // ssh隧道远程地址格式 ip:port
Meta Meta
}
var _ (mcm.SshTunnelAble) = (*DbInfo)(nil)
func (di *DbInfo) GetSshTunnelMachineId() int64 {
return int64(di.SshTunnelMachineId)
}
func (di *DbInfo) GetRemoteAddr() string {
if di.RemoteAddr != "" {
return di.RemoteAddr
}
return fmt.Sprintf("%s:%d", di.Host, di.Port)
}
// 获取记录日志的描述
func (di *DbInfo) GetLogDesc() string {
return fmt.Sprintf("DB[id=%d, tag=%s, name=%s, ip=%s:%d, database=%s]", di.Id, di.CodePath, di.Name, di.Host, di.Port, di.Database)
@@ -68,6 +81,9 @@ func (di *DbInfo) Conn(ctx context.Context, meta Meta) (*DbConn, error) {
di.Database = database
}
if err := di.IfUseSshTunnelChangeIpPort(ctx); err != nil {
return nil, err
}
conn, err := meta.GetSqlDb(ctx, di)
if err != nil {
logx.Errorf("db connection failed: %s:%d/%s, err:%s", di.Host, di.Port, database, err.Error())
@@ -99,17 +115,17 @@ func (di *DbInfo) Conn(ctx context.Context, meta Meta) (*DbConn, error) {
func (di *DbInfo) IfUseSshTunnelChangeIpPort(ctx context.Context) error {
// 开启ssh隧道
if di.SshTunnelMachineId > 0 {
sshTunnelMachine, err := GetSshTunnel(ctx, di.SshTunnelMachineId)
di.RemoteAddr = di.GetRemoteAddr()
sshTunnelMachine, err := machineapp.GetMachineApp().GetSshTunnelMachine(ctx, int(di.SshTunnelMachineId))
if err != nil {
return err
}
exposedIp, exposedPort, err := sshTunnelMachine.OpenSshTunnel(fmt.Sprintf("db:%d", di.Id), di.Host, di.Port)
exposedIp, exposedPort, err := sshTunnelMachine.OpenSshTunnel(di)
if err != nil {
return err
}
di.Host = exposedIp
di.Port = exposedPort
di.useSshTunnel = true
}
return nil
}

View File

@@ -9,7 +9,6 @@ import (
_ "mayfly-go/internal/db/dbm/oracle"
_ "mayfly-go/internal/db/dbm/postgres"
_ "mayfly-go/internal/db/dbm/sqlite"
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/pool"
)
@@ -18,22 +17,6 @@ var (
poolGroup = pool.NewPoolGroup[*dbi.DbConn]()
)
func init() {
mcm.AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
items := poolGroup.AllPool()
for _, v := range items {
conn, err := v.Get(context.Background(), pool.WithGetNoUpdateLastActive(), pool.WithGetNoNewConn())
if err != nil {
continue // 获取连接失败,跳过
}
if conn.Info.SshTunnelMachineId == machineId {
return true
}
}
return false
})
}
// GetDbConn 从连接池中获取连接信息
func GetDbConn(ctx context.Context, dbId uint64, database string, getDbInfo func() (*dbi.DbInfo, error)) (*dbi.DbConn, error) {
connId := dbi.GetDbConnId(dbId, database)

View File

@@ -7,8 +7,6 @@ import (
"mayfly-go/pkg/utils/stringx"
"strings"
"time"
_ "gitee.com/chunanyong/dm"
)
type DMDialect struct {
@@ -39,8 +37,7 @@ func (dd *DMDialect) CopyTable(copy *dbi.DbCopyTable) error {
// 复制数据
if copy.CopyData {
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
// 设置允许填充自增列之后,显示指定列名可以插入自增列\
identityInsert := fmt.Sprintf("set identity_insert \"%s\" on", newTableName)
// 获取列名
@@ -52,8 +49,7 @@ func (dd *DMDialect) CopyTable(copy *dbi.DbCopyTable) error {
columnStr := strings.Join(columnArr, ",")
// 插入新数据并显示指定列
_, _ = dd.dc.Exec(fmt.Sprintf("%s insert into \"%s\" (%s) select %s from \"%s\"", identityInsert, newTableName, columnStr, columnStr, tableName))
}()
})
}
return err
}

View File

@@ -8,6 +8,8 @@ import (
"mayfly-go/pkg/utils/collx"
"net/url"
"strings"
_ "gitee.com/chunanyong/dm"
)
func init() {
@@ -36,11 +38,6 @@ func (dm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
dbParam += "&" + d.Params
}
err := d.IfUseSshTunnelChangeIpPort(ctx)
if err != nil {
return nil, err
}
dsn := fmt.Sprintf("dm://%s:%s@%s:%d%s", d.Username, url.PathEscape(d.Password), d.Host, d.Port, dbParam)
return sql.Open(driverName, dsn)
}

View File

@@ -43,8 +43,7 @@ func (md *MssqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
}
// 复制数据
if copy.CopyData {
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
// 查询所有的列
columns, err := msMetadata.GetColumns(copy.TableName)
if err != nil {
@@ -73,7 +72,7 @@ func (md *MssqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
if err != nil {
logx.Warnf("复制表[%s]数据失败: %s", copy.TableName, err.Error())
}
}()
})
}
return err

View File

@@ -25,10 +25,6 @@ type Meta struct {
}
func (mm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
err := d.IfUseSshTunnelChangeIpPort(ctx)
if err != nil {
return nil, err
}
query := url.Values{}
// The application name (default is go-mssqldb)
query.Add("app name", "mayfly")

View File

@@ -43,10 +43,9 @@ func (md *MysqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
// 复制数据
if copy.CopyData {
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
_, _ = md.dc.Exec(fmt.Sprintf("insert into %s select * from %s", newTableName, tableName))
}()
})
}
return err
}

View File

@@ -6,9 +6,8 @@ import (
"fmt"
"mayfly-go/internal/db/dbm/dbi"
"mayfly-go/pkg/utils/collx"
"net"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
)
func init() {
@@ -26,23 +25,14 @@ type Meta struct {
}
func (mm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
// SSH Conect
if d.SshTunnelMachineId > 0 {
sshTunnelMachine, err := dbi.GetSshTunnel(ctx, d.SshTunnelMachineId)
if err != nil {
return nil, err
}
mysql.RegisterDialContext(d.Network, func(ctx context.Context, addr string) (net.Conn, error) {
return sshTunnelMachine.GetDialConn("tcp", addr)
})
}
d.Network = "tcp"
// 设置dataSourceName -> 更多参数参考https://github.com/go-sql-driver/mysql#dsn-data-source-name
dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s?parseTime=true&timeout=8s", d.Username, d.Password, d.Network, d.Host, d.Port, d.Database)
if d.Params != "" {
dsn = fmt.Sprintf("%s&%s", dsn, d.Params)
}
const driverName = "mysql"
return sql.Open(driverName, dsn)
return sql.Open("mysql", dsn)
}
func (mm *Meta) GetDialect(conn *dbi.DbConn) dbi.Dialect {

View File

@@ -25,11 +25,6 @@ type Meta struct {
}
func (om *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
err := d.IfUseSshTunnelChangeIpPort(ctx)
if err != nil {
return nil, err
}
// 参数参考 https://github.com/sijms/go-ora?tab=readme-ov-file#other-connection-options
urlOptions := make(map[string]string)

View File

@@ -27,10 +27,9 @@ func (pd *PgsqlDialect) CopyTable(copy *dbi.DbCopyTable) error {
// 复制数据
if copy.CopyData {
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
_, _ = pd.dc.Exec(fmt.Sprintf("insert into %s select * from %s", newTableName, tableName))
}()
})
}
// 查询旧表的自增字段名 重新设置新表的序列序列器

View File

@@ -3,16 +3,12 @@ package postgres
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"mayfly-go/internal/db/dbm/dbi"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/netx"
"net"
"strings"
"time"
pq "gitee.com/liuzongyang/libpq"
_ "gitee.com/liuzongyang/libpq"
)
func init() {
@@ -39,17 +35,6 @@ type Meta struct {
}
func (pm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
driverName := "postgres"
// SSH Conect
if d.SshTunnelMachineId > 0 {
// 如果使用了隧道,则使用`postgres:ssh:隧道机器id`注册名
driverName = fmt.Sprintf("postgres:ssh:%d", d.SshTunnelMachineId)
if !collx.ArrayContains(sql.Drivers(), driverName) {
sql.Register(driverName, &PqSqlDialer{sshTunnelMachineId: d.SshTunnelMachineId})
}
sql.Drivers()
}
db := d.Database
var dbParam string
existSchema := false
@@ -86,7 +71,7 @@ func (pm *Meta) GetSqlDb(ctx context.Context, d *dbi.DbInfo) (*sql.DB, error) {
dsn = fmt.Sprintf("%s %s", dsn, pm.Param)
}
return sql.Open(driverName, dsn)
return sql.Open("postgres", dsn)
}
func (pm *Meta) GetDialect(conn *dbi.DbConn) dbi.Dialect {
@@ -110,30 +95,3 @@ func (pm *Meta) GetDbDataTypes() []*dbi.DbDataType {
func (pm *Meta) GetCommonTypeConverter() dbi.CommonTypeConverter {
return &commonTypeConverter{}
}
// pgsql dialer
type PqSqlDialer struct {
sshTunnelMachineId int
}
func (pd *PqSqlDialer) Open(name string) (driver.Conn, error) {
return pq.DialOpen(pd, name)
}
func (pd *PqSqlDialer) Dial(network, address string) (net.Conn, error) {
// todo context.Background可能存在问题
sshTunnel, err := dbi.GetSshTunnel(context.Background(), pd.sshTunnelMachineId)
if err != nil {
return nil, err
}
if sshConn, err := sshTunnel.GetDialConn("tcp", address); err == nil {
// 将ssh conn包装否则会返回错误: ssh: tcpChan: deadline not supported
return &netx.WrapSshConn{Conn: sshConn}, nil
} else {
return nil, err
}
}
func (pd *PqSqlDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
return pd.Dial(network, address)
}

View File

@@ -37,11 +37,10 @@ func (sd *SqliteDialect) CopyTable(copy *dbi.DbCopyTable) error {
// 使用异步线程插入数据
if copy.CopyData {
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
// 执行插入语句
_, _ = sd.dc.Exec(fmt.Sprintf("INSERT INTO \"%s\" SELECT * FROM \"%s\"", newTableName, tableName))
}()
})
}
return err

View File

@@ -5,12 +5,12 @@ import (
)
func InitIoc() {
ioc.Register(NewInstanceRepo(), ioc.WithComponentName("DbInstanceRepo"))
ioc.Register(newDbRepo(), ioc.WithComponentName("DbRepo"))
ioc.Register(newDbSqlRepo(), ioc.WithComponentName("DbSqlRepo"))
ioc.Register(newDbSqlExecRepo(), ioc.WithComponentName("DbSqlExecRepo"))
ioc.Register(newDataSyncTaskRepo(), ioc.WithComponentName("DbDataSyncTaskRepo"))
ioc.Register(newDataSyncLogRepo(), ioc.WithComponentName("DbDataSyncLogRepo"))
ioc.Register(newDbTransferTaskRepo(), ioc.WithComponentName("DbTransferTaskRepo"))
ioc.Register(newDbTransferFileRepo(), ioc.WithComponentName("DbTransferFileRepo"))
ioc.Register(NewInstanceRepo())
ioc.Register(newDbRepo())
ioc.Register(newDbSqlRepo())
ioc.Register(newDbSqlExecRepo())
ioc.Register(newDataSyncTaskRepo())
ioc.Register(newDataSyncLogRepo())
ioc.Register(newDbTransferTaskRepo())
ioc.Register(newDbTransferFileRepo())
}

View File

@@ -1,22 +1,22 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/db/ai/tools"
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
"mayfly-go/internal/db/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
})
initialize.AddInitFunc(application.Init)
initialize.AddTerminateFunc(Terminate)
starter.AddInitFunc(application.Init)
starter.AddTerminateFunc(Terminate)
// 注册AI数据库工具
tools.Init()
}

View File

@@ -98,10 +98,9 @@ func (d *Container) GetContainersStats(rc *req.Ctx) {
var mu sync.Mutex
allStats := make([]vo.ContainerStats, 0)
for _, c := range cs {
go func(item container.Summary) {
defer gox.RecoverPanic()
gox.Go(func() {
defer wg.Done()
if item.State != "running" {
if c.State != "running" {
return
}
@@ -127,7 +126,7 @@ func (d *Container) GetContainersStats(rc *req.Ctx) {
mu.Lock()
allStats = append(allStats, cs)
mu.Unlock()
}(c)
})
}
wg.Wait()
@@ -224,8 +223,7 @@ func (d *Container) ContainerLogs(rc *req.Ctx) {
biz.ErrIsNil(err)
defer logs.Close()
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
for {
select {
case <-ctx.Done():
@@ -239,7 +237,7 @@ func (d *Container) ContainerLogs(rc *req.Ctx) {
}
}
}
}()
})
buf := make([]byte, 1024)
for {

View File

@@ -35,7 +35,7 @@ func (cc *ContainerConf) ReqConfs() *req.Confs {
}
func (cc *ContainerConf) GetContainerPage(rc *req.Ctx) {
condition := req.BindQuery[*entity.ContainerQuery](rc)
condition := req.BindQuery[entity.ContainerQuery](rc)
tags := cc.tagTreeApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
TypePaths: collx.AsArray(tagentity.NewTypePaths(tagentity.TagTypeContainer)),
@@ -68,7 +68,7 @@ func (cc *ContainerConf) GetContainerPage(rc *req.Ctx) {
}
func (c *ContainerConf) Save(rc *req.Ctx) {
machineForm, container := req.BindJsonAndCopyTo[*form.ContainerSave, *entity.Container](rc)
machineForm, container := req.BindJsonAndCopyTo[form.ContainerSave, entity.Container](rc)
rc.ReqParam = machineForm
biz.ErrIsNil(c.containerApp.SaveContainer(rc.MetaCtx, &dto.SaveContainer{

View File

@@ -5,7 +5,7 @@ import (
)
func InitIoc() {
ioc.Register(new(containerAppImpl), ioc.WithComponentName("ContainerApp"))
ioc.Register(new(containerAppImpl))
}
func GetContainerApp() Container {

View File

@@ -150,8 +150,7 @@ func (c Client) ContainerAttach(containerID string, wsConn *websocket.Conn, rows
wsConn.WriteMessage(websocket.TextMessage, []byte("\033[2J\033[3J\033[1;1H")) // 清屏
// 转发容器输出到前端
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
buf := make([]byte, 1024)
for {
select {
@@ -171,7 +170,7 @@ func (c Client) ContainerAttach(containerID string, wsConn *websocket.Conn, rows
wsConn.WriteMessage(websocket.TextMessage, buf[:n])
}
}
}()
})
for {
select {

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(newContainerRepo(), ioc.WithComponentName("ContainerRepo"))
ioc.Register(newContainerRepo())
}

View File

@@ -1,14 +1,14 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/docker/api"
"mayfly-go/internal/docker/application"
"mayfly-go/internal/docker/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()

View File

@@ -51,7 +51,7 @@ func (d *Instance) ReqConfs() *req.Confs {
}
func (d *Instance) Instances(rc *req.Ctx) {
queryCond := req.BindQuery[*entity.InstanceQuery](rc)
queryCond := req.BindQuery[entity.InstanceQuery](rc)
// 只查询实例,兼容没有录入密码的实例
instTags := d.tagApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
@@ -92,7 +92,7 @@ func (d *Instance) Instances(rc *req.Ctx) {
}
func (d *Instance) TestConn(rc *req.Ctx) {
fm, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.EsInstance](rc)
fm, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.EsInstance](rc)
var ac *tagentity.ResourceAuthCert
if len(fm.AuthCerts) > 0 {
@@ -104,7 +104,7 @@ func (d *Instance) TestConn(rc *req.Ctx) {
rc.ResData = res
}
func (d *Instance) SaveInstance(rc *req.Ctx) {
fm, instance := req.BindJsonAndCopyTo[*form.InstanceForm, *entity.EsInstance](rc)
fm, instance := req.BindJsonAndCopyTo[form.InstanceForm, entity.EsInstance](rc)
rc.ReqParam = fm
id, err := d.inst.SaveInst(rc.MetaCtx, &dto.SaveEsInstance{

View File

@@ -6,7 +6,7 @@ import (
)
func InitIoc() {
ioc.Register(new(instanceAppImpl), ioc.WithComponentName("EsInstanceApp"))
ioc.Register(new(instanceAppImpl))
}
func Init() {

View File

@@ -8,7 +8,6 @@ import (
"mayfly-go/internal/es/domain/repository"
"mayfly-go/internal/es/esm/esi"
"mayfly-go/internal/es/imsg"
"mayfly-go/internal/machine/mcm"
"mayfly-go/internal/pkg/consts"
tagapp "mayfly-go/internal/tag/application"
tagdto "mayfly-go/internal/tag/application/dto"
@@ -41,21 +40,6 @@ var _ Instance = &instanceAppImpl{}
var poolGroup = pool.NewPoolGroup[*esi.EsConn]()
func init() {
mcm.AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
items := poolGroup.AllPool()
for _, v := range items {
conn, err := v.Get(context.Background(), pool.WithGetNoUpdateLastActive(), pool.WithGetNoNewConn())
if err != nil {
continue // 获取连接失败,跳过
}
if conn.Info.SshTunnelMachineId == machineId {
return true
}
}
return false
})
}
type instanceAppImpl struct {
base.AppImpl[*entity.EsInstance, repository.EsInstance]

View File

@@ -20,10 +20,7 @@ type EsConn struct {
/******************* pool.Conn impl *******************/
func (d *EsConn) Close() error {
// 如果是使用了ssh隧道转发则需要手动将其关闭
if d.Info.useSshTunnel {
mcm.CloseSshTunnelMachine(uint64(d.Info.SshTunnelMachineId), fmt.Sprintf("es:%d", d.Id))
}
mcm.CloseSshTunnel(d.Info)
return nil
}

View File

@@ -35,13 +35,26 @@ type EsInfo struct {
CodePath []string
SshTunnelMachineId int
useSshTunnel bool // 是否使用系统自己实现的ssh隧道连接,而非库自带的
RemoteAddr string `json:"-"` // ssh隧道远程地址格式 ip:port
OriginUrl string // 原始url
baseUrl string // 发起http请求的基本url
authorization string // 发起http请求携带的认证信息
}
var _ (mcm.SshTunnelAble) = (*EsInfo)(nil)
func (di *EsInfo) GetSshTunnelMachineId() int64 {
return int64(di.SshTunnelMachineId)
}
func (di *EsInfo) GetRemoteAddr() string {
if di.RemoteAddr != "" {
return di.RemoteAddr
}
return fmt.Sprintf("%s:%d", di.Host, di.Port)
}
// 获取记录日志的描述
func (di *EsInfo) GetLogDesc() string {
return fmt.Sprintf("ES[id=%d, tag=%s, name=%s, ip=%s:%d]", di.InstanceId, di.CodePath, di.Name, di.Host, di.Port)
@@ -132,17 +145,17 @@ func (di *EsInfo) IfUseSshTunnelChangeIpPort(ctx context.Context) error {
// 开启ssh隧道
if di.SshTunnelMachineId > 0 {
di.RemoteAddr = di.GetRemoteAddr()
stm, err := GetSshTunnel(ctx, di.SshTunnelMachineId)
if err != nil {
return err
}
exposedIp, exposedPort, err := stm.OpenSshTunnel(fmt.Sprintf("es:%d", di.InstanceId), di.Host, di.Port)
exposedIp, exposedPort, err := stm.OpenSshTunnel(di)
if err != nil {
return err
}
di.Host = exposedIp
di.Port = exposedPort
di.useSshTunnel = true
di.baseUrl = fmt.Sprintf("%s://%s:%d", di.Protocol, exposedIp, exposedPort)
} else {
di.baseUrl = fmt.Sprintf("%s://%s:%d", di.Protocol, di.Host, di.Port)

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(NewInstanceRepo(), ioc.WithComponentName("EsInstanceRepo"))
ioc.Register(NewInstanceRepo())
}

View File

@@ -1,18 +1,18 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/es/api"
"mayfly-go/internal/es/application"
"mayfly-go/internal/es/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
})
initialize.AddInitFunc(application.Init)
starter.AddInitFunc(application.Init)
}

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(new(fileAppImpl), ioc.WithComponentName("FileApp"))
ioc.Register(new(fileAppImpl))
}

View File

@@ -5,5 +5,5 @@ import (
)
func InitIoc() {
ioc.Register(newFileRepo(), ioc.WithComponentName("FileRepo"))
ioc.Register(newFileRepo())
}

View File

@@ -1,14 +1,14 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/file/api"
"mayfly-go/internal/file/application"
"mayfly-go/internal/file/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()

View File

@@ -48,7 +48,7 @@ func (p *Procdef) ReqConfs() *req.Confs {
}
func (p *Procdef) GetProcdefPage(rc *req.Ctx) {
cond, page := req.BindQueryAndPage[*entity.Procdef](rc)
cond, page := req.BindQueryAndPage[entity.Procdef](rc)
res, err := p.procdefApp.GetPageList(cond, page)
biz.ErrIsNil(err)
@@ -87,7 +87,7 @@ func (p *Procdef) GetProcdef(rc *req.Ctx) {
}
func (a *Procdef) Save(rc *req.Ctx) {
form, procdef := req.BindJsonAndCopyTo[*form.Procdef, *entity.Procdef](rc)
form, procdef := req.BindJsonAndCopyTo[form.Procdef, entity.Procdef](rc)
rc.ReqParam = form
biz.ErrIsNil(a.procdefApp.SaveProcdef(rc.MetaCtx, &dto.SaveProcdef{
Procdef: procdef,
@@ -97,7 +97,7 @@ func (a *Procdef) Save(rc *req.Ctx) {
}
func (a *Procdef) SaveFlowDef(rc *req.Ctx) {
form := req.BindJson[*form.ProcdefFlow](rc)
form := req.BindJson[form.ProcdefFlow](rc)
rc.ReqParam = form
biz.ErrIsNil(a.procdefApp.SaveFlowDef(rc.MetaCtx, &dto.SaveFlowDef{

View File

@@ -35,7 +35,7 @@ func (p *Procinst) ReqConfs() *req.Confs {
}
func (p *Procinst) GetProcinstPage(rc *req.Ctx) {
cond := req.BindQuery[*entity.ProcinstQuery](rc)
cond := req.BindQuery[entity.ProcinstQuery](rc)
// 非管理员只能获取自己申请的流程
if laId := rc.GetLoginAccount().Id; laId != consts.AdminId {
cond.CreatorId = laId
@@ -47,7 +47,7 @@ func (p *Procinst) GetProcinstPage(rc *req.Ctx) {
}
func (p *Procinst) ProcinstStart(rc *req.Ctx) {
startForm := req.BindJson[*form.ProcinstStart](rc)
startForm := req.BindJson[form.ProcinstStart](rc)
_, err := p.procinstApp.StartProc(rc.MetaCtx, startForm.ProcdefId, &dto.StarProc{
BizType: startForm.BizType,
BizKey: startForm.BizKey,

View File

@@ -41,7 +41,7 @@ func (p *ProcinstTask) ReqConfs() *req.Confs {
}
func (p *ProcinstTask) GetTasks(rc *req.Ctx) {
instTaskQuery := req.BindQuery[*entity.ProcinstTaskQuery](rc)
instTaskQuery := req.BindQuery[entity.ProcinstTaskQuery](rc)
if laId := rc.GetLoginAccount().Id; laId != consts.AdminId {
// 赋值操作人为当前登录账号
instTaskQuery.Assignee = fmt.Sprintf("%d", rc.GetLoginAccount().Id)
@@ -74,7 +74,7 @@ func (p *ProcinstTask) GetTasks(rc *req.Ctx) {
}
func (p *ProcinstTask) PassTask(rc *req.Ctx) {
auditForm := req.BindJson[*form.ProcinstTaskAudit](rc)
auditForm := req.BindJson[form.ProcinstTaskAudit](rc)
rc.ReqParam = auditForm
la := rc.GetLoginAccount()
@@ -84,7 +84,7 @@ func (p *ProcinstTask) PassTask(rc *req.Ctx) {
}
func (p *ProcinstTask) RejectTask(rc *req.Ctx) {
auditForm := req.BindJson[*form.ProcinstTaskAudit](rc)
auditForm := req.BindJson[form.ProcinstTaskAudit](rc)
rc.ReqParam = auditForm
la := rc.GetLoginAccount()
@@ -94,7 +94,7 @@ func (p *ProcinstTask) RejectTask(rc *req.Ctx) {
}
func (p *ProcinstTask) BackTask(rc *req.Ctx) {
auditForm := req.BindJson[*form.ProcinstTaskAudit](rc)
auditForm := req.BindJson[form.ProcinstTaskAudit](rc)
rc.ReqParam = auditForm
la := rc.GetLoginAccount()

View File

@@ -5,12 +5,12 @@ import (
)
func InitIoc() {
ioc.Register(new(procdefAppImpl), ioc.WithComponentName("ProcdefApp"))
ioc.Register(new(procinstAppImpl), ioc.WithComponentName("ProcinstApp"))
ioc.Register(new(executionAppImpl), ioc.WithComponentName("ExecutionApp"))
ioc.Register(new(procdefAppImpl))
ioc.Register(new(procinstAppImpl))
ioc.Register(new(executionAppImpl))
ioc.Register(new(procinstTaskAppImpl), ioc.WithComponentName("ProcinstTaskApp"))
ioc.Register(new(hisProcinstOpAppImpl), ioc.WithComponentName("HisProcinstOpApp"))
ioc.Register(new(procinstTaskAppImpl))
ioc.Register(new(hisProcinstOpAppImpl))
}
func Init() {

View File

@@ -184,12 +184,11 @@ func (e *executionAppImpl) executeNode(ctx *ExecutionCtx) error {
// 执行节点逻辑
if node.IsAsync() {
go func() {
defer gox.RecoverPanic()
gox.Go(func() {
if err := node.Execute(ctx); err != nil {
logx.Errorf("async execute node error: %v, procinst_id: %d, node_key: %s", err, ctx.Procinst.Id, flowNode.Key)
}
}()
})
return nil
}

View File

@@ -10,10 +10,9 @@ import (
"mayfly-go/internal/flow/infra/persistence"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/jsonx"
"time"
"github.com/spf13/cast"
)
/******************* AI任务节点 *******************/
@@ -69,13 +68,16 @@ func (u *AiTaskNodeBehavior) Execute(ctx *ExecutionCtx) error {
flowNode := ctx.GetFlowNode()
aitaskNode := ToAiTaskNode(flowNode)
aiagent, err := agent.NewAiAgent(ctx, agent.ToolTypeDb)
aiagent, err := agent.GetOpsExpertAgent(ctx, agent.ToolTypeDb)
if err != nil {
return err
}
auditRule := aitaskNode.AuditRule
sysPrompt := prompt.GetPrompt(prompt.FLOW_BIZ_AUDIT, auditRule)
sysPrompt, err := prompt.GetPrompt("flow_biz_audit.md", collx.Kvs("rule", auditRule))
if err != nil {
return err
}
procinst := ctx.Procinst
now := time.Now()
@@ -93,18 +95,18 @@ func (u *AiTaskNodeBehavior) Execute(ctx *ExecutionCtx) error {
cancelCtx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancelFunc()
res, err := aiagent.GetChatMsg(cancelCtx, sysPrompt, jsonx.ToStr(procinst.BizForm))
res, err := aiagent.Run(cancelCtx, sysPrompt, jsonx.ToStr(procinst.BizForm))
if err != nil {
suggestion = fmt.Sprintf("AI agent response failed: %v", err)
logx.Error(suggestion)
} else {
resJson, err := jsonx.ToMap(res)
resJson, err := agent.ParseLLMJSON2Map(res)
if err != nil {
suggestion = fmt.Sprintf("AI agent response parsing to JSON failed: %v, response: %s", err, res)
logx.Error(suggestion)
} else {
allowExecute = cast.ToBool(resJson["allowExecute"])
suggestion = cast.ToString(resJson["suggestion"])
allowExecute = resJson.GetBool("allowExecute")
suggestion = resJson.GetStr("suggestion")
}
}
@@ -152,7 +154,6 @@ func (u *AiTaskNodeBehavior) Execute(ctx *ExecutionCtx) error {
// 跳转至开始节点,重新修改提交
ctx.Execution.State = entity.ExectionStateSuspended // 执行流挂起
return executionApp.MoveTo(ctx, ctx.GetFlowDef().GetNodeByType(FlowNodeTypeStart)[0])
}
return u.Leave(ctx)
})

View File

@@ -61,7 +61,7 @@ func (p *Procdef) GetFlowDef() *FlowDef {
if p.FlowDef == "" {
return nil
}
flow, err := jsonx.To[*FlowDef](p.FlowDef)
flow, err := jsonx.To[FlowDef](p.FlowDef)
if err != nil {
logx.ErrorTrace("parse flow def failed", err)
return flow

View File

@@ -43,7 +43,7 @@ func (a *Procinst) SetEnd() {
// GetProcdefFlow 获取流程定义信息
func (p *Procinst) GetFlowDef() *FlowDef {
flow, err := jsonx.To[*FlowDef](p.FlowDef)
flow, err := jsonx.To[FlowDef](p.FlowDef)
if err != nil {
logx.ErrorTrace("parse procdef flow failed", err)
return flow

View File

@@ -6,12 +6,12 @@ import (
)
func InitIoc() {
ioc.Register(newProcdefRepo(), ioc.WithComponentName("ProcdefRepo"))
ioc.Register(newProcinstRepo(), ioc.WithComponentName("ProcinstRepo"))
ioc.Register(newExectionRepo(), ioc.WithComponentName("ExectionRepo"))
ioc.Register(newProcinstTaskRepo(), ioc.WithComponentName("ProcinstTaskRepo"))
ioc.Register(newProcinstTaskCandidateRepo(), ioc.WithComponentName("ProcinstTaskCandidateRepo"))
ioc.Register(newHisProcinstOpRepo(), ioc.WithComponentName("HisProcinstTaskRepo"))
ioc.Register(newProcdefRepo())
ioc.Register(newProcinstRepo())
ioc.Register(newExectionRepo())
ioc.Register(newProcinstTaskRepo())
ioc.Register(newProcinstTaskCandidateRepo())
ioc.Register(newHisProcinstOpRepo())
}
func GetProcinstTaskCandidateRepo() repository.ProcinstTaskCandidate {

View File

@@ -1,18 +1,18 @@
package init
import (
"mayfly-go/initialize"
"mayfly-go/internal/flow/api"
"mayfly-go/internal/flow/application"
"mayfly-go/internal/flow/infra/persistence"
"mayfly-go/pkg/starter"
)
func init() {
initialize.AddInitIocFunc(func() {
starter.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
})
initialize.AddInitFunc(application.Init)
starter.AddInitFunc(application.Init)
}

View File

@@ -16,6 +16,7 @@ import (
tagentity "mayfly-go/internal/tag/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/global"
"mayfly-go/pkg/gox"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
@@ -76,7 +77,7 @@ func (m *Machine) ReqConfs() *req.Confs {
}
func (m *Machine) Machines(rc *req.Ctx) {
condition := req.BindQuery[*entity.MachineQuery](rc)
condition := req.BindQuery[entity.MachineQuery](rc)
tags := m.tagTreeApp.GetAccountTags(rc.GetLoginAccount().Id, &tagentity.TagTreeQuery{
TypePaths: collx.AsArray(tagentity.NewTypePaths(tagentity.TagTypeMachine, tagentity.TagTypeAuthCert)),
@@ -143,7 +144,7 @@ func (m *Machine) MachineStats(rc *req.Ctx) {
// 保存机器信息
func (m *Machine) SaveMachine(rc *req.Ctx) {
machineForm, me := req.BindJsonAndCopyTo[*form.MachineForm, *entity.Machine](rc)
machineForm, me := req.BindJsonAndCopyTo[form.MachineForm, entity.Machine](rc)
rc.ReqParam = machineForm
@@ -155,7 +156,7 @@ func (m *Machine) SaveMachine(rc *req.Ctx) {
}
func (m *Machine) TestConn(rc *req.Ctx) {
machineForm, me := req.BindJsonAndCopyTo[*form.MachineForm, *entity.Machine](rc)
machineForm, me := req.BindJsonAndCopyTo[form.MachineForm, entity.Machine](rc)
// 测试连接
biz.ErrIsNilAppendErr(m.machineApp.TestConn(rc.MetaCtx, me, machineForm.AuthCerts[0]), "connection error: %s")
}
@@ -378,7 +379,9 @@ func (m *Machine) WsGuacamole(rc *req.Ctx) {
defer tunnel.ReleaseWriter()
defer tunnel.ReleaseReader()
go guac.WsToGuacd(wsConn, tunnel, writer)
gox.Go(func() {
guac.WsToGuacd(wsConn, tunnel, writer)
})
guac.GuacdToWs(wsConn, tunnel, reader)
//OnConnect

View File

@@ -33,7 +33,7 @@ func (mcc *MachineCmdConf) ReqConfs() *req.Confs {
}
func (m *MachineCmdConf) MachineCmdConfs(rc *req.Ctx) {
cond := req.BindQuery[*entity.MachineCmdConf](rc)
cond := req.BindQuery[entity.MachineCmdConf](rc)
var vos []*vo.MachineCmdConfVO
err := m.machineCmdConfApp.ListByCondToAny(cond, &vos)
@@ -47,7 +47,7 @@ func (m *MachineCmdConf) MachineCmdConfs(rc *req.Ctx) {
}
func (m *MachineCmdConf) Save(rc *req.Ctx) {
cmdForm, mcj := req.BindJsonAndCopyTo[*form.MachineCmdConfForm, *entity.MachineCmdConf](rc)
cmdForm, mcj := req.BindJsonAndCopyTo[form.MachineCmdConfForm, entity.MachineCmdConf](rc)
rc.ReqParam = cmdForm
err := m.machineCmdConfApp.SaveCmdConf(rc.MetaCtx, &dto.SaveMachineCmdConf{

View File

@@ -43,7 +43,7 @@ func (mcj *MachineCronJob) ReqConfs() *req.Confs {
}
func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
cond, pageParam := req.BindQueryAndPage[*entity.MachineCronJob](rc)
cond, pageParam := req.BindQueryAndPage[entity.MachineCronJob](rc)
pageRes, err := m.machineCronJobApp.GetPageList(cond, pageParam)
biz.ErrIsNil(err)
@@ -62,7 +62,7 @@ func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
}
func (m *MachineCronJob) Save(rc *req.Ctx) {
jobForm, mcj := req.BindJsonAndCopyTo[*form.MachineCronJobForm, *entity.MachineCronJob](rc)
jobForm, mcj := req.BindJsonAndCopyTo[form.MachineCronJobForm, entity.MachineCronJob](rc)
rc.ReqParam = jobForm
err := m.machineCronJobApp.SaveMachineCronJob(rc.MetaCtx, &dto.SaveMachineCronJob{
@@ -89,7 +89,7 @@ func (m *MachineCronJob) RunCronJob(rc *req.Ctx) {
}
func (m *MachineCronJob) CronJobExecs(rc *req.Ctx) {
cond, pageParam := req.BindQueryAndPage[*entity.MachineCronJobExec](rc)
cond, pageParam := req.BindQueryAndPage[entity.MachineCronJobExec](rc)
res, err := m.machineCronJobApp.GetExecPageList(cond, pageParam)
biz.ErrIsNil(err)
rc.ResData = res

View File

@@ -91,7 +91,7 @@ func (m *MachineFile) MachineFiles(rc *req.Ctx) {
}
func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
fileForm, entity := req.BindJsonAndCopyTo[*form.MachineFileForm, *entity.MachineFile](rc)
fileForm, entity := req.BindJsonAndCopyTo[form.MachineFileForm, entity.MachineFile](rc)
rc.ReqParam = fileForm
biz.ErrIsNil(m.machineFileApp.Save(rc.MetaCtx, entity))
@@ -104,7 +104,7 @@ func (m *MachineFile) DeleteFile(rc *req.Ctx) {
/*** sftp相关操作 */
func (m *MachineFile) CreateFile(rc *req.Ctx) {
opForm := req.BindJson[*form.CreateFileForm](rc)
opForm := req.BindJson[form.CreateFileForm](rc)
path := opForm.Path
attrs := collx.Kvs("path", path)
@@ -123,7 +123,7 @@ func (m *MachineFile) CreateFile(rc *req.Ctx) {
}
func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
readPath := opForm.Path
ctx := rc.MetaCtx
@@ -155,7 +155,7 @@ func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
}
func (m *MachineFile) DownloadFile(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
readPath := opForm.Path
@@ -183,7 +183,7 @@ func (m *MachineFile) DownloadFile(rc *req.Ctx) {
}
func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
readPath := opForm.Path
rc.ReqParam = fmt.Sprintf("path: %s", readPath)
@@ -222,7 +222,7 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
}
func (m *MachineFile) GetDirSize(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
size, err := m.machineFileApp.GetDirSize(rc.MetaCtx, opForm)
biz.ErrIsNil(err)
@@ -230,14 +230,14 @@ func (m *MachineFile) GetDirSize(rc *req.Ctx) {
}
func (m *MachineFile) GetFileStat(rc *req.Ctx) {
opForm := req.BindQuery[*dto.MachineFileOp](rc)
opForm := req.BindQuery[dto.MachineFileOp](rc)
res, err := m.machineFileApp.FileStat(rc.MetaCtx, opForm)
biz.ErrIsNil(err, res)
rc.ResData = res
}
func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
opForm := req.BindJson[*form.WriteFileContentForm](rc)
opForm := req.BindJson[form.WriteFileContentForm](rc)
path := opForm.Path
mi, err := m.machineFileApp.WriteFileContent(rc.MetaCtx, opForm.MachineFileOp, []byte(opForm.Content))
@@ -421,7 +421,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
}
func (m *MachineFile) RemoveFile(rc *req.Ctx) {
opForm := req.BindJson[*form.RemoveFileForm](rc)
opForm := req.BindJson[form.RemoveFileForm](rc)
mi, err := m.machineFileApp.RemoveFile(rc.MetaCtx, opForm.MachineFileOp, opForm.Paths...)
rc.ReqParam = collx.Kvs("machine", mi, "path", opForm)
@@ -429,21 +429,21 @@ func (m *MachineFile) RemoveFile(rc *req.Ctx) {
}
func (m *MachineFile) CopyFile(rc *req.Ctx) {
opForm := req.BindJson[*form.CopyFileForm](rc)
opForm := req.BindJson[form.CopyFileForm](rc)
mi, err := m.machineFileApp.Copy(rc.MetaCtx, opForm.MachineFileOp, opForm.ToPath, opForm.Paths...)
biz.ErrIsNilAppendErr(err, "file copy error: %s")
rc.ReqParam = collx.Kvs("machine", mi, "cp", opForm)
}
func (m *MachineFile) MvFile(rc *req.Ctx) {
opForm := req.BindJson[*form.CopyFileForm](rc)
opForm := req.BindJson[form.CopyFileForm](rc)
mi, err := m.machineFileApp.Mv(rc.MetaCtx, opForm.MachineFileOp, opForm.ToPath, opForm.Paths...)
rc.ReqParam = collx.Kvs("machine", mi, "mv", opForm)
biz.ErrIsNilAppendErr(err, "file move error: %s")
}
func (m *MachineFile) Rename(rc *req.Ctx) {
renameForm := req.BindJson[*form.RenameForm](rc)
renameForm := req.BindJson[form.RenameForm](rc)
mi, err := m.machineFileApp.Rename(rc.MetaCtx, renameForm.MachineFileOp, renameForm.Newname)
rc.ReqParam = collx.Kvs("machine", mi, "rename", renameForm)
biz.ErrIsNilAppendErr(err, "file rename error: %s")

View File

@@ -54,7 +54,7 @@ func (m *MachineScript) MachineScriptCategorys(rc *req.Ctx) {
}
func (m *MachineScript) SaveMachineScript(rc *req.Ctx) {
form, machineScript := req.BindJsonAndCopyTo[*form.MachineScriptForm, *entity.MachineScript](rc)
form, machineScript := req.BindJsonAndCopyTo[form.MachineScriptForm, entity.MachineScript](rc)
rc.ReqParam = form
biz.ErrIsNil(m.machineScriptApp.Save(rc.MetaCtx, machineScript))

View File

@@ -6,12 +6,12 @@ import (
)
func InitIoc() {
ioc.Register(new(machineAppImpl), ioc.WithComponentName("MachineApp"))
ioc.Register(new(machineFileAppImpl), ioc.WithComponentName("MachineFileApp"))
ioc.Register(new(machineScriptAppImpl), ioc.WithComponentName("MachineScriptApp"))
ioc.Register(new(machineCronJobAppImpl), ioc.WithComponentName("MachineCronJobApp"))
ioc.Register(new(machineTermOpAppImpl), ioc.WithComponentName("MachineTermOpApp"))
ioc.Register(new(machineCmdConfAppImpl), ioc.WithComponentName("MachineCmdConfApp"))
ioc.Register(new(machineAppImpl))
ioc.Register(new(machineFileAppImpl))
ioc.Register(new(machineScriptAppImpl))
ioc.Register(new(machineCronJobAppImpl))
ioc.Register(new(machineTermOpAppImpl))
ioc.Register(new(machineCmdConfAppImpl))
}
func Init() {

View File

@@ -256,13 +256,11 @@ func (m *machineAppImpl) GetSshTunnelMachine(ctx context.Context, machineId int)
func (m *machineAppImpl) TimerUpdateStats() {
logx.Debug("start collecting and caching machine state information periodically...")
scheduler.AddFun("@every 2m", func() {
defer gox.RecoverPanic()
defer gox.Recover()
machineIds, _ := m.ListByCond(model.NewModelCond(&entity.Machine{Status: entity.MachineStatusEnable, Protocol: entity.MachineProtocolSsh}).Columns("id"))
for _, ma := range machineIds {
go func(mid uint64) {
defer gox.RecoverPanic(func(err error) {
logx.ErrorTrace(fmt.Sprintf("failed to get machine [id=%d] status information on time", mid), err)
})
gox.Go(func() {
mid := ma.Id
logx.Debugf("time to get machine [id=%d] status information start", mid)
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
@@ -273,7 +271,9 @@ func (m *machineAppImpl) TimerUpdateStats() {
}
cache.SaveMachineStats(mid, cli.GetAllStats())
logx.Debugf("time to get the machine [id=%d] status information end", mid)
}(ma.Id)
}, func(err error) {
logx.ErrorTrace(fmt.Sprintf("failed to get machine [id=%d] status information on time", ma.Id), err)
})
}
})
}

Some files were not shown because too many files have changed in this diff Show More