All files / src/components job-row.tsx

0% Statements 0/72
0% Branches 0/38
0% Functions 0/19
0% Lines 0/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import React, { useState } from 'react';
 
import { JupyterFrontEnd } from '@jupyterlab/application';
import { ToolbarButtonComponent } from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';
import { closeIcon, stopIcon } from '@jupyterlab/ui-components';
 
import { Signal } from '@lumino/signaling';
 
import { Scheduler } from '../handler';
import { useTranslator } from '../hooks';
import { JobParameter, CreateJobFormState } from '../create-job-form';
 
import { replayIcon } from './icons';
import { JobDetails } from './job-details';
import { outputFormatsForEnvironment } from './output-format-picker';
 
function get_file_from_path(path: string): string {
  return PathExt.basename(path);
}
 
function StopButton(props: {
  job: Scheduler.IDescribeJob;
  clickHandler: () => void;
}): JSX.Element | null {
  Iif (props.job.status !== 'IN_PROGRESS') {
    return null;
  }
 
  const trans = useTranslator('jupyterlab');
  const buttonTitle = props.job.name
    ? trans.__('Stop "%1"', props.job.name)
    : trans.__('Stop job');
 
  return (
    <ToolbarButtonComponent
      onClick={props.clickHandler}
      tooltip={buttonTitle}
      icon={stopIcon}
    />
  );
}
 
function DeleteButton(props: {
  job: Scheduler.IDescribeJob;
  clickHandler: () => void;
}): JSX.Element | null {
  const trans = useTranslator('jupyterlab');
  const buttonTitle = props.job.name
    ? trans.__('Delete "%1"', props.job.name)
    : trans.__('Delete job');
 
  return (
    <ToolbarButtonComponent
      onClick={props.clickHandler}
      tooltip={buttonTitle}
      icon={closeIcon}
    />
  );
}
 
function RefillButton(props: {
  job: Scheduler.IDescribeJob;
  signal: Signal<any, CreateJobFormState>;
  showCreateJob: () => void;
}): JSX.Element | null {
  const trans = useTranslator('jupyterlab');
  const buttonTitle = props.job.name
    ? trans.__('Rerun "%1" …', props.job.name)
    : trans.__('Rerun job …');
 
  // Convert the hash of parameters to an array.
  const jobParameters: JobParameter[] | undefined = props.job.parameters
    ? Object.keys(props.job.parameters).map(key => {
        return { name: key, value: props.job.parameters![key] };
      })
    : undefined;
 
  const clickHandler = (): void => {
    let initialState: CreateJobFormState = {
      inputFile: props.job.input_uri,
      jobName: props.job.name ?? '',
      outputPath: props.job.output_prefix,
      environment: props.job.runtime_environment_name,
      parameters: jobParameters
    };
 
    // Convert the list of output formats, if any, into a list for the initial state
    const jobOutputFormats = props.job.output_formats;
    const outputFormats = outputFormatsForEnvironment(
      props.job.runtime_environment_name
    );
    Iif (jobOutputFormats && outputFormats) {
      initialState.outputFormats = outputFormats.filter(of =>
        jobOutputFormats.some(jof => of.name == jof)
      );
    }
 
    // Switch the view to the form.
    props.showCreateJob();
    props.signal.emit(initialState);
  };
 
  return (
    <ToolbarButtonComponent
      onClick={clickHandler}
      tooltip={buttonTitle}
      icon={replayIcon}
    />
  );
}
 
function Timestamp(props: { job: Scheduler.IDescribeJob }): JSX.Element | null {
  const start_date: Date | null = props.job.start_time
    ? new Date(props.job.start_time)
    : null;
  const start_display_date: string | null = start_date
    ? start_date.toLocaleString()
    : null;
 
  return <>{start_display_date}</>;
}
 
function OutputFiles(props: {
  job: Scheduler.IDescribeJob;
  openOnClick: (e: any, output_uri: string) => void;
  outputUri: string;
}): JSX.Element | null {
  Iif (props.job.status !== 'COMPLETED') {
    return null;
  }
 
  const trans = useTranslator('jupyterlab');
 
  // Get all output files.
  const outputTypes = props.job.output_formats || ['ipynb'];
  return (
    <>
      {outputTypes.map(outputType => {
        // Compose a specific link.
        const outputName = props.job.output_uri.replace(/ipynb$/, outputType);
        return (
          <a
            key={outputType}
            href={`/lab/tree/${outputName}`}
            title={trans.__('Open "%1"', outputName)}
            onClick={e => props.openOnClick(e, outputName)}
            style={{ paddingRight: '1em' }}
          >
            {outputType}
          </a>
        );
      })}
    </>
  );
}
 
export type JobRowProps = {
  job: Scheduler.IDescribeJob;
  createJobFormSignal: Signal<any, CreateJobFormState>;
  rowClass: string;
  cellClass: string;
  app: JupyterFrontEnd;
  showCreateJob: () => void;
};
 
// Add a row for a job, with columns for each of its traits and a details view below.
export function JobRow(props: JobRowProps) {
  const [detailsVisible, setDetailsVisible] = useState(false);
 
  const job = props.job;
  const rowClass = props.rowClass;
  const cellClass = props.cellClass;
  const detailsVisibleClass = 'details-visible';
  const trans = useTranslator('jupyterlab');
 
  const input_relative_uri = job.input_uri;
  const output_relative_uri = job.output_uri;
  // Truncate the path to its filename.
  const input_file = get_file_from_path(job.input_uri);
 
  const openFileClickHandler = (e: any, output_uri: string) => {
    e.preventDefault();
    props.app.commands.execute('docmanager:open', { path: output_uri });
  };
 
  const openDetailsClickHandler = () => {
    setDetailsVisible(!detailsVisible);
  };
 
  const translatedStatus = (status: Scheduler.Status) => {
    // This may look inefficient, but it's intended to call the `trans` function
    // with distinct, static values, so that code analyzers can pick up all the
    // needed source strings.
    switch (status) {
      case 'COMPLETED':
        return trans.__('Completed');
      case 'FAILED':
        return trans.__('Failed');
      case 'IN_PROGRESS':
        return trans.__('In progress');
      case 'STOPPED':
        return trans.__('Stopped');
      case 'STOPPING':
        return trans.__('Stopping');
    }
  };
 
  const viewJobDetailsTitle = job.name
    ? trans.__('View details for "%1"', job.name)
    : trans.__('View job details');
 
  return (
    <>
      <div
        className={rowClass + (detailsVisible ? ' ' + detailsVisibleClass : '')}
        id={`${rowClass}-${job.job_id}`}
        data-job-id={job.job_id}
      >
        <div className={cellClass}>
          <a
            className="jp-notebook-job-name"
            onClick={openDetailsClickHandler}
            title={viewJobDetailsTitle}
          >
            {job.name || <em>{trans.__('unnamed')}</em>}
          </a>
        </div>
        <div className={cellClass}>
          <a
            href={`/lab/tree/${input_relative_uri}`}
            title={trans.__('Open "%1"', input_relative_uri)}
            onClick={e => openFileClickHandler(e, input_relative_uri)}
          >
            {input_file}
          </a>
        </div>
        <div className={cellClass}>
          <OutputFiles
            job={job}
            openOnClick={openFileClickHandler}
            outputUri={output_relative_uri}
          />
        </div>
        <div className={cellClass}>
          <Timestamp job={job} />
        </div>
        <div className={cellClass}>{translatedStatus(job.status)}</div>
        <div className={cellClass}>
          <StopButton
            job={job}
            clickHandler={() =>
              props.app.commands.execute('scheduling:stop-job', {
                id: job.job_id
              })
            }
          />
          <DeleteButton
            job={job}
            clickHandler={() => {
              props.app.commands.execute('scheduling:delete-job', {
                id: job.job_id
              });
              const jobContainer = document.getElementById(
                `${rowClass}-${job.job_id}`
              );
              jobContainer?.classList.add(`${rowClass}-deleted`);
            }}
          />
          <RefillButton
            job={job}
            signal={props.createJobFormSignal}
            showCreateJob={props.showCreateJob}
          />
        </div>
      </div>
      <JobDetails job={job} isVisible={detailsVisible} />
    </>
  );
}