Skip to content

Commit

Permalink
Add Studio V2 store with common, console and plugin reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
GnsP committed Jan 17, 2025
1 parent 46e0c36 commit 6f82a53
Show file tree
Hide file tree
Showing 14 changed files with 910 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/cdap/api/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import DataSourceConfigurer from 'services/datasource/DataSourceConfigurer';
import { apiCreator } from 'services/resource-helper';

const dataSrc = DataSourceConfigurer.getInstance();
const basePath = '/namespaces/:namespace/configuration';
const userSettingsPath = `${basePath}/user`;

export const SettingsApi = {
getUserSettings: apiCreator(dataSrc, 'GET', 'REQUEST', `${userSettingsPath}`),
updateUserSettings: apiCreator(dataSrc, 'PUT', 'REQUEST', `${userSettingsPath}`),
};
74 changes: 74 additions & 0 deletions app/cdap/components/StudioV2/store/common/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import { MyArtifactApi } from 'api/artifact';
import { IArtifactSummary, ILabeledArtifactSummary } from 'components/StudioV2/types';
import { getArtifactDisaplayName } from 'components/StudioV2/utils/artifactUtils';
import { getCurrentNamespace } from 'services/NamespaceStore';
import { Theme } from 'services/ThemeHelper';
import VersionStore from 'services/VersionStore';
import { GLOBALS } from 'services/global-constants';
import StudioV2Store from '..';

const PREFIX = 'COMMON_ACTIONS';

export const CommonActions = {
SET_ARTIFACTS: `${PREFIX}/SET_ARTIFACTS`,
SET_SELECTED_ARTIFACT: `${PREFIX}/SET_SELECTED_ARTIFACT`,
};

export const fetchSystemArtifacts = () => {
const cdapVersion = VersionStore.getState().version;
const namespace = getCurrentNamespace();

const uiSupportedArtifacts = [GLOBALS.etlDataPipeline];
if (Theme.showRealtimePipeline !== false) {
uiSupportedArtifacts.push(GLOBALS.etlDataStreams);
}
if (Theme.showSqlPipeline !== false) {
uiSupportedArtifacts.push(GLOBALS.eltSqlPipeline);
}

MyArtifactApi.listScopedArtifacts({
namespace,
scope: 'SYSTEM',
}).subscribe((res: IArtifactSummary[]) => {
if (!res.length) {
return;
}

const filteredArtifacts = res.filter(
(artifact) => artifact.version === cdapVersion && uiSupportedArtifacts.includes(artifact.name)
);

const labeledArtifacts = filteredArtifacts.map((artifact) => ({
...artifact,
label: getArtifactDisaplayName(artifact.name),
}));

StudioV2Store.dispatch({
type: CommonActions.SET_ARTIFACTS,
payload: labeledArtifacts,
});
});
};

export const setSelectedArtifact = (artifact: ILabeledArtifactSummary) => {
StudioV2Store.dispatch({
type: CommonActions.SET_SELECTED_ARTIFACT,
payload: artifact,
});
};
48 changes: 48 additions & 0 deletions app/cdap/components/StudioV2/store/common/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import { ILabeledArtifactSummary } from 'components/StudioV2/types';
import { getArtifactDisaplayName } from 'components/StudioV2/utils/artifactUtils';
import { GLOBALS } from 'services/global-constants';
import { CommonActions } from './actions';

interface ICommonState {
artifacts: ILabeledArtifactSummary[];
selectedArtifact: ILabeledArtifactSummary;
}

export const defaultSelectedArtifact: ILabeledArtifactSummary = {
name: GLOBALS.etlDataPipeline,
version: '',
scope: 'SYSTEM',
label: getArtifactDisaplayName(GLOBALS.etlDataPipeline),
};

export const commonDefaultInitialState: ICommonState = {
artifacts: [],
selectedArtifact: defaultSelectedArtifact,
};

export const common = (state: ICommonState = commonDefaultInitialState, action?): ICommonState => {
switch (action.type) {
case CommonActions.SET_ARTIFACTS:
return { ...state, artifacts: action.payload };
case CommonActions.SET_SELECTED_ARTIFACT:
return { ...state, selectedArtifact: action.payload };
default:
return state;
}
};
45 changes: 45 additions & 0 deletions app/cdap/components/StudioV2/store/console/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import StudioV2Store from '..';

const PREFIX = 'CONSOLE_ACTIONS';

export const ConsoleActions = {
ADD_MESSAGE: `${PREFIX}/ADD_MESSAGE`,
ADD_MESSAGES: `${PREFIX}/ADD_MESSAGES`,
RESET: `${PREFIX}/RESET`,
};

export function resetConsoleMessages() {
StudioV2Store.dispatch({
type: ConsoleActions.RESET,
});
}

export function addConsoleMessage(payload) {
StudioV2Store.dispatch({
type: ConsoleActions.ADD_MESSAGE,
payload,
});
}

