Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / debug / src / crash.jsx
1
2 /*
3  * 
4  *   Copyright 2016 RIFT.IO Inc
5  *
6  *   Licensed under the Apache License, Version 2.0 (the "License");
7  *   you may not use this file except in compliance with the License.
8  *   You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *   Unless required by applicable law or agreed to in writing, software
13  *   distributed under the License is distributed on an "AS IS" BASIS,
14  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *   See the License for the specific language governing permissions and
16  *   limitations under the License.
17  *
18  */
19 import React from 'react';
20 import './crash.scss';
21 import TreeView from 'react-treeview';
22 import '../node_modules/react-treeview/react-treeview.css';
23 import AppHeader from 'widgets/header/header.jsx';
24 var crashActions = require('./crashActions.js');
25 var crashStore = require('./crashStore.js');
26 // var MissionControlStore = require('../missioncontrol/missionControlStore.js');
27 function openDashboard() {
28   window.location.hash = "#/";
29 }
30 class CrashDetails extends React.Component {
31   constructor(props) {
32     super(props)
33     var self = this;
34     this.state = crashStore.getState();
35     crashStore.listen(this.storeListener);
36   }
37   storeListener = (data) => {
38      this.setState({
39         list:data.crashList,
40         noDebug:!this.hasDebugData(data.crashList)
41       });
42   }
43   componentWillUnmount(){
44     crashStore.unlisten(this.storeListener);
45   }
46   componentWillMount() {
47     crashStore.get();
48   }
49   hasDebugData(list) {
50     console.log(list);
51     if (list && list.length > 0) {
52       for (let i = 0; i < list.length; i++) {
53         var trace = list[i].backtrace;
54         for (let j = 0; j < trace.length; j++) {
55           console.log(trace[j])
56           if (trace[j].detail) {
57             return true;
58           }
59         }
60       }
61     }
62     return false;
63   }
64   downloadFile(fileName, urlData) {
65     var replacedNewLines = urlData.replace(/\\n/g, '\n');
66     var replacedTabs = replacedNewLines.replace(/\\t/g, '\t');
67     var replacedQuotes= replacedTabs.replace(/\\"/g, '"');
68     var textFileBlob = new Blob([replacedQuotes], {type: 'text/plain;charset=UTF-8'});
69     var aLink = document.createElement('a');
70     var evt = document.createEvent("HTMLEvents");
71     evt.initEvent("click");
72     aLink.download = fileName;
73     aLink.href = window.URL.createObjectURL(textFileBlob);
74     aLink.dispatchEvent(evt);
75   }
76   render() {
77     let html;
78     var list = null;
79     if (this.state != null) {
80       var tree = <div style={{'marginLeft':'auto', 'marginRight':'auto', 'width':'230px', 'padding':'90px'}}> No Debug Information Available </div>;
81       if (!this.state.noDebug)
82       {
83         var tree = this.state.list && this.state.list.map((node, i) => {
84                   var vm = node.name;
85                   var vm_label = <span>{vm}</span>;
86                   var backtrace = node.backtrace;
87                   return (
88                     <TreeView key={vm + '|' + i} nodeLabel={vm_label} defaultCollapsed={false}>
89                       {backtrace.map(details => {
90
91                         //Needed to decode HTML
92                         var text_temp = document.createElement("textarea")
93                         text_temp.innerHTML = details.detail;
94                         var text_temp = text_temp.value;
95                         var arr = text_temp.split(/\n/);
96                         var text = [];
97                         for (let i = 0; i < arr.length; i++) {
98                           text.push(arr[i]);
99                           text.push(<br/>)
100                         }
101
102                         return (
103                           <TreeView nodeLabel={<span>{details.id}</span>} key={vm + '||' + details.id} defaultCollapsed={false}>
104                             <p>{text}</p>
105                           </TreeView>
106                         );
107                       })}
108                     </TreeView>
109                   );
110                 })}
111       html = (
112         <div className="crash-details-wrapper">
113               <div className="form-actions">
114                 <button role="button" className="dark" onClick={this.state.noDebug ? false : this.downloadFile.bind(this, 'crash.txt', 'data:text;charset=UTF-8,' + decodeURIComponent(JSON.stringify(this.state.list, null, 2)))}> Download Crash Details</button>
115               </div>
116               <div className="crash-container">
117                 <h2> Debug Information </h2>
118                 <div className="tree">{tree}</div>
119               </div>
120         </div>
121               );
122     } else {
123       html = <div className="crash-container"></div>
124     };
125     let refPage = window.sessionStorage.getItem('refPage');
126     refPage = JSON.parse(refPage);
127     return (
128             <div className="crash-app">
129               {html}
130             </div>
131             );
132   }
133 }
134 export default CrashDetails;