pierrejeambrun commented on code in PR #68113:
URL: https://github.com/apache/airflow/pull/68113#discussion_r3452658368
##########
airflow-core/src/airflow/ui/src/queries/useTestConnection.ts:
##########
@@ -16,39 +16,81 @@
* specific language governing permissions and limitations
* under the License.
*/
-import type { Dispatch, SetStateAction } from "react";
+import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
-import { useConnectionServiceTestConnection } from "openapi/queries";
-import type { ConnectionTestResponse } from "openapi/requests/types.gen";
+import {
+ useConnectionServiceEnqueueConnectionTest,
+ useConnectionServiceGetConnectionTest,
+} from "openapi/queries";
+import type { ApiError } from "openapi/requests";
+import type { ConnectionTestRequestBody } from "openapi/requests/types.gen";
import { toaster } from "src/components/ui";
-export const useTestConnection = (setConnected:
Dispatch<SetStateAction<boolean | undefined>>) => {
+type ConnectionStatus = boolean | undefined;
+
+const ACTIVE_STATES = new Set(["pending", "queued", "running"]);
+const POLL_INTERVAL = 2000;
+
+const isActive = (state: string | undefined) => state !== undefined &&
ACTIVE_STATES.has(state);
+
+export const useTestConnection = (setConnected: (status: ConnectionStatus) =>
void) => {
const { t: translate } = useTranslation("admin");
+ const [token, setToken] = useState<string | undefined>(undefined);
- const onSuccess = (res: ConnectionTestResponse) => {
- setConnected(res.status);
- if (res.status) {
- toaster.create({
- description: res.message,
- title: translate("connections.testSuccess.title"),
- type: "success",
- });
- } else {
+ const enqueue = useConnectionServiceEnqueueConnectionTest({
+ onError: (error) => {
+ setConnected(false);
toaster.create({
- description: res.message,
- title: translate("connections.testError.title"),
+ title:
+ (error as ApiError).status === 409
+ ? translate("connections.testInProgress.title")
+ : translate("connections.testError.title"),
type: "error",
Review Comment:
You can reuse `createErrorToaster`.
##########
airflow-core/src/airflow/ui/src/queries/useTestConnection.ts:
##########
@@ -16,39 +16,81 @@
* specific language governing permissions and limitations
* under the License.
*/
-import type { Dispatch, SetStateAction } from "react";
+import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
-import { useConnectionServiceTestConnection } from "openapi/queries";
-import type { ConnectionTestResponse } from "openapi/requests/types.gen";
+import {
+ useConnectionServiceEnqueueConnectionTest,
+ useConnectionServiceGetConnectionTest,
+} from "openapi/queries";
+import type { ApiError } from "openapi/requests";
+import type { ConnectionTestRequestBody } from "openapi/requests/types.gen";
import { toaster } from "src/components/ui";
-export const useTestConnection = (setConnected:
Dispatch<SetStateAction<boolean | undefined>>) => {
+type ConnectionStatus = boolean | undefined;
+
+const ACTIVE_STATES = new Set(["pending", "queued", "running"]);
+const POLL_INTERVAL = 2000;
+
+const isActive = (state: string | undefined) => state !== undefined &&
ACTIVE_STATES.has(state);
+
+export const useTestConnection = (setConnected: (status: ConnectionStatus) =>
void) => {
const { t: translate } = useTranslation("admin");
+ const [token, setToken] = useState<string | undefined>(undefined);
- const onSuccess = (res: ConnectionTestResponse) => {
- setConnected(res.status);
- if (res.status) {
- toaster.create({
- description: res.message,
- title: translate("connections.testSuccess.title"),
- type: "success",
- });
- } else {
+ const enqueue = useConnectionServiceEnqueueConnectionTest({
+ onError: (error) => {
+ setConnected(false);
toaster.create({
- description: res.message,
- title: translate("connections.testError.title"),
+ title:
+ (error as ApiError).status === 409
+ ? translate("connections.testInProgress.title")
+ : translate("connections.testError.title"),
type: "error",
});
+ },
+ onSuccess: (response) => {
+ setToken(response.token);
+ },
+ });
+
+ const { data } = useConnectionServiceGetConnectionTest(
+ { airflowConnectionTestToken: token ?? "" },
+ undefined,
+ {
+ enabled: token !== undefined,
+ refetchInterval: (query) => (isActive(query.state.data?.state) ?
POLL_INTERVAL : false),
Review Comment:
Refresh interval should come from `useAutoRefresh`. (To be consistent in
timing with other request in the application)
##########
airflow-core/src/airflow/ui/public/i18n/locales/en/admin.json:
##########
@@ -53,6 +53,9 @@
"testError": {
"title": "Test Connection Failed"
},
+ "testInProgress": {
+ "title": "A connection test is already in progress"
+ },
Review Comment:
Don't need to do this with a specific message and error handler.
Just use a common handler that will display the error message from the
server
```
{
"detail": "An active connection test already exists for connection_id
`test_failing`."
}
```
You can take a look at other similar hooks
##########
airflow-core/src/airflow/ui/src/pages/Connections/TestConnectionButton.tsx:
##########
@@ -60,10 +60,10 @@ const TestConnectionButton = ({ connection }: Props) => {
schema: connection.schema ?? "",
};
- const { isPending, mutate } = useTestConnection((result) => {
+ const { isPending, test } = useTestConnection((result) => {
Review Comment:
`mutate` is fine it's the common convention in the code base.
```suggestion
const { isPending, mutate } = useTestConnection((result) => {
```
##########
airflow-core/src/airflow/ui/src/queries/useTestConnection.ts:
##########
@@ -16,39 +16,81 @@
* specific language governing permissions and limitations
* under the License.
*/
-import type { Dispatch, SetStateAction } from "react";
+import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
-import { useConnectionServiceTestConnection } from "openapi/queries";
-import type { ConnectionTestResponse } from "openapi/requests/types.gen";
+import {
+ useConnectionServiceEnqueueConnectionTest,
+ useConnectionServiceGetConnectionTest,
+} from "openapi/queries";
+import type { ApiError } from "openapi/requests";
+import type { ConnectionTestRequestBody } from "openapi/requests/types.gen";
import { toaster } from "src/components/ui";
-export const useTestConnection = (setConnected:
Dispatch<SetStateAction<boolean | undefined>>) => {
+type ConnectionStatus = boolean | undefined;
+
+const ACTIVE_STATES = new Set(["pending", "queued", "running"]);
+const POLL_INTERVAL = 2000;
Review Comment:
CF comment bellow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]