All files / src/models workflow.ts

30% Statements 9/30
26.66% Branches 4/15
37.5% Functions 3/8
31.03% Lines 9/29

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    1x 1x           1x           1x             1x                                                                   1x 1x 1x                                                                                                                               4x                                                                                                            
import { Model } from './model';
 
export namespace IWorkflow {
  export const Cpu = [
    { label: '0.1', value: 0.1 },
    { label: '0.25', value: 0.25 },
    { label: '0.5', value: 0.5 },
    { label: '1', value: 1 },
  ];
  export const Memory = [
    { label: '100 MB', value: 100 * 1000 * 1000 },
    { label: '250 MB', value: 250 * 1000 * 1000 },
    { label: '500 MB', value: 500 * 1000 * 1000 },
    { label: '1 GB', value: 1 * 1000 * 1000 * 1000 },
  ];
  export const Parallelisms = [
    { label: '1', value: 1 },
    { label: '2', value: 2 },
    { label: '3', value: 3 },
    { label: '4', value: 4 },
    { label: '5', value: 5 },
  ];
  export const Storage = [
    { label: '5 GB', value: 5 * 1000 * 1000 * 1000 },
    { label: '10 GB', value: 10 * 1000 * 1000 * 1000 },
    { label: '20 GB', value: 20 * 1000 * 1000 * 1000 },
  ];
 
  export interface Dag {
    tasks: Task[];
  }
 
  export interface Env {
    name: string;
    value: string;
  }
 
  export interface Node {
    _id?: string;
    children?: string[];
    displayName?: string;
    finishedAt?: Date;
    message?: string;
    name?: string;
    outboundNodes?: string[];
    phase?: string;
    startedAt?: Date;
    templatename?: string;
    type?: string;
  }
 
  export interface RetryStrategy {
    limit: number;
    retryPolicy: RetryStrategyRetryPolicy;
  }
 
  export enum RetryStrategyRetryPolicy {
    OnError = 'OnError',
    OnFailure = 'OnFailure',
  }
 
  export interface Script {
    args?: string[];
    command?: string[];
    env?: Env[];
    image: string;
    source: string;
    workingDir?: string;
  }
 
  export interface Sidecar {
    args?: string[];
    command?: string[];
    env?: Env[];
    image: string;
    name: string;
  }
 
  export interface Spec {
    entrypoint: string;
    parallelism: number;
    templates?: Template[];
  }
 
  export interface Status {
    finishedAt?: Date;
    message?: string;
    nodes?: Node[];
    phase?: string;
    startedAt?: Date;
    version?: string;
  }
 
  export interface Task {
    dependencies?: string[];
    name: string;
    template: string;
  }
 
  export interface Template {
    dag?: Dag;
    name: string;
    retryStrategy?: RetryStrategy;
    script: Script;
    sidecars?: Sidecar[];
  }
}
 
export class Workflow extends Model {
  public _id: string;
  public cpu: number;
  public createdAt: Date;
  public preemptible: boolean;
  public memory: number;
  public name: string;
  public namespaceId: string;
  public spec: IWorkflow.Spec;
  public status: IWorkflow.Status;
  public storage: number;
  public updatedAt: Date;
 
  constructor(params: Partial<Workflow> = {}) {
    super(params);
  }
 
  public getNestedStatusNodes() {
    const nodes = JSON.parse(JSON.stringify(this.status.nodes));
    const sortedNodes = nodes.sort((a, b) => {
      if (a.startedAt === b.startedAt) {
        return 0;
      }
 
      return a.startedAt > b.startedAt ? 1 : -1;
    });
 
    for (const node of sortedNodes) {
      if (node.children) {
        for (const childId of node.children) {
          const child = sortedNodes.find(n => n._id === childId);
          child.parent = node._id;
        }
      }
    }
 
    const children = this.getChildren(sortedNodes);
 
    return [
      {
        children,
        finishedAt: this.status.finishedAt,
        message: this.status.message,
        phase: this.status.phase,
        startedAt: this.status.startedAt,
        type: 'Workflow',
      },
    ];
  }
 
  private getChildren(data, parent?) {
    return data.reduce((previous, current) => {
      const obj = Object.assign({}, current);
 
      if (parent === current.parent) {
        const children = this.getChildren(data, current._id);
 
        if (children.length) {
          obj.children = children;
        }
 
        previous.push(obj);
      }
 
      return previous;
    }, []);
  }
}