2
0

ProvisionerStatus.tsx 6.7 KB

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