EdgegapServerDataManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using IO.Swagger.Model;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. namespace Edgegap
  8. {
  9. static class EdgegapServerDataManagerUtils
  10. {
  11. public static Label GetHeader(string text)
  12. {
  13. Label header = new Label(text);
  14. header.AddToClassList("label__header");
  15. return header;
  16. }
  17. public static VisualElement GetHeaderRow()
  18. {
  19. VisualElement row = new VisualElement();
  20. row.AddToClassList("row__port-table");
  21. row.AddToClassList("label__header");
  22. row.Add(new Label("Name"));
  23. row.Add(new Label("External"));
  24. row.Add(new Label("Internal"));
  25. row.Add(new Label("ProtocolStr"));
  26. row.Add(new Label("Link"));
  27. return row;
  28. }
  29. public static VisualElement GetRowFromPortResponse(PortMapping port)
  30. {
  31. VisualElement row = new VisualElement();
  32. row.AddToClassList("row__port-table");
  33. row.AddToClassList("focusable");
  34. row.Add(new Label(port.Name));
  35. row.Add(new Label(port.External.ToString()));
  36. row.Add(new Label(port.Internal.ToString()));
  37. row.Add(new Label(port.Protocol));
  38. row.Add(GetCopyButton("Copy", port.Link));
  39. return row;
  40. }
  41. public static Button GetCopyButton(string btnText, string copiedText)
  42. {
  43. Button copyBtn = new Button();
  44. copyBtn.text = btnText;
  45. copyBtn.clickable.clicked += () => GUIUtility.systemCopyBuffer = copiedText;
  46. return copyBtn;
  47. }
  48. public static Button GetLinkButton(string btnText, string targetUrl)
  49. {
  50. Button copyBtn = new Button();
  51. copyBtn.text = btnText;
  52. copyBtn.clickable.clicked += () => UnityEngine.Application.OpenURL(targetUrl);
  53. return copyBtn;
  54. }
  55. public static Label GetInfoText(string innerText)
  56. {
  57. Label infoText = new Label(innerText);
  58. infoText.AddToClassList("label__info-text");
  59. return infoText;
  60. }
  61. }
  62. /// <summary>
  63. /// Utility class to centrally manage the Edgegap server data, and create / update the elements displaying the server info.
  64. /// </summary>
  65. public static class EdgegapServerDataManager
  66. {
  67. internal static Status _serverData;
  68. private static ApiEnvironment _apiEnvironment;
  69. // UI elements
  70. private static readonly StyleSheet _serverDataStylesheet;
  71. private static readonly List<VisualElement> _serverDataContainers = new List<VisualElement>();
  72. public static Status GetServerStatus() => _serverData;
  73. static EdgegapServerDataManager()
  74. {
  75. _serverDataStylesheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Edgegap/Editor/EdgegapServerData.uss");
  76. }
  77. public static void RegisterServerDataContainer(VisualElement serverDataContainer)
  78. {
  79. _serverDataContainers.Add(serverDataContainer);
  80. }
  81. public static void DeregisterServerDataContainer(VisualElement serverDataContainer)
  82. {
  83. _serverDataContainers.Remove(serverDataContainer);
  84. }
  85. public static void SetServerData(Status serverData, ApiEnvironment apiEnvironment)
  86. {
  87. _serverData = serverData;
  88. _apiEnvironment = apiEnvironment;
  89. RefreshServerDataContainers();
  90. }
  91. private static VisualElement GetStatusSection()
  92. {
  93. ServerStatus serverStatus = _serverData.GetServerStatus();
  94. string dashboardUrl = _apiEnvironment.GetDashboardUrl();
  95. string requestId = _serverData.RequestId;
  96. string deploymentDashboardUrl = "";
  97. if (!string.IsNullOrEmpty(requestId) && !string.IsNullOrEmpty(dashboardUrl))
  98. {
  99. deploymentDashboardUrl = $"{dashboardUrl}/arbitrium/deployment/read/{requestId}/";
  100. }
  101. VisualElement container = new VisualElement();
  102. container.AddToClassList("container");
  103. container.Add(EdgegapServerDataManagerUtils.GetHeader("Server Status"));
  104. VisualElement row = new VisualElement();
  105. row.AddToClassList("row__status");
  106. // Status pill
  107. Label statusLabel = new Label(serverStatus.GetLabelText());
  108. statusLabel.AddToClassList(serverStatus.GetStatusBgClass());
  109. statusLabel.AddToClassList("label__status");
  110. row.Add(statusLabel);
  111. // Link to dashboard
  112. if (!string.IsNullOrEmpty(deploymentDashboardUrl))
  113. {
  114. row.Add(EdgegapServerDataManagerUtils.GetLinkButton("See in the dashboard", deploymentDashboardUrl));
  115. }
  116. else
  117. {
  118. row.Add(new Label("Could not resolve link to this deployment"));
  119. }
  120. container.Add(row);
  121. return container;
  122. }
  123. private static VisualElement GetDnsSection()
  124. {
  125. string serverDns = _serverData.Fqdn;
  126. VisualElement container = new VisualElement();
  127. container.AddToClassList("container");
  128. container.Add(EdgegapServerDataManagerUtils.GetHeader("Server DNS"));
  129. VisualElement row = new VisualElement();
  130. row.AddToClassList("row__dns");
  131. row.AddToClassList("focusable");
  132. row.Add(new Label(serverDns));
  133. row.Add(EdgegapServerDataManagerUtils.GetCopyButton("Copy", serverDns));
  134. container.Add(row);
  135. return container;
  136. }
  137. private static VisualElement GetPortsSection()
  138. {
  139. List<PortMapping> serverPorts = _serverData.Ports.Values.ToList();
  140. VisualElement container = new VisualElement();
  141. container.AddToClassList("container");
  142. container.Add(EdgegapServerDataManagerUtils.GetHeader("Server PortsDict"));
  143. container.Add(EdgegapServerDataManagerUtils.GetHeaderRow());
  144. VisualElement portList = new VisualElement();
  145. if (serverPorts.Count > 0)
  146. {
  147. foreach (PortMapping port in serverPorts)
  148. {
  149. portList.Add(EdgegapServerDataManagerUtils.GetRowFromPortResponse(port));
  150. }
  151. }
  152. else
  153. {
  154. portList.Add(new Label("No port configured for this app version."));
  155. }
  156. container.Add(portList);
  157. return container;
  158. }
  159. public static VisualElement GetServerDataVisualTree()
  160. {
  161. VisualElement serverDataTree = new VisualElement();
  162. serverDataTree.styleSheets.Add(_serverDataStylesheet);
  163. bool hasServerData = _serverData != null;
  164. bool isReady = hasServerData && _serverData.GetServerStatus().IsOneOf(ServerStatus.Ready, ServerStatus.Error);
  165. if (hasServerData)
  166. {
  167. serverDataTree.Add(GetStatusSection());
  168. if (isReady)
  169. {
  170. serverDataTree.Add(GetDnsSection());
  171. serverDataTree.Add(GetPortsSection());
  172. }
  173. else
  174. {
  175. serverDataTree.Add(EdgegapServerDataManagerUtils.GetInfoText("Additional information will be displayed when the server is ready."));
  176. }
  177. }
  178. else
  179. {
  180. serverDataTree.Add(EdgegapServerDataManagerUtils.GetInfoText("Server data will be displayed here when a server is running."));
  181. }
  182. return serverDataTree;
  183. }
  184. private static void RefreshServerDataContainers()
  185. {
  186. foreach (VisualElement serverDataContainer in _serverDataContainers)
  187. {
  188. serverDataContainer.Clear();
  189. serverDataContainer.Add(GetServerDataVisualTree()); // Cannot reuse a same instance of VisualElement
  190. }
  191. }
  192. }
  193. }