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