3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * EventCenter module to display a list of events from the system
21 * @module framework/widgets/skyquake_container/EventCenter
22 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
26 import React from 'react';
27 import { Link } from 'react-router';
28 import Utils from 'utils/utils.js';
29 import Crouton from 'react-crouton';
30 import TreeView from 'react-treeview';
31 import _isEqual from 'lodash/isEqual';
32 import _merge from 'lodash/merge';
33 import _indexOf from 'lodash/indexOf';
34 import _isArray from 'lodash/isArray';
35 import _sortBy from 'lodash/sortBy';
36 import '../../../node_modules/react-treeview/react-treeview.css';
37 import './eventCenter.scss';
39 export default class EventCenter extends React.Component {
43 this.state.isOpen = false;
46 componentWillReceiveProps(props) {
49 let latestNotification = sessionStorage.getItem('latestNotification');
51 if (props.newNotificationEvent && props.newNotificationMsg) {
52 if (latestNotification) {
53 latestNotification = JSON.parse(latestNotification);
54 if (!_isEqual(props.newNotificationMsg, latestNotification)) {
55 stateObject.newNotificationEvent = props.newNotificationEvent;
56 stateObject.newNotificationMsg = props.newNotificationMsg;
57 sessionStorage.setItem('latestNotification', JSON.stringify(stateObject.newNotificationMsg));
59 stateObject.newNotificationEvent = false;
60 stateObject.newNotificationMsg = null;
63 stateObject.newNotificationEvent = true;
64 stateObject.newNotificationMsg = props.newNotificationMsg;
65 sessionStorage.setItem('latestNotification', JSON.stringify(stateObject.newNotificationMsg));
68 stateObject.newNotificationEvent = false;
69 stateObject.newNotificationMsg = null;
71 stateObject.notifications = props.notifications;
73 this.setState(stateObject);
76 newNotificationReset = () => {
78 newNotificationEvent: false
82 onClickToggleOpenClose(event) {
83 this.props.onToggle();
85 isOpen: !this.state.isOpen
88 constructTree(details) {
90 for (let key in details) {
91 if (typeof (details[key]) == "object") {
93 <TreeView key={key} nodeLabel={key}>
94 {this.constructTree(details[key])}
99 markup.push((<div key={key} className="info">{key} = {details[key].toString()}</div>));
105 getNotificationFields(notification) {
106 let notificationFields = {};
109 notificationFields.source = notification.source;
110 notificationFields.eventTime = notification.eventTime;
112 Object.keys(notification).map((notificationKey) => {
113 if (_indexOf(['source', 'eventTime'], notificationKey) == -1) {
114 notificationFields.eventKey = notificationKey;
115 notificationFields.details = notification[notificationFields.eventKey];
120 return notificationFields;
125 let displayNotifications = [];
126 let displayNotificationsTableHead = null;
128 if (this.state.notifications && this.state.notifications.length > 0) {
129 displayNotificationsTableHead = (
131 <tr key='header' className='header'>
132 <th className='source column'> Source Event Stream </th>
133 <th className='timestamp column'> Timestamp </th>
134 <th className='event column'> Event </th>
135 <th className='details column'> Details </th>
141 this.state.notifications &&
142 _isArray(this.state.notifications) &&
143 this.state.notifications.map((notification, notifIndex) => {
144 let notificationFields = {};
146 notificationFields = this.getNotificationFields(notification);
148 displayNotifications.push(
149 <tr key={notifIndex} className='notificationItem'>
150 <td className='source column'> {notificationFields.source} </td>
151 <td className='timestamp column'> {notificationFields.eventTime} </td>
152 <td className='event column'> {notificationFields.eventKey} </td>
153 <td className='details column'>
154 <TreeView key={notifIndex + '-detail'} nodeLabel='Event Details'>
155 {this.constructTree(notificationFields.details)}
162 let openedClassName = this.state.isOpen ? 'open' : 'closed';
164 <div className={'eventCenter ' + openedClassName}>
166 <div className='notification'>
169 message={this.getNotificationFields(this.state.newNotificationMsg).eventKey +
170 ' notification received. Check Event Center for more details.'}
172 hidden={!(this.state.newNotificationEvent && this.state.newNotificationMsg)}
173 onDismiss={this.newNotificationReset}
174 timeout={this.props.dismissTimeout}
177 <h1 className='title' data-open-close-icon={this.state.isOpen ? 'open' : 'closed'}
178 onClick={this.onClickToggleOpenClose.bind(this)}>
181 <div className='notificationListBody'>
182 <table className='notificationList'>
183 {displayNotificationsTableHead}
185 {displayNotifications}
196 EventCenter.defaultProps = {