update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / logging / src / loggingStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18 import _cloneDeep from 'lodash/cloneDeep';
19 import _findIndex from 'lodash/findIndex';
20 import _remove from 'lodash/remove';
21 import LoggingActions from './loggingActions.js';
22 import LoggingSource from './loggingSource.js';
23
24 import alt from 'widgets/skyquake_container/skyquakeAltInstance';
25
26 class LoggingStore {
27 constructor() {
28 this.loggingConfig = {};
29 // initialLoggingConfig is the saved state
30 this.initialLoggingConfig = {};
31 this.nulledCategories = [];
32 this.bindActions(LoggingActions);
33 this.registerAsync(LoggingSource);
34 this.exportPublicMethods({
35 updateCategoryDefaultSeverity: this.updateCategoryDefaultSeverity,
36 updateCategoryDefaultSyslogSeverity: this.updateCategoryDefaultSyslogSeverity,
37 updateAllowDuplicateEvents: this.updateAllowDuplicateEvents,
38 addDenyEvent: this.addDenyEvent,
39 updateDenyEvent: this.updateDenyEvent,
40 removeDenyEvent: this.removeDenyEvent,
41 updateSyslogViewerURL: this.updateSyslogViewerURL,
42 resetLoggingConfigData: this.resetLoggingConfigData
43 });
44 }
45
46 getLoggingConfigSuccess = (data) => {
47 console.log("LoggingStore.getLoggingConfigSuccess called. data=", data);
48 // Do we need to do a deep clone?
49 const initialLoggingConfig = _cloneDeep(data);
50 console.log("initialLoggingConfig=", initialLoggingConfig);
51 this.setState({
52 loggingConfig: data,
53 initialLoggingConfig: initialLoggingConfig,
54 isLoading: false
55 });
56 }
57
58 getLoggingConfigError(data) {
59 console.log("LoggongStore.getLoggingConfigError called. data=", data);
60 }
61
62 putLoggingConfigSuccess = (data) => {
63 console.log("LoggingStore.putLoggingConfigSuccess called. data=", data);
64 const initialLoggingConfig = _cloneDeep(this.loggingConfig);
65 this.setState({
66 nulledCategories: [],
67 isLoading: false,
68 initialLoggingConfig: initialLoggingConfig
69 });
70 }
71
72 putLoggingConfigError(data) {
73 console.log("LoggingStore.putLoggingConfigError called. data=", data);
74 }
75
76 resetLoggingConfigData = (data) => {
77 console.log('LoggingStore.resetLoggingConfigData called. data=', data);
78 // Do we need to do a deep clone?
79 const loggingConfig = _cloneDeep(this.initialLoggingConfig);
80 this.setState({
81 loggingConfig: loggingConfig
82 });
83 }
84
85 updateCategoryDefaultSeverity = (catsev) => {
86 console.log("LoggingStore.updateCategoryDefaultSeverity:", catsev);
87 // find the category
88
89 let catIndex = _findIndex(this.loggingConfig.defaultSeverities, function(o) {
90 return o.category == catsev.category;
91 });
92 console.log("catIndex=", catIndex);
93 if (catIndex != -1) {
94 const loggingConfig = this.loggingConfig;
95 loggingConfig.defaultSeverities[catIndex].severity = catsev.severity;
96
97 this.setState({
98 loggingConfig: loggingConfig
99 });
100 } else {
101 console.log("ERROR: catIndex not founds for default category", catsev.category);
102 }
103 }
104
105 updateCategoryDefaultSyslogSeverity = (catsev) => {
106 console.log("LoggingStore.updateCategoryDefaultSyslogSeverity:", catsev);
107 // find the category (name) in the syslog sink
108
109 let self = this;
110 let loggingConfig = _cloneDeep(this.loggingConfig);
111 let syslogSinkIndex = -1;
112 let nulledCategories = this.nulledCategories;
113
114 loggingConfig.sinks && loggingConfig.sinks.map((sink, sinkIndex) => {
115 if (sink.name == 'syslog') {
116 if (sink.filter) {
117 if (sink.filter.category) {
118 let catIndex = _findIndex(sink.filter.category, {
119 name: catsev.name
120 });
121 if (catIndex != -1) {
122 // found it
123 if (catsev.severity == "") {
124 // blank was selected
125 nulledCategories.push(catsev.name);
126 this.setState({
127 nulledCategories: nulledCategories
128 });
129 // TODO/NOTE: Can't delete from model as sending a top-level payload with
130 // missing elements is not allowed!
131 // When backend supports it, in loggingSource change the order of operations
132 // // Delete first followed by save/put.
133 _remove(loggingConfig.sinks[sinkIndex].filter.category, {
134 name: catsev.name
135 });
136 } else {
137 sink.filter.category[catIndex] = catsev;
138 }
139 } else {
140 _remove(nulledCategories, (v) => v == catsev.name);
141 this.setState({
142 nulledCategories: nulledCategories
143 });
144 sink.filter.category.push(catsev);
145 }
146 } else {
147 sink.filter.category = [];
148 sink.filter.category.push(catsev);
149 }
150 } else {
151 sink.filter = {};
152 sink.filter.category = [];
153 sink.filter.category.push(catsev);
154 }
155 }
156 });
157
158 this.setState({
159 loggingConfig: loggingConfig
160 });
161 }
162
163 updateAllowDuplicateEvents = (allowFlag) => {
164 console.log("LoggingStore.updateAllowDuplicateEvents called. allowFlag=", allowFlag);
165 const loggingConfig = this.loggingConfig;
166 loggingConfig.allowDuplicateEvents = allowFlag;
167 this.setState({
168 loggingConfig: loggingConfig
169 });
170 }
171
172 /**
173 * Add a new empty (null) deny event to loggingConfig
174 */
175 addDenyEvent = (event) => {
176 const loggingConfig = this.loggingConfig;
177 loggingConfig.denyEventIDs.push(null);
178 this.setState({
179 loggingConfig: loggingConfig
180 });
181 }
182
183 /**
184 * Update
185 */
186 updateDenyEvent = (index, eventID) => {
187 //console.log("LoggingStore.updateDenyEventID: index=", index);
188 //console.log(" -> eventID=", eventID);
189
190 const loggingConfig = this.loggingConfig;
191 loggingConfig.denyEventIDs[index] = eventID;
192 this.setState({
193 loggingConfig: loggingConfig
194 });
195 }
196
197 /**
198 *
199 */
200 removeDenyEvent = (index) => {
201 // console.log("LoggingStore.removeDenyEvent at index %s", index);
202 const loggingConfig = this.loggingConfig;
203 // Note: we are not validating index
204 loggingConfig.denyEventIDs.splice(index, 1);
205 this.setState({
206 loggingConfig: loggingConfig
207 });
208 }
209
210 /**
211 *
212 */
213 updateSyslogViewerURL = (syslogViewerURL) => {
214 const loggingConfig = this.loggingConfig;
215 loggingConfig.syslogviewer = syslogViewerURL;
216 this.setState({
217 loggingConfig: loggingConfig
218 });
219 }
220 }
221
222 export default alt.createStore(LoggingStore, 'LoggingStore');