Unverified Commit 2bcc490d authored by zhu-mingye's avatar zhu-mingye Committed by GitHub

refactor extra global variables (#2441)

* Spotless Apply * Spotless Apply * refactor extra global variables * refactor extra global variables * Spotless Apply --------- Co-authored-by: 's avatarzhu-mingye <zhu-mingye@users.noreply.github.com>
parent ffadb0f3
...@@ -36,13 +36,4 @@ public interface FlinkSQLConstant { ...@@ -36,13 +36,4 @@ public interface FlinkSQLConstant {
/** The define identifier of FlinkSQL Variable */ /** The define identifier of FlinkSQL Variable */
String VARIABLES = ":="; String VARIABLES = ":=";
/** The define identifier of FlinkSQL Date Variable */
String INNER_DATE_KEY = "_CURRENT_DATE_";
/** The define identifier of FlinkSQL Timestamp Variable */
String INNER_TIMESTAMP_KEY = "_CURRENT_TIMESTAMP_";
/** 内置日期变量格式 确定后不能修改 */
String INNER_DATE_FORMAT = "yyyy-MM-dd";
} }
...@@ -33,17 +33,20 @@ import org.apache.flink.table.catalog.exceptions.CatalogException; ...@@ -33,17 +33,20 @@ import org.apache.flink.table.catalog.exceptions.CatalogException;
import org.apache.flink.types.Row; import org.apache.flink.types.Row;
import org.apache.flink.util.StringUtils; import org.apache.flink.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.expression.engine.jexl.JexlEngine;
/** /**
* Flink Sql Variable Manager * Flink Sql Variable Manager
* *
...@@ -55,6 +58,22 @@ public final class VariableManager { ...@@ -55,6 +58,22 @@ public final class VariableManager {
static final String SHOW_VARIABLES = "SHOW VARIABLES"; static final String SHOW_VARIABLES = "SHOW VARIABLES";
private final Map<String, String> variables; private final Map<String, String> variables;
public static final JexlEngine ENGINE = new JexlEngine();
/**
* <p>
* engine , key is variable name , value is class .
* for example:
* random -> RandomUtil -> about random operation
* date -> DateUtil -> about date operation
* id -> IdUtil -> to generate random uuid
* ...
*/
public static final Dict ENGINE_CONTEXT = Dict.create()
.set("random", RandomUtil.class)
.set("date", DateUtil.class)
.set("id", IdUtil.class);
public VariableManager() { public VariableManager() {
variables = new HashMap<>(); variables = new HashMap<>();
} }
...@@ -127,21 +146,18 @@ public final class VariableManager { ...@@ -127,21 +146,18 @@ public final class VariableManager {
* @throws CatalogException if the unregistration of the sql variable under the given name * @throws CatalogException if the unregistration of the sql variable under the given name
* failed. But at the moment, with CatalogException, not SqlException * failed. But at the moment, with CatalogException, not SqlException
*/ */
public String getVariable(String variableName) { public Object getVariable(String variableName) {
checkArgument( checkArgument(
!StringUtils.isNullOrWhitespaceOnly(variableName), "sql variableName name cannot be null or empty."); !StringUtils.isNullOrWhitespaceOnly(variableName), "sql variableName name cannot be null or empty.");
try {
if (variables.containsKey(variableName)) { if (variables.containsKey(variableName)) {
return variables.get(variableName); return variables.get(variableName);
} }
// use jexl to parse variable value
if (isInnerDateVariable(variableName)) { return ENGINE.eval(variableName, ENGINE_CONTEXT);
return parseDateVariable(variableName); } catch (Exception e) {
} else if (isInnerTimestampVariable(variableName)) { throw new CatalogException(format("The variable of sql %s does not exist.", variableName));
return parseTimestampVar(variableName);
} }
throw new CatalogException(format("The variable of sql %s does not exist.", variableName));
} }
/** /**
...@@ -188,7 +204,7 @@ public final class VariableManager { ...@@ -188,7 +204,7 @@ public final class VariableManager {
} }
public boolean checkShowVariables(String sql) { public boolean checkShowVariables(String sql) {
return SHOW_VARIABLES.equals(sql.trim().toUpperCase()); return SHOW_VARIABLES.equalsIgnoreCase(sql.trim());
} }
/** /**
...@@ -229,95 +245,11 @@ public final class VariableManager { ...@@ -229,95 +245,11 @@ public final class VariableManager {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
while (m.find()) { while (m.find()) {
String key = m.group(1); String key = m.group(1);
String value = getVariable(key); Object value = getVariable(key);
m.appendReplacement(sb, ""); m.appendReplacement(sb, "");
// if value is null, parse inner date variable
if (value == null) {
if (isInnerDateVariable(key)) {
value = parseDateVariable(key);
} else if (isInnerTimestampVariable(key)) {
value = parseTimestampVar(key);
}
}
sb.append(value == null ? "" : value); sb.append(value == null ? "" : value);
} }
m.appendTail(sb); m.appendTail(sb);
return sb.toString(); return sb.toString();
} }
/**
* verify if key is inner variable, such as _CURRENT_DATE_ - 1
*
* @param key
* @return
*/
private boolean isInnerDateVariable(String key) {
return key.startsWith(FlinkSQLConstant.INNER_DATE_KEY);
}
/**
* verify if key is inner variable, such as _CURRENT_TIMESTAMP_ - 100
*
* @param key
* @return
*/
private boolean isInnerTimestampVariable(String key) {
return key.startsWith(FlinkSQLConstant.INNER_TIMESTAMP_KEY);
}
/**
* parse date variable
*
* @param key
* @return
*/
private String parseDateVariable(String key) {
int days = 0;
if (key.contains("+")) {
int s = key.indexOf("+") + 1;
String num = key.substring(s).trim();
days = Integer.parseInt(num);
} else if (key.contains("-")) {
int s = key.indexOf("-") + 1;
String num = key.substring(s).trim();
days = Integer.parseInt(num) * -1;
}
SimpleDateFormat dtf = new SimpleDateFormat(FlinkSQLConstant.INNER_DATE_FORMAT);
Date endDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(endDate);
calendar.add(Calendar.DAY_OF_YEAR, days);
Date startDate = calendar.getTime();
return dtf.format(startDate);
}
/**
* parse timestamp variable
*
* @param key
* @return
*/
private String parseTimestampVar(String key) {
long millisecond = 0;
try {
if (key.contains("+")) {
int s = key.indexOf("+") + 1;
String num = key.substring(s).trim();
millisecond = Long.parseLong(num);
} else if (key.contains("-")) {
int s = key.indexOf("-") + 1;
String num = key.substring(s).trim();
millisecond = Long.parseLong(num) * -1;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return String.valueOf(System.currentTimeMillis() + millisecond);
}
} }
...@@ -399,8 +399,8 @@ h5 { ...@@ -399,8 +399,8 @@ h5 {
scrollbar-width: thin; scrollbar-width: thin;
} }
.editor-full-screen{ .editor-full-screen {
.ant-tabs-tabpane{ .ant-tabs-tabpane {
height: 100vh; height: 100vh;
} }
} }
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
import useThemeValue from '@/hooks/useThemeValue'; import useThemeValue from '@/hooks/useThemeValue';
import JobRunningModal from '@/pages/DataStudio/FooterContainer/JobRunningModal'; import JobRunningModal from '@/pages/DataStudio/FooterContainer/JobRunningModal';
import { getCurrentTab } from '@/pages/DataStudio/function'; import { getCurrentTab } from '@/pages/DataStudio/function';
import { StateType,TabsPageType,VIEW} from '@/pages/DataStudio/model'; import { StateType, TabsPageType, VIEW } from '@/pages/DataStudio/model';
import { getSseData } from '@/services/api'; import { getSseData } from '@/services/api';
import { l } from '@/utils/intl'; import { l } from '@/utils/intl';
import { connect } from '@@/exports'; import { connect } from '@@/exports';
......
...@@ -217,7 +217,7 @@ const HeaderContainer = (props: any) => { ...@@ -217,7 +217,7 @@ const HeaderContainer = (props: any) => {
const routes: ButtonRoute[] = [ const routes: ButtonRoute[] = [
// 保存按钮 icon // 保存按钮 icon
{ {
hotKey: (e: KeyboardEvent) => e.ctrlKey && e.key === 's' || e.metaKey && e.key === 's', hotKey: (e: KeyboardEvent) => (e.ctrlKey && e.key === 's') || (e.metaKey && e.key === 's'),
hotKeyDesc: 'Ctrl/Command +S', hotKeyDesc: 'Ctrl/Command +S',
isShow: projectCommonShow(currentTab?.type), isShow: projectCommonShow(currentTab?.type),
icon: <SaveOutlined />, icon: <SaveOutlined />,
...@@ -237,7 +237,8 @@ const HeaderContainer = (props: any) => { ...@@ -237,7 +237,8 @@ const HeaderContainer = (props: any) => {
{ {
// 检查 sql按钮 // 检查 sql按钮
icon: <ScheduleOutlined />, icon: <ScheduleOutlined />,
hotKey: (e: KeyboardEvent) => e.altKey && e.code === 'Digit2' || e.altKey && e.key === '@', hotKey: (e: KeyboardEvent) =>
(e.altKey && e.code === 'Digit2') || (e.altKey && e.key === '@'),
hotKeyDesc: 'Alt+2/@', hotKeyDesc: 'Alt+2/@',
title: l('pages.datastudio.editor.check'), title: l('pages.datastudio.editor.check'),
click: () => showExplain(), click: () => showExplain(),
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
* *
*/ */
import { useEditor } from '@/hooks/useEditor';
import { getCurrentTab } from '@/pages/DataStudio/function'; import { getCurrentTab } from '@/pages/DataStudio/function';
import { TASK_VAR_FILTER } from '@/pages/DataStudio/MiddleContainer/Editor/constants'; import { TASK_VAR_FILTER } from '@/pages/DataStudio/MiddleContainer/Editor/constants';
import DiffModal from '@/pages/DataStudio/MiddleContainer/Editor/DiffModal'; import DiffModal from '@/pages/DataStudio/MiddleContainer/Editor/DiffModal';
...@@ -37,7 +38,6 @@ import { Button, Spin } from 'antd'; ...@@ -37,7 +38,6 @@ import { Button, Spin } from 'antd';
import { editor, KeyCode, KeyMod } from 'monaco-editor'; import { editor, KeyCode, KeyMod } from 'monaco-editor';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { format } from 'sql-formatter'; import { format } from 'sql-formatter';
import { useEditor } from '@/hooks/useEditor';
export type EditorProps = { export type EditorProps = {
taskId: number; taskId: number;
...@@ -49,7 +49,7 @@ const CodeEditor: React.FC<EditorProps & any> = (props) => { ...@@ -49,7 +49,7 @@ const CodeEditor: React.FC<EditorProps & any> = (props) => {
taskId, taskId,
tabs: { panes, activeKey }, tabs: { panes, activeKey },
dispatch, dispatch,
height, height
} = props; } = props;
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
......
...@@ -34,7 +34,7 @@ export const KEY_BOARD_MIDDLE = [ ...@@ -34,7 +34,7 @@ export const KEY_BOARD_MIDDLE = [
key: 'alt3', key: 'alt3',
label: 'Alt + 3', label: 'Alt + 3',
description: l('shortcut.key.beautify') description: l('shortcut.key.beautify')
}, }
// { // {
// key: 'f2', // key: 'f2',
// label: 'F2', // label: 'F2',
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
*/ */
import { AuthorizedObject, useAccess } from '@/hooks/useAccess'; import { AuthorizedObject, useAccess } from '@/hooks/useAccess';
import { FullScreenProvider, useEditor } from '@/hooks/useEditor'; import { useEditor } from '@/hooks/useEditor';
import useThemeValue from '@/hooks/useThemeValue'; import useThemeValue from '@/hooks/useThemeValue';
import BottomContainer from '@/pages/DataStudio/BottomContainer'; import BottomContainer from '@/pages/DataStudio/BottomContainer';
import FooterContainer from '@/pages/DataStudio/FooterContainer'; import FooterContainer from '@/pages/DataStudio/FooterContainer';
...@@ -199,57 +199,52 @@ const DataStudio = (props: any) => { ...@@ -199,57 +199,52 @@ const DataStudio = (props: any) => {
/> />
); );
return ( return fullscreen ? (
fullscreen ? ( <MiddleContainer />
<MiddleContainer /> ) : (
) : ( <PageContainer title={false} breadcrumb={{ style: { display: 'none' } }}>
<PageContainer title={false} breadcrumb={{ style: { display: 'none' } }}> <PersistGate loading={null} persistor={persist}>
<PersistGate loading={null} persistor={persist}> <div style={{ marginInline: -10, marginTop: -6, width: size.width }}>
<div style={{ marginInline: -10, marginTop: -6, width: size.width }}> <SecondHeaderContainer size={size} activeBreadcrumbTitle={activeBreadcrumbTitle} />
<SecondHeaderContainer size={size} activeBreadcrumbTitle={activeBreadcrumbTitle} /> <Layout
<Layout hasSider
hasSider style={{
style={{ minHeight: size.contentHeight,
minHeight: size.contentHeight, maxHeight: size.contentHeight,
maxHeight: size.contentHeight, paddingInline: 0
paddingInline: 0 }}
}} >
> <Sider collapsed collapsedWidth={40}>
<Sider collapsed collapsedWidth={40}> <div style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
<div style={{ display: 'flex', flexDirection: 'column', height: '100%' }}> {LeftTopMenu}
{LeftTopMenu} {LeftBottomMenu}
{LeftBottomMenu} </div>
</div> </Sider>
</Sider>
<Content style={{ display: 'flex', flexDirection: 'column', minWidth: 0 }}> <Content style={{ display: 'flex', flexDirection: 'column', minWidth: 0 }}>
<div style={{ display: 'flex' }}> <div style={{ display: 'flex' }}>
<LeftContainer size={size} /> <LeftContainer size={size} />
<Content <Content
style={{ style={{
width: width:
size.width - size.width - 2 * VIEW.sideWidth - leftContainer.width - rightContainer.width
2 * VIEW.sideWidth - }}
leftContainer.width - >
rightContainer.width <MiddleContainer />
}}
>
<MiddleContainer />
</Content>
<RightContainer size={size} bottomHeight={bottomHeight} />
</div>
{<BottomContainer size={size} />}
</Content> </Content>
<RightContainer size={size} bottomHeight={bottomHeight} />
</div>
{<BottomContainer size={size} />}
</Content>
<Sider collapsed collapsedWidth={40}> <Sider collapsed collapsedWidth={40}>
{RightTopMenu} {RightTopMenu}
</Sider> </Sider>
</Layout> </Layout>
{<FooterContainer token={token} />} {<FooterContainer token={token} />}
</div> </div>
</PersistGate> </PersistGate>
</PageContainer> </PageContainer>
)
); );
}; };
......
...@@ -124,41 +124,40 @@ export function getLocalTheme(): string { ...@@ -124,41 +124,40 @@ export function getLocalTheme(): string {
* @constructor * @constructor
*/ */
export function convertCodeEditTheme() { export function convertCodeEditTheme() {
/**
/** * user can define a new theme by calling the defineTheme method on the editor.
* user can define a new theme by calling the defineTheme method on the editor. */
*/ editor.defineTheme(CODE_EDIT_THEME.VS_CUSTOME, {
editor.defineTheme(CODE_EDIT_THEME.VS_CUSTOME, { base: 'vs', // 指定基础主题 , 可选值: 'vs', 'vs-dark', 'hc-black' , base theme
base: 'vs', // 指定基础主题 , 可选值: 'vs', 'vs-dark', 'hc-black' , base theme inherit: true, // 是否继承基础主题配置 , 默认为 true, is to inherit the base theme
inherit: true, // 是否继承基础主题配置 , 默认为 true, is to inherit the base theme // rules is an array of rules. The array must not be sparse (i.e. do not use holes).
// rules is an array of rules. The array must not be sparse (i.e. do not use holes). rules: [
rules: [ { token: 'comment', foreground: '#008800', fontStyle: 'italic' },
{ token: 'comment', foreground: '#008800', fontStyle: 'italic' }, { token: 'keyword', foreground: '#064cff', fontStyle: 'bold' },
{ token: 'keyword', foreground: '#064cff', fontStyle: 'bold' }, { token: 'string', foreground: '#507dee' },
{ token: 'string', foreground: '#507dee' }, { token: 'delimiter', foreground: '#041d81' },
{ token: 'delimiter', foreground: '#041d81' }, {
{ token: 'readonly',
token: 'readonly', foreground: '#e73a6e',
foreground: '#e73a6e', background: '#141414',
background: '#141414', fontStyle: 'italic'
fontStyle: 'italic' },
}, { token: 'number', foreground: '#ffffff' }
{ token: 'number', foreground: '#ffffff' } ],
], // colors is an object of color identifiers and their color values.
// colors is an object of color identifiers and their color values. colors: {
colors: { 'editor.background': '#2f2e2e', // editor background color
'editor.background': '#2f2e2e', // editor background color 'editor.lineHighlightBackground': '#959cb6', // editor line highlight background color
'editor.lineHighlightBackground': '#959cb6', // editor line highlight background color 'editorLineNumber.foreground': '#ffffff', // editor line number color
'editorLineNumber.foreground': '#ffffff', // editor line number color 'editorCursor.foreground': '#ffffff', // editor cursor color
'editorCursor.foreground': '#ffffff', // editor cursor color 'editorIndentGuide.background': '#ffffff', // editor indent guide color
'editorIndentGuide.background': '#ffffff', // editor indent guide color 'editor.foreground': '#ffffff', // editor selection highlight border color
'editor.foreground': '#ffffff', // editor selection highlight border color 'editor.selectionBackground': '#4ba1ef', // editor selection highlight color
'editor.selectionBackground': '#4ba1ef', // editor selection highlight color 'editor.selectionHighlightBorder': '#4ba1ef', // editor selection highlight border color
'editor.selectionHighlightBorder': '#4ba1ef', // editor selection highlight border color 'editor.findMatchBackground': '#4ba1ef', // editor find match highlight color
'editor.findMatchBackground': '#4ba1ef', // editor find match highlight color 'editor.wordHighlightBackground': '#8bb2d2' // editor word highlight color
'editor.wordHighlightBackground': '#8bb2d2' // editor word highlight color }
} });
});
const theme = getLocalTheme(); const theme = getLocalTheme();
switch (theme) { switch (theme) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment