Skip to content
Snippets Groups Projects
execution-detail-planes.component.ts 2.92 KiB
Newer Older
import {Component, EventEmitter, Input, Output, OnDestroy, OnInit} from '@angular/core';
import {FormControl, FormGroup} from '@angular/forms';
import {AlertService} from '../../../../services/alert.service';
import {JobsService} from '../../../../services/jobs.service';
import {Job} from '../../../../model/job';
import {Observable} from 'rxjs';

@Component({
  selector: 'app-execution-detail-planes',
  templateUrl: './execution-detail-planes.component.html',
  styleUrls: ['./execution-detail-planes.component.scss']
})
export class ExecutionDetailPlanesComponent implements OnInit, OnDestroy {

  @Input() job: Job;
  @Output() planesWritten: EventEmitter<any> = new EventEmitter();
  planeKeys: string[];
  planesFormGroup: FormGroup;

  constructor(
    private jobService: JobsService,
    private alertService: AlertService
  ) {
    // Form group to contain the plane checkboxes
    this.planesFormGroup = new FormGroup({});
    this.planeKeys = null;
  }

  ngOnInit(): void {
  }

  ngOnDestroy(): void {

  }

  // Reads plane names from a JSON file and returns them in a list
  getPlaneKeys(): string[] {
    // Return the plane names if we already fetched them
    if (this.planeKeys !== null) {
      return this.planeKeys;
    }

    this.planeKeys = [];

    // Get the plane names from the spectral window numbers in the planes.json file
    this.jobService.getPlanes(this.job.job_id).subscribe(response => {
      this.planeKeys = Object.keys(response.body);

      // Create a form control for each of the plane checkboxes and add them into the same group
      this.planeKeys.forEach(control => this.planesFormGroup.addControl(control, new FormControl(true)));

      // Set the planes data here to signal that the controls are done being added and the HTML can finish loading
      this.planes = response.body;
    },
      error => {
      this.alertService.error('Could not retrieve planes from planes.json. ' + error);
    });

    return this.planeKeys;
  }

  // Writes to a file in lustre to flag planes to be accepted
  acceptPlanes(): void {
    this.alertService.info('Flagging accepted planes to be cached');

    // Collect the selected planes and write their names separated by a newline
    let planesText = '';
    const planes = this.planesFormGroup.value;
    Object.keys(planes).forEach(key => {
      if (planes[key] === true) {
        planesText += key + '\n';
      }
    });
    planesText = planesText.trim();

    // Write out the planes string if any are selected
    if (planesText.length > 0) {
      this.jobService.writePlanes(this.job.job_id, planesText).subscribe(
        result => {},
        error => {
          this.alertService.error('Planes did not save. ' + error);
        },
        () => {
          // Trigger acceptQa on the parent if there were no errors during the save
          this.planesWritten.emit();

          this.alertService.success('Planes Saved');
        });
    }
  }

}