kernelStatus.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 '@lumino/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(new KernelStatus.Model());
  59. this._handleClick = opts.onClick;
  60. this.addClass(interactiveItem);
  61. }
  62. /**
  63. * Render the kernel status item.
  64. */
  65. render() {
  66. if (this.model === null) {
  67. return null;
  68. } else {
  69. return (
  70. <KernelStatusComponent
  71. status={this.model.status}
  72. kernelName={this.model.kernelName}
  73. activityName={this.model.activityName}
  74. handleClick={this._handleClick}
  75. />
  76. );
  77. }
  78. }
  79. private _handleClick: () => void;
  80. }
  81. /**
  82. * A namespace for KernelStatus statics.
  83. */
  84. export namespace KernelStatus {
  85. /**
  86. * A VDomModel for the kernel status indicator.
  87. */
  88. export class Model extends VDomModel {
  89. /**
  90. * The name of the kernel.
  91. */
  92. get kernelName() {
  93. return this._kernelName;
  94. }
  95. /**
  96. * The current status of the kernel.
  97. */
  98. get status() {
  99. return this._kernelStatus;
  100. }
  101. /**
  102. * A display name for the activity.
  103. */
  104. get activityName(): string {
  105. return this._activityName;
  106. }
  107. set activityName(val: string) {
  108. const oldVal = this._activityName;
  109. if (oldVal === val) {
  110. return;
  111. }
  112. this._activityName = val;
  113. this.stateChanged.emit(void 0);
  114. }
  115. /**
  116. * The current client session associated with the kernel status indicator.
  117. */
  118. get session(): IClientSession | null {
  119. return this._session;
  120. }
  121. set session(session: IClientSession | null) {
  122. const oldSession = this._session;
  123. if (oldSession !== null) {
  124. oldSession.statusChanged.disconnect(this._onKernelStatusChanged);
  125. oldSession.kernelChanged.disconnect(this._onKernelChanged);
  126. }
  127. const oldState = this._getAllState();
  128. this._session = session;
  129. if (this._session === null) {
  130. this._kernelStatus = 'unknown';
  131. this._kernelName = 'unknown';
  132. } else {
  133. this._kernelStatus = this._session.status;
  134. this._kernelName = this._session.kernelDisplayName;
  135. this._session.statusChanged.connect(this._onKernelStatusChanged);
  136. this._session.kernelChanged.connect(this._onKernelChanged);
  137. }
  138. this._triggerChange(oldState, this._getAllState());
  139. }
  140. /**
  141. * React to changes to the kernel status.
  142. */
  143. private _onKernelStatusChanged = (
  144. _session: IClientSession,
  145. status: Kernel.Status
  146. ) => {
  147. this._kernelStatus = status;
  148. this.stateChanged.emit(void 0);
  149. };
  150. /**
  151. * React to changes in the kernel.
  152. */
  153. private _onKernelChanged = (
  154. _session: IClientSession,
  155. change: Session.IKernelChangedArgs
  156. ) => {
  157. const oldState = this._getAllState();
  158. const { newValue } = change;
  159. if (newValue !== null) {
  160. newValue
  161. .getSpec()
  162. .then(spec => {
  163. // sync setting of status and display name
  164. this._kernelStatus = newValue.status;
  165. this._kernelName = spec.display_name;
  166. this._triggerChange(oldState, this._getAllState());
  167. })
  168. .catch(err => {
  169. throw err;
  170. });
  171. } else {
  172. this._kernelStatus = 'unknown';
  173. this._kernelName = 'unknown';
  174. this._triggerChange(oldState, this._getAllState());
  175. }
  176. };
  177. private _getAllState(): [string, string, string] {
  178. return [this._kernelName, this._kernelStatus, this._activityName];
  179. }
  180. private _triggerChange(
  181. oldState: [string, string, string],
  182. newState: [string, string, string]
  183. ) {
  184. if (JSONExt.deepEqual(oldState, newState)) {
  185. this.stateChanged.emit(void 0);
  186. }
  187. }
  188. private _activityName: string = 'activity';
  189. private _kernelName: string = 'unknown';
  190. private _kernelStatus: Kernel.Status = 'unknown';
  191. private _session: IClientSession | null = null;
  192. }
  193. /**
  194. * Options for creating a KernelStatus object.
  195. */
  196. export interface IOptions {
  197. /**
  198. * A click handler for the item. By default
  199. * we launch a kernel selection dialog.
  200. */
  201. onClick: () => void;
  202. }
  203. }