All files / src/live StartProgress.ts

19.25% Statements 31/161
100% Branches 1/1
16.66% Functions 1/6
19.25% Lines 31/161

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 16292x 92x 92x 92x 92x 92x 92x                                                                                             92x                                         92x                                                                                                                     92x             92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x  
import { css, html, PropertyValueMap, TemplateResult } from 'lit';
import { RapidElement } from '../RapidElement';
import { property } from 'lit/decorators.js';
import { fetchResults, showModax } from '../utils';
 
export class StartProgress extends RapidElement {
  static styles = css`
    temba-icon[name='close'] {
      cursor: pointer;
      margin: 0 4px;
    }

    temba-icon[name='close']:hover {
      color: var(--color-primary-dark);
    }
  `;
  @property({ type: String })
  id: string;

  @property({ type: Number })
  current: number;

  @property({ type: Number })
  total: number;

  @property({ type: Number })
  refreshes: number = 0;

  @property({ type: String })
  eta: string;

  @property({ type: Boolean })
  complete = false;

  @property({ type: Boolean })
  running = false;

  @property({ type: String })
  message: string;

  @property({ type: String })
  statusEndpoint: string;

  @property({ type: String })
  interruptTitle: string;

  @property({ type: String })
  interruptEndpoint: string;

  public updated(
    changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(changes);
    if (changes.has('id')) {
      this.refresh();
    }

    // Useful for simulating progress
    /*
    if (changes.has('current')) {
      this.requestUpdate();
      setTimeout(() => {
        this.current = this.current + 100000;
        this.complete = this.current >= this.total;
        this.message = null;

        if (this.complete) {
          this.scheduleRemoval();
        }
      }, 5000);
    }*/
  }

  public interruptStart(): void {
    showModax(this.interruptTitle, this.interruptEndpoint);
  }

  public refresh(): void {
    fetchResults(this.statusEndpoint, this.getHeaders()).then((data: any) => {
      if (data.length > 0) {
        this.refreshes++;
        const start = data[0];

        this.current = start.progress.current;
        this.total = start.progress.total;

        this.complete =
          start.status == 'Completed' ||
          start.status == 'Failed' ||
          start.status == 'Interrupted';

        this.running = start.status === 'Started';

        if (start.status === 'Pending') {
          this.message = 'Preparing to start..';
        } else if (start.status === 'Queued') {
          this.message = 'Waiting..';
        } else {
          this.message = null;
        }

        if (start.status === 'Started') {
          const elapsed =
            new Date().getTime() - new Date(start.modified_on).getTime();
          const rate = this.current / elapsed;

          // only calculate eta if the rate is actually reasonable
          if (rate > 0.1) {
            const eta = new Date(
              new Date().getTime() + (this.total - this.current) / rate
            );
            // Don't bother with estimates months out
            const nextMonth = new Date();
            nextMonth.setMonth(nextMonth.getMonth() + 2);
            if (eta > nextMonth) {
              this.eta = null;
            } else {
              this.eta = eta.toISOString();
            }
          }
        }

        if (!this.complete && this.current < this.total) {
          // refresh with a backoff up to 1 minute
          setTimeout(
            () => {
              this.refresh();
            },
            Math.min(1000 * this.refreshes, 60000)
          );
        }

        if (this.complete) {
          this.scheduleRemoval();
        }
      }
    });
  }

  public scheduleRemoval(): void {
    setTimeout(() => {
      this.remove();
    }, 5000);
  }
 
  public render(): TemplateResult {
    return html`<temba-progress
      total=${this.total}
      current=${this.current}
      eta=${this.eta}
      message=${this.message}
    >
      ${this.running && this.interruptTitle && this.interruptEndpoint
        ? html`<temba-icon
            name="close"
            @click=${this.interruptStart}
          ></temba-icon>`
        : null}
    </temba-progress>`;
  }
}