kernelStatus.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import React from 'react';
  4. import { IClientSession, VDomRenderer, VDomModel } from '@jupyterlab/apputils';
  5. import { Text } from '@jupyterlab/coreutils';
  6. import { Kernel, Session } from '@jupyterlab/services';
  7. import { interactiveItem, TextItem } from '..';
  8. import { JSONExt } from '@phosphor/coreutils';
  9. /**
  10. * A pure functional component for rendering kernel status.
  11. */
  12. function KernelStatusComponent(
  13. props: KernelStatusComponent.IProps
  14. ): React.ReactElement<KernelStatusComponent.IProps> {
  15. return (
  16. <TextItem
  17. onClick={props.handleClick}
  18. source={`${props.kernelName} | ${Text.titleCase(props.status)}`}
  19. title={`Change kernel for ${props.activityName}`}
  20. />
  21. );
  22. }
  23. /**
  24. * A namespace for KernelStatusComponent statics.
  25. */
  26. namespace KernelStatusComponent {
  27. /**
  28. * Props for the kernel status component.
  29. */
  30. export interface IProps {
  31. /**
  32. * A click handler for the kernel status component. By default
  33. * we have it bring up the kernel change dialog.
  34. */
  35. handleClick: () => void;
  36. /**
  37. * The name the kernel.
  38. */
  39. kernelName: string;
  40. /**
  41. * The name of the activity using the kernel.
  42. */
  43. activityName: string;
  44. /**
  45. * The status of the kernel.
  46. */
  47. status: Kernel.Status;
  48. }
  49. }
  50. /**
  51. * A VDomRenderer widget for displaying the status of a kernel.
  52. */
  53. export class KernelStatus extends VDomRenderer<KernelStatus.Model> {
  54. /**
  55. * Construct the kernel status widget.
  56. */
  57. constructor(opts: KernelStatus.IOptions) {
  58. super();
  59. this._handleClick = opts.onClick;
  60. this.model = new KernelStatus.Model();
  61. this.addClass(interactiveItem);
  62. }
  63. /**
  64. * Render the kernel status item.
  65. */
  66. render() {
  67. if (this.model === null) {
  68. return null;
  69. } else {
  70. return (
  71. <KernelStatusComponent
  72. status={this.model.status}
  73. kernelName={this.model.kernelName}
  74. activityName={this.model.activityName}
  75. handleClick={this._handleClick}
  76. />
  77. );
  78. }
  79. }
  80. private _handleClick: () => void;
  81. }
  82. /**
  83. * A namespace for KernelStatus statics.
  84. */
  85. export namespace KernelStatus {
  86. /**
  87. * A VDomModel for the kernel status indicator.
  88. */
  89. export class Model extends VDomModel {
  90. /**
  91. * The name of the kernel.
  92. */
  93. get kernelName() {
  94. return this._kernelName;
  95. }
  96. /**
  97. * The current status of the kernel.
  98. */
  99. get status() {
  100. return this._kernelStatus;
  101. }
  102. /**
  103. * A display name for the activity.
  104. */
  105. get activityName(): string {
  106. return this._activityName;
  107. }
  108. set activityName(val: string) {
  109. const oldVal = this._activityName;
  110. if (oldVal === val) {
  111. return;
  112. }
  113. this._activityName = val;
  114. this.stateChanged.emit(void 0);
  115. }
  116. /**
  117. * The current client session associated with the kernel status indicator.
  118. */
  119. get session(): IClientSession | null {
  120. return this._session;
  121. }
  122. set session(session: IClientSession | null) {
  123. const oldSession = this._session;
  124. if (oldSession !== null) {
  125. oldSession.statusChanged.disconnect(this._onKernelStatusChanged);
  126. oldSession.kernelChanged.disconnect(this._onKernelChanged);
  127. }
  128. const oldState = this._getAllState();
  129. this._session = session;
  130. if (this._session === null) {
  131. this._kernelStatus = 'unknown';
  132. this._kernelName = 'unknown';
  133. } else {
  134. this._kernelStatus = this._session.status;
  135. this._kernelName = this._session.kernelDisplayName;
  136. this._session.statusChanged.connect(this._onKernelStatusChanged);
  137. this._session.kernelChanged.connect(this._onKernelChanged);
  138. }
  139. this._triggerChange(oldState, this._getAllState());
  140. }
  141. /**
  142. * React to changes to the kernel status.
  143. */
  144. private _onKernelStatusChanged = (
  145. _session: IClientSession,
  146. status: Kernel.Status
  147. ) => {
  148. this._kernelStatus = status;
  149. this.stateChanged.emit(void 0);
  150. };
  151. /**
  152. * React to changes in the kernel.
  153. */
  154. private _onKernelChanged = (
  155. _session: IClientSession,
  156. change: Session.IKernelChangedArgs
  157. ) => {
  158. const oldState = this._getAllState();
  159. const { newValue } = change;
  160. if (newValue !== null) {
  161. newValue.getSpec().then(spec => {
  162. // sync setting of status and display name
  163. this._kernelStatus = newValue.status;
  164. this._kernelName = spec.display_name;
  165. this._triggerChange(oldState, this._getAllState());
  166. });
  167. } else {
  168. this._kernelStatus = 'unknown';
  169. this._kernelName = 'unknown';
  170. this._triggerChange(oldState, this._getAllState());
  171. }
  172. };
  173. private _getAllState(): [string, string, string] {
  174. return [this._kernelName, this._kernelStatus, this._activityName];
  175. }
  176. private _triggerChange(
  177. oldState: [string, string, string],
  178. newState: [string, string, string]
  179. ) {
  180. if (JSONExt.deepEqual(oldState, newState)) {
  181. this.stateChanged.emit(void 0);
  182. }
  183. }
  184. private _activityName: string = 'activity';
  185. private _kernelName: string = 'unknown';
  186. private _kernelStatus: Kernel.Status = 'unknown';
  187. private _session: IClientSession | null = null;
  188. }
  189. /**
  190. * Options for creating a KernelStatus object.
  191. */
  192. export interface IOptions {
  193. /**
  194. * A click handler for the item. By default
  195. * we launch a kernel selection dialog.
  196. */
  197. onClick: () => void;
  198. }
  199. }