Skip to content

Commit

Permalink
Merge pull request opensearch-project#10 from ajygupta/pit-poc
Browse files Browse the repository at this point in the history
Pit poc - final
  • Loading branch information
bharath-techie authored May 22, 2023
2 parents bc3a702 + b1ad55d commit 2098ad3
Show file tree
Hide file tree
Showing 26 changed files with 1,358 additions and 790 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export function getSearchParamsFromRequest(
const { getConfig } = dependencies;
const searchParams = getSearchParams(getConfig);

if (searchRequest.body.pit) {
delete searchParams.preference;
return {
body: searchRequest.body,
...searchParams,
};
}
return {
index: searchRequest.index.title || searchRequest.index,
body: searchRequest.body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getDefaultSearchParams(uiSettingsClient: IUiSettingsClient
maxConcurrentShardRequests:
maxConcurrentShardRequests > 0 ? maxConcurrentShardRequests : undefined,
ignoreThrottled,
ignoreUnavailable: true, // Don't fail if the index/indices don't exist
ignoreUnavailable: false, // Don't fail if the index/indices don't exist
trackTotalHits: true,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export const opensearchSearchStrategyProvider = (
...request.params,
});

console.log('This is the params', params);

if (params.body.pit) {
delete params.ignore_unavailable;
}
try {
const client = await decideClient(context, request);
const promise = shimAbortSignal(client.search(params), options?.abortSignal);
Expand Down
168 changes: 134 additions & 34 deletions src/plugins/discover/public/application/angular/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const {
toastNotifications,
uiSettings: config,
visualizations,
savedObjectsClient,
} = getServices();

import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../helpers/breadcrumbs';
Expand All @@ -102,6 +103,33 @@ const fetchStatuses = {

const app = getAngularModule();

export async function getpits(savedObjectsClient) {
return (
savedObjectsClient
.find({
type: 'point-in-time',
perPage: 10000,
})
.then((response) =>
response.savedObjects
.map((pattern) => {
return {
...pattern,
};
})
.sort((a, b) => {
if (a.sort < b.sort) {
return -1;
} else if (a.sort > b.sort) {
return 1;
} else {
return 0;
}
})
) || []
);
}

app.config(($routeProvider) => {
const defaults = {
requireDefaultIndex: true,
Expand Down Expand Up @@ -129,14 +157,14 @@ app.config(($routeProvider) => {
template: indexTemplateLegacy,
reloadOnSearch: false,
resolve: {
savedObjects: function ($route, Promise) {
savedObjects: async function ($route, Promise) {
const history = getHistory();
const savedSearchId = $route.current.params.id;
return data.indexPatterns.ensureDefaultIndexPattern(history).then(() => {
return await data.indexPatterns.ensureDefaultIndexPattern(history).then(async () => {
const { appStateContainer } = getState({ history });
const { index } = appStateContainer.getState();
return Promise.props({
ip: indexPatterns.getCache().then((indexPatternList) => {
const { index, pitid } = appStateContainer.getState();
return await Promise.props({
ip: await indexPatterns.getCache().then(async (indexPatternList) => {
/**
* In making the indexPattern modifiable it was placed in appState. Unfortunately,
* the load order of AppState conflicts with the load order of many other things
Expand All @@ -154,6 +182,10 @@ app.config(($routeProvider) => {
stateValFound: !!index && id === index,
});
}),
pit: await Promise.props({
list: await getpits(savedObjectsClient, history),
pitid: pitid,
}),
savedSearch: getServices()
.getSavedSearchById(savedSearchId)
.then((savedSearch) => {
Expand Down Expand Up @@ -209,6 +241,7 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
const savedSearch = $route.current.locals.savedObjects.savedSearch;
$scope.searchSource = savedSearch.searchSource;
$scope.indexPattern = resolveIndexPatternLoading();
$scope.selectedPointInTime = $route.current.locals.savedObjects.pit.pitid || undefined;
//used for functional testing
$scope.fetchCounter = 0;

Expand Down Expand Up @@ -239,7 +272,7 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
setAppState({ index: $scope.indexPattern.id });
}
$scope.state = { ...appStateContainer.getState() };

console.log("state is", $scope.state);
// syncs `_g` portion of url with query services
const { stop: stopSyncingGlobalStateWithUrl } = syncQueryStateWithUrl(
data.query,
Expand Down Expand Up @@ -294,20 +327,52 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
});

$scope.setIndexPattern = async (id) => {
const nextIndexPattern = await indexPatterns.get(id);
if (nextIndexPattern) {
const nextAppState = getSwitchIndexPatternAppState(
$scope.indexPattern,
nextIndexPattern,
$scope.state.columns,
$scope.state.sort,
config.get(MODIFY_COLUMNS_ON_SWITCH)
);
await replaceUrlAppState(nextAppState);
$route.reload();
try {
const nextIndexPattern = await indexPatterns.get(id);
if (nextIndexPattern) {
const nextAppState = getSwitchIndexPatternAppState(
$scope.indexPattern,
nextIndexPattern,
$scope.state.columns,
$scope.state.sort,
config.get(MODIFY_COLUMNS_ON_SWITCH)
);
await replaceUrlAppState(nextAppState);
$route.reload();
}
} catch (e) {
const nextIndexPattern = await indexPatterns.get(id);
if (nextIndexPattern) {
const nextAppState = getSwitchIndexPatternAppState(
$scope.indexPattern,
nextIndexPattern,
$scope.state.columns,
$scope.state.sort,
config.get(MODIFY_COLUMNS_ON_SWITCH)
);
await replaceUrlAppState(nextAppState);
$route.reload();
}
}
};

$scope.setPointInTime = async (id) => {
console.log('This is the pit list', $route.current.locals.savedObjects.pit.list, id);
const pitList = await $route.current.locals.savedObjects.pit.list;
const nextPit = pitList.find((pit) => pit.attributes.pit_id === id);
console.log(nextPit);
const nextAppState = getSwitchIndexPatternAppState(
$scope.indexPattern,
nextPit,
$scope.state.columns,
$scope.state.sort,
config.get(MODIFY_COLUMNS_ON_SWITCH)
);
await replaceUrlAppState(nextAppState);
await $route.reload();
};


// update data source when filters update
subscriptions.add(
subscribeWithScope(
Expand Down Expand Up @@ -627,6 +692,7 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
timefield: getTimeField(),
savedSearch: savedSearch,
indexPatternList: $route.current.locals.savedObjects.ip.list,
pointInTimeList: $route.current.locals.savedObjects.pit.list,
config: config,
fixedScroll: createFixedScroll($scope, $timeout),
setHeaderActionMenu: getHeaderActionMenuMounter(),
Expand Down Expand Up @@ -805,26 +871,55 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
if (abortController) abortController.abort();
abortController = new AbortController();

$scope
.updateDataSource()
.then(setupVisualization)
.then(function () {
$scope.fetchStatus = fetchStatuses.LOADING;
logInspectorRequest();
return $scope.searchSource.fetch({
abortSignal: abortController.signal,
});
if ($scope.selectedPointInTime != null) {
$scope.updateDataSource().then(setupVisualization).
then(async function () {
$scope.fetchStatus = fetchStatuses.LOADING;
logInspectorRequest();
const search_source_local = _.cloneDeep($scope.searchSource);
console.log("this is local search source");
const point_in_time_object = $scope.opts.pointInTimeList.find((object) => object.id === $scope.selectedPointInTime);
const pit_object = {
pit: {
id: point_in_time_object.attributes.pit_id,
keep_alive: '1m',
},
};
const pit_json = JSON.parse(JSON.stringify(pit_object));
search_source_local.fields = { ...search_source_local.fields, ...pit_json};
delete search_source_local.fields.index;
return await search_source_local.fetch({
abortSignal: abortController.signal,
});
})
.then(onResults)
.catch((error) => {
// If the request was aborted then no need to surface this error in the UI
if (error instanceof Error && error.name === 'AbortError') return;
.then(onResults).catch((error) => {
if (error instanceof Error && error.name === 'AbortError') return;
$scope.fetchStatus = fetchStatuses.NO_RESULTS;
$scope.rows = [];
data.search.showError(error);
});
} else {
$scope
.updateDataSource()
.then(setupVisualization)
.then(function () {
$scope.fetchStatus = fetchStatuses.LOADING;
logInspectorRequest();
return $scope.searchSource.fetch({
abortSignal: abortController.signal,
});
})
.then(onResults)
.catch((error) => {
// If the request was aborted then no need to surface this error in the UI
if (error instanceof Error && error.name === 'AbortError') return;

$scope.fetchStatus = fetchStatuses.NO_RESULTS;
$scope.rows = [];
$scope.fetchStatus = fetchStatuses.NO_RESULTS;
$scope.rows = [];

data.search.showError(error);
});
data.search.showError(error);
});
}
};

$scope.handleRefresh = function (_payload, isUpdate) {
Expand Down Expand Up @@ -876,6 +971,7 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
}

function onResults(resp) {
console.log('response after fetching the results', resp);
inspectorRequest.stats(getResponseInspectorStats(resp, $scope.searchSource)).ok({ json: resp });

if (getTimeField()) {
Expand All @@ -891,6 +987,10 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
$scope.hits = resp.hits.total;
$scope.rows = resp.hits.hits;

// debugger;
// console.log($scope.hits);
// console.log($scope.rows);

// if we haven't counted yet, reset the counts
const counts = ($scope.fieldCounts = $scope.fieldCounts || {});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
saved-search="savedSearch"
search-source="searchSource"
set-index-pattern="setIndexPattern"
set-point-in-time="setPointInTime"
selected-point-in-time="selectedPointInTime"
show-save-query="showSaveQuery"
state="state"
time-filter-update-handler="timefilterUpdateHandler"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export function createDiscoverLegacyDirective(reactDirective: any) {
['savedSearch', { watchDepth: 'reference' }],
['searchSource', { watchDepth: 'reference' }],
['setIndexPattern', { watchDepth: 'reference' }],
['setPointInTime', { watchDepth: 'reference' }],
['selectedPointInTime', { watchDepth: 'reference' }],
['showSaveQuery', { watchDepth: 'reference' }],
['state', { watchDepth: 'reference' }],
['timefilterUpdateHandler', { watchDepth: 'reference' }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export interface DiscoverLegacyProps {
savedSearch: SavedSearch;
config: IUiSettingsClient;
indexPatternList: Array<SavedObject<IndexPatternAttributes>>;
pointInTimeList: any;
timefield: string;
sampleSize: number;
fixedScroll: (el: HTMLElement) => void;
Expand All @@ -99,6 +100,8 @@ export interface DiscoverLegacyProps {
updateQuery: (payload: { dateRange: TimeRange; query?: Query }, isUpdate?: boolean) => void;
updateSavedQueryId: (savedQueryId?: string) => void;
vis?: Vis;
selectedPointInTime: any;
setPointInTime: (id: string) => void;
}

export function DiscoverLegacy({
Expand Down Expand Up @@ -130,17 +133,26 @@ export function DiscoverLegacy({
updateQuery,
updateSavedQueryId,
vis,
selectedPointInTime,
setPointInTime,
}: DiscoverLegacyProps) {
const [isSidebarClosed, setIsSidebarClosed] = useState(false);
const { TopNavMenu } = getServices().navigation.ui;
const { savedSearch, indexPatternList } = opts;
const { savedSearch, indexPatternList, pointInTimeList } = opts;
const bucketAggConfig = vis?.data?.aggs?.aggs[1];
const bucketInterval =
bucketAggConfig && search.aggs.isDateHistogramBucketAggConfig(bucketAggConfig)
? bucketAggConfig.buckets?.getInterval()
: undefined;
const [fixedScrollEl, setFixedScrollEl] = useState<HTMLElement | undefined>();

// The selected Point in time here is the id of the saved object, We will be passing the complete object later
if (selectedPointInTime != null) {
selectedPointInTime = pointInTimeList.find(
(pattern) => pattern.id === selectedPointInTime
);
}

useEffect(() => (fixedScrollEl ? opts.fixedScroll(fixedScrollEl) : undefined), [
fixedScrollEl,
opts,
Expand Down Expand Up @@ -195,11 +207,14 @@ export function DiscoverLegacy({
fieldCounts={fieldCounts}
hits={rows}
indexPatternList={indexPatternList}
pointInTimeList={pointInTimeList}
onAddField={addColumn}
onAddFilter={onAddFilter}
onRemoveField={onRemoveColumn}
selectedIndexPattern={searchSource && searchSource.getField('index')}
setIndexPattern={setIndexPattern}
selectedPointInTime={selectedPointInTime} // currently search source do not support pit hence passing it in props
setPointInTime={setPointInTime} // here we have the complete object
/>
</div>
)}
Expand Down
Loading

0 comments on commit 2098ad3

Please sign in to comment.