-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(System): introduce new switch leader button
- Loading branch information
Showing
10 changed files
with
310 additions
and
5 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -166,4 +166,5 @@ export enum YTApiId { | |
removeMaintenance, | ||
maintenanceRequests, | ||
getQueryTrackerInfo, | ||
switchLeader, | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/ui/src/ui/pages/components/SwitchLeaderShortInfo/SwitchLeaderShortInfo.scss
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,13 @@ | ||
.switch-leader-short-info { | ||
&__state { | ||
text-transform: capitalize; | ||
|
||
&_state_complete { | ||
color: var(--success-color); | ||
} | ||
} | ||
|
||
&__value { | ||
text-transform: capitalize; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/ui/src/ui/pages/components/SwitchLeaderShortInfo/SwitchLeaderShortInfo.tsx
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,89 @@ | ||
import React, {useEffect, useRef, useState} from 'react'; | ||
import cn from 'bem-cn-lite'; | ||
|
||
import MetaTable from '../../../components/MetaTable/MetaTable'; | ||
// @ts-ignore | ||
import format from '../../../common/hammer/format'; | ||
import {getStateForHost, loadMasters} from '../../../store/actions/system/masters'; | ||
import {useDispatch} from 'react-redux'; | ||
import moment from 'moment'; | ||
import './SwitchLeaderShortInfo.scss'; | ||
|
||
const block = cn('switch-leader-short-info'); | ||
|
||
interface Props { | ||
newLeaderAddress: string; | ||
} | ||
|
||
export function SwitchLeaderShortInfo(props: Props) { | ||
const startTime = useRef(moment.now()); | ||
const [currentTime, setCurrentTime] = useState<any>(moment()); | ||
const [finishTime, setFinishTime] = useState<any>(); | ||
const dispatch = useDispatch(); | ||
|
||
useEffect(() => { | ||
const intervalId = setInterval(() => { | ||
setCurrentTime(moment()); | ||
|
||
if (finishTime) { | ||
clearInterval(intervalId); | ||
} | ||
}, 1 * 1000); | ||
|
||
return () => { | ||
clearInterval(intervalId); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
let stillMounted = true; | ||
|
||
const waitForState = async () => { | ||
try { | ||
const hostState = await getStateForHost(props.newLeaderAddress); | ||
|
||
if (hostState === 'leading') { | ||
setFinishTime(moment()); | ||
dispatch(loadMasters()); | ||
} | ||
} catch { | ||
if (stillMounted) { | ||
waitForState(); | ||
} | ||
} | ||
}; | ||
|
||
waitForState(); | ||
|
||
return () => { | ||
stillMounted = false; | ||
}; | ||
}, [props.newLeaderAddress]); | ||
|
||
return ( | ||
<div className={block()}> | ||
<MetaTable | ||
items={[ | ||
{ | ||
key: 'Duration', | ||
value: format.TimeDuration( | ||
(finishTime || currentTime).diff(startTime.current), | ||
), | ||
}, | ||
{ | ||
key: 'Status', | ||
value: ( | ||
<SwitchLeaderShortInfoStatus | ||
state={finishTime ? 'complete' : 'in progress'} | ||
/> | ||
), | ||
}, | ||
]} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function SwitchLeaderShortInfoStatus({state}: {state: string}) { | ||
return <span className={block('state', {state})}>{state}</span>; | ||
} |
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
147 changes: 147 additions & 0 deletions
147
packages/ui/src/ui/pages/system/Masters/SwitchLeader.tsx
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,147 @@ | ||
import React, {useState} from 'react'; | ||
import Icon from '../../../components/Icon/Icon'; | ||
import Button from '../../../components/Button/Button'; | ||
import {YTApiId, ytApiV4Id} from '../../../rum/rum-wrap-api'; | ||
import {wrapApiPromiseByToaster} from '../../../utils/utils'; | ||
import {YTDFDialog, makeErrorFields} from '../../../components/Dialog/Dialog'; | ||
import {SwitchLeaderShortInfo} from '../../../pages/components/SwitchLeaderShortInfo/SwitchLeaderShortInfo'; | ||
import {AppStoreProvider} from '../../../containers/App/AppStoreProvider'; | ||
|
||
type SwitchLeaderDialogProps = { | ||
cancel: () => void; | ||
confirm: (newLeader: string) => Promise<void>; | ||
visible: boolean; | ||
cellId: string; | ||
hosts: string[]; | ||
leadingHost: string; | ||
}; | ||
|
||
type FormValues = { | ||
leading_primary_master: string[]; | ||
}; | ||
|
||
const SwitchLeaderDialog = (props: SwitchLeaderDialogProps) => { | ||
const [error, setError] = useState(undefined); | ||
|
||
const selectLeadingHostOptions = props.hosts.map((host) => { | ||
return { | ||
value: host, | ||
content: host, | ||
}; | ||
}); | ||
|
||
return ( | ||
<YTDFDialog<FormValues> | ||
visible={props.visible} | ||
headerProps={{ | ||
title: `Switch leader for ${props.cellId}`, | ||
}} | ||
initialValues={{ | ||
leading_primary_master: [props.leadingHost], | ||
}} | ||
fields={[ | ||
{ | ||
type: 'select', | ||
caption: ' Leading primary master', | ||
name: 'leading_primary_master', | ||
required: true, | ||
extras: { | ||
options: selectLeadingHostOptions, | ||
placeholder: 'New leading primary master', | ||
width: 'max', | ||
filterable: true, | ||
}, | ||
}, | ||
...makeErrorFields([error]), | ||
]} | ||
footerProps={{ | ||
textApply: 'Switch leader', | ||
}} | ||
onAdd={(form) => { | ||
const {leading_primary_master} = form.getState().values; | ||
|
||
return props | ||
.confirm(leading_primary_master[0]) | ||
.then(() => { | ||
setError(undefined); | ||
}) | ||
.catch((e) => { | ||
setError(e); | ||
throw e; | ||
}); | ||
}} | ||
onClose={props.cancel} | ||
pristineSubmittable={true} | ||
/> | ||
); | ||
}; | ||
|
||
type SwitchLeaderButtonProps = { | ||
className: string; | ||
cellId: string; | ||
hosts: string[]; | ||
leadingHost: string; | ||
}; | ||
|
||
export const SwitchLeaderButton = ({ | ||
cellId, | ||
hosts, | ||
leadingHost, | ||
className, | ||
}: SwitchLeaderButtonProps) => { | ||
const [visible, setVisible] = useState(false); | ||
|
||
const handleClick = () => { | ||
setVisible(true); | ||
}; | ||
|
||
const handleConfirm = async (newLeader: string) => { | ||
const switchLeader = () => { | ||
return ytApiV4Id.switchLeader(YTApiId.switchLeader, { | ||
cell_id: cellId, | ||
new_leader_address: newLeader, | ||
}); | ||
}; | ||
|
||
wrapApiPromiseByToaster(switchLeader(), { | ||
toasterName: 'switch-leader', | ||
successContent() { | ||
return ( | ||
<AppStoreProvider> | ||
<SwitchLeaderShortInfo newLeaderAddress={newLeader} /> | ||
</AppStoreProvider> | ||
); | ||
}, | ||
successTitle: 'Leader switch initiated', | ||
autoHide: false, | ||
}); | ||
|
||
setVisible(false); | ||
}; | ||
|
||
const handleCancel = () => { | ||
setVisible(false); | ||
}; | ||
|
||
return ( | ||
<React.Fragment> | ||
<Button | ||
className={className} | ||
view="flat-secondary" | ||
onClick={handleClick} | ||
withTooltip | ||
tooltipProps={{content: 'Switch leader'}} | ||
> | ||
<Icon awesome="crowndiamond" /> | ||
</Button> | ||
<SwitchLeaderDialog | ||
cellId={cellId} | ||
hosts={hosts} | ||
leadingHost={leadingHost} | ||
confirm={handleConfirm} | ||
cancel={handleCancel} | ||
visible={visible} | ||
/> | ||
</React.Fragment> | ||
); | ||
}; |
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.