Skip to content
Snippets Groups Projects
jobs.service.ts 5.46 KiB
Newer Older
import {Injectable} from '@angular/core';
import {HttpClient, HttpResponse} from "@angular/common/http";
import {ConfigurationService} from "../env/configuration.service";
import {Observable} from "rxjs";
import {map, switchMap} from "rxjs/operators";
import {Job, JobExecution, JobSpec} from "../model/job";

@Injectable({
  providedIn: 'root'
})
export class JobsService {

  endPoint: string = '/services/job/';

  constructor(private http: HttpClient, private configService: ConfigurationService) {
  }

  public getJobs(queue: string, id: string, pattern: string, status: string): Observable<Array<Job>> {
    return this.http.get<Array<Job>>(this.configService.config.url + this.endPoint + 'optimized/queues/' + queue, {observe: 'response'}).pipe(
      map((response: HttpResponse<Array<Job>>) => {
        let reply = response.body;
        if (id && id.length > 0) {
          reply = reply.filter(job => job.job_id.toString() === id);
        } else {
          if (pattern && pattern.length > 0) {
            reply = reply.filter(job => job.job_name.match(pattern.replace("+", "\\+")));
          }
          if (status && status.length > 0 && status !== 'ALL') {
            reply = reply.filter(job => job.job_status == status);
          }
        }
        return reply;
      }));
  }

  public getJobSpecs(queue: string, id: string, pattern: string, status: string): Observable<Array<Job>> {
    return this.http.get<Array<Job>>(this.configService.config.url + this.endPoint + 'optimized/queues/' + queue, {observe: 'response'}).pipe(
      map((response: HttpResponse<Array<Job>>) => {
        let reply = response.body;
        if (id && id.length > 0) {
          reply = reply.filter(job => job.jobspec_id.toString() === id);
        } else {
          if (pattern && pattern.length > 0) {
            reply = reply.filter(job => job.jobspec_name.match(pattern.replace("+", "\\+")));
          }
          if (status && status.length > 0 && status !== 'ALL') {
            reply = reply.filter(job => job.job_status == status);
          }
        }
        return reply;
      }));
  }

  public getJobSpec(id: number): Observable<JobSpec> {
    return this.http.get<JobSpec>(this.configService.config.url + this.endPoint + 'specs/' + id, {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

    summary.jobspec_id = spec.id;
    summary.jobspec_name = spec.name;
    summary.jobspec_creation_date = spec.creationDate;
    summary.jobspec_status = spec.status;
    summary.jobspec_creation_date_formatted = new Date(spec.creationDate).toLocaleString("en-US");
    summary.jobspec_sdm_id = spec.sdmId;
    for (let i = 0; i < spec.executions.length; i++) {
      const exec = spec.executions[i];
      summary.job_id = exec.id;
      summary.job_name = exec.name;
      summary.job_starttime = Math.round(new Date(exec.startDate).getTime() / 1000);
      summary.job_endtime = Math.round(new Date(exec.endDate).getTime() / 1000);
      summary.job_status = exec.status;
      summary.job_arch_status = exec.archiveStatus;
      summary.job_starttime_formatted = exec.startDate;
      summary.job_endtime_formatted = exec.endDate;
    }
    return summary;
  public getJob(id: number): Observable<any> {
    return this.http.get<JobExecution>(this.configService.config.url + this.endPoint + 'jobs/' + id, {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public createJob(id: number, queue: string, version: number): Observable<any> {
    let command = {productId: id, inputProductVersions: [version]};
    return this.http.post(this.configService.config.url + this.endPoint + 'createJob?id=' + id + '&queue=' + queue, command, {observe: "response"}).pipe(
      map(response => {
        return response.status;
      }));
  }

  public submitJob(id: number, queue: string): Observable<any> {
    return this.http.put(this.configService.config.url + this.endPoint + 'queues/' + queue + '/specs/' + id + '/submit', {}, {observe: "response"}).pipe(
      map(response => {
        return response;
      }));
  public updateNotes(id: number, notes: string): Observable<any> {
    return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/notes', {notes: notes}, {observe: "response"}).pipe(
      map(response => {
        return notes;
      }));
  }

  public updateJobStatus(id: number, status: string) {
    return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/status?status=' + status, {}, {observe: "response"}).pipe(
      map(response => {
        return status;
      }));
  }

  public performQA(id: number, status: string, queue: string) {
    return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/status?status=' + status + '&queue=' + queue, {}, {observe: "response"}).pipe(
      switchMap(response => {
        return this.http.put(this.configService.config.url + this.endPoint + 'jobs/' + id + '/ingest?status=' + status + '&queue=' + queue, {}, {observe: "response"}).pipe(
          map(response => {
Reid Givens's avatar
Reid Givens committed
            return response.status;
Reid Givens's avatar
Reid Givens committed
  public deleteJobSpec(jobSpecId: number, queue: string): Observable<number> {
    return this.http.delete<string>(this.configService.config.url + this.endPoint + 'queues/' + queue + '/specs/' + jobSpecId, {observe: "response"}).pipe(
      map(response => {
        return response.status;
      }));
  }