MinionPoolMachines.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. Copyright (C) 2020 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import * as React from "react";
  15. import { Collapse } from "react-collapse";
  16. import { Link } from "react-router-dom";
  17. import styled, { createGlobalStyle, css } from "styled-components";
  18. import { DeploymentItem, ReplicaItem, TransferItem } from "@src/@types/MainItem";
  19. import { MinionMachine, MinionPool } from "@src/@types/MinionPool";
  20. import { ThemePalette, ThemeProps } from "@src/components/Theme";
  21. import Arrow from "@src/components/ui/Arrow";
  22. import DropdownLink from "@src/components/ui/Dropdowns/DropdownLink";
  23. import { ItemReplicaBadge } from "@src/components/ui/Dropdowns/NotificationDropdown";
  24. import StatusPill from "@src/components/ui/StatusComponents/StatusPill";
  25. import DateUtils from "@src/utils/DateUtils";
  26. import networkImage from "./images/network.svg";
  27. const GlobalStyle = createGlobalStyle`
  28. .ReactCollapse--collapse {
  29. transition: height 0.4s ease-in-out;
  30. }
  31. `;
  32. const Wrapper = styled.div``;
  33. const NoMachines = styled.div`
  34. text-align: center;
  35. `;
  36. const Header = styled.div`
  37. display: flex;
  38. align-items: center;
  39. margin-bottom: 32px;
  40. margin-left: 20px;
  41. `;
  42. const ArrowStyled = styled(Arrow)`
  43. position: absolute;
  44. left: -24px;
  45. `;
  46. const Row = styled.div<any>`
  47. position: relative;
  48. padding: 8px 0;
  49. border-top: 1px solid white;
  50. transition: all ${ThemeProps.animations.swift};
  51. &:last-child {
  52. border-bottom: 0;
  53. border-bottom-left-radius: ${ThemeProps.borderRadius};
  54. border-bottom-right-radius: ${ThemeProps.borderRadius};
  55. }
  56. &:hover {
  57. background: ${ThemePalette.grayscale[0]};
  58. ${ArrowStyled} {
  59. opacity: 1;
  60. }
  61. }
  62. cursor: pointer;
  63. `;
  64. const RowHeader = styled.div<any>`
  65. display: flex;
  66. align-items: center;
  67. padding: 0 16px;
  68. `;
  69. const RowHeaderColumn = styled.div<any>`
  70. display: flex;
  71. align-items: center;
  72. ${ThemeProps.exactWidth("50%")}
  73. `;
  74. const HeaderName = styled.div<any>`
  75. overflow: hidden;
  76. text-overflow: ellipsis;
  77. ${props => ThemeProps.exactWidth(`calc(100% - ${props.source ? 120 : 8}px)`)}
  78. `;
  79. const HeaderIcon = styled.div<any>`
  80. min-width: 16px;
  81. min-height: 16px;
  82. background: url("${networkImage}") center no-repeat;
  83. margin-right: 16px;
  84. `;
  85. const HeaderFilter = styled.div``;
  86. const HeaderText = styled.div`
  87. margin-left: 16px;
  88. `;
  89. const RowBody = styled.div<any>`
  90. display: flex;
  91. color: ${ThemePalette.grayscale[5]};
  92. padding: 0 16px;
  93. margin-top: 4px;
  94. `;
  95. const RowBodyColumn = styled.div<any>`
  96. margin-top: 8px;
  97. &:first-child {
  98. ${ThemeProps.exactWidth("calc(50% - 70px)")}
  99. margin-right: 88px;
  100. }
  101. &:last-child {
  102. ${ThemeProps.exactWidth("calc(50% - 16px)")}
  103. }
  104. `;
  105. const RowBodyColumnValue = styled.div<any>`
  106. overflow-wrap: break-word;
  107. `;
  108. const MachinesWrapper = styled.div``;
  109. const MachineWrapper = styled.div`
  110. background: ${ThemePalette.grayscale[1]};
  111. border-radius: ${ThemeProps.borderRadius};
  112. `;
  113. const MachineTitle = styled.div`
  114. padding: 16px;
  115. border-bottom: 1px solid #7f8795;
  116. font-size: 16px;
  117. `;
  118. const MachineBody = styled.div`
  119. padding: 16px;
  120. `;
  121. const MachineRow = styled.div<{ secondary?: boolean }>`
  122. display: flex;
  123. margin-bottom: 8px;
  124. align-items: center;
  125. ${props =>
  126. props.secondary
  127. ? css`
  128. color: ${ThemePalette.grayscale[5]};
  129. margin-bottom: 4px;
  130. `
  131. : ""}
  132. `;
  133. const ValueLink = styled(Link)`
  134. display: flex;
  135. color: ${ThemePalette.primary};
  136. text-decoration: none;
  137. cursor: pointer;
  138. `;
  139. type FilterType = "all" | "allocated" | "not-allocated";
  140. type Props = {
  141. item?: MinionPool | null;
  142. replicas: ReplicaItem[];
  143. deployments: DeploymentItem[];
  144. };
  145. type State = {
  146. filterStatus: FilterType;
  147. openedRows: string[];
  148. };
  149. class MinionPoolMachines extends React.Component<Props, State> {
  150. state = {
  151. filterStatus: "all" as FilterType,
  152. openedRows: [],
  153. };
  154. get machines() {
  155. return this.props.item?.minion_machines || [];
  156. }
  157. get filteredMachines() {
  158. switch (this.state.filterStatus) {
  159. case "all":
  160. return this.machines;
  161. case "allocated":
  162. return this.machines.filter(
  163. m =>
  164. m.allocation_status === "ALLOCATED" ||
  165. m.allocation_status === "AVAILABLE"
  166. );
  167. default:
  168. return this.machines.filter(
  169. m =>
  170. m.allocation_status !== "ALLOCATED" &&
  171. m.allocation_status !== "AVAILABLE"
  172. );
  173. }
  174. }
  175. handleRowClick(id: string) {
  176. if (this.state.openedRows.find(i => i === id)) {
  177. this.setState(prevState => ({
  178. openedRows: prevState.openedRows.filter(i => i !== id),
  179. }));
  180. } else {
  181. this.setState(prevState => ({
  182. openedRows: [...prevState.openedRows, id],
  183. }));
  184. }
  185. }
  186. renderNoMachines() {
  187. return (
  188. <NoMachines>
  189. There are no Minion Machines allocated to this Minion Pool
  190. </NoMachines>
  191. );
  192. }
  193. renderHeader() {
  194. const plural = this.machines.length === 1 ? "" : "s";
  195. return (
  196. <Header>
  197. <HeaderFilter>
  198. <DropdownLink
  199. items={[
  200. { label: "All", value: "all" },
  201. { label: "Allocated", value: "allocated" },
  202. { label: "Not Allocated", value: "not-allocated" },
  203. ]}
  204. selectedItem={this.state.filterStatus}
  205. onChange={item => {
  206. this.setState({
  207. filterStatus: item.value as FilterType,
  208. });
  209. }}
  210. />
  211. </HeaderFilter>
  212. <HeaderText>
  213. {this.machines.length} minion machine{plural},{" "}
  214. {
  215. this.machines.filter(
  216. m =>
  217. m.allocation_status === "ALLOCATED" ||
  218. m.allocation_status === "AVAILABLE"
  219. ).length
  220. }{" "}
  221. allocated
  222. </HeaderText>
  223. </Header>
  224. );
  225. }
  226. renderConnectionInfo(machine: MinionMachine) {
  227. const isOpened = Boolean(this.state.openedRows.find(i => i === machine.id));
  228. return (
  229. <Row
  230. onClick={() => {
  231. this.handleRowClick(machine.id);
  232. }}
  233. >
  234. <ArrowStyled
  235. primary
  236. orientation={isOpened ? "up" : "down"}
  237. opacity={isOpened ? 1 : 0}
  238. thick
  239. />
  240. <RowHeader>
  241. <RowHeaderColumn>
  242. <HeaderIcon />
  243. <HeaderName>Connection Info</HeaderName>
  244. </RowHeaderColumn>
  245. </RowHeader>
  246. <Collapse isOpened={isOpened}>
  247. <RowBody>
  248. <RowBodyColumn>
  249. {Object.keys(machine.connection_info).map(prop => (
  250. <RowBodyColumnValue key={prop}>
  251. {prop}: {machine.connection_info[prop]}
  252. </RowBodyColumnValue>
  253. ))}
  254. </RowBodyColumn>
  255. </RowBody>
  256. </Collapse>
  257. </Row>
  258. );
  259. }
  260. renderMachines() {
  261. if (this.filteredMachines.length === 0) {
  262. return <NoMachines>No Minion Machines found</NoMachines>;
  263. }
  264. return (
  265. <MachinesWrapper>
  266. {this.filteredMachines.map(machine => {
  267. const findTransferItem = (transferItems: TransferItem[]) =>
  268. transferItems.find(i => i.id === machine.allocated_action);
  269. const allocatedAction = machine.allocated_action
  270. ? findTransferItem(this.props.replicas) ||
  271. findTransferItem(this.props.deployments)
  272. : null;
  273. return (
  274. <MachineWrapper key={machine.id}>
  275. <MachineTitle>ID: {machine.id}</MachineTitle>
  276. <MachineBody>
  277. <MachineRow>
  278. Allocation Status:{" "}
  279. <StatusPill
  280. style={{ marginLeft: "8px" }}
  281. status={machine.allocation_status}
  282. />
  283. </MachineRow>
  284. <MachineRow style={{ marginBottom: "16px" }}>
  285. <span style={{ width: "114px" }}>Power Status:</span>{" "}
  286. <StatusPill
  287. style={{ marginLeft: "8px" }}
  288. status={machine.power_status}
  289. />
  290. </MachineRow>
  291. <MachineRow secondary>
  292. Created At:{" "}
  293. {DateUtils.getLocalDate(machine.created_at).toFormat(
  294. "yyyy-LL-dd HH:mm:ss"
  295. )}
  296. </MachineRow>
  297. {machine.updated_at ? (
  298. <MachineRow secondary>
  299. Updated At:{" "}
  300. {DateUtils.getLocalDate(machine.updated_at).toFormat(
  301. "yyyy-LL-dd HH:mm:ss"
  302. )}
  303. </MachineRow>
  304. ) : null}
  305. {machine.last_used_at ? (
  306. <MachineRow secondary>
  307. Last Used At:{" "}
  308. {DateUtils.getLocalDate(machine.last_used_at).toFormat(
  309. "yyyy-LL-dd HH:mm:ss"
  310. )}
  311. </MachineRow>
  312. ) : null}
  313. {machine.allocated_action ? (
  314. <MachineRow secondary>
  315. Allocated Action:
  316. {allocatedAction ? (
  317. <>
  318. <ItemReplicaBadge style={{ margin: "0px 4px 0 5px" }}>
  319. {allocatedAction.type === "replica" ? "TR" : "DE"}
  320. </ItemReplicaBadge>
  321. <ValueLink
  322. to={`/${allocatedAction.type === "replica" ? "transfers" : "deployments"}/${allocatedAction.id}`}
  323. >
  324. {allocatedAction.instances[0]}
  325. </ValueLink>
  326. </>
  327. ) : (
  328. <span>&nbsp;{machine.allocated_action}</span>
  329. )}
  330. </MachineRow>
  331. ) : null}
  332. </MachineBody>
  333. {machine.connection_info
  334. ? this.renderConnectionInfo(machine)
  335. : null}
  336. </MachineWrapper>
  337. );
  338. })}
  339. <GlobalStyle />
  340. </MachinesWrapper>
  341. );
  342. }
  343. render() {
  344. return (
  345. <Wrapper>
  346. {this.props.item?.minion_machines.length
  347. ? this.renderHeader()
  348. : this.renderNoMachines()}
  349. {this.props.item?.minion_machines.length ? this.renderMachines() : null}
  350. </Wrapper>
  351. );
  352. }
  353. }
  354. export default MinionPoolMachines;