export function addConsoleMessages(payload) {
StudioV2Store.dispatch({
type: ConsoleActions.ADD_MESSAGES,
payload,
});
}
44 changes: 44 additions & 0 deletions app/cdap/components/StudioV2/store/console/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import { ConsoleActions } from './actions';

interface IConsoleState {
messages: any[];
}

export const consoleInitialState: IConsoleState = {
messages: [],
};

export const consoleReducer = (
state: IConsoleState = consoleInitialState,
action?
): IConsoleState => {
switch (action.type) {
case ConsoleActions.ADD_MESSAGE:
return { ...state, messages: [...state.messages, action.payload] };

case ConsoleActions.ADD_MESSAGES:
return { ...state, messages: action.payload };

case ConsoleActions.RESET:
return { ...consoleInitialState };

default:
return state;
}
};
38 changes: 38 additions & 0 deletions app/cdap/components/StudioV2/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import { combineReducers, createStore } from 'redux';
import { common, commonDefaultInitialState } from './common/reducer';
import { consoleReducer, consoleInitialState } from './console/reducer';
import { plugins, pluginsInitialState } from './plugins/reducer';

const defaultInitialState = {
common: commonDefaultInitialState,
console: consoleInitialState,
plugins: pluginsInitialState,
};

const StudioV2Store = createStore(
combineReducers({
common,
console: consoleReducer,
plugins,
}),
defaultInitialState,
(window as any).__REDUX_DEVTOOLS_EXTENSION__ && (window as any).__REDUX_DEVTOOLS_EXTENSION__()
);

export default StudioV2Store;
89 changes: 89 additions & 0 deletions app/cdap/components/StudioV2/store/plugins/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import { GLOBALS } from 'services/global-constants';
import { getPluginIcon } from 'components/StudioV2/utils/pluginUtils';
import MySettingsService from 'components/StudioV2/utils/settings';
import { IPlugin } from 'components/StudioV2/types';
import StudioV2Store from '..';

const PREFIX = 'PLUGINS_ACTIONS';

export const PluginsActions = {
FETCH_PLUGINS: `${PREFIX}/FETCH_PLUGINS`,
FETCH_ALL_PLUGINS: `${PREFIX}/FETCH_ALL_PLUGINS`,
FETCH_PLUGIN_TEMPLATE: `${PREFIX}/FETCH_PLUGIN_TEMPLATE`,
FETCH_PLUGINS_DEFAULT_VERSIONS: `${PREFIX}/FETCH_PLUGINS_DEFAULT_VERSIONS`,
UPDATE_PLUGINS_DEFAULT_VERSIONS: `${PREFIX}/UPDATE_PLUGINS_DEFAULT_VERSIONS`,
CHECK_AND_UPDATE_PLUGIN_DEFAULT_VERSION: `${PREFIX}/CHECK_AND_UPDATE_PLUGIN_DEFAULT_VERSION`,
FETCH_EXTENSIONS: `${PREFIX}/FETCH_EXTENSIONS`,
RESET: `${PREFIX}/RESET`,
};

export const getTemplatesWithAddedInfo = (templates = [], extension = '') =>
templates.map((template) => ({
...template,
nodeClass: 'plugin-templates',
name: template.pluginTemplate,
pluginName: template.pluginName,
type: extension,
icon: getPluginIcon(template.pluginName),
allArtifacts: [template.artifact],
}));

export const keepUiSupportedExtensions = (pipelineType: string) => (extension: string) => {
const extensionMap = GLOBALS.pluginTypes[pipelineType];
return Object.keys(extensionMap).filter((ext) => extensionMap[ext] === extension).length;
};

export async function fetchPluginsDefaultVersions() {
try {
const pluginDefalutVersion = await MySettingsService.getInstance().get(
'plugin-default-version'
);
if (!pluginDefalutVersion) {
return;
}

StudioV2Store.dispatch({
type: PluginsActions.FETCH_PLUGINS_DEFAULT_VERSIONS,
payload: pluginDefalutVersion,
});
} catch (err) {
return;
}
}

export async function updatePluginDefaultVersion(plugin: IPlugin) {
try {
let pluginDefalutVersion =
(await MySettingsService.getInstance().get('plugin-default-version')) || {};
const key = `${plugin.name}-${plugin.type}-${plugin.artifact.name}`;
pluginDefalutVersion[key] = plugin.artifact;
await MySettingsService.getInstance().set('plugin-default-version', pluginDefalutVersion);
pluginDefalutVersion = await MySettingsService.getInstance().get('plugin-default-version');

if (!pluginDefalutVersion) {
return;
}
StudioV2Store.dispatch({
type: PluginsActions.FETCH_PLUGINS_DEFAULT_VERSIONS,
payload: pluginDefalutVersion,
});
} catch (err) {
return;
}
}
Loading

0 comments on commit 6f82a53

Please sign in to comment.