-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Index Management new platform migration #49359
Merged
alisonelizabeth
merged 22 commits into
elastic:master
from
alisonelizabeth:np_migration/index_management
Nov 15, 2019
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
739edd8
server np migration phase i
alisonelizabeth 82df866
prep client code for np migration
alisonelizabeth 96174a4
create plugin definition for client
alisonelizabeth 90bf7e6
move to use client http service from core
alisonelizabeth e4d273e
move to use chrome service from core
alisonelizabeth e55805a
move to use documentation service from core
alisonelizabeth 04e3001
use notification service from core
alisonelizabeth 16c751d
use i18n context from core
alisonelizabeth e903a50
move ui_metric service to shim
alisonelizabeth c15fe78
fix error messages
alisonelizabeth 1be3a10
fix build errors
alisonelizabeth e31dba7
clean up request logic
alisonelizabeth 534bd0c
fix tests
alisonelizabeth 50db455
fix ilm integration
alisonelizabeth 81d686a
Merge branch 'master' of github.com:elastic/kibana into np_migration/…
alisonelizabeth 9acf906
fix ilm integration, remove unused scss
alisonelizabeth 96156ad
Merge branch 'master' into np_migration/index_management
elasticmachine 7a69c78
Merge branch 'master' into np_migration/index_management
elasticmachine d4dcf72
Merge branch 'master' into np_migration/index_management
elasticmachine 97fd3c8
fix ts
alisonelizabeth 1138eb5
fix ts
alisonelizabeth 46e3795
Merge branch 'master' into np_migration/index_management
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
src/plugins/es_ui_shared/public/request/np_ready_request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 { useEffect, useState, useRef } from 'react'; | ||
|
||
import { HttpServiceBase } from '../../../../../src/core/public'; | ||
|
||
export interface SendRequestConfig { | ||
path: string; | ||
method: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head'; | ||
body?: any; | ||
} | ||
|
||
export interface SendRequestResponse { | ||
data: any; | ||
error: Error | null; | ||
} | ||
|
||
export interface UseRequestConfig extends SendRequestConfig { | ||
pollIntervalMs?: number; | ||
initialData?: any; | ||
deserializer?: (data: any) => any; | ||
} | ||
|
||
export interface UseRequestResponse { | ||
isInitialRequest: boolean; | ||
isLoading: boolean; | ||
error: null | unknown; | ||
data: any; | ||
sendRequest: (...args: any[]) => Promise<SendRequestResponse>; | ||
} | ||
|
||
export const sendRequest = async ( | ||
httpClient: HttpServiceBase, | ||
{ path, method, body }: SendRequestConfig | ||
): Promise<SendRequestResponse> => { | ||
try { | ||
const response = await httpClient[method](path, { body }); | ||
|
||
return { | ||
data: response.data ? response.data : response, | ||
error: null, | ||
}; | ||
} catch (e) { | ||
return { | ||
data: null, | ||
error: e.response && e.response.data ? e.response.data : e.body, | ||
}; | ||
} | ||
}; | ||
|
||
export const useRequest = ( | ||
httpClient: HttpServiceBase, | ||
{ | ||
path, | ||
method, | ||
body, | ||
pollIntervalMs, | ||
initialData, | ||
deserializer = (data: any): any => data, | ||
}: UseRequestConfig | ||
): UseRequestResponse => { | ||
// Main states for tracking request status and data | ||
const [error, setError] = useState<null | any>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
const [data, setData] = useState<any>(initialData); | ||
|
||
// Consumers can use isInitialRequest to implement a polling UX. | ||
const [isInitialRequest, setIsInitialRequest] = useState<boolean>(true); | ||
const pollInterval = useRef<any>(null); | ||
const pollIntervalId = useRef<any>(null); | ||
|
||
// We always want to use the most recently-set interval in scheduleRequest. | ||
pollInterval.current = pollIntervalMs; | ||
|
||
// Tied to every render and bound to each request. | ||
let isOutdatedRequest = false; | ||
|
||
const scheduleRequest = () => { | ||
// Clear current interval | ||
if (pollIntervalId.current) { | ||
clearTimeout(pollIntervalId.current); | ||
} | ||
|
||
// Set new interval | ||
if (pollInterval.current) { | ||
pollIntervalId.current = setTimeout(_sendRequest, pollInterval.current); | ||
} | ||
}; | ||
|
||
const _sendRequest = async () => { | ||
// We don't clear error or data, so it's up to the consumer to decide whether to display the | ||
// "old" error/data or loading state when a new request is in-flight. | ||
setIsLoading(true); | ||
|
||
const requestBody = { | ||
path, | ||
method, | ||
body, | ||
}; | ||
|
||
const response = await sendRequest(httpClient, requestBody); | ||
const { data: serializedResponseData, error: responseError } = response; | ||
const responseData = deserializer(serializedResponseData); | ||
|
||
// If an outdated request has resolved, DON'T update state, but DO allow the processData handler | ||
// to execute side effects like update telemetry. | ||
if (isOutdatedRequest) { | ||
return { data: null, error: null }; | ||
} | ||
|
||
setError(responseError); | ||
setData(responseData); | ||
setIsLoading(false); | ||
setIsInitialRequest(false); | ||
|
||
// If we're on an interval, we need to schedule the next request. This also allows us to reset | ||
// the interval if the user has manually requested the data, to avoid doubled-up requests. | ||
scheduleRequest(); | ||
|
||
return { data: serializedResponseData, error: responseError }; | ||
}; | ||
|
||
useEffect(() => { | ||
_sendRequest(); | ||
// To be functionally correct we'd send a new request if the method, path, or body changes. | ||
// But it doesn't seem likely that the method will change and body is likely to be a new | ||
// object even if its shape hasn't changed, so for now we're just watching the path. | ||
}, [path]); | ||
|
||
useEffect(() => { | ||
scheduleRequest(); | ||
|
||
// Clean up intervals and inflight requests and corresponding state changes | ||
return () => { | ||
isOutdatedRequest = true; | ||
if (pollIntervalId.current) { | ||
clearTimeout(pollIntervalId.current); | ||
} | ||
}; | ||
}, [pollIntervalMs]); | ||
|
||
return { | ||
isInitialRequest, | ||
isLoading, | ||
error, | ||
data, | ||
sendRequest: _sendRequest, // Gives the user the ability to manually request data | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was largely borrowed from https://github.com/elastic/kibana/blob/master/src/plugins/es_ui_shared/public/request/request.ts, but updated to work with the NP http service.