Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | import {Component, Input, OnInit} from '@angular/core'; |
| 2 | import * as _ from 'lodash'; |
| 3 | import {SdcService} from '../services/sdc.service'; |
| 4 | import {DialogService} from 'ng2-bootstrap-modal'; |
| 5 | import {Constants} from '../shared/utils/constants'; |
| 6 | import {CustomTableColumnDefinition, CustomTableOptions} from './vid-table/vid-table.component'; |
| 7 | import {ServicePopupComponent} from "../components/service-popup/service-popup.component"; |
| 8 | import { PreviousVersionsComponent } from './previous-versions/previous-versions.component'; |
| 9 | |
| 10 | @Component({ |
| 11 | selector: 'browse-sdc', |
| 12 | templateUrl: './browseSdc.component.html', |
| 13 | styleUrls: ['./browseSdc.component.scss'] |
| 14 | }) |
| 15 | |
| 16 | |
| 17 | export class BrowseSdcComponent implements OnInit { |
| 18 | |
| 19 | isSpinnerVisible = false; |
| 20 | isProgressVisible = false; |
| 21 | error: boolean; |
| 22 | status: string; |
| 23 | // table |
| 24 | |
| 25 | private basicColumns: CustomTableColumnDefinition[]; |
| 26 | @Input () filterQuery = ''; |
| 27 | tableOptions: CustomTableOptions; |
| 28 | private wholeData: any[]; |
| 29 | |
| 30 | constructor(private _sdcService: SdcService, private dialogService: DialogService) {} |
| 31 | |
| 32 | initTableOptions() { |
| 33 | this.basicColumns = [ |
| 34 | { displayName: 'Action', key: 'action', type: 'button' , text: 'deploy', action: 'deploy' }, |
| 35 | { displayName: 'UUID', key: 'uuid', filter: 'text'}, |
| 36 | { displayName: 'invariantUUID', key: 'invariantUUID', filter: 'text'}, |
| 37 | { displayName: 'Name', key: 'name', filter: 'text'}, |
| 38 | { displayName: 'Version', key: 'version', filter: 'text'}, |
| 39 | { displayName: 'Category', key: 'category', filter: 'text'}, |
| 40 | { displayName: 'Distribution Status', key: 'distributionStatus', filter: 'text'}, |
| 41 | { displayName: 'Last Updated By', key: 'lastUpdaterUserId', filter: 'text'}, |
| 42 | { displayName: 'Tosca Model', key: 'toscaModelUrl', filter: 'text'} |
| 43 | ]; |
| 44 | |
| 45 | let columns: CustomTableColumnDefinition[] = this.basicColumns.concat( |
| 46 | {displayName: 'Action', key: 'actions', type: 'button', text: 'Previous Versions', |
| 47 | showCondition: 'hasPreviousVersion', action: 'loadPreviousData' } |
| 48 | ); |
| 49 | |
| 50 | this.tableOptions = { |
| 51 | data: [], |
| 52 | columns: columns, |
| 53 | config: { |
| 54 | sortBy: 'name', |
| 55 | sortOrder: 'asc', |
| 56 | pageSize: 10, |
| 57 | pageNumber: 1, |
| 58 | totalCount: 0, |
| 59 | totalPages: 0, |
| 60 | maxSize: 10, |
| 61 | showSelectCheckbox: true, |
| 62 | showSelectAll: true, |
| 63 | showSort: true, |
| 64 | clientSort: true, |
| 65 | clientPaging: true, |
| 66 | // displayPager: true, |
| 67 | // displayPageSize: true, |
| 68 | stickyHeader: true, |
| 69 | stickyHeaderOffset: 0, |
| 70 | stickyContainer: '.table1-container' |
| 71 | }, |
| 72 | }; |
| 73 | } |
| 74 | private deploy(service: any): void { |
| 75 | if (service) { |
| 76 | console.log('this row uuid:' + service.uuid); |
| 77 | } |
| 78 | |
| 79 | this.dialogService.addDialog(ServicePopupComponent, { |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | private filterDataWithHigherVersion(serviceData) { |
| 84 | let delimiter = '$$'; |
| 85 | let filterDataServices = {}; |
| 86 | for (let i = 0; i < serviceData.length; i++) { |
| 87 | let index = serviceData[i].invariantUUID.trim() + delimiter + serviceData[i].name.trim(); |
| 88 | if (!filterDataServices[index]) { |
| 89 | filterDataServices[index] = { |
| 90 | service: serviceData[i], |
| 91 | hasPreviousVersion: false |
| 92 | }; |
| 93 | } else { |
| 94 | filterDataServices[index].hasPreviousVersion = true; |
| 95 | if (parseFloat(serviceData[i].version.trim()) > parseFloat(filterDataServices[index].service.version.trim())) { |
| 96 | filterDataServices[index].service = serviceData[i]; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | return Object.keys(filterDataServices).map(function (key) { |
| 101 | let service = filterDataServices[key].service; |
| 102 | service.hasPreviousVersion = filterDataServices[key].hasPreviousVersion; |
| 103 | return service; |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | private initData() { |
| 108 | this.isProgressVisible = true; |
| 109 | this.isSpinnerVisible = true; |
| 110 | console.log('getServicesModels: '); |
| 111 | this._sdcService.getServicesModels().subscribe( |
| 112 | // onNext() function |
| 113 | value => this.getServiceCallback(value), |
| 114 | // onError() function |
| 115 | error => this.getServiceOnError(error), |
| 116 | // onComplete() function |
| 117 | () => console.log('completed') |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | private getServiceCallback(responseBody: any): void { |
| 122 | console.log('response is ' , responseBody); |
| 123 | this.wholeData = responseBody.services; |
| 124 | this.tableOptions.data = this.filterDataWithHigherVersion(responseBody.services); |
| 125 | this.isSpinnerVisible = false; |
| 126 | this.isProgressVisible = false; |
| 127 | } |
| 128 | private getServiceOnError(error: any): void { |
| 129 | console.log('error is ' , error); |
| 130 | this.status = Constants.Status.FAILED_SERVICE_MODELS_ASDC; |
| 131 | this.error = true; |
| 132 | this.isSpinnerVisible = false; |
| 133 | } |
| 134 | |
| 135 | private loadPreviousVersionData(service): void { |
| 136 | let previousVersionData: any[] = _.filter(this.wholeData, function(item) { |
| 137 | return item.invariantUUID === service.invariantUUID && item.name === service.name && service.version !== item.version; |
| 138 | }); |
| 139 | |
| 140 | let modalTableOptions: CustomTableOptions = { |
| 141 | data: previousVersionData, |
| 142 | columns: this.basicColumns, |
| 143 | config: { |
| 144 | sortBy: 'version', |
| 145 | sortOrder: 'desc'} |
| 146 | }; |
| 147 | // open modal |
| 148 | this.dialogService.addDialog(PreviousVersionsComponent, { |
| 149 | title: service.name + ' - Previous Version', |
| 150 | tableOptions: modalTableOptions |
| 151 | }) |
| 152 | .subscribe( service => { |
| 153 | if (service) { |
| 154 | this.deploy(service); |
| 155 | } |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | public clickAction(row) { |
| 161 | switch (row.actionName) { |
| 162 | case 'loadPreviousData': |
| 163 | this.loadPreviousVersionData(row); |
| 164 | break; |
| 165 | case 'deploy': |
| 166 | this.deploy(row); |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | ngOnInit() { |
| 172 | console.log('Browse SDC Service Models'); |
| 173 | this.initTableOptions(); |
| 174 | this.initData(); |
| 175 | } |
| 176 | } |