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,22 +146,19 @@ public final class VariableManager { ...@@ -127,22 +146,19 @@ 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)) {
return parseTimestampVar(variableName);
}
throw new CatalogException(format("The variable of sql %s does not exist.", variableName)); throw new CatalogException(format("The variable of sql %s does not exist.", variableName));
} }
}
/** /**
* Get a table result of sql under the given name. The sql variable name must be existed. * Get a table result of sql under the given name. The sql variable name must be existed.
...@@ -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,8 +199,7 @@ const DataStudio = (props: any) => { ...@@ -199,8 +199,7 @@ const DataStudio = (props: any) => {
/> />
); );
return ( return fullscreen ? (
fullscreen ? (
<MiddleContainer /> <MiddleContainer />
) : ( ) : (
<PageContainer title={false} breadcrumb={{ style: { display: 'none' } }}> <PageContainer title={false} breadcrumb={{ style: { display: 'none' } }}>
...@@ -228,10 +227,7 @@ const DataStudio = (props: any) => { ...@@ -228,10 +227,7 @@ const DataStudio = (props: any) => {
<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 />
...@@ -249,7 +245,6 @@ const DataStudio = (props: any) => { ...@@ -249,7 +245,6 @@ const DataStudio = (props: any) => {
</div> </div>
</PersistGate> </PersistGate>
</PageContainer> </PageContainer>
)
); );
}; };
......
...@@ -124,7 +124,6 @@ export function getLocalTheme(): string { ...@@ -124,7 +124,6 @@ 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.
*/ */
......
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