summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/browseSdc/browseSdc.component.ts
blob: 46f37dc2ec4531c0fb6940b3d62141eefb3dcc4e (plain)
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
import {Component, Input, OnInit} from '@angular/core';
import * as _ from 'lodash';
import {SdcService} from '../services/sdc.service';
import {DialogService} from 'ng2-bootstrap-modal';
import {Constants} from '../shared/utils/constants';
import {CustomTableColumnDefinition, CustomTableOptions} from './vid-table/vid-table.component';
import {ServicePopupComponent} from "../components/service-popup/service-popup.component";
import { PreviousVersionsComponent } from './previous-versions/previous-versions.component';

@Component({
  selector: 'browse-sdc',
  templateUrl: './browseSdc.component.html',
  styleUrls: ['./browseSdc.component.scss']
})


export class BrowseSdcComponent implements OnInit {

  isSpinnerVisible = false;
  isProgressVisible = false;
  error: boolean;
  status: string;
  // table

  private basicColumns: CustomTableColumnDefinition[];
  @Input () filterQuery = '';
  tableOptions: CustomTableOptions;
  private wholeData: any[];

  constructor(private _sdcService: SdcService, private dialogService: DialogService) {}

  initTableOptions() {
    this.basicColumns = [
      { displayName: 'Action', key: 'action', type: 'button' , text: 'deploy', action: 'deploy' },
      { displayName: 'UUID', key: 'uuid', filter: 'text'},
      { displayName: 'invariantUUID', key: 'invariantUUID', filter: 'text'},
      { displayName: 'Name', key: 'name', filter: 'text'},
      { displayName: 'Version', key: 'version', filter: 'text'},
      { displayName: 'Category', key: 'category', filter: 'text'},
      { displayName: 'Distribution Status', key: 'distributionStatus', filter: 'text'},
      { displayName: 'Last Updated By', key: 'lastUpdaterUserId', filter: 'text'},
      { displayName: 'Tosca Model', key: 'toscaModelUrl', filter: 'text'}
    ];

    let columns: CustomTableColumnDefinition[] = this.basicColumns.concat(
      {displayName: 'Action', key: 'actions', type: 'button', text: 'Previous Versions',
        showCondition: 'hasPreviousVersion', action: 'loadPreviousData' }
    );

    this.tableOptions = {
      data: [],
      columns: columns,
      config: {
        sortBy: 'name',
        sortOrder: 'asc',
        pageSize: 10,
        pageNumber: 1,
        totalCount: 0,
        totalPages: 0,
        maxSize: 10,
        showSelectCheckbox: true,
        showSelectAll: true,
        showSort: true,
        clientSort: true,
        clientPaging: true,
        // displayPager: true,
        // displayPageSize: true,
        stickyHeader: true,
        stickyHeaderOffset: 0,
        stickyContainer: '.table1-container'
      },
    };
  }
  private deploy(service: any): void {
    if (service) {
      console.log('this row uuid:' + service.uuid);
    }

    this.dialogService.addDialog(ServicePopupComponent, {
    })
  }

  private filterDataWithHigherVersion(serviceData) {
    let delimiter = '$$';
    let filterDataServices = {};
    for (let i = 0; i < serviceData.length; i++) {
      let index = serviceData[i].invariantUUID.trim() + delimiter + serviceData[i].name.trim();
      if (!filterDataServices[index]) {
        filterDataServices[index] = {
          service: serviceData[i],
          hasPreviousVersion: false
        };
      } else {
        filterDataServices[index].hasPreviousVersion = true;
        if (parseFloat(serviceData[i].version.trim()) > parseFloat(filterDataServices[index].service.version.trim())) {
          filterDataServices[index].service = serviceData[i];
        }
      }
    }
    return Object.keys(filterDataServices).map(function (key) {
      let service = filterDataServices[key].service;
      service.hasPreviousVersion = filterDataServices[key].hasPreviousVersion;
      return service;
    });
  }

  private initData() {
    this.isProgressVisible = true;
    this.isSpinnerVisible = true;
    console.log('getServicesModels: ');
    this._sdcService.getServicesModels().subscribe(
      // onNext() function
      value => this.getServiceCallback(value),
      // onError() function
      error => this.getServiceOnError(error),
      // onComplete() function
      () => console.log('completed')
    );
  }

  private getServiceCallback(responseBody: any): void {
    console.log('response is ' , responseBody);
    this.wholeData = responseBody.services;
    this.tableOptions.data = this.filterDataWithHigherVersion(responseBody.services);
    this.isSpinnerVisible = false;
    this.isProgressVisible = false;
  }
  private getServiceOnError(error: any): void {
    console.log('error is ' , error);
    this.status =  Constants.Status.FAILED_SERVICE_MODELS_ASDC;
    this.error = true;
    this.isSpinnerVisible = false;
  }

  private loadPreviousVersionData(service): void {
    let previousVersionData: any[] = _.filter(this.wholeData, function(item) {
      return item.invariantUUID === service.invariantUUID && item.name === service.name && service.version !== item.version;
    });

    let modalTableOptions: CustomTableOptions = {
      data: previousVersionData,
      columns: this.basicColumns,
      config: {
        sortBy: 'version',
        sortOrder: 'desc'}
    };
    // open modal
    this.dialogService.addDialog(PreviousVersionsComponent, {
      title: service.name + ' - Previous Version',
      tableOptions: modalTableOptions
    })
      .subscribe( service => {
        if (service) {
          this.deploy(service);
        }
    });
  }


  public clickAction(row) {
    switch (row.actionName) {
      case 'loadPreviousData':
        this.loadPreviousVersionData(row);
        break;
      case 'deploy':
        this.deploy(row);
        break;
    }
  }

  ngOnInit() {
    console.log('Browse SDC Service Models');
    this.initTableOptions();
    this.initData();
  }
}