runningSessions.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import React from 'react';
  4. import { VDomRenderer, VDomModel } from '@jupyterlab/apputils';
  5. import {
  6. ServiceManager,
  7. Kernel,
  8. TerminalSession,
  9. TerminalManager,
  10. SessionManager
  11. } from '@jupyterlab/services';
  12. import { GroupItem, IconItem, interactiveItem, TextItem } from '..';
  13. /**
  14. * Half spacing between subitems in a status item.
  15. */
  16. const HALF_SPACING = 4;
  17. /**
  18. * A pure functional component for rendering kernel and terminal sessions.
  19. *
  20. * @param props: the props for the component.
  21. *
  22. * @returns a tsx component for the running sessions.
  23. */
  24. function RunningSessionsComponent(
  25. props: RunningSessionsComponent.IProps
  26. ): React.ReactElement<RunningSessionsComponent.IProps> {
  27. return (
  28. <GroupItem spacing={HALF_SPACING} onClick={props.handleClick}>
  29. <GroupItem spacing={HALF_SPACING}>
  30. <TextItem source={props.terminals} />
  31. <IconItem source={'jp-StatusItem-terminal'} />
  32. </GroupItem>
  33. <GroupItem spacing={HALF_SPACING}>
  34. <TextItem source={props.kernels} />
  35. <IconItem source={'jp-StatusItem-kernel'} />
  36. </GroupItem>
  37. </GroupItem>
  38. );
  39. }
  40. /**
  41. * A namepsace for RunningSessionsComponents statics.
  42. */
  43. namespace RunningSessionsComponent {
  44. /**
  45. * The props for rendering the RunningSessionsComponent.
  46. */
  47. export interface IProps {
  48. /**
  49. * A click handler for the component. By defult this is used
  50. * to activate the running sessions side panel.
  51. */
  52. handleClick: () => void;
  53. /**
  54. * The number of running kernels.
  55. */
  56. kernels: number;
  57. /**
  58. * The number of active terminal sessions.
  59. */
  60. terminals: number;
  61. }
  62. }
  63. /**
  64. * A VDomRenderer for a RunningSessions status item.
  65. */
  66. export class RunningSessions extends VDomRenderer<RunningSessions.Model> {
  67. /**
  68. * Create a new RunningSessions widget.
  69. */
  70. constructor(opts: RunningSessions.IOptions) {
  71. super();
  72. this._serviceManager = opts.serviceManager;
  73. this._handleClick = opts.onClick;
  74. this._serviceManager.sessions.runningChanged.connect(
  75. this._onKernelsRunningChanged,
  76. this
  77. );
  78. this._serviceManager.terminals.runningChanged.connect(
  79. this._onTerminalsRunningChanged,
  80. this
  81. );
  82. this.model = new RunningSessions.Model();
  83. this.addClass(interactiveItem);
  84. }
  85. /**
  86. * Render the running sessions widget.
  87. */
  88. render() {
  89. if (!this.model) {
  90. return null;
  91. }
  92. this.title.caption = `${this.model.terminals} Terminals, ${
  93. this.model!.kernels
  94. } Kernels`;
  95. return (
  96. <RunningSessionsComponent
  97. kernels={this.model.kernels}
  98. terminals={this.model.terminals}
  99. handleClick={this._handleClick}
  100. />
  101. );
  102. }
  103. /**
  104. * Dispose of the status item.
  105. */
  106. dispose() {
  107. super.dispose();
  108. this._serviceManager.sessions.runningChanged.disconnect(
  109. this._onKernelsRunningChanged,
  110. this
  111. );
  112. this._serviceManager.terminals.runningChanged.disconnect(
  113. this._onTerminalsRunningChanged,
  114. this
  115. );
  116. }
  117. /**
  118. * Set the number of model kernels when the list changes.
  119. */
  120. private _onKernelsRunningChanged(
  121. manager: SessionManager,
  122. kernels: Kernel.IModel[]
  123. ): void {
  124. this.model!.kernels = kernels.length;
  125. }
  126. /**
  127. * Set the number of model terminal sessions when the list changes.
  128. */
  129. private _onTerminalsRunningChanged(
  130. manager: TerminalManager,
  131. terminals: TerminalSession.IModel[]
  132. ): void {
  133. this.model!.terminals = terminals.length;
  134. }
  135. private _handleClick: () => void;
  136. private _serviceManager: ServiceManager;
  137. }
  138. /**
  139. * A namespace for RunninSessions statics.
  140. */
  141. export namespace RunningSessions {
  142. /**
  143. * A VDomModel for the RunninSessions status item.
  144. */
  145. export class Model extends VDomModel {
  146. /**
  147. * The number of active kernels.
  148. */
  149. get kernels(): number {
  150. return this._kernels;
  151. }
  152. set kernels(kernels: number) {
  153. const oldKernels = this._kernels;
  154. this._kernels = kernels;
  155. if (oldKernels !== this._kernels) {
  156. this.stateChanged.emit(void 0);
  157. }
  158. }
  159. /**
  160. * The number of active terminal sessions.
  161. */
  162. get terminals(): number {
  163. return this._terminals;
  164. }
  165. set terminals(terminals: number) {
  166. const oldTerminals = this._terminals;
  167. this._terminals = terminals;
  168. if (oldTerminals !== this._terminals) {
  169. this.stateChanged.emit(void 0);
  170. }
  171. }
  172. private _terminals: number = 0;
  173. private _kernels: number = 0;
  174. }
  175. /**
  176. * Options for creating a RunningSessions item.
  177. */
  178. export interface IOptions {
  179. /**
  180. * The application service manager.
  181. */
  182. serviceManager: ServiceManager;
  183. /**
  184. * A click handler for the item. By defult this is used
  185. * to activate the running sessions side panel.
  186. */
  187. onClick: () => void;
  188. }
  189. }