Skip to content

Commit

Permalink
fix error that local cluster cannot get version (#606)
Browse files Browse the repository at this point in the history
Signed-off-by: Kama Huang <kamahuan@amazon.com>
  • Loading branch information
kamahuan authored Feb 17, 2025
1 parent c9aa3db commit 39ed7aa
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function ProcessorsList(props: ProcessorsListProps) {
return;
}

if (dataSourceId) {
if (dataSourceId !== undefined) {
getEffectiveVersion(dataSourceId)
.then((ver) => {
setVersion(ver);
Expand Down
4 changes: 4 additions & 0 deletions public/pages/workflows/new_workflow/new_workflow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const initialState = {
presetWorkflows: loadPresetWorkflowTemplates(),
},
workflows: INITIAL_WORKFLOWS_STATE,
opensearch: {
loading: false,
localClusterVersion: null,
},
};

const mockDispatch = jest.fn();
Expand Down
27 changes: 16 additions & 11 deletions public/pages/workflows/new_workflow/new_workflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
MIN_SUPPORTED_VERSION,
MINIMUM_FULL_SUPPORTED_VERSION,
} from '../../../../common/constants';
import { getLocalClusterVersion } from '../../../store/reducers/opensearch_reducer';

interface NewWorkflowProps {}

Expand All @@ -52,7 +53,7 @@ const filterPresetsByVersion = async (
return workflows;
}

if (!dataSourceId) {
if (dataSourceId === undefined) {
return [];
}

Expand Down Expand Up @@ -92,14 +93,18 @@ export function NewWorkflow(props: NewWorkflowProps) {
const dataSourceId = getDataSourceId();
const dataSourceEnabled = getDataSourceEnabled().enabled;
// workflows state
const { presetWorkflows, loading } = useSelector(
const { presetWorkflows, loading: presetsLoading } = useSelector(
(state: AppState) => state.presets
);
const { loading: opensearchLoading, localClusterVersion } = useSelector(
(state: AppState) => state.opensearch
);
const isLoading = presetsLoading || opensearchLoading;

const [allWorkflows, setAllWorkflows] = useState<WorkflowTemplate[]>([]);
const [filteredWorkflows, setFilteredWorkflows] = useState<
WorkflowTemplate[]
>([]);
const [isVersionLoading, setIsVersionLoading] = useState(false);

// search bar state
const [searchQuery, setSearchQuery] = useState<string>('');
Expand All @@ -111,6 +116,7 @@ export function NewWorkflow(props: NewWorkflowProps) {
// 1. fetch the workflow presets persisted on server-side
// 2. fetch the ML models and connectors. these may be used in quick-create views when selecting a preset,
// so we optimize by fetching once at the top-level here.
// 3. fetch local cluster version if applicable
useEffect(() => {
dispatch(getWorkflowPresets());
if (isDataSourceReady(dataSourceId)) {
Expand All @@ -119,6 +125,10 @@ export function NewWorkflow(props: NewWorkflowProps) {
searchConnectors({ apiBody: FETCH_ALL_QUERY_LARGE, dataSourceId })
);
}
// if use local cluster
if (dataSourceId === '') {
dispatch(getLocalClusterVersion());
}
}, [dataSourceId, dataSourceEnabled]);

// initial hook to populate all workflows
Expand All @@ -140,19 +150,15 @@ export function NewWorkflow(props: NewWorkflowProps) {
);
setAllWorkflows(enrichedWorkflows);
setFilteredWorkflows(enrichedWorkflows);
setIsVersionLoading(false);
return;
}

if (!dataSourceId) {
if (dataSourceId === undefined) {
setAllWorkflows([]);
setFilteredWorkflows([]);
setIsVersionLoading(true);
return;
}

setIsVersionLoading(true);

const version = await getEffectiveVersion(dataSourceId);

const enrichedWorkflows = presetWorkflows.map((presetWorkflow) =>
Expand All @@ -166,11 +172,10 @@ export function NewWorkflow(props: NewWorkflowProps) {

setAllWorkflows(versionFilteredWorkflows);
setFilteredWorkflows(versionFilteredWorkflows);
setIsVersionLoading(false);
};

loadWorkflows();
}, [presetWorkflows, dataSourceId, dataSourceEnabled]);
}, [presetWorkflows, dataSourceId, dataSourceEnabled, localClusterVersion]);

// When search query updated, re-filter preset list
useEffect(() => {
Expand All @@ -191,7 +196,7 @@ export function NewWorkflow(props: NewWorkflowProps) {
/>
</EuiFlexItem>
<EuiFlexItem>
{loading || isVersionLoading ? (
{isLoading ? (
<EuiFlexGroup
justifyContent="center"
alignItems="center"
Expand Down
12 changes: 12 additions & 0 deletions public/route_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export interface RouteService {
pipelineId: string,
dataSourceId?: string
) => Promise<any | HttpFetchError>;
getLocalClusterVersion: () => Promise<any | HttpFetchError>;
}

export function configureRoutes(core: CoreStart): RouteService {
Expand Down Expand Up @@ -323,6 +324,17 @@ export function configureRoutes(core: CoreStart): RouteService {
return e as HttpFetchError;
}
},
getLocalClusterVersion: async () => {
try {
const response = await core.http.post('/api/console/proxy', {
query: { path: '/', method: 'GET', dataSourceId: '' },
});
return response.version.number;
} catch (e: any) {
return e as HttpFetchError;
}
},

getIndex: async (index: string, dataSourceId?: string) => {
try {
const url = dataSourceId
Expand Down
35 changes: 35 additions & 0 deletions public/store/reducers/opensearch_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ export const INITIAL_OPENSEARCH_STATE = {
indexDetails: {} as { [key: string]: IndexConfiguration },
ingestPipelineDetails: {} as { [key: string]: IngestPipelineConfig },
searchPipelineDetails: {} as { [key: string]: SearchPipelineConfig },
localClusterVersion: null as string | null,
};

const OPENSEARCH_PREFIX = 'opensearch';
const GET_LOCAL_CLUSTER_VERSION_ACTION = `${OPENSEARCH_PREFIX}/getLocalClusterVersion`;
const SET_OPENSEARCH_ERROR = `${OPENSEARCH_PREFIX}/setError`;
const CAT_INDICES_ACTION = `${OPENSEARCH_PREFIX}/catIndices`;
const GET_MAPPINGS_ACTION = `${OPENSEARCH_PREFIX}/mappings`;
const SEARCH_INDEX_ACTION = `${OPENSEARCH_PREFIX}/search`;
Expand All @@ -42,6 +45,25 @@ const GET_INGEST_PIPELINE_ACTION = `${OPENSEARCH_PREFIX}/getIngestPipeline`;
const GET_SEARCH_PIPELINE_ACTION = `${OPENSEARCH_PREFIX}/getSearchPipeline`;
const GET_INDEX_ACTION = `${OPENSEARCH_PREFIX}/getIndex`;

export const getLocalClusterVersion = createAsyncThunk(
GET_LOCAL_CLUSTER_VERSION_ACTION,
async (_, { rejectWithValue }) => {
try {
const version = await getRouteService().getLocalClusterVersion();
return version;
} catch (error) {
return rejectWithValue('Error getting local cluster version: ' + error);
}
}
);

export const setOpenSearchError = createAsyncThunk(
SET_OPENSEARCH_ERROR,
async ({ error }: { error: string }, { rejectWithValue }) => {
return error;
}
);

export const catIndices = createAsyncThunk(
CAT_INDICES_ACTION,
async (
Expand Down Expand Up @@ -417,6 +439,19 @@ const opensearchSlice = createSlice({
.addCase(bulk.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
})
.addCase(getLocalClusterVersion.pending, (state) => {
state.loading = true;
state.errorMessage = '';
})
.addCase(getLocalClusterVersion.fulfilled, (state, action) => {
state.localClusterVersion = action.payload;
state.loading = false;
state.errorMessage = '';
})
.addCase(getLocalClusterVersion.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
});
},
});
Expand Down
2 changes: 1 addition & 1 deletion public/store/reducers/presets_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ const presetsSlice = createSlice({
state.errorMessage = '';
})
.addCase(getWorkflowPresets.rejected, (state, action) => {
state.errorMessage = action.payload as string;
state.loading = false;
state.errorMessage = action.payload as string;
});
},
});
Expand Down
6 changes: 6 additions & 0 deletions public/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
import {
getCore,
getDataSourceEnabled,
getRouteService,
getSavedObjectsClient,
} from '../services';
import {
Expand Down Expand Up @@ -893,6 +894,11 @@ export const getEffectiveVersion = async (
throw new Error('Data source is required');
}

if (dataSourceId === '') {
// Use route service for local cluster case
return await getRouteService().getLocalClusterVersion();
}

const dataSource = await getSavedObjectsClient().get<DataSourceAttributes>(
'data-source',
dataSourceId
Expand Down

0 comments on commit 39ed7aa

Please sign in to comment.