import {Component, Input, 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'; @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; 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 { // Create a form control for each of the plane checkboxes and add them into the same group this.getPlaneKeys().forEach(control => this.planesFormGroup.addControl(control, new FormControl(true))); } 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(JSON.parse(response)); }, 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 cached cachePlanes(): void { this.alertService.info('Flagging 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(() => { this.alertService.success('Planes Saved'); }, error => { this.alertService.error('Planes did not save. ' + error); }); } } }