ProvisionerStatus.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { Steps } from "main/home/onboarding/types";
  2. import React, { useState } from "react";
  3. import { integrationList } from "shared/common";
  4. import loading from "assets/loading.gif";
  5. import styled, { keyframes } from "styled-components";
  6. type Props = {
  7. modules: TFModule[];
  8. };
  9. export interface TFModule {
  10. id: number;
  11. kind: string;
  12. status: string;
  13. created_at: string;
  14. updated_at: string;
  15. global_errors?: TFResourceError[];
  16. got_desired: boolean;
  17. // optional resources, if not created
  18. resources?: TFResource[];
  19. }
  20. export interface TFResourceError {
  21. errored_out?: boolean;
  22. error_context?: string;
  23. }
  24. export interface TFResource {
  25. addr: string;
  26. provisioned: boolean;
  27. errored: TFResourceError;
  28. }
  29. const nameMap: { [key: string]: string } = {
  30. eks: "Elastic Kubernetes Service (EKS)",
  31. ecr: "Elastic Container Registry (ECR)",
  32. doks: "DigitalOcean Kubernetes Service (DOKS)",
  33. docr: "DigitalOcean Container Registry (DOCR)",
  34. gke: "Google Kubernetes Engine (GKE)",
  35. gcr: "Google Container Registry (GCR)",
  36. rds: "Amazon Relational Database (RDS)",
  37. };
  38. const ProvisionerStatus: React.FC<Props> = ({ modules }) => {
  39. const renderStatus = (status: string) => {
  40. if (status === "successful") {
  41. return (
  42. <StatusIcon successful={true}>
  43. <i className="material-icons">done</i>
  44. </StatusIcon>
  45. );
  46. } else if (status === "loading") {
  47. return (
  48. <StatusIcon>
  49. <LoadingGif src={loading} />
  50. </StatusIcon>
  51. );
  52. } else if (status === "error") {
  53. return (
  54. <StatusIcon>
  55. <i className="material-icons">error_outline</i>
  56. </StatusIcon>
  57. );
  58. }
  59. };
  60. const readableDate = (s: string) => {
  61. const ts = new Date(s);
  62. const date = ts.toLocaleDateString();
  63. const time = ts.toLocaleTimeString([], {
  64. hour: "numeric",
  65. minute: "2-digit",
  66. });
  67. return `${time} on ${date}`;
  68. };
  69. const renderModules = () => {
  70. return modules.map((val) => {
  71. const totalResources = val.resources?.length;
  72. const provisionedResources = val.resources?.filter((resource) => {
  73. return resource.provisioned;
  74. }).length;
  75. let errors: string[] = [];
  76. if (val.status == "destroyed") {
  77. errors.push("Note: this infrastructure was automatically destroyed.");
  78. }
  79. let hasError =
  80. val.resources?.filter((resource) => {
  81. if (resource.errored?.errored_out) {
  82. errors.push(resource.errored?.error_context);
  83. }
  84. return resource.errored?.errored_out;
  85. }).length > 0;
  86. if (val.global_errors) {
  87. for (let globalErr of val.global_errors) {
  88. errors.push(globalErr.error_context);
  89. hasError = true;
  90. }
  91. }
  92. // remove duplicate errors
  93. errors = errors.filter(
  94. (error, index, self) =>
  95. index ===
  96. self.findIndex((e) => {
  97. if (e && error) {
  98. return e === error || e.includes(error) || error.includes(e);
  99. }
  100. })
  101. );
  102. const width =
  103. val.status == "created"
  104. ? 100
  105. : 100 * (provisionedResources / (totalResources * 1.0)) || 0;
  106. let error = null;
  107. if (hasError) {
  108. error = errors.map((error, index) => {
  109. return <ExpandedError key={index}>{error}</ExpandedError>;
  110. });
  111. }
  112. let loadingFill;
  113. let status;
  114. if (hasError || val.status == "destroyed") {
  115. loadingFill = <LoadingFill status="error" width={width + "%"} />;
  116. status = renderStatus("error");
  117. } else if (width == 100) {
  118. loadingFill = <LoadingFill status="successful" width={width + "%"} />;
  119. status = renderStatus("successful");
  120. } else {
  121. loadingFill = <LoadingFill status="loading" width={width + "%"} />;
  122. status = renderStatus("loading");
  123. }
  124. return (
  125. <InfraObject key={val.id}>
  126. <InfraHeader>
  127. <Flex>
  128. {status}
  129. {integrationList[val.kind] && (
  130. <Icon src={integrationList[val.kind].icon} />
  131. )}
  132. {nameMap[val.kind]}
  133. </Flex>
  134. <Timestamp>Started {readableDate(val.created_at)}</Timestamp>
  135. </InfraHeader>
  136. <LoadingBar>{loadingFill}</LoadingBar>
  137. <ErrorWrapper>{error}</ErrorWrapper>
  138. </InfraObject>
  139. );
  140. });
  141. };
  142. return <StyledProvisionerStatus>{renderModules()}</StyledProvisionerStatus>;
  143. };
  144. export default ProvisionerStatus;
  145. const Flex = styled.div`
  146. display: flex;
  147. align-items: center;
  148. `;
  149. const Timestamp = styled.div`
  150. font-size: 13px;
  151. font-weight: 400;
  152. color: #ffffff55;
  153. `;
  154. const Icon = styled.img`
  155. height: 20px;
  156. margin-right: 10px;
  157. `;
  158. const ErrorWrapper = styled.div`
  159. max-height: 150px;
  160. margin-top: 20px;
  161. overflow-y: auto;
  162. user-select: text;
  163. padding: 0 15px;
  164. `;
  165. const ExpandedError = styled.div`
  166. background: #ffffff22;
  167. border-radius: 5px;
  168. padding: 15px;
  169. font-size: 13px;
  170. font-family: monospace;
  171. border: 1px solid #aaaabb;
  172. margin-bottom: 17px;
  173. padding-bottom: 17px;
  174. `;
  175. const movingGradient = keyframes`
  176. 0% {
  177. background-position: left bottom;
  178. }
  179. 100% {
  180. background-position: right bottom;
  181. }
  182. `;
  183. const LoadingFill = styled.div<{ width: string; status: string }>`
  184. width: ${(props) => props.width};
  185. background: ${(props) =>
  186. props.status === "successful"
  187. ? "rgb(56, 168, 138)"
  188. : props.status === "error"
  189. ? "#fcba03"
  190. : "linear-gradient(to right, #8ce1ff, #616FEE)"};
  191. height: 100%;
  192. background-size: 250% 100%;
  193. animation: ${movingGradient} 2s infinite;
  194. animation-timing-function: ease-in-out;
  195. animation-direction: alternate;
  196. `;
  197. const StatusIcon = styled.div<{ successful?: boolean }>`
  198. display: flex;
  199. align-items: center;
  200. font-family: "Work Sans", sans-serif;
  201. font-size: 13px;
  202. color: #ffffff55;
  203. max-width: 500px;
  204. overflow: hidden;
  205. text-overflow: ellipsis;
  206. > i {
  207. font-size: 18px;
  208. margin-right: 10px;
  209. float: left;
  210. color: ${(props) => (props.successful ? "rgb(56, 168, 138)" : "#fcba03")};
  211. }
  212. `;
  213. const LoadingGif = styled.img`
  214. width: 15px;
  215. height: 15px;
  216. margin-right: 9px;
  217. margin-bottom: 0px;
  218. `;
  219. const StyledProvisionerStatus = styled.div`
  220. margin-top: 25px;
  221. `;
  222. const LoadingBar = styled.div`
  223. width: calc(100% - 30px);
  224. background: #ffffff22;
  225. border: 100px;
  226. margin: 15px 15px 0;
  227. height: 18px;
  228. overflow: hidden;
  229. border-radius: 100px;
  230. `;
  231. const InfraObject = styled.div`
  232. background: #ffffff22;
  233. padding: 15px 0 0;
  234. border: 1px solid #aaaabb;
  235. border-radius: 5px;
  236. margin-bottom: 10px;
  237. position: relative;
  238. `;
  239. const InfraHeader = styled.div`
  240. font-size: 13px;
  241. font-weight: 500;
  242. justify-content: space-between;
  243. padding: 0 15px;
  244. display: flex;
  245. align-items: center;
  246. `;