runningSessions.tsx 5.0 KB

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