Skip to content

Commit

Permalink
feat(temp): annotation template
Browse files Browse the repository at this point in the history
  • Loading branch information
aide-cloud committed Apr 30, 2024
1 parent 6863e94 commit 5b394bd
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 52 deletions.
9 changes: 4 additions & 5 deletions app/prom_server/internal/biz/bo/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aide-family/moon/api"
"github.com/aide-family/moon/app/prom_server/internal/biz/do"
"github.com/aide-family/moon/app/prom_server/internal/biz/vobj"
"github.com/aide-family/moon/pkg/util/slices"
)

type (
Expand Down Expand Up @@ -67,11 +68,9 @@ func (d *DictBO) ToApiSelectV1() *api.DictSelectV1 {
}

func ListToApiDictSelectV1(values ...*DictBO) []*api.DictSelectV1 {
list := make([]*api.DictSelectV1, 0, len(values))
for _, v := range values {
list = append(list, v.ToApiSelectV1())
}
return list
return slices.To(values, func(v *DictBO) *api.DictSelectV1 {
return v.ToApiSelectV1()
})
}

func (d *DictBO) ToModel() *do.SysDict {
Expand Down
2 changes: 1 addition & 1 deletion app/prom_server/internal/biz/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"strings"

"github.com/go-kratos/kratos/v2/log"
"github.com/aide-family/moon/api/perrors"
"github.com/aide-family/moon/app/prom_server/internal/biz/bo"
"github.com/aide-family/moon/app/prom_server/internal/biz/do"
"github.com/aide-family/moon/app/prom_server/internal/biz/do/basescopes"
"github.com/aide-family/moon/app/prom_server/internal/biz/repository"
"github.com/aide-family/moon/app/prom_server/internal/biz/vobj"
"github.com/aide-family/moon/pkg/util/slices"
"github.com/go-kratos/kratos/v2/log"
)

type (
Expand Down
10 changes: 10 additions & 0 deletions web/src/components/Data/DataInput/DataInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import {
DingTemplateEditor,
DingTemplateEditorProps
} from './child/DingTemplateEditor'
import {
AnnotationEditor,
AnnotationEditorProps
} from './child/AnnotationEditor'

export type DataInputProps =
| (
Expand Down Expand Up @@ -126,6 +130,10 @@ export type DataInputProps =
type: 'ding-template-editor'
parentProps?: DingTemplateEditorProps
}
| {
type: 'annotation-template-editor'
parentProps?: AnnotationEditorProps
}
) & {
width?: number | string
value?: any
Expand Down Expand Up @@ -267,6 +275,8 @@ const DataInput: FC<DataInputProps> = (props) => {
return <FeishuTemplateEditor {...props} {...parentProps} />
case 'ding-template-editor':
return <DingTemplateEditor {...props} {...parentProps} />
case 'annotation-template-editor':
return <AnnotationEditor {...props} {...parentProps} />
default:
return (
<Input
Expand Down
225 changes: 225 additions & 0 deletions web/src/components/Data/DataInput/child/AnnotationEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import React, { useContext } from 'react'
import { useRef, useState, useEffect } from 'react'

import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import { GlobalToken, theme } from 'antd'
import './userWorker'

import { GlobalContext, ThemeType } from '@/context'
import { defaultTheme } from './color'

import './style.css'

export interface AnnotationEditorProps {
value?: string
defaultValue?: string
onChange?: (value: string) => void
width?: number | string
height?: number | string
disabled?: boolean
}

const { useToken } = theme

const AnnotationTemplate = 'AnnotationTemplate'
const AnnotationTemplateTheme = 'AnnotationTemplateTheme'

const tpl = '告警实例: {{ \\$labels.instance }}告警,当前值: {{ \\$value }}'

// TODO 根据元数据补充
const keywords: string[] = [
'app_kubernetes_io_managed_by',
'chart',
'component',
'heritage',
'instance',
'job',
'namespace',
'node',
'release',
'service',
'app',
'instance'
]

function createDependencyProposals(range: monaco.IRange) {
return [
{
label: '"labels"',
kind: monaco.languages.CompletionItemKind.Keyword,
insertText: '{{ \\$labels.${1:labelName} }}',
range: range,
insertTextRules:
monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
},
{
label: '"eventsAt"',
kind: monaco.languages.CompletionItemKind.Keyword,
insertText: '{{ \\$eventsAt }}',
range: range,
insertTextRules:
monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
},
...keywords.map((key) => {
return {
label: `"${key}"`,
kind: monaco.languages.CompletionItemKind.Keyword,
insertText: `{{ \\$labels.${key} }}`,
range: range,
insertTextRules:
monaco.languages.CompletionItemInsertTextRule
.InsertAsSnippet
}
}),
{
label: '"value"',
kind: monaco.languages.CompletionItemKind.Function,
insertText: '{{ \\$value }}',
range: range
},

{
label: 'tpl',
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: tpl,
insertTextRules:
monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
range: range
}
]
}

const createKeyDependencyProposals = (range: monaco.IRange) => {
return keywords.map((key) => {
return {
label: key,
kind: monaco.languages.CompletionItemKind.Keyword,
insertText: key,
range: range,
insertTextRules:
monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
}
})
}

const provideCompletionItems = (
model: monaco.editor.ITextModel,
position: monaco.Position
) => {
const word = model.getWordUntilPosition(position)

const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: word.startColumn,
endColumn: word.endColumn
}

// 如果都匹配,返回空建议(这种情况理论上不应该发生,除非正则表达式有误)
return {
suggestions: [
...createDependencyProposals(range),
...createKeyDependencyProposals(range)
]
}
}

const init = (token: GlobalToken, theme?: ThemeType) => {
monaco.languages.register({ id: AnnotationTemplate })
// Register a tokens provider for the language
monaco.languages.setMonarchTokensProvider(AnnotationTemplate, {
tokenizer: {
root: [[/\{\{[ ]*\$[ ]*[^}]*[ ]*\}\}/, 'keyword']]
}
})

// Define a new theme that contains only rules that match this language
monaco.editor.defineTheme(
AnnotationTemplateTheme,
defaultTheme(token, theme)
)

monaco.languages.registerCompletionItemProvider(AnnotationTemplate, {
provideCompletionItems: provideCompletionItems
})
}

export const AnnotationEditor: React.FC<AnnotationEditorProps> = (props) => {
const {
value,
defaultValue,
onChange,
disabled,
width = '100%',
height = '100%'
} = props

const { token } = useToken()
const { sysTheme } = useContext(GlobalContext)

const [editor, setEditor] =
useState<monaco.editor.IStandaloneCodeEditor | null>(null)
const monacoEl = useRef(null)

useEffect(() => {
setEditor((editor) => {
if (editor) {
if (!editor.getValue()) {
editor.setValue(value || defaultValue || '')
}
editor.updateOptions({
readOnly: disabled
})
editor.layout()
return editor
}

const curr = monacoEl.current!
const e = monaco.editor.create(curr, {
theme: AnnotationTemplateTheme,
language: AnnotationTemplate,
value: value || defaultValue,
lineNumbers: 'off',
// 展示行号和内容的边框
lineNumbersMinChars: 4,
readOnly: disabled,
minimap: {
enabled: false
}
})

return e
})
}, [defaultValue, editor, monacoEl, onChange, value])

useEffect(() => {
init(token, sysTheme)
if (editor) {
editor.onDidChangeModelContent(() => {
onChange?.(editor.getValue())
})
}
}, [])

useEffect(() => {
if (editor) {
editor.updateOptions({
readOnly: disabled
})
}
}, [disabled, editor])

return (
<div
style={{
width: width,
height: height,
borderColor: token.colorBorder
// 设置disabled
// pointerEvents: disabled ? 'none' : 'auto'
}}
className="editorInput"
ref={monacoEl}
/>
)
}
4 changes: 4 additions & 0 deletions web/src/components/Data/DataInput/child/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
min-height: 40px;
box-sizing: border-box;
margin: 0;
/* padding: -4px; */
padding: 4px 11px;
/* color: rgba(0, 0, 0, 0.88); */
font-size: 14px;
Expand Down Expand Up @@ -29,6 +30,9 @@
overflow-x: auto;
}

.monaco-scrollable-element{
left: 0 !important;
}

::-webkit-scrollbar {
width: 12px;
Expand Down
68 changes: 36 additions & 32 deletions web/src/pages/home/monitor/chat-hook/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,40 +231,44 @@ export const searchItems: DataFormItem[] = [
export const addChatGroupItems = (
app: NotifyApp
): (DataFormItem | DataFormItem[])[] => [
{
name: 'name',
label: '名称',
rules: [
{
required: true,
message: '请输入名称'
}
]
},
{
name: 'app',
label: '所属平台',
rules: [
{
required: true,
message: '请选择所属平台'
}
],
dataProps: {
type: 'select',
parentProps: {
placeholder: '请选择所属平台',
options: Object.entries(NotifyAppData)
.filter(([key]) => {
return key !== NotifyApp.NOTIFY_APP_UNKNOWN.toString()
})
.map(([key, value]) => ({
label: value,
value: Number(key)
}))
[
{
name: 'name',
label: '名称',
rules: [
{
required: true,
message: '请输入名称'
}
]
},
{
name: 'app',
label: '所属平台',
rules: [
{
required: true,
message: '请选择所属平台'
}
],
dataProps: {
type: 'select',
parentProps: {
placeholder: '请选择所属平台',
options: Object.entries(NotifyAppData)
.filter(([key]) => {
return (
key !== NotifyApp.NOTIFY_APP_UNKNOWN.toString()
)
})
.map(([key, value]) => ({
label: value,
value: Number(key)
}))
}
}
}
},
],
{
name: 'hookName',
label: 'Hook名称',
Expand Down
Loading

0 comments on commit 5b394bd

Please sign in to comment.