-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show and modify routing rules from the UI
- Loading branch information
Showing
8 changed files
with
349 additions
and
22 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
gateway-ha/src/main/java/io/trino/gateway/ha/domain/RoutingRules.java
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,38 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package io.trino.gateway.ha.domain; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* RoutingRules | ||
* | ||
* @param name name of the routing rule | ||
* @param description description of the routing rule | ||
* @param priority priority of the routing rule | ||
* @param actions actions of the routing rule | ||
* @param condition condition of the routing rule | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record RoutingRules( | ||
@JsonProperty("name") String name, | ||
@JsonProperty("description") String description, | ||
@JsonProperty("priority") Integer priority, | ||
@JsonProperty("actions") List<String> actions, | ||
@JsonProperty("condition") String condition) | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {api} from "../base"; | ||
import {RoutingRulesData} from "../../types/routing-rules"; | ||
|
||
export async function routingRulesApi(): Promise<any> { | ||
const response = await api.get('/webapp/getRoutingRules'); | ||
return response; | ||
} | ||
|
||
export async function updateRoutingRulesApi(body: Record<string, any>): Promise<RoutingRulesData> { | ||
return api.post('/webapp/updateRoutingRules', body) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import {useEffect, useState} from "react"; | ||
import {routingRulesApi, updateRoutingRulesApi} from "../api/webapp/routing-rules.ts"; | ||
import {RoutingRulesData} from "../types/routing-rules"; | ||
import {Button, Card, Form, Toast} from "@douyinfe/semi-ui"; | ||
import {FormApi} from "@douyinfe/semi-ui/lib/es/form"; | ||
import {Role, useAccessStore} from "../store"; | ||
|
||
export function RoutingRules() { | ||
const [rules, setRules] = useState<RoutingRulesData[]>([]); | ||
const [editingStates, setEditingStates] = useState<boolean[]>([]); | ||
const [formApis, setFormApis] = useState<(FormApi<any> | null)[]>([]); | ||
const access = useAccessStore(); | ||
|
||
useEffect(() => { | ||
fetchRoutingRules(); | ||
}, []); | ||
|
||
const fetchRoutingRules = () => { | ||
routingRulesApi() | ||
.then(data => { | ||
setRules(data); | ||
setEditingStates(new Array(data.length).fill(false)); | ||
setFormApis(new Array(data.length).fill(null)); | ||
}).catch((error) => { | ||
console.error("Error fetching routing rules: ", error); | ||
Toast.error("Failed to fetch routing rules"); | ||
}); | ||
}; | ||
|
||
const handleEdit = (index: number) => { | ||
setEditingStates(prev => { | ||
const newStates = [...prev]; | ||
newStates[index] = true; | ||
return newStates; | ||
}); | ||
}; | ||
|
||
const handleSave = async (index: number) => { | ||
const formApi = formApis[index]; | ||
if (formApi) { | ||
try { | ||
const values = formApi.getValues(); | ||
const actionsArray = Array.isArray(values.actions) | ||
? values.actions.map((action: string) => action.trim()) | ||
: [values.actions.trim()]; | ||
|
||
const updatedRule: RoutingRulesData = { | ||
...rules[index], | ||
...values, | ||
actions: actionsArray | ||
}; | ||
|
||
await updateRoutingRulesApi(updatedRule); | ||
|
||
setEditingStates(prev => { | ||
const newStates = [...prev]; | ||
newStates[index] = false; | ||
return newStates; | ||
}); | ||
|
||
setRules(prev => { | ||
const newRules = [...prev]; | ||
newRules[index] = updatedRule; | ||
return newRules; | ||
}); | ||
|
||
Toast.success("Routing rule updated successfully"); | ||
} catch (error) { | ||
console.error("Error updating routing rule: ", error); | ||
Toast.error("Failed to update routing rule"); | ||
} | ||
} | ||
}; | ||
|
||
const setFormApiForIndex = (index: number) => (api: FormApi<any>) => { | ||
setFormApis(prev => { | ||
const newApis = [...prev]; | ||
newApis[index] = api; | ||
return newApis; | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{rules.map((rule, index) => ( | ||
<div key={index} style={{marginBottom: '20px'}}> | ||
<Card | ||
shadows='always' | ||
title={`Routing rule #${index + 1}`} | ||
style={{maxWidth: 800, padding: 20}} | ||
bordered={false} | ||
headerExtraContent={ | ||
(access.hasRole(Role.ADMIN) && ( | ||
<Button onClick={() => handleEdit(index)}>Edit</Button> | ||
)) | ||
} | ||
footerStyle={{ | ||
display: 'flex', | ||
justifyContent: 'flex-end', | ||
...(editingStates[index] ? {} : { display: 'none' }) | ||
}} | ||
footer={ | ||
(access.hasRole(Role.ADMIN) && ( | ||
<Button onClick={() => handleSave(index)}>Save</Button> | ||
)) | ||
} | ||
> | ||
<Form | ||
labelPosition="left" | ||
labelAlign="left" | ||
labelWidth={150} | ||
style={{ paddingRight: '20px' }} | ||
getFormApi={setFormApiForIndex(index)} | ||
> | ||
<Form.Input | ||
field="name" | ||
label="Name" | ||
style={{ width: 600 }} | ||
rules={[ | ||
{required: true, message: 'required error'}, | ||
{type: 'string', message: 'type error'}, | ||
]} | ||
disabled={true} | ||
initValue={rule.name} | ||
/> | ||
<Form.Input | ||
field="description" | ||
label="Description" | ||
style={{ width: 600 }} | ||
rules={[ | ||
{required: false, message: 'required error'}, | ||
{type: 'string', message: 'type error'}, | ||
]} | ||
disabled={!editingStates[index]} | ||
initValue={rule.description} | ||
/> | ||
<Form.Input | ||
field="priority" | ||
label="Priority" | ||
style={{ width: 600 }} | ||
rules={[ | ||
{required: false, message: 'required error'}, | ||
{type: 'string', message: 'type error'}, | ||
]} | ||
disabled={!editingStates[index]} | ||
initValue={rule.priority} | ||
/> | ||
<Form.Input | ||
field="condition" | ||
label="Condition" | ||
style={{ width: 600 }} | ||
rules={[ | ||
{required: true, message: 'required error'}, | ||
{type: 'string', message: 'type error'}, | ||
]} | ||
disabled={!editingStates[index]} | ||
initValue={rule.condition} | ||
/> | ||
<Form.TextArea | ||
field="actions" | ||
label="Actions" | ||
style={{ width: 600, overflowY: 'auto', overflowX: 'auto' }} | ||
autosize rows={1} | ||
rules={[ | ||
{required: true, message: 'required error'}, | ||
{type: 'string', message: 'type error'}, | ||
]} | ||
disabled={!editingStates[index]} | ||
initValue={rule.actions} | ||
/> | ||
</Form> | ||
</Card> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
} |
Oops, something went wrong.