JSON objects now displaying as string
[so.git] / so-monitoring / so-monitoring-ui / src / main / frontend / src / app / details / details.component.ts
1 /**\r
2 ============LICENSE_START=======================================================\r
3  Copyright (C) 2018 Ericsson. All rights reserved.\r
4 ================================================================================\r
5 Licensed under the Apache License, Version 2.0 (the "License");\r
6 you may not use this file except in compliance with the License.\r
7 You may obtain a copy of the License at\r
8 \r
9     http://www.apache.org/licenses/LICENSE-2.0\r
10 \r
11 Unless required by applicable law or agreed to in writing, software\r
12 distributed under the License is distributed on an "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 See the License for the specific language governing permissions and\r
15  limitations under the License.\r
16 \r
17 SPDX-License-Identifier: Apache-2.0\r
18 ============LICENSE_END=========================================================\r
19 \r
20 @authors: ronan.kenny@ericsson.com, waqas.ikram@ericsson.com\r
21 */\r
22 \r
23 import { Component, OnInit } from '@angular/core';\r
24 import { DataService } from '../data.service';\r
25 import { ActivatedRoute, Router } from "@angular/router";\r
26 import { Process } from '../model/process.model';\r
27 import { ACTINST } from '../model/activityInstance.model';\r
28 import { PII } from '../model/processInstance.model';\r
29 import { PDI } from '../model/processDefinition.model';\r
30 import { CommonModule } from '@angular/common';\r
31 import Viewer from 'bpmn-js/lib/NavigatedViewer';\r
32 import { ViewEncapsulation } from '@angular/core';\r
33 import { MatTabsModule } from '@angular/material/tabs';\r
34 import { VarInstance } from '../model/variableInstance.model';\r
35 import { ToastrNotificationService } from '../toastr-notification-service.service';\r
36 import { NgxSpinnerService } from 'ngx-spinner';\r
37 \r
38 @Component({\r
39   selector: 'app-details',\r
40   templateUrl: './details.component.html',\r
41   styleUrls: ['./details.component.scss'],\r
42   encapsulation: ViewEncapsulation.None\r
43 })\r
44 \r
45 export class DetailsComponent implements OnInit {\r
46   bpmnViewer: any;\r
47 \r
48   processInstanceID: string;\r
49 \r
50   processDefinitionID: string;\r
51 \r
52   processDefinitionName: string;\r
53 \r
54   activityInstance: ACTINST[];\r
55 \r
56   processInstance: PII;\r
57 \r
58   processDefinition: PDI;\r
59 \r
60   variableInstance: VarInstance[];\r
61 \r
62   displayedColumns = ['activityId', 'activityName', 'activityType', 'startTime', 'endTime', 'durationInMillis'];\r
63 \r
64   displayedColumnsVariable = ['name', 'type', 'value'];\r
65 \r
66   constructor(private route: ActivatedRoute, private data: DataService, private popup: ToastrNotificationService,\r
67     private router: Router, private spinner: NgxSpinnerService) { }\r
68 \r
69   async getActInst(procInstId: string) {\r
70     await this.data.getActivityInstance(procInstId).then(\r
71       (data: ACTINST[]) => {\r
72         this.activityInstance = data;\r
73         console.log(data);\r
74       }, error => {\r
75         console.log(error);\r
76         this.popup.error("Unable to get activity instance details for id: " + procInstId + " Error code:" + error.status);\r
77       });\r
78   }\r
79 \r
80   async getProcessDefinition(procDefId) {\r
81     await this.data.getProcessDefinition(procDefId).subscribe(\r
82       async (data: PDI) => {\r
83         this.processDefinition = data;\r
84         console.log(data);\r
85         await this.displayCamundaflow(this.processDefinition.processDefinitionXml, this.activityInstance, this.router);\r
86       }, error => {\r
87         console.log(error);\r
88         this.popup.error("Unable to get process definition for id: " + procDefId + " Error code:" + error.status);\r
89       });\r
90   }\r
91 \r
92   async getProcInstance(procInstId) {\r
93     await this.data.getProcessInstance(procInstId).then(\r
94       async (data: PII) => {\r
95         this.processInstance = data;\r
96         this.processDefinitionID = this.processInstance.processDefinitionId;\r
97         this.processDefinitionName = this.processInstance.processDefinitionName;\r
98         console.log("Process definition id: " + this.processDefinitionID);\r
99         await this.getActInst(this.processInstanceID);\r
100         await this.getProcessDefinition(this.processDefinitionID);\r
101       }, error => {\r
102         console.log(error);\r
103         this.popup.error("Unable to get process instance for id: " + procInstId + " Error code:" + error.status);\r
104       });\r
105   }\r
106 \r
107   displayCamundaflow(bpmnXml, activities: ACTINST[], r: Router) {\r
108     this.spinner.show();\r
109 \r
110     this.bpmnViewer.importXML(bpmnXml, (error) => {\r
111       if (error) {\r
112         console.error('Unable to load BPMN flow ', error);\r
113         this.popup.error('Unable to load BPMN flow ');\r
114         this.spinner.hide();\r
115       } else {\r
116         this.spinner.hide();\r
117         let canvas = this.bpmnViewer.get('canvas');\r
118         var eventBus = this.bpmnViewer.get('eventBus');\r
119         eventBus.on('element.click', function(e) {\r
120 \r
121           activities.forEach(a => {\r
122             if (a.activityId == e.element.id && a.calledProcessInstanceId !== null) {\r
123               console.log("will drill down to : " + a.calledProcessInstanceId);\r
124               r.navigate(['/details/' + a.calledProcessInstanceId]);\r
125               this.spinner.show();\r
126             }\r
127           });\r
128         });\r
129         // zoom to fit full viewport\r
130         canvas.zoom('fit-viewport');\r
131         activities.forEach(a => {\r
132           canvas.addMarker(a.activityId, 'highlight');\r
133         });\r
134       }\r
135     });\r
136   }\r
137 \r
138   getVarInst(procInstId) {\r
139     this.data.getVariableInstance(procInstId).subscribe(\r
140       (data: VarInstance[]) => {\r
141         this.variableInstance = [];\r
142         for (let i = 0; i < data.length; i++) {\r
143           var value = data[i]['value'];\r
144           var type = data[i]['type'];\r
145           if ((type == 'Object') && !(value == null)) {\r
146             try {\r
147               data[i]['value'] = JSON.stringify(value, null, 2);\r
148             }\r
149             catch (error) {\r
150               console.log("Unable to \nError Code: " + error);\r
151             }\r
152           }\r
153           this.variableInstance[i] = data[i];\r
154         }\r
155         console.log(data);\r
156       }, error => {\r
157         console.log(error);\r
158         this.popup.error("Unable to get Variable instances for id: " + procInstId + " Error code:" + error.status);\r
159       });\r
160   }\r
161 \r
162   async ngOnInit() {\r
163     this.bpmnViewer = new Viewer({\r
164       container: '.canvas'\r
165     });\r
166     this.route.params.subscribe(\r
167       async params => {\r
168         this.processInstanceID = params.id as string;\r
169         console.log("Will GET Process instanc using id: " + this.processInstanceID);\r
170         await this.getProcInstance(this.processInstanceID);\r
171 \r
172         this.getVarInst(this.processInstanceID);\r
173       });\r
174   }\r
175 \r
176 }\r