-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
194 additions
and
38 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
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
136 changes: 136 additions & 0 deletions
136
frontend/packages/kubevirt-plugin/src/components/vms/vm-users.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,136 @@ | ||
import * as React from 'react'; | ||
import * as classNames from 'classnames'; | ||
import { sortable } from '@patternfly/react-table'; | ||
import { fromNow } from '@console/internal/components/utils/datetime'; | ||
import { Table, TableRow, TableData } from '@console/internal/components/factory'; | ||
import { Timestamp } from '@console/internal/components/utils/timestamp'; | ||
import { | ||
useURLPoll, | ||
URL_POLL_DEFAULT_DELAY, | ||
} from '@console/internal/components/utils/url-poll-hook'; | ||
import { StatusItem } from '@console/shared/src/components/dashboard/status-card/AlertItem'; | ||
import { BlueInfoCircleIcon } from '@console/shared/src/components/status'; | ||
import { | ||
VIRTUAL_MACHINE_IS_NOT_RUNNING, | ||
NO_GUEST_AGENT_MESSAGE, | ||
} from '../../constants/vm/constants'; | ||
import { VMStatus } from '../../constants/vm/vm-status'; | ||
import { isGuestAgentInstalled } from '../dashboards-page/vm-dashboard/vm-alerts'; | ||
import { VMStatusBundle } from '../../statuses/vm/types'; | ||
import { VMIKind } from '../../types'; | ||
import { getVMIApiPath, getVMISubresourcePath } from '../../selectors/vmi/selectors'; | ||
|
||
const guestAgentURL = (vmi: VMIKind) => | ||
vmi && | ||
isGuestAgentInstalled(vmi) && | ||
`/${getVMISubresourcePath()}/${getVMIApiPath(vmi)}/guestosinfo`; | ||
|
||
const tableColumnClasses = [ | ||
classNames('col-lg-3', 'col-md-3', 'col-sm-4', 'col-sm-4'), | ||
classNames('col-lg-3', 'col-md-3', 'col-sm-4', 'col-sm-4'), | ||
classNames('col-lg-3', 'col-md-3', 'col-sm-4', 'col-sm-4'), | ||
classNames('col-lg-3', 'col-md-3', 'hidden-sm', 'hidden-xs'), | ||
]; | ||
|
||
const UsersTableHeader = () => { | ||
return [ | ||
{ | ||
title: 'User Name', | ||
sortField: 'metadata.userName', | ||
transforms: [sortable], | ||
props: { className: tableColumnClasses[0] }, | ||
}, | ||
{ | ||
title: 'Domain', | ||
sortField: 'metadata.domain', | ||
transforms: [sortable], | ||
props: { className: tableColumnClasses[1] }, | ||
}, | ||
{ | ||
title: 'Login time', | ||
sortField: 'metadata.loginTime', | ||
transforms: [sortable], | ||
props: { className: tableColumnClasses[2] }, | ||
}, | ||
{ | ||
title: 'Elapsed logged in time', | ||
props: { className: tableColumnClasses[3] }, | ||
}, | ||
]; | ||
}; | ||
UsersTableHeader.displayName = 'UsersTableHeader'; | ||
|
||
const UsersTableRow = ({ obj: user, index, key, style }) => { | ||
return ( | ||
<TableRow id={user?.metadata?.uid} index={index} trKey={key} style={style}> | ||
<TableData className={tableColumnClasses[0]}>{user?.metadata?.userName}</TableData> | ||
<TableData className={classNames(tableColumnClasses[1], 'co-break-word')}> | ||
{user?.metadata?.domain} | ||
</TableData> | ||
<TableData className={tableColumnClasses[2]}> | ||
<Timestamp timestamp={new Date(user?.metadata?.loginTime).toUTCString()} /> | ||
</TableData> | ||
<TableData className={tableColumnClasses[3]}>{fromNow(user?.metadata?.loginTime)}</TableData> | ||
</TableRow> | ||
); | ||
}; | ||
|
||
export const VMUsersList: React.FC<VMUsersListProps> = ({ | ||
vmi, | ||
vmStatusBundle, | ||
delay = URL_POLL_DEFAULT_DELAY, | ||
}) => { | ||
const [response, error, loading] = useURLPoll<VirtualMachineInstanceGuestAgentInfo>( | ||
guestAgentURL(vmi), | ||
delay, | ||
); | ||
|
||
if (vmStatusBundle.status !== VMStatus.RUNNING) { | ||
return <div className="text-center">{VIRTUAL_MACHINE_IS_NOT_RUNNING}</div>; | ||
} | ||
|
||
if (!isGuestAgentInstalled(vmi)) { | ||
return <StatusItem Icon={BlueInfoCircleIcon} message={NO_GUEST_AGENT_MESSAGE} />; | ||
} | ||
|
||
const data = | ||
response && | ||
response?.userList && | ||
response?.userList.map((user, uid) => ({ | ||
metadata: { | ||
uid, | ||
userName: user?.userName, | ||
domain: user?.domain, | ||
loginTime: user?.loginTime && user.loginTime * 1000, | ||
}, | ||
})); | ||
|
||
return ( | ||
<Table | ||
aria-label="Users" | ||
Header={UsersTableHeader} | ||
Row={UsersTableRow} | ||
data={data} | ||
loadError={error?.message} | ||
loaded={!loading} | ||
EmptyMsg={() => <div className="text-center">No Logged In Users</div>} | ||
virtualize | ||
/> | ||
); | ||
}; | ||
|
||
type VirtualMachineInstanceGuestOSUser = { | ||
userName: string; | ||
domain?: string; | ||
loginTime?: number; | ||
}; | ||
|
||
type VirtualMachineInstanceGuestAgentInfo = { | ||
userList?: VirtualMachineInstanceGuestOSUser[]; | ||
}; | ||
|
||
type VMUsersListProps = { | ||
vmi?: VMIKind; | ||
vmStatusBundle?: VMStatusBundle; | ||
delay?: number; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
import { useCallback, useState } from 'react'; | ||
import { usePoll } from './poll-hook'; | ||
import { useSafeFetch } from './safe-fetch-hook'; | ||
|
||
export const URL_POLL_DEFAULT_DELAY = 15000; // 15 seconds | ||
|
||
export const useURLPoll = <R>( | ||
url: string, | ||
delay = URL_POLL_DEFAULT_DELAY, | ||
...dependencies: any[] | ||
): URLPoll<R> => { | ||
const [error, setError] = useState(); | ||
const [response, setResponse] = useState<R>(); | ||
const [loading, setLoading] = useState(true); | ||
const safeFetch = useSafeFetch(); | ||
const tick = useCallback(() => { | ||
if (url) { | ||
safeFetch(url) | ||
.then((data) => { | ||
setResponse(data); | ||
setError(undefined); | ||
setLoading(false); | ||
}) | ||
.catch((err) => { | ||
if (err.name !== 'AbortError') { | ||
setError(err); | ||
setLoading(false); | ||
// eslint-disable-next-line no-console | ||
console.error(`Error polling URL: ${err}`); | ||
} | ||
}); | ||
} else { | ||
setLoading(false); | ||
} | ||
}, [url]); | ||
|
||
usePoll(tick, delay, ...dependencies); | ||
|
||
return [response, error, loading]; | ||
}; | ||
|
||
export type URLPoll<R> = [R, any, boolean]; |