runningSessions.tsx 4.9 KB

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