dialog.ts 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
import {HttpClient, HttpEventType, HttpParams, HttpRequest, HttpResponse} from '@angular/common/http';
16
import {Component, Inject, OnDestroy} from '@angular/core';
S
Sebastian Florek 已提交
17
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
18
import {LogOptions} from '@api/root.api';
19
import {saveAs} from 'file-saver';
20 21
import {Subject, Subscription} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
22

23
import {LogService} from '../../services/global/logs';
24 25 26 27

export interface LogsDownloadDialogMeta {
  pod: string;
  container: string;
S
Sebastian Florek 已提交
28
  namespace: string;
29 30 31 32 33 34 35 36 37 38
}

@Component({
  selector: 'kd-logs-download-dialog',
  templateUrl: 'template.html',
  styleUrls: ['style.scss'],
})
export class LogsDownloadDialog implements OnDestroy {
  loaded = 0;
  finished = false;
39 40 41 42 43 44 45 46 47 48 49

  private _result: Blob;
  private _error: number;
  private _unsubscribe = new Subject<void>();

  private get _logOptions(): LogOptions {
    return {
      previous: this.logService.getPrevious(),
      timestamps: this.logService.getShowTimestamp(),
    };
  }
50 51

  constructor(
52
    private readonly _dialogRef: MatDialogRef<LogsDownloadDialog>,
S
Shu Muto 已提交
53 54
    @Inject(MAT_DIALOG_DATA) public data: LogsDownloadDialogMeta,
    private readonly logService: LogService,
55
    private readonly http_: HttpClient
S
Shu Muto 已提交
56
  ) {
57
    const logUrl = `api/v1/log/file/${data.namespace}/${data.pod}/${data.container}`;
58

59 60 61 62 63 64 65 66 67 68
    this.http_
      .request(
        new HttpRequest(
          'GET',
          logUrl,
          {},
          {reportProgress: true, responseType: 'blob', params: new HttpParams({fromObject: this._logOptions})}
        )
      )
      .pipe(takeUntil(this._unsubscribe))
S
Shu Muto 已提交
69 70 71 72 73 74
      .subscribe(
        event => {
          if (event.type === HttpEventType.DownloadProgress) {
            this.loaded = event.loaded;
          } else if (event instanceof HttpResponse) {
            this.finished = true;
75
            this._result = new Blob([event.body as BlobPart], {type: 'text/plan'});
S
Shu Muto 已提交
76 77
          }
        },
78
        error => (this._error = error.status)
S
Shu Muto 已提交
79
      );
80 81 82
  }

  ngOnDestroy(): void {
83 84
    this._unsubscribe.next();
    this._unsubscribe.complete();
85 86 87
  }

  hasForbiddenError(): boolean {
88
    return this._error !== undefined && this._error === 403;
89 90 91
  }

  save(): void {
92 93
    saveAs(this._result, this.logService.getLogFileName(this.data.pod, this.data.container));
    this._dialogRef.close();
94 95 96
  }

  abort(): void {
97
    this._dialogRef.close();
98 99 100 101 102 103
  }

  getDownloadMode(): string {
    return this.finished ? 'determinate' : 'indeterminate';
  }
}