added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / layout / test-instances-catalog / test-instances-catalog.component.ts
diff --git a/otf-frontend/client/src/app/layout/test-instances-catalog/test-instances-catalog.component.ts b/otf-frontend/client/src/app/layout/test-instances-catalog/test-instances-catalog.component.ts
new file mode 100644 (file)
index 0000000..cb5a6e5
--- /dev/null
@@ -0,0 +1,436 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+import { Component, OnInit, ViewChild, ViewContainerRef, AfterViewInit, OnDestroy, ViewChildren, ElementRef, QueryList } from '@angular/core';\r
+import { MatDialog, MatPaginator, MatSnackBar, MatTableDataSource, MatListItem } from '@angular/material';\r
+import { ActivatedRoute, Router } from '@angular/router';\r
+import { ListService } from '../../shared/services/list.service';\r
+import { AlertModalComponent } from '../../shared/modules/alert-modal/alert-modal.component';\r
+import { TestInstanceService } from '../../shared/services/test-instance.service';\r
+import { routerTransition } from 'app/router.animations';\r
+import { SchedulingService } from '../../shared/services/scheduling.service';\r
+import { TestInstanceModalComponent } from '../../shared/modules/test-instance-modal/test-instance-modal.component';\r
+import { AlertSnackbarComponent } from '../../shared/modules/alert-snackbar/alert-snackbar.component';\r
+import { ScheduleTestModalComponent } from '../../shared/modules/schedule-test-modal/schedule-test-modal.component';\r
+import { animate, state, style, transition, trigger } from '@angular/animations';\r
+import { TestDefinitionService } from '../../shared/services/test-definition.service';\r
+import { Observable, Subscription } from 'rxjs';\r
+import { ExecuteService } from 'app/shared/services/execute.service';\r
+import { GroupService } from 'app/shared/services/group.service';\r
+import { GridOptions } from "ag-grid-community";\r
+\r
+\r
+@Component({\r
+    selector: 'app-test-instances-catalog',\r
+    templateUrl: './test-instances-catalog.component.pug',\r
+    styleUrls: ['./test-instances-catalog.component.scss'],\r
+    animations: [routerTransition(),\r
+    trigger('detailExpand', [\r
+        state('collapsed', style({ height: '0px', minHeight: '0', display: 'none' })),\r
+        state('expanded', style({ height: '*' })),\r
+        transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),\r
+    ])\r
+    ]\r
+})\r
+export class TestInstancesCatalogComponent implements OnInit, AfterViewInit, OnDestroy {\r
+\r
+\r
+    public resultsLength;\r
+    public loading = false;\r
+\r
+\r
+    public columnDefs = [\r
+        {headerName: '', field: 'disabled', cellRenderer: this.disabledIndicator, width: 100, hide: false, checkboxSelection:true, headerCheckboxSelection: true, headerCheckboxSelectionFilteredOnly: true},\r
+        {headerName: 'Name', field: 'testInstanceName', sortable: true, filter: true, resizable: true, width: 300},\r
+        {headerName: 'Description', field: 'testInstanceDescription', sortable: true, filter: true, resizable: true, width: 100},\r
+        {headerName: 'Id', field: '_id', sortable: true, filter: true, resizable: true, width: 200, editable: true},\r
+        {headerName: 'Test Definition', field: 'testDefinitionId.testName', sortable: true, filter: true, resizable: true}\r
+        \r
+      ];\r
+\r
+    public selectedRows;\r
+    public hasSelectedRows;\r
+    public selectedSingleRow;\r
+    private gridApi;\r
+    private gridColumnApi;\r
+    public selectedUnlockedRows;\r
+    public rowData;\r
+\r
+    public gridOptions: GridOptions;\r
+\r
+    \r
+    public params;\r
+\r
+    private subscriptions: Subscription[] = [];\r
+\r
+    @ViewChild(MatPaginator) paginator: MatPaginator;\r
+\r
+    constructor(\r
+\r
+        private router: Router,\r
+        private viewRef: ViewContainerRef,\r
+        private testInstance: TestInstanceService,\r
+        private modal: MatDialog,\r
+        private schedulingService: SchedulingService,\r
+        private snack: MatSnackBar,\r
+        private route: ActivatedRoute,\r
+        private testDefinitionService: TestDefinitionService,\r
+        private _execute: ExecuteService,\r
+        private _groups: GroupService\r
+    ) {  }\r
+\r
+    ngOnInit() {\r
+        \r
+        this.setComponentData(this._groups.getGroup());\r
+\r
+        this.subscriptions.push(this._groups.groupChange().subscribe(group => {\r
+            this.setComponentData(group);\r
+            \r
+        }));\r
+\r
+        // this.subscriptions.push(this._groups.groupChange().subscribe( group => {\r
+        //     if(!group["_id"]){\r
+        //         this.setComponentData(this._groups.getGroup());\r
+        //     }\r
+        //     this.setComponentData(group);\r
+        // }));\r
+\r
+\r
+        this.route.queryParams.subscribe( params => {\r
+           \r
+            this.params = params;\r
+\r
+\r
+        });\r
+\r
+\r
+    }\r
+\r
+    setComponentData(group) {\r
+\r
+        if(!group){\r
+            return;\r
+        }\r
+        this.loading = true;\r
+        let params = {\r
+            groupId: group['_id'],\r
+            $limit: -1,\r
+            $populate: ['testDefinitionId'],\r
+            $sort: {\r
+                createdAt: -1\r
+            },\r
+            $select: ['testInstanceName', 'testInstanceDescription', 'testDefinitionId.testName', 'disabled']\r
+        }\r
+\r
+        if (this.route.snapshot.params['filter']) {\r
+            params['_id'] = this.route.snapshot.params['filter'];\r
+        }\r
+\r
+        this.testInstance.find(params).subscribe((list) => {\r
+            this.resultsLength = list['length'];\r
+            this.loading = false;\r
+            this.rowData = list;\r
+        },\r
+        err => {\r
+            console.log(err);\r
+        });\r
+        \r
+    }\r
+\r
+    ngAfterViewInit() {\r
+   \r
+    }\r
+\r
+    ngOnDestroy() {\r
+        this.subscriptions.forEach(e => e.unsubscribe());\r
+    }\r
+\r
+\r
+    schedule() {\r
+        this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName, testDefinitionId}) => ({_id, testInstanceName, testDefinitionId}));\r
+        console.log("The new element is: "+JSON.stringify(this.selectedRows[0]._id));\r
+        \r
+        console.log("Here is the selected Row: "+JSON.stringify(this.gridApi.getSelectedRows()[0]));\r
+        const dialogRef = this.modal.open(ScheduleTestModalComponent, {\r
+            width: '90%',\r
+            data: {\r
+                id: this.selectedRows[0]._id\r
+            }\r
+        });\r
+\r
+        dialogRef.afterClosed().subscribe(result => {\r
+            /*if(result != ''){\r
+              this.test_instance_selected = result;\r
+              this.strategy_selected = true;\r
+            }else{\r
+              this.strategy_selected = false;\r
+            }*/\r
+        });\r
+    }\r
+\r
+    executeMultipleTestInstance(){\r
+        for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
+            this.executeTestInstance(i);\r
+        }\r
+    }\r
+\r
+    executeTestInstance(ti) {\r
+        \r
+        this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName, testDefinitionId}) => ({_id, testInstanceName, testDefinitionId}));\r
+\r
+\r
+        if (this.selectedRows[ti].testDefinitionId) {\r
+            const executer = this.modal.open(AlertModalComponent, {\r
+                width: '250px',\r
+                data: {\r
+                    type: 'confirmation',\r
+                    message: 'Are you sure you want to run ' + this.selectedRows[ti].testInstanceName + '?'\r
+                }\r
+            });\r
+\r
+            executer.afterClosed().subscribe(result => {\r
+                if (result) {\r
+                    this._execute.create({\r
+                        _id: this.selectedRows[ti]._id,\r
+                        async: true\r
+                    }).subscribe((result) => {\r
+                        console.log(result);\r
+                        if (result) {\r
+                            this.snack.openFromComponent(AlertSnackbarComponent, {\r
+                                duration: 1500,\r
+                                data: {\r
+                                    message: 'Test Instance Executed'\r
+                                }\r
+                            });\r
+                        }\r
+                    },\r
+                        (error) => {\r
+                            console.log(error);\r
+                            this.modal.open(AlertModalComponent, {\r
+                                width: '450px',\r
+                                data: {\r
+                                    type: 'Alert',\r
+                                    message: 'Failed to execute Test Instance!\n' + JSON.stringify(error)\r
+                                }\r
+                            });\r
+                        })\r
+                }\r
+            });\r
+        }\r
+\r
+    }\r
+\r
+    createTestInstance() {\r
+        const create = this.modal.open(TestInstanceModalComponent, {\r
+            width: '90%',\r
+            data: null,\r
+            disableClose: true\r
+        });\r
+\r
+        create.afterClosed().subscribe(result => {\r
+          this.ngOnInit();\r
+        });\r
+    }\r
+\r
+\r
+    editTestInstance() {\r
+        this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName}) => ({_id, testInstanceName}));\r
+\r
+        const edit = this.modal.open(TestInstanceModalComponent, {\r
+            data: {\r
+                ti: this.selectedRows[0]._id,\r
+                isEdit: true\r
+            },\r
+            width: '90%',\r
+            disableClose: true\r
+        });\r
+\r
+        edit.afterClosed().subscribe(result => {\r
+\r
+        });\r
+    }\r
+\r
+    cloneTestInstance() {\r
+        this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName}) => ({_id, testInstanceName}));\r
+        this.testInstance.get(this.selectedRows[0]._id).subscribe(\r
+            result => {\r
+                var temp = Object.assign({}, result);\r
+                delete result['_id'];\r
+                delete result['createdAt'];\r
+                delete result['updatedAt'];\r
+                if (this.selectedRows[0].testInstanceName) {\r
+                    result['testInstanceName'] = this.selectedRows[0].testInstanceName + '_Clone';\r
+                } else {\r
+                    result['testInstanceName'] = result['testInstanceName'] + '_Clone';\r
+                }\r
+                this.testInstance.create(result).subscribe(\r
+                    resp => {\r
+                        //this.editTestInstance(resp);\r
+                        this.editTestInstance();\r
+                    },\r
+                    err => {\r
+                        if (err) {\r
+                      \r
+                            result['_id'] = temp['_id'];\r
+                           \r
+                            //this.cloneTestInstance(result);\r
+                        }\r
+                    }\r
+                );\r
+            }\r
+        )\r
+    }\r
+\r
+    deleteMultipleTestInstance(){\r
+        for(let i = 0; i < this.gridApi.getSelectedNodes().length; i++){\r
+            this.deleteTestInstance(i);\r
+        }\r
+    }\r
+\r
+    deleteTestInstance(ti) {\r
+        this.selectedRows = this.gridApi.getSelectedRows().map(({_id, testInstanceName}) => ({_id, testInstanceName}));\r
+\r
+\r
+        const deleter = this.modal.open(AlertModalComponent, {\r
+            width: '250px',\r
+            data: {\r
+                type: 'confirmation',\r
+                message: 'Are you sure you want to delete ' + this.selectedRows[ti].testInstanceName + ' ? Executions of this instance will no longer display everything.'\r
+            }\r
+        });\r
+\r
+        deleter.afterClosed().subscribe(result => {\r
+            if (result) {\r
+                this.testInstance.delete(this.selectedRows[ti]._id).subscribe(response => {\r
+                    this.snack.openFromComponent(AlertSnackbarComponent, {\r
+                        duration: 1500,\r
+                        data: {\r
+                            message: 'Test Instance Deleted'\r
+                        }\r
+                    });\r
+\r
+                });\r
+                this.setComponentData(this._groups.getGroup());\r
+            }\r
+        });\r
+        \r
+    }\r
+\r
+\r
+    disabledIndicator(params){\r
+        if (params.value){\r
+          return `<mat-icon class="mat-icon mat-icon-no-color" role="img" >\r
+           locked</mat-icon>`;   \r
+    }\r
+\r
\r
+    }\r
+\r
+    setParams(element) {\r
+   \r
+        if (JSON.stringify(element) == JSON.stringify({testInstanceId: this.params.testInstanceId})){\r
+            element = {};\r
+        }\r
+\r
+        \r
+        this.router.navigate([], {\r
+           //queryParams: {testInstanceId: element.testInstanceId, page: this.paginator.pageIndex, instancePerPage: this.paginator.pageSize }\r
+            queryParams: {testInstanceId: element._id}\r
+        })\r
+           \r
+    }\r
+\r
+    onGridReady(params){\r
+        this.gridApi = params.api;\r
+        console.log(params.columnApi.autoSizeColumns)\r
+        this.gridColumnApi = params.columnApi;\r
+    \r
+        //auto size the column widths\r
+        this.gridColumnApi.autoSizeColumns(['name']);\r
+\r
+      }\r
+\r
+      selectActiveInstance($event){\r
+        if(this.params.testInstanceId)\r
+        {\r
+            this.gridApi.forEachNode( (node, index) => {\r
+\r
+                if(node.data._id ==this.params.testInstanceId)\r
+                {\r
+                    // Pre selects the row that was last selected when on the page\r
+                    node.setSelected(true, true);\r
+                    //Vertically scrolls to that row so it is visible\r
+                    this.gridApi.ensureIndexVisible(index, "middle");\r
+                    \r
+                }\r
+              \r
+            });\r
+        }\r
+        \r
+      }\r
+\r
+\r
+    onRowSelected(event){\r
+\r
+        this.selectedRows = this.gridApi.getSelectedRows().map(({ _id, disabled, testInstanceName  }) => ({ _id, disabled, testInstanceName}));\r
+\r
+\r
+        \r
+        if(event.api.getSelectedNodes().length > 0){\r
+          this.hasSelectedRows = true;\r
+          \r
+            //Checks for all Unlocked rows\r
+            for (let i = 0; i < event.api.getSelectedNodes().length; i++ )\r
+            {\r
+                if(!this.selectedRows[i].disabled)\r
+                {\r
+                    this.selectedUnlockedRows = true;\r
+                }\r
+                else{\r
+                    this.selectedUnlockedRows = false;\r
+                    break;\r
+                }\r
+            }\r
+        }\r
+        else{\r
+          this.hasSelectedRows = false;\r
+          this.selectedUnlockedRows = false;\r
+          \r
+          this.setParams({_id: null});\r
+        }\r
+\r
+        \r
+        //One Row was selected\r
+        if((event.api.getSelectedNodes().length == 1)){\r
+          this.selectedSingleRow = true;\r
+         \r
+          this.setParams({_id: this.selectedRows[0]._id});\r
+         \r
+        }else{\r
+          this.selectedSingleRow = false;\r
+          this.setParams({_id: null});\r
+        }\r
+       \r
+    }\r
+\r
+\r
+\r
+      \r
+\r
+\r
+\r
+    \r
+}\r