# All files / src/app/ceph/block/rbd-trash-list rbd-trash-list.component.ts

82.14% Statements 69/84
71.05% Branches 27/38
57.14% Functions 12/21
82.5% Lines 66/80

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
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    2084x

    4x
    4x
    4x
    4x

    4x
    4x
    4x
    4x
    4x

    4x

    4x

    4x
    4x
    4x
    4x
    4x
    4x

    4x

    4x

    4x

    4x

    8x

    8x

    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x

    8x

    8x

    8x
    8x

    8x
    5x
    2x
    2x

    4x
    2x
    2x
    2x

    2x

    2x
    2x

    2x
    2x
    2x

    2x
    2x
    4x

    2x

    4x

    4x
    10x

    4x
    3x

    4x
    1x

    4x

    4x

    4x

    4x

    4x
     import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';

    import { I18n } from '@ngx-translate/i18n-polyfill';
    import * as _ from 'lodash';
    import * as moment from 'moment';
    import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';

    import { RbdService } from '../../../shared/api/rbd.service';
    import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
    import { TableComponent } from '../../../shared/datatable/table/table.component';
    import { CellTemplate } from '../../../shared/enum/cell-template.enum';
    import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
    import { CdTableAction } from '../../../shared/models/cd-table-action';
    import { CdTableColumn } from '../../../shared/models/cd-table-column';
    import { CdTableSelection } from '../../../shared/models/cd-table-selection';
    import { ExecutingTask } from '../../../shared/models/executing-task';
    import { FinishedTask } from '../../../shared/models/finished-task';
    import { Permission } from '../../../shared/models/permissions';
    import { CdDatePipe } from '../../../shared/pipes/cd-date.pipe';
    import { AuthStorageService } from '../../../shared/services/auth-storage.service';
    import { TaskListService } from '../../../shared/services/task-list.service';
    import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
    import { RbdTrashPurgeModalComponent } from '../rbd-trash-purge-modal/rbd-trash-purge-modal.component';
    import { RbdTrashRestoreModalComponent } from '../rbd-trash-restore-modal/rbd-trash-restore-modal.component';

    @Component({
      selector: 'cd-rbd-trash-list',
      template: require('./rbd-trash-list.component.html'),
      styles: [],
      providers: [TaskListService]
    })
    export class RbdTrashListComponent implements OnInit {
      @ViewChild(TableComponent)
      table: TableComponent;
      @ViewChild('expiresTpl')
      expiresTpl: TemplateRef<any>;
      @ViewChild('deleteTpl')
      deleteTpl: TemplateRef<any>;

      columns: CdTableColumn[];
      executingTasks: ExecutingTask[] = [];
      images: any;
      modalRef: BsModalRef;
      permission: Permission;
      retries: number;
      selection = new CdTableSelection();
      tableActions: CdTableAction[];
      viewCacheStatusList: any[];

      constructor(
        private authStorageService: AuthStorageService,
        private rbdService: RbdService,
        private modalService: BsModalService,
        private cdDatePipe: CdDatePipe,
        private taskListService: TaskListService,
        private taskWrapper: TaskWrapperService,
        private i18n: I18n
      ) {
        this.permission = this.authStorageService.getPermissions().rbdImage;

        const restoreAction: CdTableAction = {
          permission: 'update',
          icon: 'fa-undo',
          click: () => this.restoreModal(),
          name: this.i18n('Restore')
        };
        const deleteAction: CdTableAction = {
          permission: 'delete',
          icon: 'fa-times',
          click: () => this.deleteModal(),
          name: this.i18n('Delete')
        };
        this.tableActions = [restoreAction, deleteAction];
      }

      ngOnInit() {
        this.columns = [
          {
            name: this.i18n('ID'),
            prop: 'id',
            flexGrow: 1,
            cellTransformation: CellTemplate.executing
          },
          {
            name: this.i18n('Name'),
            prop: 'name',
            flexGrow: 1
          },
          {
            name: this.i18n('Pool'),
            prop: 'pool_name',
            flexGrow: 1
          },
          {
            name: this.i18n('Status'),
            prop: 'deferment_end_time',
            flexGrow: 1,
            cellTemplate: this.expiresTpl
          },
          {
            name: this.i18n('Deleted At'),
            prop: 'deletion_time',
            flexGrow: 1,
            pipe: this.cdDatePipe
          }
        ];

        this.taskListService.init(
          () => this.rbdService.listTrash(),
          (resp) => this.prepareResponse(resp),
          (images) => (this.images = images),
          () => this.onFetchError(),
          this.taskFilter,
          this.itemFilter,
          undefined
        );
      }

      prepareResponse(resp: any[]): any[] {
        let images = [];
        const viewCacheStatusMap = {};
        resp.forEach((pool) => {
          if (_.isUndefined(viewCacheStatusMap[pool.status])) {
            viewCacheStatusMap[pool.status] = [];
          }
          viewCacheStatusMap[pool.status].push(pool.pool_name);
          images = images.concat(pool.value);
        });

        const viewCacheStatusList = [];
        _.forEach(viewCacheStatusMap, (value: any, key) => {
          viewCacheStatusList.push({
            status: parseInt(key, 10),
            statusFor:
              (value.length > 1 ? 'pools ' : 'pool ') +
              '<strong>' +
              value.join('</strong>, <strong>') +
              '</strong>'
          });
        });
        this.viewCacheStatusList = viewCacheStatusList;
        images.forEach((image) => {
          image.cdIsExpired = moment().isAfter(image.deferment_end_time);
        });
        return images;
      }

      onFetchError() {
        this.table.reset(); // Disable loading indicator.
        this.viewCacheStatusList = [{ status: ViewCacheStatus.ValueException }];
      }

      itemFilter(entry, task) {
        return entry.id === task.metadata['image_id'];
      }

      taskFilter(task) {
        return ['rbd/trash/remove', 'rbd/trash/restore'].includes(task.name);
      }

      updateSelection(selection: CdTableSelection) {
        this.selection = selection;
      }

      restoreModal() {
        const initialState = {
          metaType: 'RBD',
          poolName: this.selection.first().pool_name,
          imageName: this.selection.first().name,
          imageId: this.selection.first().id
        };

        this.modalRef = this.modalService.show(RbdTrashRestoreModalComponent, { initialState });
      }

      deleteModal() {
        const poolName = this.selection.first().pool_name;
        const imageName = this.selection.first().name;
        const imageId = this.selection.first().id;
        const expiresAt = this.selection.first().deferment_end_time;

        this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
          initialState: {
            itemDescription: 'RBD',
            bodyTemplate: this.deleteTpl,
            bodyContext: { $implicit: expiresAt },
            submitActionObservable: () =>
              this.taskWrapper.wrapTaskAroundCall({
                task: new FinishedTask('rbd/trash/remove', {
                  pool_name: poolName,
                  image_id: imageId,
                  image_name: imageName
                }),
                call: this.rbdService.removeTrash(poolName, imageId, imageName, true)
              })
          }
        });
      }

      isExpired(expiresAt): boolean {
        return moment().isAfter(expiresAt);
      }

      purgeModal() {
        this.modalService.show(RbdTrashPurgeModalComponent);
      }
    }